-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kevinbot_v3.spin2
1243 lines (893 loc) · 36.6 KB
/
Kevinbot_v3.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'' =================================================================================================
''
'' File....... Kevinbot_v3.spin2
'' Purpose.... Code for Kevinbot v3
'' Author..... Kevin Ahr
'' Copyright (c) 2022 - 2023 Kevin Ahr
'' -- see below for terms of use
'' Started.... 06 JUN 2022
''
'' {$P2}
''
'' =================================================================================================
{{
PCA9685 Servo Config
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| | | | | |
----------- ----------- ----
' ' '
Left Arm Right Arm Head
}}
{{
Error Codes:
=============================================
| BIN |DEC| ERROR |
|-------------------------------------------|
|NOBEEP | 0 | Everything OK |
|(0001) | 1 | Invalid Startup Arm Positions |
|NOBEEP | 2 | Invalid Command from Pi |
|(0011) | 3 | BME280 Setup Failed |
|(0100) | 4 | BME280 Read Failed |
|(0101) | 5 | One Wire Bus Short |
|(0110) | 6 | One Wire Bus Error |
|(0111) | 7 | One Wire Device not Found |
=============================================
}}
con { timing }
CLK_FREQ = 200_000_000 ' system freq as a constant
MS_001 = CLK_FREQ / 1_000 ' ticks in 1ms
US_001 = CLK_FREQ / 1_000_000 ' ticks in 1us
_clkfreq = CLK_FREQ ' set system clock
con { terminal }
BR_TERM = 230_400 ' terminal baud rate
BR_RPI = 230_400 ' raspberrypi / xbee baud rate
#0, T_PST, T_ANSI ' terminal types
T_TYPE = T_ANSI
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 }
SD_SCK = 61 { O } ' sd card
SD_CS = 60 { O }
SD_SDO = 59 { O }
SD_SDI = 58 { I }
LED2 = 57 { O } ' Eval and Edge LEDs
LED1 = 56 { O }
con { app io pins }
RPI_TX = 35 { I } ' Raspberry Pi
RPI_RX = 36 { O }
LED = 34 { O } ' all robot leds (daisy-chained)
SDA = 41 { IO } ' i2c bus
SCL = 42 { IO } ' SCL pin must be +1 of SDA
VOLT1_P = 37 { I } ' voltage sensors
VOLT2_P = 38 { I }
ONE_WIRE = 39 { IO } ' one wire bus
BUZZER_PIN = 40 { O } ' alert buzzer
con { settings }
#true, ON, OFF
#false, NO, YES
''''''''''''''''''''''''''''''
''' LED Settings '''
S_LIGHT_UPDATE_HEAD = 200
S_LIGHT_UPDATE_BODY = 200
S_LIGHT_UPDATE_BASE = 200
S_BODY_BRIGHTNESS = 120
BODY_MAX = 250
BODY_MIN = 10
BODY_INC = 20
S_BASE_BRIGHTNESS = 120
BASE_MAX = 250
BASE_MIN = 10
BASE_INC = 20
S_HEAD_EFFECT = 1
S_BODY_EFFECT = 1
S_BASE_EFFECT = 1
#0, L_2811, L_2812
LED_TYPE = L_2811
''''''''''''''''''''''''''''''
''' Sensor Settings '''
MONITOR_BATTS = YES ' use 1 or 2 battery sensors
MONITOR_TEMPS = YES ' enable 3x DS18b20
SENSOR_UPDATE_INT = 3000 ' sensor update interval
''''''''''''''''''''''''''''''
''' Misc Settings '''
BUFFER_SIZE = 32 ' command buffer size
CMD_SIZE = 32 ' max cmd and val name length
HEAD_LEDS = 13 ' number of leds in head
BODY_LEDS = 13 ' number of leds in body
CAM_LEDS = 11 ' number of leds in camera ring
BASE_LEDS = 13 ' number of leds in base
TOTAL_LEDS = HEAD_LEDS+BODY_LEDS+CAM_LEDS+BASE_LEDS ' total leds in whole robot
TERMINAL = YES ' enable terminal debug over usb
STARTUP_BEEP = YES ' beep on startup
ALIVE_INTERVAL = 2000
''''''''''''''''''''''''''''''
con { servo channels }
US_MIN = 500 ' min and max microseconds for pca
US_MAX = 2400
HEAD_PAN_CH = 11 ' pan channel
HEAD_TILT_CH = 10 ' tilt channel
HEAD_PAN_MIN = 0 ' pan servo min angle
HEAD_PAN_MAX = 160 ' pan servo max angle
HEAD_TILT_MIN = 0 ' tilt servo min angle
HEAD_TILT_MAX = 160 ' tilt servo max angle
obj
' main ' * master Spin cog
term : "jm_fullduplexserial" ' * serial IO for terminal
rpi : "jm_fullduplexserial" ' * serial IO for RPi
ansi : "jm_ansi" ' ANSI terminal control sequences
strs : "ka_strings" ' string manipulation
timer1 : "jm_ez_timer" ' timer for leds
timer2 : "jm_ez_timer" ' timer for leds
timer3 : "jm_ez_timer" ' timer for leds
timer4 : "jm_ez_timer" ' timer for sensors
timer5 : "jm_ez_timer" ' timer for eyes
timer6 : "jm_ez_timer" ' timer for alive updates
prng : "jm_prng" ' TEMPORARY RNG
nstr : "jm_nstr" ' number-to-string
leds : "jm_rgbx_pixel" ' * WS2812b leds
pca : "ka_pca9685" ' pca9685 servo / pwm
beeper : "jm_pwm" ' error buzzer pwm
volt1 : "jm_ez_analog" ' smart pin analog input for voltage meter
volt2 : "jm_ez_analog" ' smart pin analog input for voltage meter
tsns : "jm_ds18b20" ' 1-Wire temperature sensor
' * uses cog when loaded
dat
Banner byte "===============", 13, 10
byte " Kevinbot v3 ", 13, 10
byte "===============", 13, 10, 0
Version byte "version=v0.1.0", 10, 0 ' version
' rr gg bb ww
CameraColor long $FF_FF_FF_00 ' color for camera ring
ArmStartup byte 0, 90, 45, 80, 180, 0, 0, 0, 0, 0 ' startup positions for arms
HeadStartup byte 80, 80 ' startup positions for head
' DS18b20 serial numbers
SN0 byte $28, $98, $C0, $23, $0C, $32, $20, $F0 ' left motor
SN1 byte $28, $BF, $08, $42, $0C, $32, $20, $C0 ' right motor
SN2 byte $28, $2E, $5C, $47, $0C, $32, $20, $4D ' robot temp
dat { command tables }
NumCmds byte 35 ' number of commands in table
Commands
byte "robot_version" , 0
byte "head_color1" , 0
byte "head_color2" , 0
byte "head_effect" , 0
byte "body_color1" , 0
byte "body_color2" , 0
byte "body_effect" , 0
byte "body_bright-" , 0
byte "body_bright+" , 0
byte "cam_brightness" , 0
byte "body_update" , 0
byte "head_x" , 0
byte "head_y" , 0
byte "base_color1" , 0
byte "base_color2" , 0
byte "base_effect" , 0
byte "base_bright-" , 0
byte "base_bright+" , 0
byte "base_update" , 0
byte "left_us" , 0
byte "right_us" , 0
byte "shutdown" , 0
byte "head_update" , 0
byte "pinl" , 0
byte "pinh" , 0
byte "pint" , 0
byte "get_clk" , 0
byte "getms" , 0
byte "stop" , 0
byte 0
NumHead byte 6 ' number of commands in table
HeadEffMult byte 0, 0, 500, 10, 200, 200 ' effect speed multiplier
HeadEffects
byte "color1" , 0
byte "color2" , 0
byte "flash" , 0
byte "fade" , 0
byte "jump3" , 0
byte "twinkle" , 0
byte 0
NumBody byte 10 ' number of commands in table
BodyEffMult byte 0, 0, 500, 10, 200, 200, 70, 5, 8, 7 ' effect speed multiplier
BodyEffects
byte "color1" , 0
byte "color2" , 0
byte "flash" , 0
byte "fade" , 0
byte "jump3" , 0
byte "twinkle" , 0
byte "swipe" , 0
byte "rainbow" , 0
byte "magic" , 0
byte "fire" , 0
byte 0
NumBase byte 10 ' number of commands in table
BaseEffMult byte 0, 0, 500, 10, 200, 200, 70, 5, 8, 7 ' effect speed multiplier
BaseEffects
byte "color1" , 0
byte "color2" , 0
byte "flash" , 0
byte "fade" , 0
byte "jump3" , 0
byte "twinkle" , 0
byte "swipe" , 0
byte "rainbow" , 0
byte "magic" , 0
byte "fire" , 0
byte 0
var { globals }
long scstack[128]
byte running
byte serial_buf[BUFFER_SIZE]
byte cmdstr[CMD_SIZE] ' command as string
byte valstr[CMD_SIZE] ' value as string
byte sens_cog
long pixbuf[TOTAL_LEDS] ' pixel buffer
''' leds and animations '''
long light_update_head
long light_update_body
long light_update_base
long head_color1
long head_color2
byte head_effect
long body_color1
long body_color2
byte body_effect
byte body_bright
long base_color1
long base_color2
byte base_effect
byte base_bright
byte head_animation_pos
byte head_ani_phase
byte body_animation_pos
byte body_ani_phase
byte base_animation_pos
byte base_ani_phase
byte error_state ' current hardware error (0 for none)
pub main() | r, d, tc, tf, p, h, status, tc_left, tc_right, ta, tb, tc2, td, te
light_update_head, light_update_body, head_effect, body_effect := S_LIGHT_UPDATE_HEAD, S_LIGHT_UPDATE_BODY, S_HEAD_EFFECT, S_BODY_EFFECT
light_update_base, base_effect := S_LIGHT_UPDATE_BASE, S_BASE_EFFECT
body_bright, base_bright := S_BODY_BRIGHTNESS, S_BASE_BRIGHTNESS
error_state := 0
running := 1
setup()
sens_cog := cogspin(newcog, sensor_cog(), @scstack) ' sensor update cog
if STARTUP_BEEP ' make startup beeps
tone(20, 1600, 100)
tone(20, 800, 150)
tone(20, 1000, 100)
if TERMINAL
term.str(@Banner)
repeat
if running == 1
check_serial()
if timer6.millis() >= ALIVE_INTERVAL
timer6.start()
rpi.fstr1(string("alive=%d", 10), getsec())
if timer1.millis() >= (light_update_head * byte[@HeadEffMult][head_effect-1]) / 100
timer1.start()
case head_effect
3:
flash_head()
5:
jump3_head()
4:
fade_head()
6:
twinkle_head()
if timer2.millis() >= (light_update_body * byte[@BodyEffMult][body_effect-1]) / 100
timer2.start()
case body_effect
3:
flash_body()
5:
jump3_body()
4:
fade_body()
6:
twinkle_body()
7:
swipe_body()
8:
rainbow_cycle_body()
9:
magic_cycle_body()
10:
fire_cycle_body()
if timer3.millis() >= (light_update_base * byte[@BaseEffMult][base_effect-1]) / 100
timer3.start()
case base_effect
3:
flash_base()
5:
jump3_base()
4:
fade_base()
6:
twinkle_base()
7:
swipe_base()
8:
rainbow_cycle_base()
9:
magic_cycle_base()
10:
fire_cycle_base()
if timer4.millis() >= SENSOR_UPDATE_INT
timer4.start()
update_sensors()
if error_state > 0
rpi.fstr1(string("error=%d", 10), error_state)
' handle errors with beeper
if error_state == 1
repeat
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 1000)
waitms(1050)
elseif error_state == 3
repeat
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 1000)
waitms(500)
tone(50, 2000, 1000)
waitms(1050)
elseif error_state == 4
repeat
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 1000)
waitms(500)
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 100)
waitms(1050)
elseif error_state == 5
repeat
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 1000)
waitms(500)
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 1000)
waitms(1050)
elseif error_state == 6
repeat
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 1000)
waitms(500)
tone(50, 2000, 1000)
waitms(500)
tone(50, 2000, 100)
waitms(1050)
elseif error_state == 7
repeat
tone(50, 2000, 100)
waitms(500)
tone(50, 2000, 1000)
waitms(500)
tone(50, 2000, 1000)
waitms(500)
tone(50, 2000, 1000)
waitms(1050)
pub stop_robot()
' Shutdown robot, stop motors, stop cogs
running := 0
waitms(100)
fill_base($00_00_00_00)
fill_body($00_00_00_00)
fill_head($00_00_00_00)
fill_cam($00_00_00_00)
' TODO: Add stop code
cogstop(sens_cog)
pub check_serial() | index, i, v, vn, len, c
get_msg(@serial_buf) ' get message from serial
strs.split_str2(@serial_buf, "=", @cmdstr, @valstr) ' split cmd=val message
index := cmd_index(@cmdstr, @Commands, NumCmds) ' get cmd index from command table
case index
1:
rpi.str(@Version) ' robot_version (no value)
2:
head_color1 := hex2val(@valstr) ' head_color1
if head_effect == 1
fill_head(head_color1)
3: ' head_color2
head_color2 := hex2val(@valstr)
if head_effect == 2
fill_head(head_color2)
4: ' head_effect
head_effect := cmd_index(@valstr, @HeadEffects, NumHead)
if head_effect == 1
fill_head(head_color1)
elseif head_effect == 2
fill_head(head_color2)
5:
body_color1 := hex2val(@valstr) ' body_color1
if body_effect == 1
fill_body(body_color1)
6: ' body_color2
body_color2 := hex2val(@valstr)
if body_effect == 2
fill_body(body_color2)
7: ' body_effect
body_effect := cmd_index(@valstr, @BodyEffects, NumBody)
if body_effect == 1
fill_body(body_color1)
elseif body_effect == 2
fill_body(body_color2)
8: ' body_bright- (no value)
ifnot body_bright - BODY_INC < BODY_MIN
body_bright -= BODY_INC
9: ' body_bright+ (no value)
ifnot body_bright + BODY_INC > BODY_MAX
body_bright += BODY_INC
10: ' cam_brightness
fill_cam(leds.scale_rgbw(CameraColor, dec2val(@valstr)))
11: ' body_update
light_update_body := dec2val(@valstr)
12: ' head_x
pca.set_us(HEAD_PAN_CH, deg2us(map(dec2val(@valstr) + 60, 0, 120, HEAD_PAN_MIN, HEAD_PAN_MAX)))
13: ' head_y
pca.set_us(HEAD_TILT_CH, deg2us(map(dec2val(@valstr) + 60, 0, 120, HEAD_TILT_MIN, HEAD_TILT_MAX)))
14: ' base_color1
base_color1 := hex2val(@valstr)
if base_effect == 1
fill_base(base_color1)
15: ' base_color2
base_color2 := hex2val(@valstr)
if base_effect == 2
fill_base(base_color2)
16: ' base_effect
base_effect := cmd_index(@valstr, @BaseEffects, NumBase)
if base_effect == 1
fill_base(base_color1)
elseif base_effect == 2
fill_base(base_color2)
17: ' base_bright- (no value)
ifnot base_bright - BASE_INC < BASE_MIN
base_bright -= BASE_INC
18: ' base_bright+ (no value)
ifnot base_bright + BASE_INC > BASE_MAX
base_bright += BASE_INC
19: ' base_update
light_update_base := dec2val(@valstr)
20: ' left_us
21: ' right_us
22: ' shutdown
stop_robot()
23: ' head_update
light_update_head := dec2val(@valstr)
24: ' pinl
pinl(dec2val(@valstr))
25: ' pinh
pinh(dec2val(@valstr))
26: ' pint
pint(dec2val(@valstr))
27: ' get_clk
rpi.fstr1(string("CLK_FREQ=%dzh", 10), CLK_FREQ)
28: ' getms
rpi.fstr1(string("getms=%d", 10), getms())
29: ' stop
pub fill_body(color) | ch
repeat ch from BASE_LEDS to BODY_LEDS + BASE_LEDS - 1
leds.set(ch, color)
pub fill_base(color) | ch
repeat ch from 0 to BASE_LEDS-1
leds.set(ch, color)
pub fill_head(color) | ch
repeat ch from BODY_LEDS + BASE_LEDS to BODY_LEDS + BASE_LEDS + HEAD_LEDS - 1
leds.set(ch, color)
pub fill_cam(color) | ch
repeat ch from BODY_LEDS + BASE_LEDS + HEAD_LEDS to BODY_LEDS + BASE_LEDS + HEAD_LEDS + CAM_LEDS - 1
leds.set(ch, color)
pub set_base(ch, color)
leds.set(ch, color)
pub set_body(ch, color)
leds.set(ch+BASE_LEDS, color)
pub set_head(ch, color)
leds.set(ch + BASE_LEDS + BODY_LEDS, color)
pub setx_body(ch, color, level)
leds.setx(ch+BASE_LEDS, color, level)
pub setx_head(ch, color, level)
leds.setx(ch + BASE_LEDS + BODY_LEDS, color, level)
pub update_sensors() | level1, level2, voltage1, voltage2, r, tc, tf, p, h, roll, pitch, yaw
level1 := volt1.read()
level2 := volt2.read()
voltage1 := map(level1, 0, 1000, 0, 358)
voltage2 := map(level2, 0, 1000, 0, 358)
if MONITOR_BATTS
rpi.fstr2(string("batt_volts=%d,%d", 10), voltage1, voltage2)
waitms(2)
pub sensor_cog() | ta, tb, tc_left, tc_right, tc_robot, status
tsns.start(ONE_WIRE, tsns.PU_1K5) ' connect to 1W bus with internal pullups
status := tsns.reset() ' check for device
ifnot (status == tsns.GOOD_RESET)
case status
tsns.BUS_SHORT : error_state := 5
tsns.BAD_RESET : error_state := 6
tsns.NO_DEVICE : error_state := 7
repeat
if MONITOR_TEMPS
ta := getms()
tc_left := tsns.read_tca(@SN0) ' read from specific address
tc_right := tsns.read_tca(@SN1) ' read from specific address
tc_robot := tsns.read_tca(@SN2) ' read from specific address
rpi.fstr3(string("temps=%6.2f,%6.2f,%6.2f", 10), (tc_left+50)/100, (tc_right+50)/100, (tc_robot+50)/100)
tb := getms()
waitms(SENSOR_UPDATE_INT)
pub twinkle_head() | ch, level, idx
repeat ch from 0 to HEAD_LEDS-1 ' fill string
level := prng.randomize(0, 255) ' randomize level
setx_head(ch, head_color1, level) ' update channel
pub fade_head()
if head_animation_pos > 255
head_animation_pos := 0
if head_ani_phase > 1
head_ani_phase := 0
if head_animation_pos == 255
head_ani_phase := 1
elseif head_animation_pos == 0
head_ani_phase := 0
fill_head(leds.morph(head_color1, head_color2, head_animation_pos))
if head_ani_phase == 0
head_animation_pos++
else
head_animation_pos--
pub flash_head()
if head_animation_pos == 0
fill_head(head_color1)
head_animation_pos++
elseif head_animation_pos == 1
fill_head(head_color2)
head_animation_pos := 0
else
head_animation_pos := 0
pub jump3_head()
if head_animation_pos == 0
fill_head($FF_00_00_00)
head_animation_pos++
elseif head_animation_pos == 1
fill_head($00_FF_00_00)
head_animation_pos++
elseif head_animation_pos == 2
fill_head($00_00_FF_00)
head_animation_pos := 0
else
head_animation_pos := 0
pub flash_body()
if body_animation_pos == 0
fill_body(body_color1)
body_animation_pos++
elseif body_animation_pos == 1
fill_body(body_color2)
body_animation_pos := 0
else
body_animation_pos := 0
pub jump3_body()
if body_animation_pos == 0
fill_body($FF_00_00_00)
body_animation_pos++
elseif body_animation_pos == 1
fill_body($00_FF_00_00)
body_animation_pos++
elseif body_animation_pos == 2
fill_body($00_00_FF_00)
body_animation_pos := 0
else
body_animation_pos := 0
pub jump3_base()
if base_animation_pos == 0
fill_base($FF_00_00_00)
base_animation_pos++
elseif base_animation_pos == 1
fill_base($00_FF_00_00)
base_animation_pos++
elseif base_animation_pos == 2
fill_base($00_00_FF_00)
base_animation_pos := 0
else
base_animation_pos := 0
pub flash_base()
if base_animation_pos == 0
fill_base(base_color1)
base_animation_pos++
elseif base_animation_pos == 1
fill_base(base_color2)
base_animation_pos := 0
else
base_animation_pos := 0
pub twinkle_body() | ch, level, idx
repeat ch from 0 to BODY_LEDS - 1 ' fill body leds
level := prng.randomize(0, 255) ' randomize level
setx_body(ch, body_color1, level) ' update channel
pub twinkle_base() | ch, level, idx
repeat ch from 0 to BASE_LEDS-1 ' fill base leds
level := prng.randomize(0, 255) ' randomize level
leds.setx(ch, base_color1, level) ' update channel
pub fade_body()
if body_animation_pos > 255
body_animation_pos := 0
if body_ani_phase > 1
body_ani_phase := 0
if body_animation_pos == 255
body_ani_phase := 1
elseif body_animation_pos == 0
body_ani_phase := 0
fill_body(leds.morph(body_color1, body_color2, body_animation_pos))
if body_ani_phase == 0
body_animation_pos++
else
body_animation_pos--
pub fade_base()
if base_animation_pos > 255
base_animation_pos := 0
if base_ani_phase > 1
base_ani_phase := 0
if base_animation_pos == 255
base_ani_phase := 1
elseif base_animation_pos == 0
base_ani_phase := 0
fill_base(leds.morph(base_color1, base_color2, base_animation_pos))
if base_ani_phase == 0
base_animation_pos++
else
base_animation_pos--
pub swipe_body() | i, nf
if body_ani_phase == 0
set_body(body_animation_pos, body_color1)
if body_animation_pos == BODY_LEDS - 2
body_ani_phase := 1
body_animation_pos++
elseif body_ani_phase == 1
set_body(body_animation_pos, body_color2)
if body_animation_pos == 0
body_ani_phase := 0
body_animation_pos--
else
body_ani_phase := 0
pub swipe_base() | i, nf
if base_ani_phase == 0
leds.set(base_animation_pos, base_color1)
if base_animation_pos == BASE_LEDS - 1
base_ani_phase := 1
base_animation_pos++
elseif base_ani_phase == 1
leds.set(base_animation_pos-1, base_color2)
if base_animation_pos == 0
base_ani_phase := 0
base_animation_pos--
else
base_ani_phase := 0
pub rainbow_cycle_body() | ch
if ++body_animation_pos > 255
body_animation_pos := 0
repeat ch from 0 to BODY_LEDS - 1
set_body(ch, leds.scale_rgbw(leds.wheel(((ch * 256 / BODY_LEDS) + body_animation_pos) & $FF), body_bright))
pub rainbow_cycle_base() | ch
if ++base_animation_pos > 255
base_animation_pos := 0
repeat ch from 0 to BASE_LEDS - 1
leds.set(ch, leds.scale_rgbw(leds.wheel(((ch * 256 / BASE_LEDS) + base_animation_pos) & $FF), base_bright))
pub magic_cycle_body() | ch, pixel_index, color
if ++body_animation_pos > 255
body_animation_pos := 0
repeat ch from 0 to BODY_LEDS - 1
pixel_index := (ch * 256 / BODY_LEDS) + body_animation_pos
' 20 can be changed to control how many leds are in a color
color := qsin(1000, pixel_index / 4 - BODY_LEDS, 20)
' 120 and 200 is the range of colors
color := map(color, -1000, 1000, 120, 200)
set_body(ch, leds.scale_rgbw(leds.wheel(color & 255), body_bright))
pub magic_cycle_base() | ch, pixel_index, color
if ++base_animation_pos > 255
base_animation_pos := 0
repeat ch from 0 to BASE_LEDS - 1
pixel_index := (ch * 256 / BASE_LEDS) + base_animation_pos
' 20 can be changed to control how many leds are in a color
color := qsin(1000, pixel_index / 4 - BASE_LEDS, 20)
' 120 and 200 is the range of colors
color := map(color, -1000, 1000, 120, 200)
leds.set(ch, leds.scale_rgbw(leds.wheel(color & 255), base_bright))
pub fire_cycle_body() | ch, pixel_index, color
if ++body_animation_pos > 255
body_animation_pos := 0
repeat ch from 0 to BODY_LEDS - 1
pixel_index := (ch * 256 / BODY_LEDS) + body_animation_pos
' 20 can be changed to control how many leds are in a color
color := qsin(1000, pixel_index / 4 - BODY_LEDS, 20)
' 120 and 200 is the range of colors
color := map(color, -1000, 1000, 0, 20)
set_body(ch, leds.scale_rgbw(leds.wheel(color & 255), body_bright))
pub fire_cycle_base() | ch, pixel_index, color
if ++base_animation_pos > 255
base_animation_pos := 0
repeat ch from 0 to BASE_LEDS - 1
pixel_index := (ch * 256 / BASE_LEDS) + base_animation_pos
' 20 can be changed to control how many leds are in a color
color := qsin(1000, pixel_index / 4 - BASE_LEDS, 20)
' 120 and 200 is the range of colors
color := map(color, -1000, 1000, 0, 20)
leds.set(ch, leds.scale_rgbw(leds.wheel(color & 255), base_bright))