-
Notifications
You must be signed in to change notification settings - Fork 25
/
LightSaberOS.ino
1709 lines (1515 loc) · 48.3 KB
/
LightSaberOS.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
/*
* LightSaberOS V1.0
*
* released on: 10 mar 2016
* author: Sebastien CAPOU (neskweek@gmail.com)
* Source : https://github.com/neskweek/LightSaberOS
* Description: Operating System for Arduino based LightSaber
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
*/
#include <Arduino.h>
#include <DFPlayer.h>
#include <I2Cdev.h>
#include <MPU6050_6Axis_MotionApps20.h>
#include <EEPROMex.h>
#include <OneButton.h>
#include <LinkedList.h>
#include "Buttons.h"
#include "Config.h"
#include "ConfigMenu.h"
#include "Light.h"
#include "SoundFont.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include <Wire.h>
#endif
#if defined NEOPIXEL
#include <WS2812.h>
#endif
/*
* DO NOT MODIFY
* Unless you know what you're doing
*************************************/
#define CONFIG_VERSION "L01"
#define MEMORYBASE 32
#define SWING_SUPPRESS 420
/************************************/
/*
* DEFAULT CONFIG PARAMETERS
* Will be overriden by EEPROM settings
* once the first save will be done
*************************************/
#define VOL 13
#define SOUNDFONT 2
#define SWING 300
/************************************/
/***************************************************************************************************
* Motion detection Variables
*/
MPU6050 mpu;
// MPU control/status vars
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint8_t fifoBuffer[64]; // FIFO storage buffer
uint16_t mpuFifoCount; // count of all bytes currently in FIFO
// orientation/motion vars
Quaternion curRotation; // [w, x, y, z] quaternion container
Quaternion prevRotation; // [w, x, y, z] quaternion container
static Quaternion prevOrientation; // [w, x, y, z] quaternion container
static Quaternion curOrientation; // [w, x, y, z] quaternion container
VectorInt16 curAccel;
VectorInt16 prevAccel;
VectorInt16 curDeltAccel;
VectorInt16 prevDeltAccel;
/***************************************************************************************************
* LED String variables
*/
#if defined LEDSTRINGS
uint8_t ledPins[] = { LEDSTRING1, LEDSTRING2, LEDSTRING3, LEDSTRING4,
LEDSTRING5, LEDSTRING6 };
uint8_t blasterPin;
#endif
#if defined LUXEON
uint8_t ledPins[] = {LED_RED, LED_GREEN, LED_BLUE};
uint8_t currentColor[4]; //0:Red 1:Green 2:Blue 3:ColorID
#endif
#if defined NEOPIXEL
uint8_t ledPins[] = {STRING1, STRING2, STRING3};
WS2812 pixels(NUMPIXELS);
cRGB color;
volatile bool isFlickering = false;
cRGB currentColor;
uint8_t blasterPixel;
#endif
# if defined ACCENT_LED
unsigned long lastAccent = millis();
#if defined SOFT_ACCENT
unsigned long lastAccentTick = micros();
struct softPWM {
uint8_t dutyCycle; // in percent
bool revertCycle;
uint8_t state;
uint16_t tick;
} pwmPin = { 100, false, LOW, 0 };
#endif
#endif
uint8_t blaster = 0;
bool blasterBlocks = false;
uint8_t clash = 0;
bool lockup = false;
uint8_t blink = 0;
uint8_t randomBlink = 0;
/***************************************************************************************************
* Buttons variables
*/
OneButton mainButton(MAIN_BUTTON, true);
OneButton lockupButton(LOCKUP_BUTTON, true);
bool actionMode = false; // Play with your saber
bool configMode = false; // Configure your saber
static bool ignition = false;
static bool browsing = false;
/***************************************************************************************************
* DFPLAYER variables
*/
DFPlayer dfplayer;
SoundFont soundFont;
unsigned long sndSuppress = millis();
unsigned long sndSuppress2 = millis();
/***************************************************************************************************
* ConfigMode Variables
*/
int8_t modification = 0;
int16_t value = 0;
uint8_t menu = 0;
bool enterMenu = false;
bool changeMenu = false;
bool play = false;
unsigned int configAdress = 0;
volatile uint8_t portbhistory = 0xFF; // default is high because the pull-up
#if defined LEDSTRINGS
struct StoreStruct {
// This is for mere detection if they are our settings
char version[5];
// The settings
uint8_t volume; // 0 to 30
uint8_t soundFont; // as many Sound font you have defined in Soundfont.h Max:253
uint16_t swingTreshold; // treshold acceleration for Swing
uint8_t sndProfile[SOUNDFONT_QUANTITY + 2][3]; // sndProfile[sndft][0] : PowerOn effect
// sndProfile[sndft][1] : PowerOff effect
// sndProfile[sndft][2] : Flicker effect
} storage;
#endif
#if defined LUXEON
struct StoreStruct {
// This is for mere detection if they are our settings
char version[5];
// The settings
uint8_t volume;// 0 to 30
uint8_t soundFont;// as many as Sound font you have defined in Soundfont.h Max:253
uint16_t swingTreshold;// treshold acceleration for Swing
uint8_t mainColor;//colorID
uint8_t clashColor;//colorID
uint8_t sndProfile[SOUNDFONT_QUANTITY + 2][2]; // sndProfile[sndft][0] : main colorID
// sndProfile[sndft][1] : clash colorID
}storage;
#endif
#if defined NEOPIXEL
struct StoreStruct {
// This is for mere detection if they are our settings
char version[5];
// The settings
uint8_t volume;// 0 to 30
uint8_t soundFont;// as many as Sound font you have defined in Soundfont.h Max:253
uint16_t swingTreshold;// treshold acceleration for Swing
struct Profile {
uint8_t mainColor; //colorID
uint8_t clashColor;//colorID
uint8_t pwrOn;
uint8_t pwrOff;
uint8_t flicker;
}sndProfile[SOUNDFONT_QUANTITY + 2];
}storage;
#endif
/***************************************************************************************************
* Function Prototypes
* The following prototypes are not correctly generated by Arduino IDE 1.6.5-r5 or previous
*/
inline void printQuaternion(Quaternion quaternion, long multiplier);
inline void printAcceleration(VectorInt16 aaWorld);
// ====================================================================================
// === SETUP ROUTINE ===
// ====================================================================================
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Comment this line if having compilation difficulties with TWBR.
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
//#if defined LS_INFO
// Serial line for debug
Serial.begin(9600);
//#endif
#if defined LS_DEBUG
// Serial line for debug
Serial.begin(9600);
#endif
/***** LOAD CONFIG *****/
// Get config from EEPROM if there is one
// or initialise value with default ones set in StoreStruct
EEPROM.setMemPool(MEMORYBASE, EEPROMSizeATmega328); //Set memorypool base to 32, assume Arduino Uno board
configAdress = EEPROM.getAddress(sizeof(StoreStruct)); // Size of config object
if (!loadConfig()) {
for (uint8_t i = 0; i <= 3; i++)
storage.version[i] = CONFIG_VERSION[i];
storage.soundFont = SOUNDFONT;
storage.volume = VOL;
storage.swingTreshold = SWING;
#if defined LEDSTRINGS
storage.sndProfile[2][0] = 0; //PowerOn
storage.sndProfile[2][1] = 0; //PowerOff
storage.sndProfile[2][2] = 0; //Flickering
storage.sndProfile[3][0] = 1;
storage.sndProfile[3][1] = 1;
storage.sndProfile[3][2] = 0;
#endif
#if defined LUXEON
storage.mainColor = 4;
storage.clashColor = 5;
storage.sndProfile[2][0] = 2;
storage.sndProfile[2][1] = 0;
storage.sndProfile[3][0] = 0;
storage.sndProfile[3][1] = 5;
#endif
#if defined NEOPIXEL
storage.sndProfile[2].mainColor = 1;
storage.sndProfile[2].clashColor = 0;
// storage.sndProfile[3].mainColor = 0;
// storage.sndProfile[3].clashColor = 2;
#endif
#if defined LS_INFO
Serial.println(F("DEFAULT VALUE"));
#endif
}
#if defined LS_INFO
else {
Serial.println(F("EEPROM LOADED"));
}
#endif
/***** LOAD CONFIG *****/
/***** MP6050 MOTION DETECTOR INITIALISATION *****/
// initialize device
#if defined LS_INFO
Serial.println(F("Initializing I2C devices..."));
#endif
mpu.initialize();
// verify connection
#if defined LS_INFO
Serial.println(F("Testing device connections..."));
Serial.println(
mpu.testConnection() ?
F("MPU6050 connection successful") :
F("MPU6050 connection failed"));
// load and configure the DMP
Serial.println(F("Initializing DMP..."));
#endif
devStatus = mpu.dmpInitialize();
/*
* Those offsets are specific to each MPU6050 device.
* they are found via calibration process.
* See this script http://www.i2cdevlib.com/forums/index.php?app=core&module=attach§ion=attach&attach_id=27
*/
/* UNIT1 */
mpu.setXAccelOffset(46);
mpu.setYAccelOffset(-4942);
mpu.setZAccelOffset(4721);
mpu.setXGyroOffset(23);
mpu.setYGyroOffset(-11);
mpu.setZGyroOffset(44);
/* DIYino*/
// mpu.setXAccelOffset(-84);
// mpu.setYAccelOffset(788);
// mpu.setZAccelOffset(1137);
// mpu.setXGyroOffset(7);
// mpu.setYGyroOffset(6);
// mpu.setZGyroOffset(7);
// make sure it worked (returns 0 if so)
if (devStatus == 0) {
// turn on the DMP, now that it's ready
#if defined LS_INFO
Serial.println(F("Enabling DMP..."));
#endif
mpu.setDMPEnabled(true);
// enable Arduino interrupt detection
#if defined LS_INFO
Serial.println(
F(
"Enabling interrupt detection (Arduino external interrupt 0)..."));
#endif
// attachInterrupt(0, dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();
// set our DMP Ready flag so the main loop() function knows it's okay to use it
#if defined LS_INFO
Serial.println(F("DMP ready! Waiting for first interrupt..."));
#endif
dmpReady = true;
// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
} else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
#if defined LS_INFO
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
#endif
}
// configure the motion interrupt for clash recognition
// INT_PIN_CFG register
// in the working code of MPU6050_DMP all bits of the INT_PIN_CFG are false (0)
// mpu.setInterruptMode(false); // INT_PIN_CFG register INT_LEVEL (0-active high, 1-active low)
// mpu.setInterruptDrive(false); // INT_PIN_CFG register INT_OPEN (0-push/pull, 1-open drain)
// mpu.setInterruptLatch(false); // INT_PIN_CFG register LATCH_INT_EN (0 - emits 50us pulse upon trigger, 1-pin is held until int is cleared)
// mpu.setInterruptLatchClear(false); // INT_PIN_CFG register INT_RD_CLEAR (0-clear int only on reading int status reg, 1-any read clears int)
// mpu.setFSyncInterruptLevel(false);
// mpu.setFSyncInterruptEnabled(false);
// mpu.setI2CBypassEnabled(false);
// // Enable/disable interrupt sources - enable only motion interrupt
// mpu.setIntFreefallEnabled(false);
// mpu.setIntMotionEnabled(true);
// mpu.setIntZeroMotionEnabled(false);
// mpu.setIntFIFOBufferOverflowEnabled(false);
// mpu.setIntI2CMasterEnabled(false);
// mpu.setIntDataReadyEnabled(false);
mpu.setDLPFMode(3);
mpu.setDHPFMode(0);
//mpu.setFullScaleAccelRange(3);
mpu.setIntMotionEnabled(true); // INT_ENABLE register enable interrupt source motion detection
mpu.setIntZeroMotionEnabled(false);
mpu.setIntFIFOBufferOverflowEnabled(false);
mpu.setIntI2CMasterEnabled(false);
mpu.setIntDataReadyEnabled(false);
mpu.setMotionDetectionThreshold(10); // 1mg/LSB
mpu.setMotionDetectionDuration(2); // number of consecutive samples above threshold to trigger int
mpuIntStatus = mpu.getIntStatus();
#if defined LS_CLASH_DEBUG
Serial.println("MPU6050 register setup:");
Serial.print("INT_PIN_CFG\t");
Serial.print(mpu.getInterruptMode());
Serial.print("\t");
Serial.print(mpu.getInterruptDrive());
Serial.print("\t");
Serial.print(mpu.getInterruptLatch());
Serial.print("\t");
Serial.print(mpu.getInterruptLatchClear());
Serial.print("\t");
Serial.print(mpu.getFSyncInterruptLevel());
Serial.print("\t");
Serial.print(mpu.getFSyncInterruptEnabled());
Serial.print("\t");
Serial.println(mpu.getI2CBypassEnabled());
// list INT_ENABLE register contents
Serial.print("INT_ENABLE\t");
Serial.print(mpu.getIntFreefallEnabled());
Serial.print("\t");
Serial.print(mpu.getIntMotionEnabled());
Serial.print("\t");
Serial.print(mpu.getIntZeroMotionEnabled());
Serial.print("\t");
Serial.print(mpu.getIntFIFOBufferOverflowEnabled());
Serial.print("\t");
Serial.print(mpu.getIntI2CMasterEnabled());
Serial.print("\t");
Serial.println(mpu.getIntDataReadyEnabled());
#endif
/***** MP6050 MOTION DETECTOR INITIALISATION *****/
/***** LED SEGMENT INITIALISATION *****/
// initialize ledstrings segments
DDRD |= B01101000;
DDRB |= B00101110;
//We shut off all pins that could wearing leds,just to be sure
PORTD &= B10010111;
PORTB &= B11010001;
#if defined LUXEON
//initialise start color
getColor(currentColor, storage.mainColor);
#endif
#if defined NEOPIXEL
pixels.setOutput(DATA_PIN); // This initializes the NeoPixel library.
lightOff();
getColor(storage.sndProfile[storage.soundFont].mainColor);
#endif
#if defined FoCSTRING
pinMode(FoCSTRING, OUTPUT);
FoCOff(FoCSTRING);
#endif
#if defined ACCENT_LED
pinMode(ACCENT_LED, OUTPUT);
#endif
//Randomize randomness (no really that's what it does)
randomSeed(analogRead(2));
/***** LED SEGMENT INITIALISATION *****/
/***** BUTTONS INITIALISATION *****/
// link the Main button functions.
mainButton.setClickTicks(CLICK);
mainButton.setPressTicks(PRESS_CONFIG);
mainButton.attachClick(mainClick);
//mainButton.attachDoubleClick(mainDoubleClick);
mainButton.attachLongPressStart(mainLongPressStart);
mainButton.attachLongPressStop(mainLongPressStop);
mainButton.attachDuringLongPress(mainLongPress);
// link the Lockup button functions.
lockupButton.setClickTicks(CLICK);
lockupButton.setPressTicks(PRESS_CONFIG);
lockupButton.attachClick(lockupClick);
//lockupButton.attachDoubleClick(lockupDoubleClick);
lockupButton.attachLongPressStart(lockupLongPressStart);
lockupButton.attachLongPressStop(lockupLongPressStop);
lockupButton.attachDuringLongPress(lockupLongPress);
/***** BUTTONS INITIALISATION *****/
/***** DF PLAYER INITIALISATION *****/
dfplayer.setSerial(DFPLAYER_TX, DFPLAYER_RX);
dfplayer.setVolume(storage.volume);
delay(200);
pinMode(SPK1, INPUT);
pinMode(SPK2, INPUT);
soundFont.setID(storage.soundFont);
/***** DF PLAYER INITIALISATION *****/
//setup finished. Boot ready. We notify !
dfplayer.playPhysicalTrack(16);
delay(20);
}
// ====================================================================================
// === LOOP ROUTINE ===
// ====================================================================================
void loop() {
// if MPU6050 DMP programming failed, don't try to do anything : EPIC FAIL !
if (!dmpReady) {
return;
}
mainButton.tick();
lockupButton.tick();
/*//////////////////////////////////////////////////////////////////////////////////////////////////////////
* ACTION MODE HANDLER
*/ /////////////////////////////////////////////////////////////////////////////////////////////////////////
if (actionMode) {
/*
// In case we want to time the loop
Serial.print(F("Action Mode"));
Serial.print(F(" time="));
Serial.println(millis());
*/
if (!ignition) {
/*
* This is the very first loop after Action Mode has been turned on
*/
attachInterrupt(0, dmpDataReady, RISING);
// Reduce lockup trigger time for faster lockup response
lockupButton.setPressTicks(PRESS_ACTION);
#if defined LS_INFO
Serial.println(F("START ACTION"));
#endif
//Play powerons wavs
dfplayer.playPhysicalTrack(soundFont.getPowerOn());
// Light up the ledstrings
#if defined LEDSTRINGS
lightIgnition(ledPins, soundFont.getPowerOnTime(),
storage.sndProfile[storage.soundFont][0]);
#endif
#if defined LUXEON
lightIgnition(ledPins, currentColor, soundFont.getPowerOnTime());
#endif
#if defined NEOPIXEL
for (uint8_t i = 0; i < 3; i++) {
digitalWrite(ledPins[i], HIGH);
}
lightIgnition(currentColor, soundFont.getPowerOnTime(), 0);
#endif
sndSuppress = millis();
sndSuppress2 = millis();
#if defined LIGHT_EFFECTS
/*
* Interrupt Timer2 configuration
*/
OCR2A = 2; // Around 44100 Hz
TCCR2A |= (1 << WGM21); // Set to CTC Mode
// set prescaler to 256
TCCR2B |= (1 << CS21) | (1 << CS22);
// start timer2 compare interrupt:
TIMSK2 |= (1 << OCIE2A);
#endif
// Get the initial position of the motion detector
motionEngine();
ignition = true;
#if defined ACCENT_LED
// turns accent LED On
analogWrite(ACCENT_LED, HIGH);
#endif
}
// ************************* blade movement detection ************************************
//Let's get our values !
motionEngine();
/*
* CLASH DETECTION :
* A clash is a violent deceleration when 2 blades hit each other
* For a realistic clash detection it's imperative to detect
* such a deceleration instantenously, which is only feasible
* using the motion interrupt feature of the MPU6050.
*/
if (mpuIntStatus > 60 and mpuIntStatus < 70 and not lockup) {
/*
* THIS IS A CLASH !
*/
#if defined LUXEON
getColor(currentColor, storage.clashColor);
lightOn(ledPins, currentColor);
#endif
#if defined LS_CLASH_DEBUG
Serial.print(F("CLASH\tmpuIntStatus="));
Serial.println(mpuIntStatus);
#endif
if (millis() - sndSuppress >= 100) {
blink = 0;
clash = CLASH_FLASH_TIME;
dfplayer.playPhysicalTrack(soundFont.getClash());
sndSuppress = millis();
sndSuppress2 = millis();
}
}
/*
* SWING DETECTION
* We detect swings as hilt's orientation change
* since IMUs sucks at determining relative position in space
*/
else if (
not lockup
and abs(curRotation.w * 1000) < 999 // some rotation movement have been initiated
and (
#if defined BLADE_X
(
(millis() - sndSuppress > SWING_SUPPRESS) // The movement doesn't follow another to closely
and (abs(curDeltAccel.y) > storage.swingTreshold // and it has suffisent power on a certain axis
or abs(curDeltAccel.z) > storage.swingTreshold
or abs(curDeltAccel.x) > storage.swingTreshold*10)
)
or (// A reverse movement follow a first one
(millis() - sndSuppress2 > SWING_SUPPRESS) // The reverse movement doesn't follow another reverse movement to closely
// and it must be a reverse movement on Vertical axis
and (
abs(curDeltAccel.y) > abs(curDeltAccel.z)
and abs(prevDeltAccel.y) > storage.swingTreshold
and (
(prevDeltAccel.y > 0
and curDeltAccel.y < -storage.swingTreshold)
or (
prevDeltAccel.y < 0
and curDeltAccel.y > storage.swingTreshold
)
)
)
)
or (// A reverse movement follow a first one
(millis() - sndSuppress2 > SWING_SUPPRESS) // The reverse movement doesn't follow another reverse movement to closely
and ( // and it must be a reverse movement on Horizontal axis
abs(curDeltAccel.z) > abs(curDeltAccel.y)
and abs(prevDeltAccel.z) > storage.swingTreshold
and (
(prevDeltAccel.z > 0
and curDeltAccel.z < -storage.swingTreshold)
or (
prevDeltAccel.z < 0
and curDeltAccel.z > storage.swingTreshold
)
)
)
)
)
// the movement must not be triggered by pure blade rotation (wrist rotation)
and not (
abs(prevRotation.x * 1000 - curRotation.x * 1000) > abs(prevRotation.y * 1000 - curRotation.y * 1000)
and
abs(prevRotation.x * 1000 - curRotation.x * 1000) > abs(prevRotation.z * 1000 - curRotation.z * 1000)
)
#endif
#if defined BLADE_Y
(
(millis() - sndSuppress > SWING_SUPPRESS) // The movement doesn't follow another to closely
and (abs(curDeltAccel.x) > storage.swingTreshold // and it has suffisent power on a certain axis
or abs(curDeltAccel.z) > storage.swingTreshold
or abs(curDeltAccel.y) > storage.swingTreshold*10)
)
or (// A reverse movement follow a first one
(millis() - sndSuppress2 > SWING_SUPPRESS) // The reverse movement doesn't follow another reverse movement to closely
// and it must be a reverse movement on Vertical axis
and (
abs(curDeltAccel.x) > abs(curDeltAccel.z)
and abs(prevDeltAccel.x) > storage.swingTreshold
and (
(prevDeltAccel.x > 0
and curDeltAccel.x < -storage.swingTreshold)
or (
prevDeltAccel.x < 0
and curDeltAccel.x > storage.swingTreshold
)
)
)
)
or (// A reverse movement follow a first one
(millis() - sndSuppress2 > SWING_SUPPRESS) // The reverse movement doesn't follow another reverse movement to closely
and ( // and it must be a reverse movement on Horizontal axis
abs(curDeltAccel.z) > abs(curDeltAccel.x)
and abs(prevDeltAccel.z) > storage.swingTreshold
and (
(prevDeltAccel.z > 0
and curDeltAccel.z < -storage.swingTreshold)
or (
prevDeltAccel.z < 0
and curDeltAccel.z > storage.swingTreshold
)
)
)
)
)
// the movement must not be triggered by pure blade rotation (wrist rotation)
and not (
abs(prevRotation.y * 1000 - curRotation.y * 1000) > abs(prevRotation.x * 1000 - curRotation.x * 1000)
and
abs(prevRotation.y * 1000 - curRotation.y * 1000) > abs(prevRotation.z * 1000 - curRotation.z * 1000)
)
#endif
#if defined BLADE_Z
(
(millis() - sndSuppress > SWING_SUPPRESS) // The movement doesn't follow another to closely
and (abs(curDeltAccel.y) > storage.swingTreshold // and it has suffisent power on a certain axis
or abs(curDeltAccel.x) > storage.swingTreshold
or abs(curDeltAccel.z) > storage.swingTreshold*10)
)
or (// A reverse movement follow a first one
(millis() - sndSuppress2 > SWING_SUPPRESS) // The reverse movement doesn't follow another reverse movement to closely
// and it must be a reverse movement on Vertical axis
and (
abs(curDeltAccel.y) > abs(curDeltAccel.x)
and abs(prevDeltAccel.y) > storage.swingTreshold
and (
(prevDeltAccel.y > 0
and curDeltAccel.y < -storage.swingTreshold)
or (
prevDeltAccel.y < 0
and curDeltAccel.y > storage.swingTreshold
)
)
)
)
or (// A reverse movement follow a first one
(millis() - sndSuppress2 > SWING_SUPPRESS) // The reverse movement doesn't follow another reverse movement to closely
and ( // and it must be a reverse movement on Horizontal axis
abs(curDeltAccel.x) > abs(curDeltAccel.y)
and abs(prevDeltAccel.x) > storage.swingTreshold
and (
(prevDeltAccel.x > 0
and curDeltAccel.x < -storage.swingTreshold)
or (
prevDeltAccel.x < 0
and curDeltAccel.x > storage.swingTreshold
)
)
)
)
)
// the movement must not be triggered by pure blade rotation (wrist rotation)
and not (
abs(prevRotation.z * 1000 - curRotation.z * 1000) > abs(prevRotation.y * 1000 - curRotation.y * 1000)
and
abs(prevRotation.z * 1000 - curRotation.z * 1000) > abs(prevRotation.x * 1000 - curRotation.x * 1000)
)
#endif
){
if (!blasterBlocks) {
/*
* THIS IS A SWING !
*/
prevDeltAccel = curDeltAccel;
#if defined LS_SWING_DEBUG
Serial.print(F("SWING\ttime="));
Serial.println(millis() - sndSuppress);
Serial.print(F("\t\tcurRotation\tw="));
Serial.print(curRotation.w * 1000);
Serial.print(F("\t\tx="));
Serial.print(curRotation.x);
Serial.print(F("\t\ty="));
Serial.print(curRotation.y);
Serial.print(F("\t\tz="));
Serial.print(curRotation.z);
Serial.print(F("\t\tAcceleration\tx="));
Serial.print(curDeltAccel.x);
Serial.print(F("\ty="));
Serial.print(curDeltAccel.y);
Serial.print(F("\tz="));
Serial.println(curDeltAccel.z);
// Serial.print(F("\t\tprevRotation\tw="));
// Serial.print(prevRotation.w * 1000);
// Serial.print(F("\t\tx="));
// Serial.print(prevRotation.x);
// Serial.print(F("\t\ty="));
// Serial.print(prevRotation.y);
// Serial.print(F("\t\tz="));
// Serial.println(prevRotation.z);
// Serial.print(F("\tprevOrientation="));
// Serial.print(F("\t"));
// Serial.print(prevOrientation.w * 1000);
// Serial.print(F("\t\tx="));
// Serial.print(prevOrientation.x);
// Serial.print(F("\t\ty="));
// Serial.print(prevOrientation.y);
// Serial.print(F("\t\tz="));
// Serial.println(prevOrientation.z);
#endif
/* SPIN DETECTION */
if( soundFont.getSpin()
and (millis() - sndSuppress > SWING_SUPPRESS) // movement follow the precedent one shortly
and (millis() - sndSuppress <= SWING_SUPPRESS +10)
and (
#if defined BLADE_X
(
abs(curDeltAccel.y) > abs(curDeltAccel.z)
and abs(prevDeltAccel.y) > storage.swingTreshold
and (
(
prevDeltAccel.y > 0
and curDeltAccel.y > storage.swingTreshold)
or (
prevDeltAccel.y < 0
and curDeltAccel.y < -storage.swingTreshold
)
)
)
or
( // and it must be a reverse movement on Horizontal axis
abs(curDeltAccel.z) > abs(curDeltAccel.y)
and abs(prevDeltAccel.z) > storage.swingTreshold
and (
(
prevDeltAccel.z > 0
and curDeltAccel.z > storage.swingTreshold)
or (
prevDeltAccel.z < 0
and curDeltAccel.z < -storage.swingTreshold
)
)
)
#endif
#if defined BLADE_Y
(
abs(curDeltAccel.x) > abs(curDeltAccel.z)
and abs(prevDeltAccel.x) > storage.swingTreshold
and (
(
prevDeltAccel.x > 0
and curDeltAccel.x > storage.swingTreshold)
or (
prevDeltAccel.x < 0
and curDeltAccel.x < -storage.swingTreshold
)
)
)
or
( // and it must be a reverse movement on Horizontal axis
abs(curDeltAccel.z) > abs(curDeltAccel.x)
and abs(prevDeltAccel.z) > storage.swingTreshold
and (
(
prevDeltAccel.z > 0
and curDeltAccel.z > storage.swingTreshold)
or (
prevDeltAccel.z < 0
and curDeltAccel.z < -storage.swingTreshold
)
)
)
#endif
#if defined BLADE_Z
(
abs(curDeltAccel.y) > abs(curDeltAccel.x)
and abs(prevDeltAccel.y) > storage.swingTreshold
and (
(
prevDeltAccel.y > 0
and curDeltAccel.y > storage.swingTreshold)
or (
prevDeltAccel.y < 0
and curDeltAccel.y < -storage.swingTreshold
)
)
)
or
( // and it must be a reverse movement on Horizontal axis
abs(curDeltAccel.x) > abs(curDeltAccel.y)
and abs(prevDeltAccel.x) > storage.swingTreshold
and (
(
prevDeltAccel.x > 0
and curDeltAccel.x > storage.swingTreshold)
or (
prevDeltAccel.x < 0
and curDeltAccel.x < -storage.swingTreshold
)
)
)
#endif
)
){
dfplayer.playPhysicalTrack(soundFont.getSpin());
}/* SPIN DETECTION */
else{ /* NORMAL SWING */
dfplayer.playPhysicalTrack(soundFont.getSwing());
}/* NORMAL SWING */
if (millis() - sndSuppress > SWING_SUPPRESS) {
sndSuppress = millis();
}
if (millis() - sndSuppress2 > SWING_SUPPRESS) {
sndSuppress2 = millis();
}
} else { /* BLASTER MODE */
if (soundFont.getBlaster()) {
#if defined LEDSTRINGS
blasterPin = random(6); //momentary shut off one led segment
blink = 0;
#endif
#if defined LUXEON
getColor(currentColor, storage.clashColor);
lightOn(ledPins, currentColor);
#endif //LUXEON
#if defined NEOPIXEL
blasterPixel = random(20, NUMPIXELS - 20); //momentary shut off one led segment
blink = 0;
// getColor(storage.sndProfile[storage.soundFont].clashColor);
getColor(255);//Pure white
#endif
blaster = BLASTER_FLASH_TIME;
// Some Soundfont may not have #endifBlaster sounds
if (millis() - sndSuppress > 50) {
dfplayer.playPhysicalTrack(soundFont.getBlaster());
sndSuppress = millis();
}
}
} /* BLASTER MODE */
}
// ************************* blade movement detection ends***********************************
} ////END ACTION MODE HANDLER///////////////////////////////////////////////////////////////////////////////////////
/*//////////////////////////////////////////////////////////////////////////////////////////////////////////
* CONFIG MODE HANDLER
*//////////////////////////////////////////////////////////////////////////////////////////////////////////
else if (configMode) {
if (!browsing) {
dfplayer.playPhysicalTrack(3);
delay(600);
#if defined LS_INFO
Serial.println(F("START CONF"));
#endif
browsing = true;
enterMenu = true;
}
if (modification == -1) {
#if defined LS_INFO
Serial.print(F("-:"));
#endif
dfplayer.playPhysicalTrack(2);
delay(50);
} else if (modification == 1) {
#if defined LS_INFO
Serial.print(F("+:"));
#endif
dfplayer.playPhysicalTrack(1);
delay(50);
}
switch (menu) {
case 0: //VOLUME
confMenuStart(storage.volume, 4, dfplayer);
confParseValue(storage.volume, 0, 30, 1, dfplayer);
if (modification) {
modification = 0;
storage.volume = value;
dfplayer.setVolume(storage.volume); // Too Slow: we'll change volume on exit
delay(50);
#if defined LS_INFO
Serial.println(storage.volume);
#endif
}
break;
case 1: // SOUNDFONT
confMenuStart(storage.soundFont, 5, dfplayer);
play = false;
confParseValue(storage.soundFont, 2, SOUNDFONT_QUANTITY + 1, 1,
dfplayer);
if (modification) {
modification = 0;
storage.soundFont = value;
soundFont.setID(value);
dfplayer.playPhysicalTrack(soundFont.getBoot());
delay(150);
#if defined LUXEON
storage.mainColor = storage.sndProfile[value][0];
storage.clashColor = storage.sndProfile[value][1];
#endif
#if defined LS_INFO
Serial.println(soundFont.getID());
#endif
}
break;
#if defined LUXEON
case 2: // BLADE MAIN COLOR