-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjm_rgbx_pixel.spin2
586 lines (385 loc) · 19.6 KB
/
jm_rgbx_pixel.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
'' =================================================================================================
''
'' File....... jm_rgbx_pixel.spin2
'' Purpose.... 800kHz driver for WS281x & SK681xRGBW LEDs
'' Author..... Jon "JonnyMac" McPhalen
'' Copyright (c) 2016-2020 Jon McPhalen
'' -- see below for terms of use
'' E-mail..... jon.mcphalen@gmail.com
'' Started....
'' Updated.... 02 AUG 2020
'' -- translated to P2
''
'' =================================================================================================
{
References:
-- https://cdn-shop.adafruit.com/datasheets/WS2811.pdf
-- https://cdn-shop.adafruit.com/datasheets/WS2812.pdf
-- https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf
-- http://www.szledcolor.com/download/WS2813%20LED.pdf
-- https://cdn-shop.adafruit.com/product-files/1138/SK6812+LED+datasheet+.pdf
-- https://cdn-shop.adafruit.com/product-files/2757/p2757_SK6812RGBW_REV01.pdf
Important Note:
In order to accomdate 24- and 32-bit pixels, the data for 24-bit pixels must be left
aligned in long holding the RGB value. In previous drivers, the data was right aligned.
old format: $00_RR_GG_BB
new format: $RR_GG_BB_00
BYTE0 of the pixel data holds the white channel for RBGW pixels.
$RR_GG_BB_WW
The driver needs to know the data pixel length: 24 (rgb) or 32 (rgbw) bits
}
con { fixed io pins }
RX1 = 63 { I } ' programming / debug
TX1 = 62 { O }
SF_CS = 61 { O } ' serial flash
SF_SCK = 60 { O }
SF_SDO = 59 { O }
SF_SDI = 58 { I }
con { rgbw colors }
' borrowed from Gavin Garner's TM1804 LED driver
' -- additional colors by Lachlan
' -- some alterations by JM
' -- modified for RGB and RGBW pixels
' 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
obj
gc : "jm_gamma8" ' gamma correction
var
long cog
' do not modify order; this structure passed to PASM cog
'
long newconnection ' new conection when != 0
long p_pixels ' pointer to active pixel buffer
long npixels ' number of pixels in buffer
long txpin ' active transmit pin
long pixbits ' bits/pixel (24 or 32)
long resetticks ' ticks in reset period
long rgfix ' swap r&g?
long t0h ' bit0 high time (ticks)
long t1h ' bit1 high time (ticks)
long cycleticks ' ticks in 1.25us
pub null()
'' This is not a top-level object
pub start_2811(p_buf, count, pin, holdoff) : result
'' Start pixel driver for WS2811 ICs
'' -- p_buf is pointer to [long] array holding pixel data
'' -- count is # of pixels supported by array at p_buf
'' -- pin is serial output to pixels
'' -- hold-off is delay (0.1 to 5.0ms) between tranmissions
'' * units are 100us (0.1ms) 10 units = 1ms
return startx(p_buf, count, pin, holdoff, false, 24, 250, 600)
pub start_2812(p_buf, count, pin, holdoff) : result
'' Start pixel driver for WS2812 LEDs
'' -- p_buf is pointer to [long] array holding pixel data
'' -- count is # of pixels supported by array at p_buf
'' -- pin is serial output to pixels
'' -- holdoff is the delay between data bursts
'' * units are 100us (0.1ms) 10 units = 1ms
return startx(p_buf, count, pin, holdoff, true, 24, 350, 700)
pub start_2812b(p_buf, count, pin, holdoff) : result
'' Start pixel driver for WS2812b LEDs
'' -- p_buf is pointer to [long] array holding pixel data
'' -- count is # of pixels supported by array at p_buf
'' -- pin is serial output to pixels
'' -- holdoff is the delay between data bursts
'' * units are 100us (0.1ms) 10 units = 1ms
return startx(p_buf, count, pin, holdoff, true, 24, 400, 800)
pub start_2813(p_buf, count, pin, holdoff) : result
'' Start pixel driver for WS2813 LEDs
'' -- p_buf is pointer to [long] array holding pixel data
'' -- count is # of pixels supported by array at p_buf
'' -- pin is serial output to pixels
'' -- holdoff is the delay between data bursts
'' * units are 100us (0.1ms) 10 units = 1ms
'' * minimum hold-off is 300us
return startx(p_buf, count, pin, (holdoff #> 3), true, 24, 375, 875)
pub start_6812x(p_buf, count, pin, holdoff, bits) : result
'' Start pixel driver for SK6812RBGW LEDs
'' -- p_buf is pointer to [long] array holding pixel data
'' -- count is # of pixels supported by array at p_buf
'' -- pin is serial output to pixels
'' -- holdoff is the delay between data bursts
'' * units are 100us (0.1ms) 10 units = 1ms
'' -- bits is 24 for SK6812, or 32 for SK6812RGBW
return startx(p_buf, count, pin, holdoff, true, bits, 300, 600)
pub startx(p_buf, count, pin, holdoff, rgswap, bits, ns0h, ns1h) : result | ustix
'' Start smart pixel driver driver
'' -- p_buf is pointer to [long] array holding pixel data
'' -- count is # of pixels supported by array at p_buf
'' -- pin is serial output to pixels
'' -- holdoff is the delay between data bursts
'' * units are 100us (0.1ms) 10 units = 1ms
'' * long pixel strings tend to require long hold-off delays (e.g. 10 for 1ms)
'' -- rgswap is red/green swap flag
'' -- bits is 24 for RGB (WS2812x, WS2813, SK6812), 32 for RBGW (SK6812RGBW)
'' -- ns0h is 0-bit high timing (ns)
'' -- ns1h is 1-bit high timing (ns)
stop() ' stop if running
pinclear(pin) ' clear tx pin
ustix := clkfreq / 1_000_000 ' ticks in 1us
' set cog parameters
use(p_buf, count, pin, bits) ' set connection details
newconnection := true
longmove(@p_pixels, @p_buf, 3) ' connection details
pixbits := bits ' set bit count
resetticks := ustix * 100 * (1 #> holdoff <# 50) ' note: 80us min reset timing
rgfix := rgswap <> 0 ' promote non-zero to true
t0h := ustix * ns0h / 1000 - 9 ' pulse widths in ticks (adjusted)
t1h := ustix * ns1h / 1000 - 9
cycleticks := ustix * 1_25 / 1_00 ' ticks in 1.25us (800kHz)
cog := coginit(COGEXEC_NEW, @pix_driver, @newconnection) + 1 ' start the cog
if (cog) ' if it started
repeat until (newconnection == 0) ' wait until ready
return cog
pub stop()
'' Stops pixel driver cog (if running)
if (cog)
cogstop(cog - 1)
cog := 0
pub use(p_buf, count, pin, bits) : c
'' Assigns buffer at p_buf to pixel driver
'' -- p_buf is pointer to long array
'' -- count is # of elements in the array
'' -- pin is serial output to pixels
'' -- bits is bit count for pixel type (24 or 32)
longmove(@p_pixels, @p_buf, 4) ' copy parameters
if ((pixbits <> 24) && (pixbits <> 32)) ' fix bits/pixel if broken
pixbits := 24
newconnection := true ' alert pixel cog
pub connected() : result
'' Returns true when latest connection details picked up by driver
return (newconnection == 0)
pub color(r, g, b, w) : result
'' Packs r-g-b-w bytes into long
result.byte[3] := r
result.byte[2] := g
result.byte[1] := b
result.byte[0] := w
pub colorx(r, g, b, w, level) : newcolor
'' Packs r-g-b-w bytes into long
'' -- r, g, b, and w are element levels, 0..255
'' -- level is brightness, 0..255 (0..100%)
if (level <= 0)
newcolor := $00_00_00_00
elseif (level >= 255)
newcolor := color(r, g, b, w)
else
r := r * level / 255 ' apply level to rgbw
g := g * level / 255
b := b * level / 255
w := w * level / 255
newcolor := color(r, g, b, w)
pub wheel(pos) : result
'' Creates color from 0 to 255 position input
'' -- colors transition r->g->b back to r
'' -- does not use white channel
pos &= $FF
if (pos < 85)
return color(255-pos*3, pos*3, 0, 0)
elseif (pos < 170)
pos -= 85
return color(0, 255-pos*3, pos*3, 0)
else
pos -= 170
return color(pos*3, 0, 255-pos*3, 0)
pub wheelx(pos, level) : result
'' Creates color from 0 to 255 position input
'' -- colors transition r-g-b back to r
'' -- level is brightness, 0..255 (0..100%)
'' -- does not use white channel
pos &= $FF
if (pos < 85)
return colorx(255-pos*3, pos*3, 0, 0, level)
elseif (pos < 170)
pos -= 85
return colorx(0, 255-pos*3, pos*3, 0, level)
else
pos -= 170
return colorx(pos*3, 0, 255-pos*3, 0, level)
pub set(ch, rgbw)
'' Writes rgbw value to channel ch in buffer
'' -- rgbw is packed long in form $RR_GG_BB_WW
if ((ch >= 0) && (ch < npixels))
long[p_pixels][ch] := rgbw
pub setx(ch, rgbw, level)
'' Writes scaled rgbw value to channel ch in buffer
'' -- rgbw is packed long in form $RR_GG_BB_WW
'' -- level is brightness, 0..255 (0..100%)
if ((ch >= 0) && (ch < npixels))
long[p_pixels][ch] := scale_rgbw(rgbw, level)
pub scale_rgbw(rgbw, level) : newcolor
'' Scales rgbw value to level
'' -- level is brightness, 0..255 (0..100%)
if (level <= 0)
newcolor := $00_00_00_00
elseif (level >= 255)
newcolor := rgbw
else
newcolor.byte[3] := rgbw.byte[3] * level / 255
newcolor.byte[2] := rgbw.byte[2] * level / 255
newcolor.byte[1] := rgbw.byte[1] * level / 255
newcolor.byte[0] := rgbw.byte[0] * level / 255
pub set_rgbw(ch, r, g, b, w)
'' Writes rgbw elements to channel ch in buffer
'' -- r, g, b, and w are byte values, 0 to 255
set(ch, color(r, g, b, w))
pub set_red(ch, level)
'' Sets red led level of selected channel
'' -- level is brightness, 0..255 (0..100%)
if ((ch >= 0) && (ch < npixels)) ' valid?
byte[p_pixels + (ch << 2) + 3] := level ' set it
pub set_green(ch, level)
'' Sets green led level of selected channel
'' -- level is brightness, 0..255 (0..100%)
if ((ch >= 0) && (ch < npixels))
byte[p_pixels + (ch << 2) + 2] := level
pub set_blue(ch, level)
'' Sets blue led level of selected channel
'' -- level is brightness, 0..255 (0..100%)
if ((ch >= 0) && (ch < npixels))
byte[p_pixels + (ch << 2) + 1] := level
pub set_white(ch, level)
'' Sets white led level of selected channel
'' -- level is brightness, 0..255 (0..100%)
if ((ch >= 0) && (ch < npixels))
byte[p_pixels + (ch << 2) + 0] := level
pub set_all(rgbw)
'' Sets all channels to rgb
'' -- rgbw is packed long in form $RR_GG_BB_WW
longfill(p_pixels, rgbw, npixels)
pub fill(first, last, rgbw) | swap
'' Fills first through last channels with rgb
'' -- rgbw is packed long in form $RR_GG_BB_WW
first := 0 #> first <# npixels-1
last := 0 #> last <# npixels-1
if (first > last)
first, last := last, first
longfill(p_pixels+(first << 2), rgbw, last-first+1)
pub morph(c1, c2, phase) : newcolor
'' Morph between c1 and c2
'' -- phase 0 is color1
'' -- phase 255 is color2
if (phase <= 0)
newcolor := c1
elseif (phase >= 255)
newcolor := c2
else
c1 := scale_rgbw(c1, 255-phase)
c2 := scale_rgbw(c2, phase)
newcolor := c1 + c2
pub clear()
'' Turns off all LEDs
longfill(p_pixels, $00_00_00_00, npixels)
pub read(ch) : rgbw
'' Returns color of channel
if ((ch >= 0) && (ch < npixels))
return long[p_pixels][ch]
else
return $00_00_00_00
pub gamma(idx) : result
'' Adjusts gamma for better midrange colors
return gc.gamma(0 #> idx <# 255)
pub running() : result
'' Returns true if running
return (cog <> 0)
pub address() : result
'' Returns address of assigned pixel array
return p_pixels
pub num_pixels() : result
'' Returns number of pixels in assiged pixel array
return npixels
pub last_pixel() : result
'' Returns last legal pixel index for current buffer
return npixels - 1
dat { auto-run driver }
org 0
pix_driver setq #10-1 ' get 10 longs from hub
rdlong connect, ptra
mov t1, #0
wrlong t1, ptra ' tell hub we're connected
rgbx_main rdlong connect, ptra wz ' check for new connection
if_nz jmp #pix_driver
mov addr, p_hub ' point to rgbbuf[0]
mov npix, pixcount ' set # active pixels
frame_loop rdlong colorbits, addr ' read a channel
add addr, #4 ' point to next
tjz swapflag, #shift_out ' skip fix if swap = 0
' Correct placement of rg color bytes
' -- $RR_GG_BB_WW --> $GG_RR_BB_WW
mov t1, colorbits ' copy
shr t1, #16 ' isolate r & g
setbyte colorbits, t1, #3 ' colorbits.byte[3] = g
shr t1, #8
setbyte colorbits, t1, #2 ' colorbits.byte[2] = r
shift_out getct bittimer ' start timing frame
rep @.bitz, pixelbits ' loop through all bits
rol colorbits, #1 wc ' get MSB
drvh tx ' pin on
if_nc waitx bit0hi ' hold for bit timing
if_c waitx bit1hi
drvl tx ' pin off
addct1 bittimer, cycletix ' update cycle timer
waitct1 ' let cycle finish
.bitz
next_pixel djnz npix, #frame_loop ' done with all leds?
reset_delay getct bittimer ' reset delay
addct1 bittimer, resettix
waitct1
jmp #rgbx_main ' back to top
' --------------------------------------------------------------------------------------------------
connect res 1 ' new connection flag
p_hub res 1 ' pointer to pixel buffer in use
pixcount res 1 ' # pixels in buffer
tx res 1 ' output pin
pixelbits res 1 ' bits per pixel
resettix res 1 ' frame reset timing
swapflag res 1 ' if !0, swap R & G
bit0hi res 1 ' bit0 high timing
bit1hi res 1 ' bit1 high timing
cycletix res 1 ' 1.25us cycle ticks
addr res 1 ' address of current rgbw pixel
npix res 1 ' # of pixels to process
colorbits res 1 ' rgbw for current pixel
bittimer res 1 ' timer for reset/bit
t1 res 1 ' work vars
t2 res 1
t3 res 1
fit 496
con { license }
{{
Terms of Use: MIT License
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 NON-INFRINGEMENT. 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.
}}