forked from xfjx/TonUINO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTonuino.ino
916 lines (808 loc) · 23.1 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
/*
_____ _____ _____ _____ _____
|_ _|___ ___| | | | | | |
| | | . | | | |- -| | | | | |
|_| |___|_|_|_____|_____|_|___|_____|
TonUINO Version 2.3
created by Thorsten Voß and licensed under GNU/GPL.
Information and contribution at https://tonuino.de.
*/
#include <EEPROM.h>
#include <MFRC522.h>
#include <JC_Button.h>
#include <SPI.h>
#include <avr/sleep.h>
/**
* Compile-time configuration
*/
#include "Tonuino.h"
#include "Logging.h"
#include "PlayerMode.h"
/**
* MFRC522
*/
byte blockAddr = 4;
byte trailerBlock = 7;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522
MFRC522::MIFARE_Key key;
/**
* Buttons
*/
Button pauseButton(buttonPause);
Button upButton(buttonUp);
Button downButton(buttonDown);
/* Cookie to identify our cards */
const byte cardCookie[4] = { 0x13, 0x37, 0xb3, 0x47 };
/* DFPlayer Mini */
SoftwareSerial mySoftwareSerial(2, 3); // RX, TX
/**
* Global variables.
* TODO: Move to global state object
*/
uint8_t myVolume;
unsigned long sleepAtMillis = 0;
bool knownCard = false;
folderSettings *myFolder;
nfcTagObject myCard;
adminSettings mySettings;
static DfMp3 mp3(mySoftwareSerial);
/**
* Forward declarations.
*/
uint8_t voiceMenu(
int numberOfOptions,
int startMessage,
int messageOffset,
bool preview = false,
int defaultValue = 0);
bool isPlaying();
void writeCard(nfcTagObject nfcTag);
void adminMenu(bool adminCard);
/**
* Function definitions
*/
void storeSettings() {
DEBUG_PRINTLN(F("=== storeSettings"));
int address = sizeof(myFolder->folder) * 100;
EEPROM.put(address, mySettings);
}
void resetSettings() {
DEBUG_PRINTLN(F("=== resetSettings"));
memcpy(&mySettings.cookie[0], &cardCookie[0], sizeof(cardCookie));
mySettings.version = MY_SETTINGS_VERSION;
mySettings.maxVolume = 25;
mySettings.minVolume = 5;
mySettings.initVolume = 15;
mySettings.eq = DfMp3_Eq_Normal;
mySettings.standbyTimer = 0;
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;
storeSettings();
}
void loadSettings() {
DEBUG_PRINTLN(F("=== loadSettings"));
int address = sizeof(myFolder->folder) * 100;
EEPROM.get(address, mySettings);
if (memcmp(&mySettings.cookie[0], &cardCookie[0], sizeof(cardCookie))
!= 0) {
resetSettings();
}
DEBUG_PRINT(F("Version: "));
DEBUG_PRINTLN(mySettings.version);
DEBUG_PRINT(F("Maximal Volume: "));
DEBUG_PRINTLN(mySettings.maxVolume);
DEBUG_PRINT(F("Minimal Volume: "));
DEBUG_PRINTLN(mySettings.minVolume);
DEBUG_PRINT(F("Initial Volume: "));
DEBUG_PRINTLN(mySettings.initVolume);
DEBUG_PRINT(F("EQ: "));
DEBUG_PRINTLN(mySettings.eq);
DEBUG_PRINT(F("Sleep Timer: "));
DEBUG_PRINTLN(mySettings.standbyTimer);
DEBUG_PRINT(F("Admin Menu locked: "));
DEBUG_PRINTLN(mySettings.adminMenuLocked);
DEBUG_PRINT(F("Admin Menu Pin: "));
DEBUG_PRINT(mySettings.adminMenuPin[0]);
DEBUG_PRINT(mySettings.adminMenuPin[1]);
DEBUG_PRINT(mySettings.adminMenuPin[2]);
DEBUG_PRINTLN(mySettings.adminMenuPin[3]);
}
/// Funktionen für den Standby Timer (z.B. über Pololu-Switch oder Mosfet)
void setstandbyTimer() {
DEBUG_PRINTLN(F("=== setstandbyTimer()"));
if (mySettings.standbyTimer != 0)
sleepAtMillis = millis() + (mySettings.standbyTimer * 60 * 1000);
else
sleepAtMillis = 0;
DEBUG_PRINTLN(sleepAtMillis);
}
void disablestandbyTimer() {
DEBUG_PRINTLN(F("=== disablestandby()"));
sleepAtMillis = 0;
}
void checkStandbyAtMillis() {
if (sleepAtMillis != 0 && millis() > sleepAtMillis) {
DEBUG_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();
}
}
/**
* Check line busy.
*/
bool isPlaying() {
return !digitalRead(busyPin);
}
void waitForTrackToFinish() {
// TODO: Add time-out as parameter
do {
delay(500);
} while (isPlaying());
}
void setup() {
Serial.begin(115200); // set-up serial for debug messages
// init randomSeed() from ADC LSBs
uint32_t ADC_LSB = 0;
uint32_t ADCSeed = 0;
for (uint8_t i = 0; i < 128; i++) {
ADC_LSB = analogRead(openAnalogPin) & 0x1;
ADCSeed ^= ADC_LSB << (i % 32);
}
randomSeed(ADCSeed); // init PRNG
// Dieser Hinweis darf nicht entfernt werden
DEBUG_PRINTLN(F("\n _____ _____ _____ _____ _____"));
DEBUG_PRINTLN(F("|_ _|___ ___| | | | | | |"));
DEBUG_PRINTLN(F(" | | | . | | | |- -| | | | | |"));
DEBUG_PRINTLN(F(" |_| |___|_|_|_____|_____|_|___|_____|\n"));
DEBUG_PRINTLN(F("TonUINO Version 2.3"));
DEBUG_PRINTLN(F("created by Thorsten Voß and licensed under GNU/GPL."));
DEBUG_PRINTLN(F("Information and contribution at https://tonuino.de.\n"));
// Busy Pin
pinMode(busyPin, INPUT);
// load Settings from EEPROM
loadSettings();
// activate standby timer
setstandbyTimer();
// DFPlayer Mini initialisieren
mp3.begin();
// Zwei Sekunden warten bis der DFPlayer Mini initialisiert ist
delay(2000);
// 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);
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) {
DEBUG_PRINTLN(F("Reset -> EEPROM wird gelöscht"));
for (unsigned int i = 0; i < EEPROM.length(); i++) {
EEPROM.update(i, 0);
}
loadSettings();
}
// Set volume
myVolume = mySettings.initVolume;
mp3.setVolume(myVolume);
// Set eq mode
mp3.setEq(mySettings.eq);
// Play welcome jingle
mp3.playMp3FolderTrack(400);
waitForTrackToFinish();
}
void readButtons() {
pauseButton.read();
upButton.read();
downButton.read();
}
void loop() {
do {
checkStandbyAtMillis();
mp3.loop();
// check buttons
readButtons();
// check for admin menu request
if (upButton.pressedFor(LONG_PRESS) && downButton.pressedFor(LONG_PRESS)
&& pauseButton.isReleased())
{
PlayerMode* savedMode = activeMode;
if (activeMode) {
activeMode->handleStop();
activeMode = NULL;
}
adminMenu(false);
activeMode = savedMode;
if (activeMode) activeMode->handlePlay();
}
// handle pause/play button
// TODO: Action for LONG_PRESS?
if (pauseButton.wasReleased()) {
if (isPlaying()) {
mp3.pause();
setstandbyTimer();
} else if (knownCard) {
disablestandbyTimer();
mp3.start();
}
}
// handle up button
if (upButton.pressedFor(LONG_PRESS) && downButton.isReleased()) {
if (activeMode) {
DEBUG_PRINTLN(F("=== nextTrack"));
activeMode->handleNextTrack();
delay(500);
}
// wait for key to be released
do {
readButtons();
} while (upButton.isPressed());
readButtons();
} else if (upButton.wasReleased()) {
DEBUG_PRINTLN(F("=== volumeUp"));
if (myVolume < mySettings.maxVolume) {
mp3.increaseVolume();
myVolume++;
}
DEBUG_PRINTLN(myVolume);
}
// handle down button
if (downButton.pressedFor(LONG_PRESS) && upButton.isReleased()) {
if (activeMode) {
DEBUG_PRINTLN(F("=== previousTrack"));
activeMode->handlePreviousTrack();
delay(1000);
}
// wait for key to be released
do {
readButtons();
} while (downButton.isPressed());
readButtons();
} else if (downButton.wasReleased()) {
DEBUG_PRINTLN(F("=== volumeDown"));
if (myVolume > mySettings.minVolume) {
mp3.decreaseVolume();
myVolume--;
}
DEBUG_PRINTLN(myVolume);
}
// Ende der Buttons
} while (!mfrc522.PICC_IsNewCardPresent());
// RFID Karte wurde aufgelegt
if (!mfrc522.PICC_ReadCardSerial())
return;
/* Invalidate card and active mode */
knownCard = false;
if (activeMode != NULL) {
delete activeMode;
activeMode = NULL;
}
if (readCard(&myCard) == true) {
// valid card found, stop playing current title
if (activeMode) activeMode->handleStop();
// read tracks in folder of card
uint16_t numTracksInFolder = 0;
disablestandbyTimer();
if (myFolder->folder != 0) {
numTracksInFolder = mp3.getFolderTrackCount(myFolder->folder);
DEBUG_PRINT(numTracksInFolder);
DEBUG_PRINT(F(" Dateien in Ordner "));
DEBUG_PRINTLN(myFolder->folder);
}
// set mode of card
if (myCard.nfcFolderSettings.mode != MODE_INVALID) {
DEBUG_PRINTLN(F("== set mode of folder"));
myFolder->mode = myCard.nfcFolderSettings.mode;
}
switch (myFolder->mode) {
case MODE_ALBUM:
knownCard = true;
activeMode = new AlbumMode(numTracksInFolder);
break;
case MODE_AUDIO_BOOK:
knownCard = true;
activeMode = new AudiobookMode(numTracksInFolder);
break;
case MODE_RANDOM:
knownCard = true;
activeMode = new RandomMode(numTracksInFolder);
break;
case MODE_ADMIN:
adminMenu(true);
return;
default:
DEBUG_PRINTLN(F("Error: Unknown mode on card"));
myFolder->mode = MODE_INVALID;
myFolder->folder = 0;
}
}
if (knownCard) {
activeMode->handlePlay();
} else {
mp3.playMp3FolderTrack(300);
waitForTrackToFinish();
setupCard();
}
// Leave card alone
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
void adminMenu(bool adminCard) {
int res;
disablestandbyTimer();
mp3.playMp3FolderTrack(401); // play ok
waitForTrackToFinish();
DEBUG_PRINTLN(F("=== adminMenu"));
if (adminCard == false) {
// wait for keys to be released
do {
readButtons();
} while (upButton.isPressed() || downButton.isPressed());
readButtons();
// Admin menu has been locked - it still can be trigged via admin card
if (mySettings.adminMenuLocked == 1) return;
// Pin check
if (mySettings.adminMenuLocked == 2) {
uint8_t pin[4];
mp3.playMp3FolderTrack(991);
if (askCode(pin) == true) {
if (memcmp(pin, mySettings.adminMenuPin, sizeof(pin)) != 0) {
return;
}
} else {
return;
}
}
// Match check
else if (mySettings.adminMenuLocked == 3) {
uint8_t a = random(10, 20);
uint8_t b = random(1, 10);
uint8_t c;
mp3.playMp3FolderTrack(992);
waitForTrackToFinish();
mp3.playMp3FolderTrack(a);
if (random(1, 3) == 2) {
// a + b
c = a + b;
waitForTrackToFinish();
mp3.playMp3FolderTrack(993);
} else {
// a - b
c = a - b;
waitForTrackToFinish();
mp3.playMp3FolderTrack(994);
}
waitForTrackToFinish();
mp3.playMp3FolderTrack(b);
DEBUG_PRINTLN(c);
uint8_t temp = voiceMenu(255, 0, 0, false);
if (temp != c) return;
}
}
int subMenu = voiceMenu(9, 900, 900, false, 0);
switch (subMenu) {
case 1: // reset card
resetCard();
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
break;
case 2: // set max. volume
mySettings.maxVolume = mySettings.minVolume + voiceMenu(
30 - mySettings.minVolume,
930,
mySettings.minVolume,
false,
mySettings.maxVolume - mySettings.minVolume);
break;
case 3: // set min. volume
mySettings.minVolume = voiceMenu(
mySettings.maxVolume - 1,
931,
0,
false,
mySettings.minVolume);
break;
case 4: // set initial volume
mySettings.initVolume = mySettings.minVolume - 1 + voiceMenu(
mySettings.maxVolume - mySettings.minVolume + 1,
932,
mySettings.minVolume - 1,
false,
mySettings.initVolume - mySettings.minVolume + 1);
break;
case 5: // set EQ
res = voiceMenu(6, 920, 920, false, mySettings.eq);
if (res <= DfMp3_Eq_Bass) {
mySettings.eq = static_cast<DfMp3_Eq>(res);
}
mp3.setEq(mySettings.eq);
break;
case 6: // set short-cuts
res = voiceMenu(4, 940, 940, true, 0);
if (res > 0) {
if (setupFolder(&mySettings.shortCuts[res - 1])) {
mp3.playMp3FolderTrack(401); // play ok
} else {
mp3.playMp3FolderTrack(402); // play error
}
} else {
mp3.playMp3FolderTrack(402); // play error
}
waitForTrackToFinish();
break;
case 7: // set stand-by time
switch (voiceMenu(5, 960, 960)) {
case 1:
mySettings.standbyTimer = 5;
break;
case 2:
mySettings.standbyTimer = 15;
break;
case 3:
mySettings.standbyTimer = 30;
break;
case 4:
mySettings.standbyTimer = 60;
break;
case 5:
mySettings.standbyTimer = 0;
break;
}
setstandbyTimer();
break;
case 8: // reset EEPROM
DEBUG_PRINTLN(F("Reset -> EEPROM wird gelöscht"));
for (uint16_t i = 0; i < EEPROM.length(); i++) {
EEPROM.update(i, 0);
}
resetSettings();
mp3.playMp3FolderTrack(999);
break;
case 9: // lock admin menu
res = voiceMenu(4, 980, 980, false);
if (res == 1) {
mySettings.adminMenuLocked = 0;
} else if (res == 2) {
mySettings.adminMenuLocked = 1;
} else if (res == 3) {
uint8_t pin[4];
mp3.playMp3FolderTrack(991);
if (askCode(&pin[0])) {
memcpy(mySettings.adminMenuPin, pin, 4);
mySettings.adminMenuLocked = 2;
}
} else if (res == 4) {
mySettings.adminMenuLocked = 3;
}
break;
default:
return;
}
storeSettings();
}
bool askCode(uint8_t *code) {
uint8_t x = 0;
while (x < 4) {
readButtons();
if (pauseButton.pressedFor(LONG_PRESS)) {
do {
readButtons();
} while (pauseButton.isPressed());
readButtons();
break;
}
if (pauseButton.wasReleased())
code[x++] = 1;
if (upButton.wasReleased())
code[x++] = 2;
if (downButton.wasReleased())
code[x++] = 3;
}
return true;
}
uint8_t voiceMenu(
int numberOfOptions,
int startMessage,
int messageOffset,
bool preview,
int defaultValue )
{
// first key-press increases returnValue to 1 or defaultValue
int16_t returnValue = max(0, defaultValue-1);
// play start message
if (startMessage != 0) {
DEBUG_PRINT(F("=== Menu: Play message "));
DEBUG_PRINTLN(startMessage);
mp3.playMp3FolderTrack(startMessage);
}
DEBUG_PRINT(F("=== voiceMenu ("));
DEBUG_PRINT(returnValue);
DEBUG_PRINT(F(" / "));
DEBUG_PRINT(numberOfOptions);
DEBUG_PRINTLN(F(")"));
do {
mp3.loop();
readButtons();
if (pauseButton.pressedFor(LONG_PRESS)) {
mp3.playMp3FolderTrack(802);
waitForTrackToFinish();
do {
readButtons();
} while (pauseButton.isPressed());
readButtons();
return defaultValue;
}
if (pauseButton.wasReleased()) {
DEBUG_PRINT(F("=== Selected option "));
DEBUG_PRINTLN(returnValue);
if ( (returnValue > 0) && (returnValue <= numberOfOptions) ) {
mp3.playMp3FolderTrack(401); // play ok
waitForTrackToFinish();
return returnValue;
}
mp3.playMp3FolderTrack(402); // play error
waitForTrackToFinish();
return defaultValue;
}
if (upButton.pressedFor(LONG_PRESS)) {
returnValue = min(returnValue + 10, numberOfOptions);
if (preview) {
mp3.playFolderTrack(returnValue, 1);
} else {
mp3.playMp3FolderTrack(messageOffset + returnValue);
}
do {
readButtons();
} while (upButton.isPressed());
readButtons();
} else if (upButton.wasReleased()) {
returnValue = min(returnValue + 1, numberOfOptions);
if (preview) {
mp3.playFolderTrack(returnValue, 1);
} else {
mp3.playMp3FolderTrack(messageOffset + returnValue);
}
}
if (downButton.pressedFor(LONG_PRESS)) {
returnValue = max(returnValue - 10, 1);
if (preview) {
mp3.playFolderTrack(returnValue, 1);
} else {
mp3.playMp3FolderTrack(messageOffset + returnValue);
}
do {
readButtons();
} while (downButton.isPressed());
readButtons();
} else if (downButton.wasReleased()) {
returnValue = max(returnValue - 1, 1);
if (preview) {
mp3.playFolderTrack(returnValue, 1);
} else {
mp3.playMp3FolderTrack(messageOffset + returnValue);
}
}
} while (true);
}
void resetCard() {
mp3.playMp3FolderTrack(800);
do {
pauseButton.read();
upButton.read();
downButton.read();
if (upButton.wasReleased() || downButton.wasReleased()) {
DEBUG_PRINT(F("Abgebrochen!"));
mp3.playMp3FolderTrack(802);
return;
}
} while (!mfrc522.PICC_IsNewCardPresent());
if (!mfrc522.PICC_ReadCardSerial())
return;
DEBUG_PRINT(F("Karte wird neu konfiguriert!"));
setupCard();
}
// TODO: Umstellen auf PlayerMode
bool setupFolder(folderSettings *theFolder) {
// Ordner abfragen
theFolder->folder = voiceMenu(99, 301, 0, true, 0);
if (theFolder->folder == 0) return false;
// Wiedergabemodus abfragen
theFolder->mode = voiceMenu(4, 310, 310, false, MODE_INVALID);
if (theFolder->mode == MODE_INVALID) return false;
// Admin Funktionen
if (theFolder->mode == MODE_ADMIN_MENU) {
theFolder->folder = 0;
theFolder->mode = MODE_ADMIN;
}
return true;
}
void setupCard() {
DEBUG_PRINTLN(F("=== setupCard"));
nfcTagObject newCard;
if (setupFolder(&newCard.nfcFolderSettings) == true) {
// folder and mode selected, write to card
writeCard(newCard);
}
}
bool readCard(nfcTagObject *nfcTag) {
nfcTagObject tempCard;
MFRC522::StatusCode status = MFRC522::STATUS_ERROR;
// Show some details of the PICC (that is: the tag/card)
DEBUG_PRINT(F("Card UID:"));
DEBUG_PRINTB(mfrc522.uid.uidByte, mfrc522.uid.size);
DEBUG_PRINTLN();
DEBUG_PRINT(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
DEBUG_PRINTLN(mfrc522.PICC_GetTypeName(piccType));
byte buffer[18];
// Authenticate using key A
if ((piccType == MFRC522::PICC_TYPE_MIFARE_MINI)
|| (piccType == MFRC522::PICC_TYPE_MIFARE_1K)
|| (piccType == MFRC522::PICC_TYPE_MIFARE_4K)) {
DEBUG_PRINTLN(F("Authenticating Classic using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
trailerBlock, &key, &(mfrc522.uid));
} else if (piccType == MFRC522::PICC_TYPE_MIFARE_UL) {
byte pACK[] = { 0, 0 }; //16 bit PassWord ACK returned by the tempCard
// Authenticate using key A
DEBUG_PRINTLN(F("Authenticating MIFARE UL..."));
status = mfrc522.PCD_NTAG216_AUTH(key.keyByte, pACK);
}
if (status != MFRC522::STATUS_OK) {
DEBUG_PRINT(F("PCD_Authenticate() failed: "));
DEBUG_PRINTLN(mfrc522.GetStatusCodeName(status));
return false;
}
// Read data from the block
if ((piccType == MFRC522::PICC_TYPE_MIFARE_MINI)
|| (piccType == MFRC522::PICC_TYPE_MIFARE_1K)
|| (piccType == MFRC522::PICC_TYPE_MIFARE_4K)) {
DEBUG_PRINT(F("Reading data from block "));
DEBUG_PRINT(blockAddr);
DEBUG_PRINTLN(F(" ..."));
byte size = sizeof(buffer);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer,
&size);
if (status != MFRC522::STATUS_OK) {
DEBUG_PRINT(F("MIFARE_Read() failed: "));
DEBUG_PRINTLN(mfrc522.GetStatusCodeName(status));
return false;
}
} else if (piccType == MFRC522::PICC_TYPE_MIFARE_UL) {
byte buffer2[18];
byte size2 = sizeof(buffer2);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(8, buffer2, &size2);
if (status != MFRC522::STATUS_OK) {
DEBUG_PRINT(F("MIFARE_Read_1() failed: "));
DEBUG_PRINTLN(mfrc522.GetStatusCodeName(status));
return false;
}
memcpy(buffer, buffer2, 4);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(9, buffer2, &size2);
if (status != MFRC522::STATUS_OK) {
DEBUG_PRINT(F("MIFARE_Read_2() failed: "));
DEBUG_PRINTLN(mfrc522.GetStatusCodeName(status));
return false;
}
memcpy(buffer + 4, buffer2, 4);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(10, buffer2, &size2);
if (status != MFRC522::STATUS_OK) {
DEBUG_PRINT(F("MIFARE_Read_3() failed: "));
DEBUG_PRINTLN(mfrc522.GetStatusCodeName(status));
return false;
}
memcpy(buffer + 8, buffer2, 4);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(11, buffer2, &size2);
if (status != MFRC522::STATUS_OK) {
DEBUG_PRINT(F("MIFARE_Read_4() failed: "));
DEBUG_PRINTLN(mfrc522.GetStatusCodeName(status));
return false;
}
memcpy(buffer + 12, buffer2, 4);
}
DEBUG_PRINTLN(F("Data on Card:"));
DEBUG_PRINTB(buffer, sizeof(buffer));
DEBUG_PRINTLN();
DEBUG_PRINTLN();
memcpy(&tempCard.cookie[0], &buffer[0], sizeof(cardCookie));
tempCard.version = buffer[4];
tempCard.nfcFolderSettings.folder = buffer[5];
tempCard.nfcFolderSettings.mode = buffer[6];
if (memcmp(&tempCard.cookie[0], &cardCookie[0], sizeof(cardCookie)) == 0) {
memcpy(nfcTag, &tempCard, sizeof(nfcTagObject));
myFolder = &nfcTag->nfcFolderSettings;
DEBUG_PRINT(F("myFolder: "));
DEBUG_PRINTLN(myFolder->folder);
return true;
}
return false;
}
void writeCard(nfcTagObject nfcTag) {
MFRC522::PICC_Type mifareType;
MFRC522::StatusCode status = MFRC522::STATUS_ERROR;
byte buffer[16] = { cardCookie[0], cardCookie[1], cardCookie[2],
cardCookie[3], // magic cookie
MY_NFC_TAG_VERSION, // actual version
nfcTag.nfcFolderSettings.folder, // the folder picked by the user
nfcTag.nfcFolderSettings.mode, // the play-back mode picked by the user
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
mifareType = mfrc522.PICC_GetType(mfrc522.uid.sak);
// Authenticate using key B
//authentificate with the card and set card specific parameters
if ((mifareType == MFRC522::PICC_TYPE_MIFARE_MINI)
|| (mifareType == MFRC522::PICC_TYPE_MIFARE_1K)
|| (mifareType == MFRC522::PICC_TYPE_MIFARE_4K)) {
DEBUG_PRINTLN(F("Authenticating again using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
trailerBlock, &key, &(mfrc522.uid));
} else if (mifareType == MFRC522::PICC_TYPE_MIFARE_UL) {
byte pACK[] = { 0, 0 }; //16 bit PassWord ACK returned by the NFCtag
// Authenticate using key A
DEBUG_PRINTLN(F("Authenticating UL..."));
status = mfrc522.PCD_NTAG216_AUTH(key.keyByte, pACK);
}
if (status != MFRC522::STATUS_OK) {
DEBUG_PRINT(F("PCD_Authenticate() failed: "));
DEBUG_PRINTLN(mfrc522.GetStatusCodeName(status));
mp3.playMp3FolderTrack(402); // play error
waitForTrackToFinish();
return;
}
// Write data to the block
DEBUG_PRINT(F("Writing data into block "));
DEBUG_PRINT(blockAddr);
DEBUG_PRINTLN(F(" ..."));
DEBUG_PRINTB(buffer, 16);
DEBUG_PRINTLN();
if ((mifareType == MFRC522::PICC_TYPE_MIFARE_MINI)
|| (mifareType == MFRC522::PICC_TYPE_MIFARE_1K)
|| (mifareType == MFRC522::PICC_TYPE_MIFARE_4K)) {
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, buffer,
16);
} else if (mifareType == MFRC522::PICC_TYPE_MIFARE_UL) {
byte buffer2[16];
byte size2 = sizeof(buffer2);
memset(buffer2, 0, size2);
memcpy(buffer2, buffer, 4);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(8, buffer2, 16);
memset(buffer2, 0, size2);
memcpy(buffer2, buffer + 4, 4);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(9, buffer2, 16);
memset(buffer2, 0, size2);
memcpy(buffer2, buffer + 8, 4);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(10, buffer2, 16);
memset(buffer2, 0, size2);
memcpy(buffer2, buffer + 12, 4);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(11, buffer2, 16);
}
if (status != MFRC522::STATUS_OK) {
DEBUG_PRINT(F("MIFARE_Write() failed: "));
DEBUG_PRINTLN(mfrc522.GetStatusCodeName(status));
mp3.playMp3FolderTrack(402); // play error
} else {
mp3.playMp3FolderTrack(801); // play card config ok
}
waitForTrackToFinish();
}