-
Notifications
You must be signed in to change notification settings - Fork 1
/
display.led.smart.spin2
394 lines (333 loc) · 13.2 KB
/
display.led.smart.spin2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
{
---------------------------------------------------------------------------------------------------
Filename: display.led.smart.spin2
Description: Driver for various smart LED arrays
Author: Jesse Burt
Started: Jun 13, 2021
Updated: Feb 4, 2024
Copyright (c) 2024 - See end of file for terms of use.
---------------------------------------------------------------------------------------------------
NOTE: This driver requires a system clock of at least 16MHz
}
#define MEMMV_NATIVE longmove
#include "graphics.common.spin2h"
#ifdef GFX_DIRECT
# error "GFX_DIRECT/direct-to-display drawing not supported by this driver"
#endif
CON
{ -- default I/O configuration - can be overridden in the parent object }
LED_PIN = 0
WIDTH = 1
HEIGHT = 1
MODEL = WS2812B
{ -- }
MAX_COLOR = 16_777_215
BYTESPERPX = 4
BUFF_SZ = (WIDTH*HEIGHT)*BYTESPERPX
NR_PIXELS = WIDTH*HEIGHT
' RR GG BB WW
BLACK = $00_00_00_00
RED = $FF_00_00_00
GREEN = $00_FF_00_00
BLUE = $00_00_FF_00
WHITE = $FF_FF_FF_00
WHITE2 = $00_00_00_FF
CYAN = $00_FF_FF_00
MAGENTA = $FF_00_FF_00
YELLOW = $FF_FF_00_00
CHARTREUSE = $7F_FF_00_00
ORANGE = $FF_60_00_00
AQUAMARINE = $7F_FF_D4_00
PINK = $FF_5F_5F_00
TURQUOISE = $3F_E0_C0_00
REALWHITE = $C8_FF_FF_00
INDIGO = $3F_00_7F_00
VIOLET = $BF_7F_BF_00
MAROON = $32_00_10_00
BROWN = $0E_06_00_00
CRIMSON = $DC_28_3C_00
PURPLE = $8C_00_FF_00
' TH+TL should be 1250ns +/- 600ns
' TH and TL each should be within 150ns of spec
WS2811 = $2811
WS2811_BITPER = 1_250 ' nsec
WS2811_T0H = 250_000 -20_000 ' nsec
WS2811_T0L = 1000_000 -120_000 ' (adjusted for overhead)
WS2811_T1H = 600_000 -20_000
WS2811_T1L = 650_000 -130_000
WS2811_TRES = 50
' TH+TL should be 1250ns +/- 600ns
' TH and TL each should be within 150ns of spec
WS2812 = $2812
WS2812_BITPER = 1_250 ' nsec
WS2812_T0H = 350_000 -20_000 ' nsec
WS2812_T0L = 800_000 -120_000 ' (adjusted for overhead)
WS2812_T1H = 700_000 -20_000
WS2812_T1L = 600_000 -130_000
WS2812_TRES = 50
' TH+TL should be 1250ns +/- 600ns
' TH and TL each should be within 150ns of spec
WS2812B = $2812B
WS2812B_BITPER = 1_250 ' nsec
WS2812B_T0H = 400_000 -20_000 ' nsec
WS2812B_T0L = 850_000 -120_000 ' (adjusted for overhead)
WS2812B_T1H = 800_000 -20_000
WS2812B_T1L = 450_000 -130_000
WS2812B_TRES = 50
' TH+TL should be 1250ns +/- 300ns
' TH and TL each should be within 150ns of spec
WS2813 = $2813
WS2813_BITPER = 1_250 ' nsec
WS2813_T0H = 375_000 -20_000 ' nsec
WS2813_T0L = 875_000 -120_000 ' (adjusted for overhead)
WS2813_T1H = 875_000 -20_000
WS2813_T1L = 375_000 -130_000
WS2813_TRES = 300 ' usec
' TH+TL should be 1250ns +/- 600ns
' TH and TL each should be within 150ns of spec
SK6812_24 = $6812_24
SK6812_32 = $6812_32
SK6812_BITPER = 1_250 ' nsec
SK6812_T0H = 300_000 -20_000 ' nsec
SK6812_T0L = 950_000 -120_000 ' (adjusted for overhead)
SK6812_T1H = 600_000 -20_000
SK6812_T1L = 650_000 -130_000
SK6812_TRES = 80 ' usec
' TH+TL should be 1250ns +/- 600ns
' TH and TL each should be within 150ns of spec
TM1803 = $1803
TM1803_BITPER = 2_330 ' nsec
TM1803_T0H = 680_000 -20_000 ' nsec
TM1803_T0L = 1360_000 -120_000 ' (adjusted for overhead)
TM1803_T1H = 1360_000 -20_000
TM1803_T1L = 680_000 -130_000
TM1803_TRES = 24 ' usec
VAR
long _cog
long _nr_pix
long _mdl_t0h, _mdl_t0l, _mdl_t1h, _mdl_t1l, _mdl_per, _show, _model
long _t0h, _t0l, _t1h, _t1l, _smled_pin, _tres
long _framebuffer[(WIDTH*HEIGHT)*BYTESPERPX]
PUB null()
' This is not a top-level object
PUB start(): status
' Start the driver using default I/O settings
return startx(LED_PIN, WIDTH, HEIGHT, MODEL, @_framebuffer)
PUB startx(SMLED_PIN, DISP_W, DISP_H, led_model, ptr_fb): status
' Start smart-LED engine
' SMLED_PIN: I/O pin connected to smart-LED strip/array (0..63)
' WIDTH: strip/array width, in pixels (1..1024)
' HEIGHT: strip/array height, in pixels (1..1024) (NOTE: (WIDTH * HEIGHT) must be <= 1024)
' led_model: Specific model of LEDs; see set_model()
' ptr_fb: pointer to display buffer
' Returns: cogid+1 of parent cog
if (lookdown(SMLED_PIN: 0..63) and ((DISP_W * DISP_H) <= 1024))
_smled_pin := SMLED_PIN
set_model(led_model)
_disp_width := DISP_W
_disp_height := DISP_H
_disp_xmax := _disp_width - 1
_disp_ymax := _disp_height - 1
_buff_sz := _nr_pix := (DISP_W * DISP_H)
_bytesperln := _disp_width * BYTESPERPX
set_address(ptr_fb)
pinl(_smled_pin)
return _cog := (cogid() + 1)
return FALSE
PUB stop()
' Deinitialize engine
pinf(_smled_pin)
longfill(@_ptr_drawbuffer, 0, 22)
PUB clear()
' Clear the display buffer
longfill(_ptr_drawbuffer, _bgcolor, _nr_pix)
PUB plot(x, y, color)
' Plot pixel at (x, y) in color
if (x < 0 or x > _disp_xmax) or (y < 0 or y > _disp_ymax)
return ' coords out of bounds, ignore
long[_ptr_drawbuffer][x + (y * _disp_width)] := color
PUB point(x, y): pix_clr
' Get color of pixel at x, y
x := 0 #> x <# _disp_xmax
y := 0 #> y <# _disp_ymax
return long[_ptr_drawbuffer][x + (y * _disp_width)]
PUB set_clock(freq) | bitper
' Set clock frequency used to calculate timings
' Most strips operate at 800_000 (800kHz)
bitper := clkfreq / freq ' get bit period for freq
' calculate T0 and T1 high and low periods
_t0h := (((_mdl_t0h / _mdl_per) * bitper) / 1_000)
_t0l := (((_mdl_t0l / _mdl_per) * bitper) / 1_000)
_t1h := (((_mdl_t1h / _mdl_per) * bitper) / 1_000)
_t1l := (((_mdl_t1l / _mdl_per) * bitper) / 1_000)
PUB set_model(model): curr_model
' Set model of smart-LED array
' Valid values:
' WS2811 ($2811)
' WS2812 ($2812)
' WS2812B ($2812B)
' WS2813 ($2813)
' SK6812_24 ($6812_24)
' SK6812_32 ($6812_32)
' TM1803 ($1803)
case model
WS2811:
_mdl_t0h := WS2811_T0H
_mdl_t0l := WS2811_T0L
_mdl_t1h := WS2811_T1H
_mdl_t1l := WS2811_T1L
_mdl_per := WS2811_BITPER
_tres := WS2811_TRES
_show := @show_rgb24
WS2812:
_mdl_t0h := WS2812_T0H
_mdl_t0l := WS2812_T0L
_mdl_t1h := WS2812_T1H
_mdl_t1l := WS2812_T1L
_mdl_per := WS2812_BITPER
_tres := WS2812_TRES
_show := @show_grb24
WS2812B:
_mdl_t0h := WS2812B_T0H
_mdl_t0l := WS2812B_T0L
_mdl_t1h := WS2812B_T1H
_mdl_t1l := WS2812B_T1L
_mdl_per := WS2812B_BITPER
_tres := WS2812B_TRES
_show := @show_grb24
WS2813:
_mdl_t0h := WS2813_T0H
_mdl_t0l := WS2813_T0L
_mdl_t1h := WS2813_T1H
_mdl_t1l := WS2813_T1L
_mdl_per := WS2813_BITPER
_tres := WS2813_TRES
_show := @show_rgb24
SK6812_24:
_mdl_t0h := SK6812_T0H
_mdl_t0l := SK6812_T0L
_mdl_t1h := SK6812_T1H
_mdl_t1l := SK6812_T1L
_mdl_per := SK6812_BITPER
_tres := SK6812_TRES
_show := @show_grb24
SK6812_32:
_mdl_t0h := SK6812_T0H
_mdl_t0l := SK6812_T0L
_mdl_t1h := SK6812_T1H
_mdl_t1l := SK6812_T1L
_mdl_per := SK6812_BITPER
_tres := SK6812_TRES
_show := @show_grb32
TM1803:
_mdl_t0h := TM1803_T0H
_mdl_t0l := TM1803_T0L
_mdl_t1h := TM1803_T1H
_mdl_t1l := TM1803_T1L
_mdl_per := TM1803_BITPER
_tres := TM1803_TRES
_show := @show_rgb24
other:
return _model
set_clock(800_000)
return model
PUB show()
' Send display buffer to smart-LED array
' Call method pointed to (set by set_model())
_show()
PUB show_grb24() | t0h, t0l, t1h, t1l, smled_pin, tres, pix_nr, color, bit, tmp
' Update G-R-B 24bpp smart-LED array
longmove(@t0h, @_t0h, 6) ' copy timings to local vars
repeat pix_nr from 0 to _nr_pix-1
color := long[_ptr_drawbuffer][pix_nr] ' get current pixel color data
tmp := color.byte[3]
color.byte[3] := color.byte[2] ' swap red, green bytes
color.byte[2] := tmp
repeat bit from 0 to 23
if (color rol= 1) & 1 ' move msb to lsb and isolate
asm
drvh smled_pin ' 1-bit
waitx t1h
drvl smled_pin
waitx t1l
endasm
else
asm
drvh smled_pin ' 0-bit
waitx t0h
drvl smled_pin
waitx t0l
endasm
pinl(smled_pin) ' reset code (end of array)
waitus(tres)
PUB show_grb32() | t0h, t0l, t1h, t1l, smled_pin, tres, pix_nr, color, bit, tmp
' Update G-R-B 32bpp smart-LED array
longmove(@t0h, @_t0h, 6) ' copy timings to local vars
repeat pix_nr from 0 to _nr_pix-1
color := long[_ptr_drawbuffer][pix_nr] ' get current pixel color data
tmp := color.byte[3]
color.byte[3] := color.byte[2] ' swap red, green bytes
color.byte[2] := tmp
repeat bit from 0 to 31
if (color rol= 1) & 1 ' move msb to lsb and isolate
asm
drvh smled_pin ' 1-bit
waitx t1h
drvl smled_pin
waitx t1l
endasm
else
asm
drvh smled_pin ' 0-bit
waitx t0h
drvl smled_pin
waitx t0l
endasm
pinl(smled_pin) ' reset code (end of array)
waitus(tres)
PUB show_rgb24() | t0h, t0l, t1h, t1l, smled_pin, tres, pix_nr, color, bit
' Update R-G-B 24bpp smart-LED array
longmove(@t0h, @_t0h, 6) ' copy timings to local vars
repeat pix_nr from 0 to _nr_pix-1
color := long[_ptr_drawbuffer][pix_nr] ' get current pixel color data
repeat bit from 0 to 23
if (color rol= 1) & 1 ' move msb to lsb and isolate
asm
drvh smled_pin ' 1-bit
waitx t1h
drvl smled_pin
waitx t1l
endasm
else
asm
drvh smled_pin ' 0-bit
waitx t0h
drvl smled_pin
waitx t0l
endasm
pinl(smled_pin) ' reset code (end of array)
waitus(tres)
PRI memfill(xs, ys, val, count)
' Fill region of display buffer memory
' xs, ys: Start of region
' val: Color
' count: Number of consecutive memory locations to write
longfill( _ptr_drawbuffer + ((xs << 1) + (ys * _bytesperln)), ...
((val >> 8) & $FF) | ((val << 8) & $FF00), ...
count )
DAT
{
Copyright 2024 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}