forked from profezzorn/ProffieOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProffieOS.ino
1808 lines (1640 loc) · 48.9 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.
#define CONFIG_FILE "config/default_proffieboard_config.h"
// #define CONFIG_FILE "config/default_v3_config.h"
// #define CONFIG_FILE "config/crossguard_config.h"
// #define CONFIG_FILE "config/graflex_v1_config.h"
// #define CONFIG_FILE "config/prop_shield_fastled_v1_config.h"
// #define CONFIG_FILE "config/owk_v2_config.h"
// #define CONFIG_FILE "config/test_bench_config.h"
// #define CONFIG_FILE "config/toy_saber_config.h"
// #define CONFIG_FILE "config/proffieboard_v1_test_bench_config.h"
// #define CONFIG_FILE "config/td_proffieboard_config.h"
// #define CONFIG_FILE "config/teensy_audio_shield_micom.h"
// #define CONFIG_FILE "config/proffieboard_v2_ob4.h"
#ifdef CONFIG_FILE_TEST
#undef CONFIG_FILE
#define CONFIG_FILE CONFIG_FILE_TEST
#endif
#define CONFIG_TOP
#include CONFIG_FILE
#undef CONFIG_TOP
#ifdef SAVE_STATE
#define SAVE_VOLUME
#define SAVE_PRESET
#define SAVE_COLOR_CHANGE
#endif
// #define ENABLE_DEBUG
//
// 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>
#ifdef TEENSYDUINO
#include <DMAChannel.h>
#include <usb_dev.h>
#ifndef USE_TEENSY4
#include <kinetis.h>
#endif
#include <i2c_t3.h>
#include <SD.h>
#include <SPI.h>
#define INPUT_ANALOG INPUT
#else
// This is a hack to let me access the internal stuff..
#define private public
#include <Wire.h>
#undef private
#include <FS.h>
#define digitalWriteFast digitalWrite
#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))
#endif
#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 $";
const char install_time[] = __DATE__ " " __TIME__;
#include "common/state_machine.h"
#include "common/monitoring.h"
#include "common/stdout.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 wav_interrupt_cycles = 0;
uint64_t loop_cycles = 0;
#include "common/loop_counter.h"
#define NELEM(X) (sizeof(X)/sizeof((X)[0]))
#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;
bool SaberBase::on_ = false;
uint32_t SaberBase::last_motion_request_ = 0;
uint32_t SaberBase::current_variation_ = 0;
#include "common/box_filter.h"
// Returns the decimals of a number, ie 12.2134 -> 0.2134
float fract(float x) { return x - floorf(x); }
// clamp(x, a, b) makes sure that x is between a and b.
float clamp(float x, float a, float b) {
if (x < a) return a;
if (x > b) return b;
return x;
}
float Fmod(float a, float b) {
return a - floorf(a / b) * b;
}
int32_t clampi32(int32_t x, int32_t a, int32_t b) {
if (x < a) return a;
if (x > b) return b;
return x;
}
int16_t clamptoi16(int32_t x) {
return clampi32(x, -32768, 32767);
}
int32_t clamptoi24(int32_t x) {
return clampi32(x, -8388608, 8388607);
}
#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;
}
#include "sound/sound.h"
#include "common/battery_monitor.h"
#include "common/color.h"
#include "common/range.h"
#include "common/fuse.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_effect.h"
#include "styles/transition_loop.h"
#include "styles/effect_sequence.h"
// functions
#include "functions/ifon.h"
#include "functions/change_slowly.h"
#include "functions/int.h"
#include "functions/int_arg.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"
// 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"
//responsive styles
#include "styles/responsive_styles.h"
// This macro has a problem with commas, please don't use it.
#define EASYBLADE(COLOR, CLASH_COLOR) \
SimpleClash<Lockup<Blast<COLOR, WHITE>, AudioFlicker<COLOR, WHITE> >, CLASH_COLOR>
// Use EasyBlade<COLOR, CLASH_COLOR> instead of EASYBLADE(COLOR, CLASH_COLOR)
template<class color, class clash_color, class lockup_flicker_color = WHITE>
using EasyBlade = SimpleClash<Lockup<Blast<color, WHITE>, AudioFlicker<color, lockup_flicker_color> >, clash_color>;
// The following functions are mostly for illustration.
// The templates above gives you more power and functionality.
// Arguments: color, clash color, turn-on/off time
template<class base_color,
class clash_color,
int out_millis,
int in_millis,
class lockup_flicker_color = WHITE,
class blast_color = WHITE>
StyleAllocator StyleNormalPtr() {
#if 0
typedef AudioFlicker<base_color, lockup_flicker_color> AddFlicker;
typedef Blast<base_color, blast_color> AddBlast;
typedef Lockup<AddBlast, AddFlicker> AddLockup;
typedef SimpleClash<AddLockup, clash_color> AddClash;
return StylePtr<InOutHelper<AddClash, out_millis, in_millis> >();
#else
typedef Layers<base_color,
SimpleClashL<clash_color>,
LockupL<AudioFlickerL<lockup_flicker_color> >,
BlastL<blast_color> > Blade;
return StylePtr<InOutHelper<Blade, out_millis, in_millis> >();
#endif
}
// Arguments: color, clash color, turn-on/off time
template<class base_color,
class clash_color,
class out_millis,
class in_millis,
class lockup_flicker_color = WHITE,
class blast_color = WHITE>
StyleAllocator StyleNormalPtrX() {
typedef AudioFlicker<base_color, lockup_flicker_color> AddFlicker;
typedef Blast<base_color, blast_color> AddBlast;
typedef Lockup<AddBlast, AddFlicker> AddLockup;
typedef SimpleClash<AddLockup, clash_color> AddClash;
return StylePtr<InOutHelperX<AddClash, InOutFuncX<out_millis, in_millis>> >();
}
// Rainbow blade.
// Arguments: color, clash color, turn-on/off time
template<int out_millis,
int in_millis,
class clash_color = WHITE,
class lockup_flicker_color = WHITE>
StyleAllocator StyleRainbowPtr() {
typedef AudioFlicker<Rainbow, lockup_flicker_color> AddFlicker;
typedef Lockup<Rainbow, AddFlicker> AddLockup;
typedef SimpleClash<AddLockup, clash_color> AddClash;
return StylePtr<InOutHelper<AddClash, out_millis, in_millis> >();
}
// Rainbow blade.
// Arguments: color, clash color, turn-on/off time
template<class out_millis,
class in_millis,
class clash_color = WHITE,
class lockup_flicker_color = WHITE>
StyleAllocator StyleRainbowPtrX() {
typedef AudioFlicker<Rainbow, lockup_flicker_color> AddFlicker;
typedef Lockup<Rainbow, AddFlicker> AddLockup;
typedef SimpleClash<AddLockup, clash_color> AddClash;
return StylePtr<InOutHelperX<AddClash, InOutFuncX<out_millis, in_millis>> >();
}
// Stroboscope, flickers the blade at the desired frequency.
// Arguments: color, clash color, turn-on/off time
template<class strobe_color,
class clash_color,
int frequency,
int out_millis,
int in_millis>
StyleAllocator StyleStrobePtr() {
typedef Strobe<BLACK, strobe_color, frequency, 1> strobe;
typedef Strobe<BLACK, strobe_color, 3* frequency, 1> fast_strobe;
typedef Lockup<strobe, fast_strobe> AddLockup;
typedef SimpleClash<AddLockup, clash_color> clash;
return StylePtr<InOutHelper<clash, out_millis, in_millis> >();
}
#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/simple_blade.h"
#include "blades/sub_blade.h"
#include "blades/dim_blade.h"
#include "blades/leds.h"
#include "blades/blade_id.h"
#include "common/preset.h"
#include "common/blade_config.h"
#include "common/current_preset.h"
#include "styles/style_parser.h"
#include "styles/length_finder.h"
BladeConfig* current_config = nullptr;
class BladeBase* GetPrimaryBlade() {
#if NUM_BLADES == 0
return nullptr;
#else
return current_config->blade1;
#endif
}
const char* GetSaveDir() {
if (!current_config) return "";
if (!current_config->save_dir) return "";
return current_config->save_dir;
}
ArgParserInterface* CurrentArgParser;
#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;
#if 0
#include "scripts/v3_test_script.h"
#warning !!! V3 TEST SCRIPT ACTIVE !!!
V3TestScript script;
#endif
#if 0
#include "scripts/proffieboard_test_script.h"
#warning !!! PROFFIEBOARD TEST SCRIPT ACTIVE !!!
V4TestScript script;
Blinker1 blinker1;
Blinker2 blinker2;
CapTest captest;
#endif
#include "buttons/floating_button.h"
#include "buttons/latching_button.h"
#include "buttons/button.h"
#ifdef TEENSYDUINO
#include "buttons/touchbutton.h"
#else
#include "buttons/stm32l4_touchbutton.h"
#endif
#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");
#endif
#include "common/sd_test.h"
class Commands : public CommandParser {
public:
enum PinType {
PinTypeFloating,
PinTypePulldown,
PinTypeCap,
PinTypeOther,
};
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 {
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
if (!strcmp(cmd, "help")) {
CommandParser::DoHelp();
return true;
}
#endif
#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
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;
}
#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
#if 0
if (!strcmp(cmd, "df")) {
STDOUT.print(SerialFlashChip::capacity());
STDOUT.println(" bytes available.");
return true;
}
#endif
#ifdef ENABLE_DEVELOPER_COMMANDS
if (!strcmp(cmd, "high") && e) {
pinMode(atoi(e), OUTPUT);
digitalWrite(atoi(e), HIGH);
STDOUT.println("Ok.");
return true;
}
#endif // ENABLE_DEVELOPER_COMMANDS
#ifdef ENABLE_DEVELOPER_COMMANDS
if (!strcmp(cmd, "low") && e) {
pinMode(atoi(e), OUTPUT);
digitalWrite(atoi(e), LOW);
STDOUT.println("Ok.");
return true;
}
#endif // ENABLE_DEVELOPER_COMMANDS
#if VERSION_MAJOR >= 4
if (!strcmp(cmd, "booster")) {
if (!strcmp(e, "on")) {
digitalWrite(boosterPin, HIGH);
STDOUT.println("Booster on.");
return true;
}
if (!strcmp(e, "off")) {
digitalWrite(boosterPin, LOW);
STDOUT.println("Booster off.");
return true;
}
}
#endif
#ifdef ENABLE_AUDIO
#if 0
if (!strcmp(cmd, "ton")) {
EnableAmplifier();
dac.SetStream(&saber_synth);
saber_synth.on_ = true;
return true;
}
if (!strcmp(cmd, "tof")) {
saber_synth.on_ = false;
return true;
}
#endif
#ifdef ENABLE_DEVELOPER_COMMANDS
if (!strcmp(cmd, "dumpwav")) {
int16_t tmp[32];
wav_players[0].Stop();
wav_players[0].read(tmp, NELEM(tmp));
wav_players[0].Play(e);
for (int j = 0; j < 64; j++) {
int k = wav_players[0].read(tmp, NELEM(tmp));
for (int i = 0; i < k; i++) {
STDOUT.print(tmp[i]);
STDOUT.print(" ");
}
STDOUT.println("");
}
wav_players[0].Stop();
return true;
}
#endif // ENABLE_DEVELOPER_COMMANDS
#endif
#ifdef ENABLE_DEVELOPER_COMMANDS
if (!strcmp(cmd, "twiddle")) {
int pin = strtol(e, NULL, 0);
STDOUT.print("twiddling ");
STDOUT.println(pin);
pinMode(pin, OUTPUT);
for (int i = 0; i < 1000; i++) {
digitalWrite(pin, HIGH);
delay(10);
digitalWrite(pin, LOW);
delay(10);
}
STDOUT.println("done");
return true;
}
#endif // ENABLE_DEVELOPER_COMMANDS
#ifdef ENABLE_DEVELOPER_COMMANDS
if (!strcmp(cmd, "twiddle2")) {
int pin = strtol(e, NULL, 0);
STDOUT.print("twiddling ");
STDOUT.println(pin);
pinMode(pin, OUTPUT);
for (int i = 0; i < 1000; i++) {
for (int i = 0; i < 500; i++) {
digitalWrite(pin, HIGH);
delayMicroseconds(1);
digitalWrite(pin, LOW);
delayMicroseconds(1);
}
delay(10);
}
STDOUT.println("done");
return true;
}
#endif // ENABLE_DEVELOPER_COMMANDS
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
if (!strcmp(cmd, "malloc")) {
STDOUT.print("alloced: ");
STDOUT.println(mallinfo().uordblks);
STDOUT.print("Free: ");
STDOUT.println(mallinfo().fordblks);
return true;
}
#endif
if (!strcmp(cmd, "make_default_console")) {
default_output = stdout_output;
return true;
}
#if 0
// Not finished yet
if (!strcmp(cmd, "selftest")) {
struct PinDefs { int8_t pin; PinType type; };
static PinDefs pin_defs[] = {
{ bladePowerPin1, PinTypePulldown },
{ bladePowerPin2, PinTypePulldown },
{ bladePowerPin3, PinTypePulldown },
{ bladePowerPin4, PinTypePulldown },
{ bladePowerPin5, PinTypePulldown },
{ bladePowerPin6, PinTypePulldown },
{ bladePin, PinTypeOther },
{ blade2Pin, PinTypeFloating },
{ blade3Pin, PinTypeFloating },
{ blade4Pin, PinTypeFloating },
{ blade5Pin, PinTypeFloating },
{ amplifierPin, PinTypeFloating },
{ boosterPin, PinTypeFloating },
{ powerButtonPin, PinTypeFloating },
{ auxPin, PinTypeFloating },
{ aux2Pin, PinTypeFloating },
{ rxPin, PinTypeOther },
{ txPin, PinTypeFloating },
};
for (size_t test_index = 0; test_index < NELEM(pin_defs); test_index++) {
int pin = pin_defs[test_index].pin;
for (size_t i = 0; i < NELEM(pin_defs); i++)
pinMode(pin_defs[i].pin, INPUT);
// test
for (size_t i = 0; i < NELEM(pin_defs); i++) {
pinMode(pin_defs[i].pin, OUTPUT);
digitalWrite(pin_defs[i].pin, HIGH);
// test
digitalWrite(pin_defs[i].pin, LOW);
// test
pinMode(pin_defs[i].pin, INPUT);
}
}
}
#endif
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
if (!strcmp(cmd, "top")) {
#ifdef TEENSYDUINO
if (!(ARM_DWT_CTRL & ARM_DWT_CTRL_CYCCNTENA)) {
ARM_DEMCR |= ARM_DEMCR_TRCENA;
ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
STDOUT.println("Cycle counting enabled, top will work next time.");
return true;
}