forked from tonuino/TonUINO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tonuino.ino
1807 lines (1649 loc) · 52.5 KB
/
Tonuino.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
#include <DFMiniMp3.h>
#include <EEPROM.h>
#include <JC_Button.h>
#include <MFRC522.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <avr/sleep.h>
/*
_____ _____ _____ _____ _____
|_ _|___ ___| | | | | | |
| | | . | | | |- -| | | | | |
|_| |___|_|_|_____|_____|_|___|_____|
TonUINO Version 2.1
created by Thorsten Voß and licensed under GNU/GPL.
Information and contribution at https://tonuino.de.
*/
// uncomment the below line to enable five button support
//#define FIVEBUTTONS
static const uint32_t cardCookie = 322417479;
// DFPlayer Mini
SoftwareSerial mySoftwareSerial(2, 3); // RX, TX
uint16_t numTracksInFolder;
uint16_t currentTrack;
uint16_t firstTrack;
uint8_t queue[255];
uint8_t volume;
struct folderSettings {
uint8_t folder;
uint8_t mode;
uint8_t special;
uint8_t special2;
};
// this object stores nfc tag data
struct nfcTagObject {
uint32_t cookie;
uint8_t version;
folderSettings nfcFolderSettings;
// uint8_t folder;
// uint8_t mode;
// uint8_t special;
// uint8_t special2;
};
// admin settings stored in eeprom
struct adminSettings {
uint32_t cookie;
byte version;
uint8_t maxVolume;
uint8_t minVolume;
uint8_t initVolume;
uint8_t eq;
bool locked;
long standbyTimer;
bool invertVolumeButtons;
folderSettings shortCuts[4];
uint8_t adminMenuLocked;
uint8_t adminMenuPin[4];
};
adminSettings mySettings;
nfcTagObject myCard;
folderSettings *myFolder;
unsigned long sleepAtMillis = 0;
static uint16_t _lastTrackFinished;
static void nextTrack(uint16_t track);
uint8_t voiceMenu(int numberOfOptions, int startMessage, int messageOffset,
bool preview = false, int previewFromFolder = 0, int defaultValue = 0, bool exitWithLongPress = false);
bool isPlaying();
bool checkTwo ( uint8_t a[], uint8_t b[] );
void writeCard(nfcTagObject nfcTag);
void dump_byte_array(byte * buffer, byte bufferSize);
void adminMenu(bool fromCard = false);
bool knownCard = false;
// implement a notification class,
// its member methods will get called
//
class Mp3Notify {
public:
static void OnError(uint16_t errorCode) {
// see DfMp3_Error for code meaning
Serial.println();
Serial.print("Com Error ");
Serial.println(errorCode);
}
static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action) {
if (source & DfMp3_PlaySources_Sd) Serial.print("SD Karte ");
if (source & DfMp3_PlaySources_Usb) Serial.print("USB ");
if (source & DfMp3_PlaySources_Flash) Serial.print("Flash ");
Serial.println(action);
}
static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track) {
// Serial.print("Track beendet");
// Serial.println(track);
// delay(100);
nextTrack(track);
}
static void OnPlaySourceOnline(DfMp3_PlaySources source) {
PrintlnSourceAction(source, "online");
}
static void OnPlaySourceInserted(DfMp3_PlaySources source) {
PrintlnSourceAction(source, "bereit");
}
static void OnPlaySourceRemoved(DfMp3_PlaySources source) {
PrintlnSourceAction(source, "entfernt");
}
};
static DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(mySoftwareSerial);
void shuffleQueue() {
// Queue für die Zufallswiedergabe erstellen
for (uint8_t x = 0; x < numTracksInFolder - firstTrack + 1; x++)
queue[x] = x + firstTrack;
// Rest mit 0 auffüllen
for (uint8_t x = numTracksInFolder - firstTrack + 1; x < 255; x++)
queue[x] = 0;
// Queue mischen
for (uint8_t i = 0; i < numTracksInFolder - firstTrack + 1; i++)
{
uint8_t j = random (0, numTracksInFolder - firstTrack + 1);
uint8_t t = queue[i];
queue[i] = queue[j];
queue[j] = t;
}
/* Serial.println(F("Queue :"));
for (uint8_t x = 0; x < numTracksInFolder - firstTrack + 1 ; x++)
Serial.println(queue[x]);
*/
}
void writeSettingsToFlash() {
Serial.println(F("=== writeSettingsToFlash()"));
int address = sizeof(myFolder->folder) * 100;
EEPROM.put(address, mySettings);
}
void resetSettings() {
Serial.println(F("=== resetSettings()"));
mySettings.cookie = cardCookie;
mySettings.version = 2;
mySettings.maxVolume = 25;
mySettings.minVolume = 5;
mySettings.initVolume = 15;
mySettings.eq = 1;
mySettings.locked = false;
mySettings.standbyTimer = 0;
mySettings.invertVolumeButtons = true;
mySettings.shortCuts[0].folder = 0;
mySettings.shortCuts[1].folder = 0;
mySettings.shortCuts[2].folder = 0;
mySettings.shortCuts[3].folder = 0;
mySettings.adminMenuLocked = 0;
mySettings.adminMenuPin[0] = 1;
mySettings.adminMenuPin[1] = 1;
mySettings.adminMenuPin[2] = 1;
mySettings.adminMenuPin[3] = 1;
writeSettingsToFlash();
}
void migrateSettings(int oldVersion) {
if (oldVersion == 1) {
Serial.println(F("=== resetSettings()"));
Serial.println(F("1 -> 2"));
mySettings.version = 2;
mySettings.adminMenuLocked = 0;
mySettings.adminMenuPin[0] = 1;
mySettings.adminMenuPin[1] = 1;
mySettings.adminMenuPin[2] = 1;
mySettings.adminMenuPin[3] = 1;
writeSettingsToFlash();
}
}
void loadSettingsFromFlash() {
Serial.println(F("=== loadSettingsFromFlash()"));
int address = sizeof(myFolder->folder) * 100;
EEPROM.get(address, mySettings);
if (mySettings.cookie != cardCookie)
resetSettings();
migrateSettings(mySettings.version);
Serial.print(F("Version: "));
Serial.println(mySettings.version);
Serial.print(F("Maximal Volume: "));
Serial.println(mySettings.maxVolume);
Serial.print(F("Minimal Volume: "));
Serial.println(mySettings.minVolume);
Serial.print(F("Initial Volume: "));
Serial.println(mySettings.initVolume);
Serial.print(F("EQ: "));
Serial.println(mySettings.eq);
Serial.print(F("Locked: "));
Serial.println(mySettings.locked);
Serial.print(F("Sleep Timer: "));
Serial.println(mySettings.standbyTimer);
Serial.print(F("Inverted Volume Buttons: "));
Serial.println(mySettings.invertVolumeButtons);
Serial.print(F("Admin Menu locked: "));
Serial.println(mySettings.adminMenuLocked);
Serial.print(F("Admin Menu Pin: "));
Serial.print(mySettings.adminMenuPin[0]);
Serial.print(mySettings.adminMenuPin[1]);
Serial.print(mySettings.adminMenuPin[2]);
Serial.println(mySettings.adminMenuPin[3]);
}
class Modifier {
public:
virtual void loop() {}
virtual bool handlePause() {
return false;
}
virtual bool handleNext() {
return false;
}
virtual bool handlePrevious() {
return false;
}
virtual bool handleNextButton() {
return false;
}
virtual bool handlePreviousButton() {
return false;
}
virtual bool handleVolumeUp() {
return false;
}
virtual bool handleVolumeDown() {
return false;
}
virtual bool handleRFID(nfcTagObject *newCard) {
return false;
}
virtual uint8_t getActive() {
return 0;
}
Modifier() {
}
};
Modifier *activeModifier = NULL;
class SleepTimer: public Modifier {
private:
unsigned long sleepAtMillis = 0;
public:
void loop() {
if (this->sleepAtMillis != 0 && millis() > this->sleepAtMillis) {
Serial.println(F("=== SleepTimer::loop() -> SLEEP!"));
mp3.pause();
setstandbyTimer();
activeModifier = NULL;
delete this;
}
}
SleepTimer(uint8_t minutes) {
Serial.println(F("=== SleepTimer()"));
Serial.println(minutes);
this->sleepAtMillis = millis() + minutes * 60000;
// if (isPlaying())
// mp3.playAdvertisement(302);
// delay(500);
}
uint8_t getActive() {
Serial.println(F("== SleepTimer::getActive()"));
return 1;
}
};
class FreezeDance: public Modifier {
private:
unsigned long nextStopAtMillis = 0;
const uint8_t minSecondsBetweenStops = 5;
const uint8_t maxSecondsBetweenStops = 30;
void setNextStopAtMillis() {
uint16_t seconds = random(this->minSecondsBetweenStops, this->maxSecondsBetweenStops + 1);
Serial.println(F("=== FreezeDance::setNextStopAtMillis()"));
Serial.println(seconds);
this->nextStopAtMillis = millis() + seconds * 1000;
}
public:
void loop() {
if (this->nextStopAtMillis != 0 && millis() > this->nextStopAtMillis) {
Serial.println(F("== FreezeDance::loop() -> FREEZE!"));
if (isPlaying()) {
mp3.playAdvertisement(301);
delay(500);
}
setNextStopAtMillis();
}
}
FreezeDance(void) {
Serial.println(F("=== FreezeDance()"));
if (isPlaying()) {
delay(1000);
mp3.playAdvertisement(300);
delay(500);
}
setNextStopAtMillis();
}
uint8_t getActive() {
Serial.println(F("== FreezeDance::getActive()"));
return 2;
}
};
class Locked: public Modifier {
public:
virtual bool handlePause() {
Serial.println(F("== Locked::handlePause() -> LOCKED!"));
return true;
}
virtual bool handleNextButton() {
Serial.println(F("== Locked::handleNextButton() -> LOCKED!"));
return true;
}
virtual bool handlePreviousButton() {
Serial.println(F("== Locked::handlePreviousButton() -> LOCKED!"));
return true;
}
virtual bool handleVolumeUp() {
Serial.println(F("== Locked::handleVolumeUp() -> LOCKED!"));
return true;
}
virtual bool handleVolumeDown() {
Serial.println(F("== Locked::handleVolumeDown() -> LOCKED!"));
return true;
}
virtual bool handleRFID(nfcTagObject *newCard) {
Serial.println(F("== Locked::handleRFID() -> LOCKED!"));
return true;
}
Locked(void) {
Serial.println(F("=== Locked()"));
// if (isPlaying())
// mp3.playAdvertisement(303);
}
uint8_t getActive() {
return 3;
}
};
class ToddlerMode: public Modifier {
public:
virtual bool handlePause() {
Serial.println(F("== ToddlerMode::handlePause() -> LOCKED!"));
return true;
}
virtual bool handleNextButton() {
Serial.println(F("== ToddlerMode::handleNextButton() -> LOCKED!"));
return true;
}
virtual bool handlePreviousButton() {
Serial.println(F("== ToddlerMode::handlePreviousButton() -> LOCKED!"));
return true;
}
virtual bool handleVolumeUp() {
Serial.println(F("== ToddlerMode::handleVolumeUp() -> LOCKED!"));
return true;
}
virtual bool handleVolumeDown() {
Serial.println(F("== ToddlerMode::handleVolumeDown() -> LOCKED!"));
return true;
}
ToddlerMode(void) {
Serial.println(F("=== ToddlerMode()"));
// if (isPlaying())
// mp3.playAdvertisement(304);
}
uint8_t getActive() {
Serial.println(F("== ToddlerMode::getActive()"));
return 4;
}
};
class KindergardenMode: public Modifier {
private:
nfcTagObject nextCard;
bool cardQueued = false;
public:
virtual bool handleNext() {
Serial.println(F("== KindergardenMode::handleNext() -> NEXT"));
//if (this->nextCard.cookie == cardCookie && this->nextCard.nfcFolderSettings.folder != 0 && this->nextCard.nfcFolderSettings.mode != 0) {
//myFolder = &this->nextCard.nfcFolderSettings;
if (this->cardQueued == true) {
this->cardQueued = false;
myCard = nextCard;
myFolder = &myCard.nfcFolderSettings;
Serial.println(myFolder->folder);
Serial.println(myFolder->mode);
playFolder();
return true;
}
return false;
}
// virtual bool handlePause() {
// Serial.println(F("== KindergardenMode::handlePause() -> LOCKED!"));
// return true;
// }
virtual bool handleNextButton() {
Serial.println(F("== KindergardenMode::handleNextButton() -> LOCKED!"));
return true;
}
virtual bool handlePreviousButton() {
Serial.println(F("== KindergardenMode::handlePreviousButton() -> LOCKED!"));
return true;
}
virtual bool handleRFID(nfcTagObject * newCard) { // lot of work to do!
Serial.println(F("== KindergardenMode::handleRFID() -> queued!"));
this->nextCard = *newCard;
this->cardQueued = true;
if (!isPlaying()) {
handleNext();
}
return true;
}
KindergardenMode() {
Serial.println(F("=== KindergardenMode()"));
// if (isPlaying())
// mp3.playAdvertisement(305);
// delay(500);
}
uint8_t getActive() {
Serial.println(F("== KindergardenMode::getActive()"));
return 5;
}
};
class RepeatSingleModifier: public Modifier {
public:
virtual bool handleNext() {
Serial.println(F("== RepeatSingleModifier::handleNext() -> REPEAT CURRENT TRACK"));
delay(50);
if (isPlaying()) return true;
if (myFolder->mode == 3 || myFolder->mode == 9){
mp3.playFolderTrack(myFolder->folder, queue[currentTrack - 1]);
}
else{
mp3.playFolderTrack(myFolder->folder, currentTrack);
}
_lastTrackFinished = 0;
return true;
}
RepeatSingleModifier() {
Serial.println(F("=== RepeatSingleModifier()"));
}
uint8_t getActive() {
Serial.println(F("== RepeatSingleModifier::getActive()"));
return 6;
}
};
// An modifier can also do somethings in addition to the modified action
// by returning false (not handled) at the end
// This simple FeedbackModifier will tell the volume before changing it and
// give some feedback once a RFID card is detected.
class FeedbackModifier: public Modifier {
public:
virtual bool handleVolumeDown() {
if (volume > mySettings.minVolume) {
mp3.playAdvertisement(volume - 1);
}
else {
mp3.playAdvertisement(volume);
}
delay(500);
Serial.println(F("== FeedbackModifier::handleVolumeDown()!"));
return false;
}
virtual bool handleVolumeUp() {
if (volume < mySettings.maxVolume) {
mp3.playAdvertisement(volume + 1);
}
else {
mp3.playAdvertisement(volume);
}
delay(500);
Serial.println(F("== FeedbackModifier::handleVolumeUp()!"));
return false;
}
virtual bool handleRFID(nfcTagObject *newCard) {
Serial.println(F("== FeedbackModifier::handleRFID()"));
return false;
}
};
// Leider kann das Modul selbst keine Queue abspielen, daher müssen wir selbst die Queue verwalten
static void nextTrack(uint16_t track) {
Serial.println(track);
if (activeModifier != NULL)
if (activeModifier->handleNext() == true)
return;
if (track == _lastTrackFinished) {
return;
}
_lastTrackFinished = track;
if (knownCard == false)
// Wenn eine neue Karte angelernt wird soll das Ende eines Tracks nicht
// verarbeitet werden
return;
Serial.println(F("=== nextTrack()"));
if (myFolder->mode == 1 || myFolder->mode == 7) {
Serial.println(F("Hörspielmodus ist aktiv -> keinen neuen Track spielen"));
setstandbyTimer();
// mp3.sleep(); // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
}
if (myFolder->mode == 2 || myFolder->mode == 8) {
if (currentTrack != numTracksInFolder) {
currentTrack = currentTrack + 1;
mp3.playFolderTrack(myFolder->folder, currentTrack);
Serial.print(F("Albummodus ist aktiv -> nächster Track: "));
Serial.print(currentTrack);
} else
// mp3.sleep(); // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
setstandbyTimer();
{ }
}
if (myFolder->mode == 3 || myFolder->mode == 9) {
if (currentTrack != numTracksInFolder - firstTrack + 1) {
Serial.print(F("Party -> weiter in der Queue "));
currentTrack++;
} else {
Serial.println(F("Ende der Queue -> beginne von vorne"));
currentTrack = 1;
//// Wenn am Ende der Queue neu gemischt werden soll bitte die Zeilen wieder aktivieren
// Serial.println(F("Ende der Queue -> mische neu"));
// shuffleQueue();
}
Serial.println(queue[currentTrack - 1]);
mp3.playFolderTrack(myFolder->folder, queue[currentTrack - 1]);
}
if (myFolder->mode == 4) {
Serial.println(F("Einzel Modus aktiv -> Strom sparen"));
// mp3.sleep(); // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
setstandbyTimer();
}
if (myFolder->mode == 5) {
if (currentTrack != numTracksInFolder) {
currentTrack = currentTrack + 1;
Serial.print(F("Hörbuch Modus ist aktiv -> nächster Track und "
"Fortschritt speichern"));
Serial.println(currentTrack);
mp3.playFolderTrack(myFolder->folder, currentTrack);
// Fortschritt im EEPROM abspeichern
EEPROM.update(myFolder->folder, currentTrack);
} else {
// mp3.sleep(); // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
// Fortschritt zurück setzen
EEPROM.update(myFolder->folder, 1);
setstandbyTimer();
}
}
delay(500);
}
static void previousTrack() {
Serial.println(F("=== previousTrack()"));
/* if (myCard.mode == 1 || myCard.mode == 7) {
Serial.println(F("Hörspielmodus ist aktiv -> Track von vorne spielen"));
mp3.playFolderTrack(myCard.folder, currentTrack);
}*/
if (myFolder->mode == 2 || myFolder->mode == 8) {
Serial.println(F("Albummodus ist aktiv -> vorheriger Track"));
if (currentTrack != firstTrack) {
currentTrack = currentTrack - 1;
}
mp3.playFolderTrack(myFolder->folder, currentTrack);
}
if (myFolder->mode == 3 || myFolder->mode == 9) {
if (currentTrack != 1) {
Serial.print(F("Party Modus ist aktiv -> zurück in der Qeueue "));
currentTrack--;
}
else
{
Serial.print(F("Anfang der Queue -> springe ans Ende "));
currentTrack = numTracksInFolder;
}
Serial.println(queue[currentTrack - 1]);
mp3.playFolderTrack(myFolder->folder, queue[currentTrack - 1]);
}
if (myFolder->mode == 4) {
Serial.println(F("Einzel Modus aktiv -> Track von vorne spielen"));
mp3.playFolderTrack(myFolder->folder, currentTrack);
}
if (myFolder->mode == 5) {
Serial.println(F("Hörbuch Modus ist aktiv -> vorheriger Track und "
"Fortschritt speichern"));
if (currentTrack != 1) {
currentTrack = currentTrack - 1;
}
mp3.playFolderTrack(myFolder->folder, currentTrack);
// Fortschritt im EEPROM abspeichern
EEPROM.update(myFolder->folder, currentTrack);
}
delay(1000);
}
// MFRC522
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522
MFRC522::MIFARE_Key key;
bool successRead;
byte sector = 1;
byte blockAddr = 4;
byte trailerBlock = 7;
MFRC522::StatusCode status;
#define buttonPause A0
#define buttonUp A1
#define buttonDown A2
#define busyPin 4
#define shutdownPin 7
#define openAnalogPin A7
#ifdef FIVEBUTTONS
#define buttonFourPin A3
#define buttonFivePin A4
#endif
#define LONG_PRESS 1000
Button pauseButton(buttonPause);
Button upButton(buttonUp);
Button downButton(buttonDown);
#ifdef FIVEBUTTONS
Button buttonFour(buttonFourPin);
Button buttonFive(buttonFivePin);
#endif
bool ignorePauseButton = false;
bool ignoreUpButton = false;
bool ignoreDownButton = false;
#ifdef FIVEBUTTONS
bool ignoreButtonFour = false;
bool ignoreButtonFive = false;
#endif
/// Funktionen für den Standby Timer (z.B. über Pololu-Switch oder Mosfet)
void setstandbyTimer() {
Serial.println(F("=== setstandbyTimer()"));
if (mySettings.standbyTimer != 0)
sleepAtMillis = millis() + (mySettings.standbyTimer * 60 * 1000);
else
sleepAtMillis = 0;
Serial.println(sleepAtMillis);
}
void disablestandbyTimer() {
Serial.println(F("=== disablestandby()"));
sleepAtMillis = 0;
}
void checkStandbyAtMillis() {
if (sleepAtMillis != 0 && millis() > sleepAtMillis) {
Serial.println(F("=== power off!"));
// enter sleep state
digitalWrite(shutdownPin, HIGH);
delay(500);
// http://discourse.voss.earth/t/intenso-s10000-powerbank-automatische-abschaltung-software-only/805
// powerdown to 27mA (powerbank switches off after 30-60s)
mfrc522.PCD_AntennaOff();
mfrc522.PCD_SoftPowerDown();
mp3.sleep();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli(); // Disable interrupts
sleep_mode();
}
}
bool isPlaying() {
return !digitalRead(busyPin);
}
void waitForTrackToFinish() {
long currentTime = millis();
#define TIMEOUT 1000
do {
mp3.loop();
} while (!isPlaying() && millis() < currentTime + TIMEOUT);
delay(1000);
do {
mp3.loop();
} while (isPlaying());
}
void setup() {
Serial.begin(115200); // Es gibt ein paar Debug Ausgaben über die serielle Schnittstelle
// Wert für randomSeed() erzeugen durch das mehrfache Sammeln von rauschenden LSBs eines offenen Analogeingangs
uint32_t ADC_LSB;
uint32_t ADCSeed;
for (uint8_t i = 0; i < 128; i++) {
ADC_LSB = analogRead(openAnalogPin) & 0x1;
ADCSeed ^= ADC_LSB << (i % 32);
}
randomSeed(ADCSeed); // Zufallsgenerator initialisieren
// Dieser Hinweis darf nicht entfernt werden
Serial.println(F("\n _____ _____ _____ _____ _____"));
Serial.println(F("|_ _|___ ___| | | | | | |"));
Serial.println(F(" | | | . | | | |- -| | | | | |"));
Serial.println(F(" |_| |___|_|_|_____|_____|_|___|_____|\n"));
Serial.println(F("TonUINO Version 2.1"));
Serial.println(F("created by Thorsten Voß and licensed under GNU/GPL."));
Serial.println(F("Information and contribution at https://tonuino.de.\n"));
// Busy Pin
pinMode(busyPin, INPUT);
// load Settings from EEPROM
loadSettingsFromFlash();
// activate standby timer
setstandbyTimer();
// DFPlayer Mini initialisieren
mp3.begin();
// Zwei Sekunden warten bis der DFPlayer Mini initialisiert ist
delay(2000);
volume = mySettings.initVolume;
mp3.setVolume(volume);
mp3.setEq(mySettings.eq - 1);
// Fix für das Problem mit dem Timeout (ist jetzt in Upstream daher nicht mehr nötig!)
//mySoftwareSerial.setTimeout(10000);
// NFC Leser initialisieren
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
mfrc522
.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
pinMode(buttonPause, INPUT_PULLUP);
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
#ifdef FIVEBUTTONS
pinMode(buttonFourPin, INPUT_PULLUP);
pinMode(buttonFivePin, INPUT_PULLUP);
#endif
pinMode(shutdownPin, OUTPUT);
digitalWrite(shutdownPin, LOW);
// RESET --- ALLE DREI KNÖPFE BEIM STARTEN GEDRÜCKT HALTEN -> alle EINSTELLUNGEN werden gelöscht
if (digitalRead(buttonPause) == LOW && digitalRead(buttonUp) == LOW &&
digitalRead(buttonDown) == LOW) {
Serial.println(F("Reset -> EEPROM wird gelöscht"));
for (int i = 0; i < EEPROM.length(); i++) {
EEPROM.update(i, 0);
}
loadSettingsFromFlash();
}
// Start Shortcut "at Startup" - e.g. Welcome Sound
playShortCut(3);
}
void readButtons() {
pauseButton.read();
upButton.read();
downButton.read();
#ifdef FIVEBUTTONS
buttonFour.read();
buttonFive.read();
#endif
}
void volumeUpButton() {
if (activeModifier != NULL)
if (activeModifier->handleVolumeUp() == true)
return;
Serial.println(F("=== volumeUp()"));
if (volume < mySettings.maxVolume) {
mp3.increaseVolume();
volume++;
}
Serial.println(volume);
}
void volumeDownButton() {
if (activeModifier != NULL)
if (activeModifier->handleVolumeDown() == true)
return;
Serial.println(F("=== volumeDown()"));
if (volume > mySettings.minVolume) {
mp3.decreaseVolume();
volume--;
}
Serial.println(volume);
}
void nextButton() {
if (activeModifier != NULL)
if (activeModifier->handleNextButton() == true)
return;
nextTrack(random(65536));
delay(1000);
}
void previousButton() {
if (activeModifier != NULL)
if (activeModifier->handlePreviousButton() == true)
return;
previousTrack();
delay(1000);
}
void playFolder() {
Serial.println(F("== playFolder()")) ;
disablestandbyTimer();
knownCard = true;
_lastTrackFinished = 0;
numTracksInFolder = mp3.getFolderTrackCount(myFolder->folder);
firstTrack = 1;
Serial.print(numTracksInFolder);
Serial.print(F(" Dateien in Ordner "));
Serial.println(myFolder->folder);
// Hörspielmodus: eine zufällige Datei aus dem Ordner
if (myFolder->mode == 1) {
Serial.println(F("Hörspielmodus -> zufälligen Track wiedergeben"));
currentTrack = random(1, numTracksInFolder + 1);
Serial.println(currentTrack);
mp3.playFolderTrack(myFolder->folder, currentTrack);
}
// Album Modus: kompletten Ordner spielen
if (myFolder->mode == 2) {
Serial.println(F("Album Modus -> kompletten Ordner wiedergeben"));
currentTrack = 1;
mp3.playFolderTrack(myFolder->folder, currentTrack);
}
// Party Modus: Ordner in zufälliger Reihenfolge
if (myFolder->mode == 3) {
Serial.println(
F("Party Modus -> Ordner in zufälliger Reihenfolge wiedergeben"));
shuffleQueue();
currentTrack = 1;
mp3.playFolderTrack(myFolder->folder, queue[currentTrack - 1]);
}
// Einzel Modus: eine Datei aus dem Ordner abspielen
if (myFolder->mode == 4) {
Serial.println(
F("Einzel Modus -> eine Datei aus dem Odrdner abspielen"));
currentTrack = myFolder->special;
mp3.playFolderTrack(myFolder->folder, currentTrack);
}
// Hörbuch Modus: kompletten Ordner spielen und Fortschritt merken
if (myFolder->mode == 5) {
Serial.println(F("Hörbuch Modus -> kompletten Ordner spielen und "
"Fortschritt merken"));
currentTrack = EEPROM.read(myFolder->folder);
if (currentTrack == 0 || currentTrack > numTracksInFolder) {
currentTrack = 1;
}
mp3.playFolderTrack(myFolder->folder, currentTrack);
}
// Spezialmodus Von-Bin: Hörspiel: eine zufällige Datei aus dem Ordner
if (myFolder->mode == 7) {
Serial.println(F("Spezialmodus Von-Bin: Hörspiel -> zufälligen Track wiedergeben"));
Serial.print(myFolder->special);
Serial.print(F(" bis "));
Serial.println(myFolder->special2);
numTracksInFolder = myFolder->special2;
currentTrack = random(myFolder->special, numTracksInFolder + 1);
Serial.println(currentTrack);
mp3.playFolderTrack(myFolder->folder, currentTrack);
}
// Spezialmodus Von-Bis: Album: alle Dateien zwischen Start und Ende spielen
if (myFolder->mode == 8) {
Serial.println(F("Spezialmodus Von-Bis: Album: alle Dateien zwischen Start- und Enddatei spielen"));
Serial.print(myFolder->special);
Serial.print(F(" bis "));
Serial.println(myFolder->special2);
numTracksInFolder = myFolder->special2;
currentTrack = myFolder->special;
mp3.playFolderTrack(myFolder->folder, currentTrack);
}
// Spezialmodus Von-Bis: Party Ordner in zufälliger Reihenfolge
if (myFolder->mode == 9) {
Serial.println(
F("Spezialmodus Von-Bis: Party -> Ordner in zufälliger Reihenfolge wiedergeben"));
firstTrack = myFolder->special;
numTracksInFolder = myFolder->special2;
shuffleQueue();
currentTrack = 1;
mp3.playFolderTrack(myFolder->folder, queue[currentTrack - 1]);
}
}
void playShortCut(uint8_t shortCut) {
Serial.println(F("=== playShortCut()"));
Serial.println(shortCut);
if (mySettings.shortCuts[shortCut].folder != 0) {
myFolder = &mySettings.shortCuts[shortCut];
playFolder();
disablestandbyTimer();
delay(1000);
}
else
Serial.println(F("Shortcut not configured!"));
}
void loop() {
do {
checkStandbyAtMillis();
mp3.loop();
// Modifier : WIP!
if (activeModifier != NULL) {
activeModifier->loop();
}
// Buttons werden nun über JS_Button gehandelt, dadurch kann jede Taste
// doppelt belegt werden
readButtons();
// admin menu
if ((pauseButton.pressedFor(LONG_PRESS) || upButton.pressedFor(LONG_PRESS) || downButton.pressedFor(LONG_PRESS)) && pauseButton.isPressed() && upButton.isPressed() && downButton.isPressed()) {
mp3.pause();
do {
readButtons();
} while (pauseButton.isPressed() || upButton.isPressed() || downButton.isPressed());
readButtons();
adminMenu();
break;
}
if (pauseButton.wasReleased()) {
if (activeModifier != NULL)
if (activeModifier->handlePause() == true)
return;
if (ignorePauseButton == false)
if (isPlaying()) {
mp3.pause();
setstandbyTimer();
}
else if (knownCard) {
mp3.start();
disablestandbyTimer();
}
ignorePauseButton = false;
} else if (pauseButton.pressedFor(LONG_PRESS) &&
ignorePauseButton == false) {
if (activeModifier != NULL)
if (activeModifier->handlePause() == true)
return;
if (isPlaying()) {
uint8_t advertTrack;
if (myFolder->mode == 3 || myFolder->mode == 9) {
advertTrack = (queue[currentTrack - 1]);
}
else {
advertTrack = currentTrack;
}
// Spezialmodus Von-Bis für Album und Party gibt die Dateinummer relativ zur Startposition wieder