-
Notifications
You must be signed in to change notification settings - Fork 84
/
ProffieOS.ino
1703 lines (1527 loc) · 44.8 KB
/
ProffieOS.ino
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
/*
ProffieOS: Control software for lightsabers and other props.
http://fredrik.hubbe.net/lightsaber/teensy_saber.html
Copyright (c) 2016-2019 Fredrik Hubinette
Additional copyright holders listed inline below.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*-----------------------------------------------------------------*\
| You can have multiple configuration files, and specify which one |
| to use here by removing the two slashes at the beginning. |
| **NOTE** Only ONE line should be left uncommented at a time! |
| Add the slashes to any that you are not using. |
\*-----------------------------------------------------------------*/
// #define CONFIG_FILE "config/YOUR_CONFIG_FILE_NAME_HERE.h"
// #define CONFIG_FILE "config/default_proffieboard_config.h"
// #define CONFIG_FILE "config/proffieboard_v1_test_bench_config.h"
// #define CONFIG_FILE "config/proffieboard_v2_testing_config.h"
// #define CONFIG_FILE "config/td_proffieboard_config.h"
// #define CONFIG_FILE "config/proffieboard_v1_graflex.h"
// #define CONFIG_FILE "config/teensy_audio_shield_micom.h"
// #define CONFIG_FILE "config/proffieboard_v2_ob4.h"
#ifndef CONFIG_FILE
#error Please set CONFIG_FILE as shown above.
#endif
#include "common/resources.h"
#define CONFIG_TOP
#include CONFIG_FILE
#undef CONFIG_TOP
#include "common/capabilities.h"
#if !defined(ENABLE_AUDIO) && !defined(DISABLE_AUDIO)
#define ENABLE_AUDIO
#endif
#if !defined(ENABLE_MOTION) && !defined(DISABLE_MOTION)
#define ENABLE_MOTION
#endif
#if !defined(ENABLE_WS2811) && !defined(DISABLE_WS2811)
#define ENABLE_WS2811
#endif
#if !defined(ENABLE_SD) && !defined(DISABLE_SD)
#define ENABLE_SD
#endif
#if !defined(KILL_OLD_PLAYERS) && !defined(DISABLE_KILL_OLD_PLAYERS)
#define KILL_OLD_PLAYERS
#endif
#if !defined(NO_REPEAT_RANDOM) && !defined(DISABLE_NO_REPEAT_RANDOM)
#define NO_REPEAT_RANDOM
#endif
#ifndef BOOT_VOLUME
#define BOOT_VOLUME VOLUME
#endif
#ifndef FONT_PATTERN
#define FONT_PATTERN "*;common"
#endif
#ifdef SAVE_STATE
#define SAVE_VOLUME
#define SAVE_PRESET
#define SAVE_COLOR_CHANGE
#define SAVE_BLADE_DIMMING
#endif
#ifdef ENABLE_ALL_EDIT_OPTIONS
#define DYNAMIC_BLADE_LENGTH
#define DYNAMIC_BLADE_DIMMING
#define DYNAMIC_CLASH_THRESHOLD
#define SAVE_VOLUME
#define SAVE_BLADE_DIMMING
#define SAVE_CLASH_THRESHOLD
#define SAVE_COLOR_CHANGE
#endif
// #define ENABLE_DEBUG
#ifdef KEEP_SAVEFILES_WHEN_PROGRAMMING
#warning Your config file has KEEP_SAVEFILES_WHEN_PROGRAMMING in it. If you experience problems, please remove it and try again before asking for help. For more information, see: https://pod.hubbe.net/config/keeping-edits-when-uploading.html
#endif
//
// OVERVIEW
//
// Here explain some general code concepts to make it easier
// to understand the code below.
//
// Most things start with the ProbBase class. Depending on the
// configuration, this class is extended by the Saber class,
// the Detonator class, or some other class. The extended class
// is instantiated as "prop", and is responsible for handling
// button clicks, clashes, swings and other events. These events
// are then send to all registered SaberBase classes.
///
// Generally speaking, there are usually two registered SaberBase
// classes listening for events. One for sound and one for
// the blade. Sound and blade effects are generally executed
// separately by separate clases.
//
// Blades are generally handled by one of the child-classes of
// BladeBase. These classes know how many LEDs the current
// blade has, and how to set those LEDs to a given color, but
// they don't actually decide what the blade should look like.
// Instead they just call the current BladeStyle class and
// asks it to set the colors. The BladeStyle classes don't
// need to know what kind of blade is attached, although
// some combinations of BladeBases and BladeStyles just don't
// make any sense.
//
// Sounds are also abstracted. It starts with scanning a directory
// on the SD card for files that match known patterns of file names.
// The Effect class is responsible for keeping track of all numbered
// files that for a particular filename prefix.
//
// Once the directory has been scanned, we'll decide how to play
// sounds. In the past, there was one class for handling NEC style
// fonts and another for handling Plecter style fonts. However,
// both of those have now been merged into the HybridFont class
// which can do both. It is also capable of doing some mix and matching,
// so you can use a plecter style hum together with a NEC style
// swing if you so desire. The HybridFont class inherit from
// SaberBase and listen on on/off/clash/etc. events, just like
// BladeBase classes do.
//
// HybridFont tells the audio subsystem
// to trigger and mix sounds as aproperiate. The sound subsystem
// starts with an DMA channel which feeds data to a digital-to-analog
// converter. Once the data buffer is half-gone, and interrupt is
// triggered in the DAC class, which tries to fill it up by
// reading data from a int16_t AudioStream. Generally, that data
// stream is hooked up to the AudioDynamicMixer class. This
// class is responsible for taking multiple audio inputs,
// summing them up and then adjusting the volume to minimize
// clipping.
// TODO LIST:
// stab detect/effect
//
// Audio work items:
// select clash from force
// stab effect
// Blade stuff
// better clash
// Allow several blades to share power pins.
// If defined, DAC vref will be 3 volts, resulting in louder sound. (teensy only)
#define LOUD
// You can get better SD card performance by
// activating the USE_TEENSY3_OPTIMIZED_CODE define
// in SD.h in the teensy library, however, my sd card
// did not work with that define.
#include <Arduino.h>
#if !defined(ENABLE_SD)
// No SD support, no mount_sd_setting
#undef MOUNT_SD_SETTING
#endif
#if !defined(USB_CLASS_MSC)
// No mass storage, no mount_sd_setting
#undef MOUNT_SD_SETTING
#endif
#ifdef TEENSYDUINO
#include <DMAChannel.h>
#include <usb_dev.h>
#ifndef USE_TEENSY4
#include <kinetis.h>
#include <i2c_t3.h>
#else
// This is a hack to let me access the internal stuff..
#define private public
#include <Wire.h>
#undef private
#endif
#include <SD.h>
#include <SPI.h>
#ifdef abs
#undef abs
namespace {
template<typename T> constexpr auto abs(T x) -> decltype(-x) {
return x < 0 ? -x : x;
}
}
#endif
#else // TEENSYDUINO
#define digitalWriteFast digitalWrite
#endif // TEENSYDUINO
#ifdef ARDUINO_ARCH_STM32L4
// This is a hack to let me access the internal stuff..
#define private public
#include <Wire.h>
#undef private
#include <FS.h>
#include <stm32l4_wiring_private.h>
#include <stm32l4xx.h>
#include <armv7m.h>
#include <stm32l4_gpio.h>
#include <stm32l4_sai.h>
#include <stm32l4_dma.h>
#include <stm32l4_system.h>
#include <arm_math.h>
#include <STM32.h>
#define DMAChannel stm32l4_dma_t
#define DMAMEM
#define NVIC_SET_PRIORITY(X, Y) NVIC_SetPriority((X), (IRQn_Type)(Y))
#else // ARDUINO_ARCH_STM32L4
#define INPUT_ANALOG INPUT
#endif // ARDUINO_ARCH_STM32L4
#include <math.h>
#include <malloc.h>
#ifdef ENABLE_SERIALFLASH
// This is a hack to let me access the internal stuff..
#define private public
#define protected public
#include <SerialFlash.h>
#undef private
#undef protected
#endif
#ifdef ENABLE_SNOOZE
#define startup_early_hook DISABLE_startup_early_hook
#include <Snooze.h>
#undef startup_early_hook
SnoozeTimer snooze_timer;
SnoozeDigital snooze_digital;
SnoozeTouch snooze_touch;
SnoozeBlock snooze_config(snooze_touch, snooze_digital, snooze_timer);
#endif
const char version[] = "$Id: ce12a06a1e236b5101ec60c950530a9a4719a74d $";
#include "common/common.h"
#include "common/state_machine.h"
#include "common/monitoring.h"
#include "common/stdout.h"
#include "common/errors.h"
Monitoring monitor;
DEFINE_COMMON_STDOUT_GLOBALS;
void PrintQuotedValue(const char* name, const char* str) {
STDOUT.print(name);
STDOUT.write('=');
if (str) {
while (*str) {
switch (*str) {
case '\n':
STDOUT.print("\\n");
break;
case '\t':
STDOUT.print("\\t");
break;
case '\\':
STDOUT.write('\\');
default:
STDOUT.write(*str);
}
++str;
}
}
STDOUT.write('\n');
}
#ifdef ENABLE_DEBUG
// This class is really useful for finding crashes
// basically, the pin you give it will be held high
// while this function is running. After that it will
// be set to low. If a crash occurs in this function
// it will stay high.
class ScopedPinTracer {
public:
explicit ScopedPinTracer(int pin)
: pin_(pin) {
pinMode(pin_, OUTPUT);
digitalWriteFast(pin, HIGH);
}
~ScopedPinTracer() {
digitalWriteFast(pin_, LOW);
}
private:
int pin_;
};
class ScopedTracer3 {
public:
explicit ScopedTracer3(int code) {
pinMode(bladePowerPin1, OUTPUT);
pinMode(bladePowerPin2, OUTPUT);
pinMode(bladePowerPin3, OUTPUT);
digitalWriteFast(bladePowerPin1, !!(code & 1));
digitalWriteFast(bladePowerPin2, !!(code & 2));
digitalWriteFast(bladePowerPin3, !!(code & 4));
}
~ScopedTracer3() {
digitalWriteFast(bladePowerPin1, LOW);
digitalWriteFast(bladePowerPin2, LOW);
digitalWriteFast(bladePowerPin3, LOW);
}
};
#endif
#include "common/scoped_cycle_counter.h"
#include "common/profiling.h"
uint64_t audio_dma_interrupt_cycles = 0;
uint64_t pixel_dma_interrupt_cycles = 0;
uint64_t motion_interrupt_cycles = 0;
uint64_t wav_interrupt_cycles = 0;
uint64_t loop_cycles = 0;
#include "common/loop_counter.h"
#if defined(ENABLE_SSD1306) || defined(INCLUDE_SSD1306)
#define ENABLE_DISPLAY_CODE
#endif
#ifdef DOSFS_CONFIG_STARTUP_DELAY
#define PROFFIEOS_SD_STARTUP_DELAY DOSFS_CONFIG_STARTUP_DELAY
#else
#define PROFFIEOS_SD_STARTUP_DELAY 1000
#endif
#ifndef CONFIG_STARTUP_DELAY
#define CONFIG_STARTUP_DELAY 0
#endif
#if PROFFIEOS_SD_STARTUP_DELAY > CONFIG_STARTUP_DELAY
#define PROFFIEOS_STARTUP_DELAY PROFFIEOS_SD_STARTUP_DELAY
#else
#define PROFFIEOS_STARTUP_DELAY CONFIG_STARTUP_DELAY
#endif
#include "common/linked_list.h"
#include "common/looper.h"
#include "common/command_parser.h"
#include "common/monitor_helper.h"
CommandParser* parsers = NULL;
MonitorHelper monitor_helper;
#include "common/vec3.h"
#include "common/quat.h"
#include "common/ref.h"
#include "common/events.h"
#include "common/saber_base.h"
#include "common/saber_base_passthrough.h"
SaberBase* saberbases = NULL;
SaberBase::LockupType SaberBase::lockup_ = SaberBase::LOCKUP_NONE;
SaberBase::ColorChangeMode SaberBase::color_change_mode_ =
SaberBase::COLOR_CHANGE_MODE_NONE;
uint32_t SaberBase::last_motion_request_ = 0;
uint32_t SaberBase::current_variation_ = 0;
float SaberBase::sound_length = 0.0;
int SaberBase::sound_number = -1;
float SaberBase::clash_strength_ = 0.0;
#ifdef DYNAMIC_BLADE_DIMMING
int SaberBase::dimming_ = 16384;
#endif
#include "common/box_filter.h"
#include "common/math.h"
#include "common/sin_table.h"
void EnableBooster();
void EnableAmplifier();
bool AmplifierIsActive();
void MountSDCard();
const char* GetSaveDir();
#include "common/lsfs.h"
#include "common/strfun.h"
// Double-zero terminated array of search paths.
// No trailing slashes!
char current_directory[128];
const char* next_current_directory(const char* dir) {
dir += strlen(dir);
dir++;
if (!*dir) return NULL;
return dir;
}
const char* last_current_directory() {
const char* ret = current_directory;
while (true) {
const char* tmp = next_current_directory(ret);
if (!tmp) return ret;
ret = tmp;
}
}
const char* previous_current_directory(const char* dir) {
if (dir == current_directory) return nullptr;
dir -= 2;
while (true) {
if (dir == current_directory) return current_directory;
if (!*dir) return dir + 1;
dir--;
}
}
#include "sound/sound.h"
#include "common/battery_monitor.h"
#include "common/color.h"
#include "common/range.h"
#include "common/fuse.h"
#include "common/config_file.h"
#include "blades/blade_base.h"
#include "blades/blade_wrapper.h"
class MicroEventTime {
void SetToNow() {
micros_ = micros();
millis_ = millis();
}
uint32_t millis_since() {
return millis() - millis_;
}
uint32_t micros_since() {
if (millis_since() > (0xFFFF0000UL / 1000)) return 0xFFFFFFFFUL;
return micros() - micros_;
}
private:
uint32_t millis_;
uint32_t micros_;
};
template<class T, class U>
struct is_same_type { static const bool value = false; };
template<class T>
struct is_same_type<T, T> { static const bool value = true; };
// This really ought to be a typedef, but it causes problems I don't understand.
#define StyleAllocator class StyleFactory*
#include "styles/rgb.h"
#include "styles/rgb_arg.h"
#include "styles/charging.h"
#include "styles/fire.h"
#include "styles/sparkle.h"
#include "styles/gradient.h"
#include "styles/random_flicker.h"
#include "styles/random_per_led_flicker.h"
#include "styles/audio_flicker.h"
#include "styles/brown_noise_flicker.h"
#include "styles/hump_flicker.h"
#include "styles/rainbow.h"
#include "styles/color_cycle.h"
#include "styles/cylon.h"
#include "styles/ignition_delay.h"
#include "styles/retraction_delay.h"
#include "styles/pulsing.h"
#include "styles/blinking.h"
#include "styles/on_spark.h"
#include "styles/rgb_cycle.h"
#include "styles/clash.h"
#include "styles/lockup.h" // Also does "drag"
#include "styles/blast.h"
#include "styles/strobe.h"
#include "styles/inout_helper.h"
#include "styles/inout_sparktip.h"
#include "styles/colors.h"
#include "styles/mix.h"
#include "styles/style_ptr.h"
#include "styles/file.h"
#include "styles/stripes.h"
#include "styles/random_blink.h"
#include "styles/sequence.h"
#include "styles/byteorder.h"
#include "styles/rotate_color.h"
#include "styles/colorchange.h"
#include "styles/transition_pulse.h"
#include "styles/transition_effect.h"
#include "styles/transition_loop.h"
#include "styles/effect_sequence.h"
#include "styles/color_select.h"
#include "styles/remap.h"
#include "styles/edit_mode.h"
#include "styles/pixelate.h"
#include "styles/display.h"
// functions
#include "functions/ifon.h"
#include "functions/change_slowly.h"
#include "functions/int.h"
#include "functions/int_arg.h"
#include "functions/int_select.h"
#include "functions/sin.h"
#include "functions/scale.h"
#include "functions/battery_level.h"
#include "functions/trigger.h"
#include "functions/bump.h"
#include "functions/smoothstep.h"
#include "functions/swing_speed.h"
#include "functions/sound_level.h"
#include "functions/blade_angle.h"
#include "functions/variation.h"
#include "functions/twist_angle.h"
#include "functions/layer_functions.h"
#include "functions/islessthan.h"
#include "functions/circular_section.h"
#include "functions/marble.h"
#include "functions/slice.h"
#include "functions/mult.h"
#include "functions/wavlen.h"
#include "functions/wavnum.h"
#include "functions/effect_position.h"
#include "functions/time_since_effect.h"
#include "functions/sum.h"
#include "functions/ramp.h"
#include "functions/center_dist.h"
#include "functions/linear_section.h"
#include "functions/hold_peak.h"
#include "functions/clash_impact.h"
#include "functions/effect_increment.h"
#include "functions/increment.h"
#include "functions/subtract.h"
#include "functions/divide.h"
#include "functions/isbetween.h"
#include "functions/clamp.h"
#include "functions/alt.h"
#include "functions/volume_level.h"
#include "functions/mod.h"
#include "functions/readpin.h"
#include "functions/bullet_count.h"
#include "functions/blaster_mode.h"
// transitions
#include "transitions/fade.h"
#include "transitions/join.h"
#include "transitions/concat.h"
#include "transitions/instant.h"
#include "transitions/delay.h"
#include "transitions/wipe.h"
#include "transitions/join.h"
#include "transitions/boing.h"
#include "transitions/random.h"
#include "transitions/colorcycle.h"
#include "transitions/wave.h"
#include "transitions/select.h"
#include "transitions/extend.h"
#include "transitions/center_wipe.h"
#include "transitions/sequence.h"
#include "transitions/blink.h"
#include "transitions/doeffect.h"
#include "transitions/loop.h"
#include "styles/legacy_styles.h"
//responsive styles
#include "styles/responsive_styles.h"
#include "styles/pov.h"
class NoLED;
#include "blades/power_pin.h"
#include "blades/drive_logic.h"
#include "blades/pwm_pin.h"
#include "blades/ws2811_blade.h"
#include "blades/fastled_blade.h"
#include "blades/leds.h"
#include "blades/simple_blade.h"
#include "blades/saviblade.h"
#include "blades/sub_blade.h"
#include "blades/dim_blade.h"
#include "blades/blade_id.h"
#include "common/preset.h"
#include "common/blade_config.h"
#include "common/current_preset.h"
#include "common/status_led.h"
#include "styles/style_parser.h"
#include "styles/length_finder.h"
#include "styles/show_color.h"
#include "styles/blade_shortener.h"
#include "sound/sound_library.h"
#include "modes/mode.h"
#include "modes/stepped_mode.h"
#include "modes/color_change_modes.h"
#include "modes/menu_list.h"
#include "modes/bool_setting.h"
#include "modes/color_menues.h"
#include "modes/sorted_list_menues.h"
#include "modes/preset_modes.h"
#include "modes/style_option_modes.h"
#include "modes/settings_menues.h"
#include "modes/default_spec.h"
BladeConfig* current_config = nullptr;
class BladeBase* GetPrimaryBlade() {
#if NUM_BLADES == 0
return nullptr;
#else
return current_config->blade1;
#endif
}
#define BLADE_NUMBER_FINDER(N) if (current_config->blade##N == blade) return N;
// Returns 1..NUM_BLADES (0 if not found)
int GetBladeNumber(BladeBase *blade) {
ONCEPERBLADE(BLADE_NUMBER_FINDER);
return 0;
}
#define RETURN_BLADE_BY_NUMBER(N) case N: return current_config->blade##N;
// 1 is first blade
BladeBase* GetBladeByNumber(int n) {
if (!current_config) return nullptr;
switch (n) {
ONCEPERBLADE(RETURN_BLADE_BY_NUMBER);
}
return nullptr;
}
const char* GetSaveDir() {
if (!current_config) return "";
if (!current_config->save_dir) return "";
return current_config->save_dir;
}
ArgParserInterface* CurrentArgParser;
#define CONFIG_STYLES
#include CONFIG_FILE
#undef CONFIG_STYLES
#define CONFIG_PRESETS
#include CONFIG_FILE
#undef CONFIG_PRESETS
#define CONFIG_PROP
#include CONFIG_FILE
#undef CONFIG_PROP
#ifndef PROP_TYPE
#include "props/saber.h"
#endif
PROP_TYPE prop;
#ifdef BLADE_ID_SCAN_MILLIS
bool ScanBladeIdNow() {
return prop.ScanBladeIdNow();
}
#endif
#ifdef BLASTER_SHOTS_UNTIL_EMPTY
int prop_GetBulletCount() {
return prop.GetBulletCount();
}
#endif
#ifdef PROP_HAS_GETBLASTERMODE
int prop_GetBlasterMode() {
return prop.GetBlasterMode();
}
#endif
#ifdef DYNAMIC_CLASH_THRESHOLD
int prop_GetCurrentClashThreshold() {
return prop.GetCurrentClashThreshold();
}
void prop_SetClashThreshold(int clash_threshold) {
prop.SetClashThreshold(clash_threshold);
}
#endif
void prop_SaveState() { prop.SaveState(); }
void prop_UpdateStyle() { prop.UpdateStyle(); }
#ifdef DYNAMIC_BLADE_LENGTH
int prop_GetBladeLength(int blade) {
return prop.GetBladeLength(blade);
}
int prop_GetMaxBladeLength(int blade) {
return prop.GetMaxBladeLength(blade);
}
void prop_SetBladeLength(int blade, int len) {
prop.SetBladeLength(blade, len);
}
#endif
int prop_GetPresetPosition() {
return prop.GetPresetPosition();
}
void prop_MovePreset(int position) {
prop.MovePreset(position);
}
const char* GetStyle(int blade) { return prop.GetStyle(blade); }
void SetStyle(int blade, LSPtr<char> style);
void SetStyle(int blade, LSPtr<char> style) { prop.SetStyle(blade, std::move(style)); }
void SetFont(const char* font) { prop.SetFont(font); }
void SetTrack(const char* track) { prop.SetTrack(track); }
const char* GetFont() { return prop.GetFont(); }
const char* GetTrack() { return prop.GetTrack(); }
void chdir(const StringPiece font) { prop.chdir(font); }
#if 0
#include "scripts/test_motion_timeout.h"
#warning MOTION TEST SCRIPT ACTIVE
MotionTimeoutScript script;
#endif
#if 0
#include "scripts/v3_test_script.h"
#warning !!! V3 TEST SCRIPT ACTIVE !!!
V3TestScript script;
#endif
#include "buttons/floating_button.h"
#include "buttons/latching_button.h"
#include "buttons/button.h"
#ifdef TEENSYDUINO
#include "buttons/touchbutton.h"
#endif
#ifdef ARDUINO_ARCH_STM32L4
#include "buttons/stm32l4_touchbutton.h"
#endif
#include "buttons/rotary.h"
#include "buttons/pots.h"
#include "buttons/fast_button.h"
#include "ir/ir.h"
#include "ir/receiver.h"
#include "ir/blaster.h"
#include "ir/print.h"
#include "ir/nec.h"
#include "ir/rc6.h"
#include "ir/stm32_ir.h"
#ifndef TEENSYDUINO
uint32_t startup_AHB1ENR;
uint32_t startup_AHB2ENR;
uint32_t startup_AHB3ENR;
uint32_t startup_APB1ENR1;
uint32_t startup_APB1ENR2;
uint32_t startup_APB2ENR;
uint32_t startup_MODER[4];
#endif
#define CONFIG_BUTTONS
#include CONFIG_FILE
#undef CONFIG_BUTTONS
#ifdef BLADE_DETECT_PIN
LatchingButtonTemplate<FloatingButtonBase<BLADE_DETECT_PIN>>
BladeDetect(BUTTON_BLADE_DETECT, BLADE_DETECT_PIN, "blade_detect");
USE_PIN_OUTPUT(BLADE_DETECT_PIN, PO_SubSystems::PO_BLADE_DETECT);
#endif
#include "common/sd_test.h"
class I2CDevice;
class Commands : public CommandParser {
public:
enum PinType {
PinTypeFloating,
PinTypePulldown,
PinTypeCap,
PinTypeOther,
};
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
File current_file;
#endif
bool TestPin(int pin, PinType t) {
int ret = 0;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(20);
ret <<= 1;
ret |= digitalRead(pin);
digitalWrite(pin, HIGH);
delayMicroseconds(20);
ret <<= 1;
ret |= digitalRead(pin);
// Discharge time
pinMode(pin, INPUT_PULLDOWN);
uint32_t start = micros();
uint32_t end;
while (digitalRead(pin)) {
end = micros();
if (end - start > 32768) break; // 32 millis
}
ret <<= 16;
ret |= (end - start);
pinMode(pin, INPUT_PULLUP);
delayMicroseconds(20);
ret <<= 1;
ret |= digitalRead(pin);
pinMode(pin, INPUT);
return ret;
}
bool Parse(const char* cmd, const char* e) override {
#ifdef ENABLE_SERIALFLASH
if (!strcmp(cmd, "ls")) {
char tmp[128];
SerialFlashChip::opendir();
uint32_t size;
while (SerialFlashChip::readdir(tmp, sizeof(tmp), size)) {
STDOUT.print(tmp);
STDOUT.print(" ");
STDOUT.println(size);
}
STDOUT.println("Done listing files.");
return true;
}
if (!strcmp(cmd, "rm")) {
if (SerialFlashChip::remove(e)) {
STDOUT.println("Removed.\n");
} else {
STDOUT.println("No such file.\n");
}
return true;
}
if (!strcmp(cmd, "format")) {
STDOUT.print("Erasing ... ");
SerialFlashChip::eraseAll();
while (!SerialFlashChip::ready())
;
STDOUT.println("Done");
return true;
}
#endif
#ifdef ENABLE_SD
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
if (!strcmp(cmd, "dir")) {
LOCK_SD(true);
if (!e || LSFS::Exists(e)) {
for (LSFS::Iterator dir(e ? e : ""); dir; ++dir) {
STDOUT.print(dir.name());
STDOUT.print(" ");
STDOUT.println(dir.size());
}
STDOUT.println("Done listing files.");
} else {
STDOUT.println("No such directory.");
}
LOCK_SD(false);
return true;
}
#endif
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
// TODO: Maybe use the commands from the workbench?
if (!strcmp(cmd, "cat") && e) {
LOCK_SD(true);
File f = LSFS::Open(e);
while (f.available()) {
STDOUT.write(f.read());
}
f.close();
LOCK_SD(false);
return true;
}
if (!strcmp(cmd, "openfile") && e) {
LOCK_SD(true);
current_file = LSFS::OpenRW(e);
LOCK_SD(false);
return true;
}
if (!strcmp(cmd, "closefile")) {
LOCK_SD(true);
current_file.close();
LOCK_SD(false);
return true;
}
if (!strcmp(cmd, ">>")) {
LOCK_SD(true);
current_file.println(e ? e : "");
LOCK_SD(false);
return true;
}
#endif
if (!strcmp(cmd, "del") && e) {
LOCK_SD(true);
LSFS::Remove(e);
LOCK_SD(false);
return true;
}
#ifdef ENABLE_DEVELOPER_COMMANDS
if (!strcmp(cmd, "readalot")) {
uint8_t tmp[10];
LOCK_SD(true);
File f = LSFS::Open(e);
for (int i = 0; i < 10000; i++) {
f.seek(0);
f.read(tmp, 10);
f.seek(1000);
f.read(tmp, 10);
}
f.close();
LOCK_SD(false);
STDOUT.println("Done");
return true;
}
#endif // ENABLE_DEVELOPER_COMMANDS
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
if (!strcmp(cmd, "sdtest")) {
SDTestHelper sdtester;
if (e && !strcmp(e, "all")) {
sdtester.TestDir("");
} else {
sdtester.TestFont();
}
return true;
}
#endif
#endif // ENABLE_SD
#if defined(ENABLE_SD) && defined(ENABLE_SERIALFLASH)
if (!strcmp(cmd, "cache")) {
LOCK_SD(true);
File f = LSFS::Open(e);
if (!f) {
STDOUT.println("File not found.");
return true;
}
int bytes = f.size();
if (!SerialFlashChip::create(e, bytes)) {
STDOUT.println("Not enough space on serial flash chip.");
return true;
}
SerialFlashFile o = SerialFlashChip::open(e);
while (bytes) {
char tmp[256];
int b = f.read(tmp, min(bytes, (int)NELEM(tmp)));
o.write(tmp, b);
bytes -= b;
}
LOCK_SD(false);
STDOUT.println("Cached!");
return true;
}
#endif
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
if (!strcmp(cmd, "effects")) {
Effect::ShowAll();
return true;
}
#endif