forked from xfjx/TonUINO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tonuino.ino
703 lines (628 loc) · 21.2 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
#include <DFMiniMp3.h>
#include <EEPROM.h>
#include <JC_Button.h>
#include <MFRC522.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// DFPlayer Mini
SoftwareSerial mySoftwareSerial(2, 3); // RX, TX
uint16_t numTracksInFolder;
uint16_t currentTrack;
uint8_t volume;
// this object stores nfc tag data
struct nfcTagObject {
uint32_t cookie;
uint8_t version;
uint8_t folder;
uint8_t mode;
uint8_t special;
};
// ADXL345 (Beschleunigungssensor)
#define DEVICE (0x53) // Device address as specified in data sheet
byte _buff[6];
char POWER_CTL = 0x2D; //Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0 (0x32) - Z-Axis Data 1 (0x37)
bool show_Accel = false; // Zeige die Beschleunigung in der Console (false: Nein; true: Ja)
int x_axis, y_axis, z_axis, y_axis_init;
bool nextTrackReleased = false;
bool nextTrackFlag = false;
nfcTagObject myCard;
static void nextTrack(uint16_t track);
int voiceMenu(int numberOfOptions, int startMessage, int messageOffset,
bool preview = false, int previewFromFolder = 0);
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 OnPlayFinished(uint16_t track) {
Serial.print("Track beendet");
Serial.println(track);
delay(100);
nextTrack(track);
}
static void OnCardOnline(uint16_t code) {
Serial.println(F("SD Karte online "));
}
static void OnCardInserted(uint16_t code) {
Serial.println(F("SD Karte bereit "));
}
static void OnCardRemoved(uint16_t code) {
Serial.println(F("SD Karte entfernt "));
}
static void OnUsbOnline(uint16_t code) {
Serial.println(F("USB online "));
}
static void OnUsbInserted(uint16_t code) {
Serial.println(F("USB bereit "));
}
static void OnUsbRemoved(uint16_t code) {
Serial.println(F("USB entfernt "));
}
};
static DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(mySoftwareSerial);
// Leider kann das Modul keine Queue abspielen.
static uint16_t _lastTrackFinished;
static void nextTrack(uint16_t track) {
if (track == _lastTrackFinished) {
return;
}
_lastTrackFinished = track;
if (knownCard == false)
// Wenn eine neue Karte angelernt wird soll das Ende eines Tracks nicht
// verarbeitet werden
return;
if (myCard.mode == 1) {
Serial.println(F("Hörspielmodus ist aktiv -> keinen neuen Track spielen"));
// mp3.sleep(); // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
}
if (myCard.mode == 2) {
if (currentTrack != numTracksInFolder) {
currentTrack = currentTrack + 1;
mp3.playFolderTrack(myCard.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!
{ }
}
if (myCard.mode == 3) {
uint16_t oldTrack = currentTrack;
currentTrack = random(1, numTracksInFolder + 1);
if (currentTrack == oldTrack)
currentTrack = currentTrack == numTracksInFolder ? 1 : currentTrack+1;
Serial.print(F("Party Modus ist aktiv -> zufälligen Track spielen: "));
Serial.println(currentTrack);
mp3.playFolderTrack(myCard.folder, currentTrack);
}
if (myCard.mode == 4) {
Serial.println(F("Einzel Modus aktiv -> Strom sparen"));
// mp3.sleep(); // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
}
if (myCard.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(myCard.folder, currentTrack);
// Fortschritt im EEPROM abspeichern
EEPROM.write(myCard.folder, currentTrack);
}
else {
// mp3.sleep(); // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
// Fortschritt zurück setzen
EEPROM.write(myCard.folder, 1);
}
}
if (myCard.mode == 6) {
if ((currentTrack != numTracksInFolder) && (nextTrackFlag == true)) {
currentTrack = currentTrack + 1;
mp3.playFolderTrack(myCard.folder, currentTrack);
Serial.print(F("Einschlafmodus ist aktiv -> ein Track wird gespielt, ja"));
Serial.print(currentTrack);
nextTrackFlag = false;
} else
// mp3.sleep(); // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
{ }
}
}
static void previousTrack() {
if (myCard.mode == 1) {
Serial.println(F("Hörspielmodus ist aktiv -> Track von vorne spielen"));
mp3.playFolderTrack(myCard.folder, currentTrack);
}
if (myCard.mode == 2) {
Serial.println(F("Albummodus ist aktiv -> vorheriger Track"));
if (currentTrack != 1) {
currentTrack = currentTrack - 1;
}
mp3.playFolderTrack(myCard.folder, currentTrack);
}
if (myCard.mode == 3) {
Serial.println(F("Party Modus ist aktiv -> Track von vorne spielen"));
mp3.playFolderTrack(myCard.folder, currentTrack);
}
if (myCard.mode == 4) {
Serial.println(F("Einzel Modus aktiv -> Track von vorne spielen"));
mp3.playFolderTrack(myCard.folder, currentTrack);
}
if (myCard.mode == 5) {
Serial.println(F("Hörbuch Modus ist aktiv -> vorheriger Track und "
"Fortschritt speichern"));
if (currentTrack != 1) {
currentTrack = currentTrack - 1;
}
mp3.playFolderTrack(myCard.folder, currentTrack);
// Fortschritt im EEPROM abspeichern
EEPROM.write(myCard.folder, currentTrack);
}
if (myCard.mode == 6) {
Serial.println(F("Einschlafmodus ist aktiv -> vorheriger Track"));
if (currentTrack != 1) {
currentTrack = currentTrack - 1;
}
mp3.playFolderTrack(myCard.folder, currentTrack);
}
}
// 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 LONG_PRESS 1000
Button pauseButton(buttonPause);
Button upButton(buttonUp);
Button downButton(buttonDown);
bool ignorePauseButton = false;
bool ignoreUpButton = false;
bool ignoreDownButton = false;
uint8_t numberOfCards = 0;
bool isPlaying() { return !digitalRead(busyPin); }
void setup() {
Wire.begin(); // join i2c bus (for accerleration)
//Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
writeTo(POWER_CTL, 0x08);
Serial.begin(115200); // Es gibt ein paar Debug Ausgaben über die serielle
// Schnittstelle
randomSeed(analogRead(A0)); // Zufallsgenerator initialisieren
Serial.println(F("TonUINO-LEGO Version 2.0.1"));
Serial.println(F("(c) Michael Grelcke"));
Serial.println();
Serial.println(F("Original: https://www.voss.earth/tonuino by Thorsten Voß"));
// Knöpfe mit PullUp
pinMode(buttonPause, INPUT_PULLUP);
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(5, OUTPUT); //LEDs
digitalWrite(5, LOW);
// Beschleunigungssensor kalibrieren
readAccel();
y_axis_init = y_axis;
// Busy Pin
pinMode(busyPin, INPUT);
// DFPlayer Mini initialisieren
mp3.begin();
mp3.setVolume(15);
// 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;
}
// RESET --- ALLE DREI KNÖPFE BEIM STARTEN GEDRÜCKT HALTEN -> alle bekannten
// Karten 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.write(i, 0);
}
}
}
void loop(){
do {
mp3.loop();
// Buttons werden nun über JS_Button gehandelt, dadurch kann jede Taste
// doppelt belegt werden
pauseButton.read();
upButton.read();
downButton.read();
readAccel(); // read the x/y/z tilt
if (pauseButton.wasReleased()) {
if (ignorePauseButton == false) {
if (isPlaying())
mp3.pause();
else
mp3.start();
}
ignorePauseButton = false;
} else if (pauseButton.pressedFor(LONG_PRESS) &&
ignorePauseButton == false) {
if (isPlaying())
mp3.playAdvertisement(currentTrack);
else {
knownCard = false;
mp3.playMp3FolderTrack(800);
Serial.println(F("Karte resetten..."));
resetCard();
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
ignorePauseButton = true;
}
if (upButton.pressedFor(LONG_PRESS)) {
volume = mp3.getVolume();
if (volume < 18)
mp3.increaseVolume();
Serial.print("Volume Up: ");
Serial.println(volume);
ignoreUpButton = true;
} else if (upButton.wasReleased()) {
volume = mp3.getVolume();
if (volume < 18)
mp3.increaseVolume();
Serial.print("Volume Up: ");
Serial.println(volume);
ignoreUpButton = true;
}
if (downButton.pressedFor(LONG_PRESS)) {
Serial.println(F("Volume Down"));
mp3.decreaseVolume();
ignoreDownButton = true;
} else if (downButton.wasReleased()) {
Serial.println(F("Volume Down"));
mp3.decreaseVolume();
ignoreDownButton = true;
}
// Ende der Buttons
if (nextTrackReleased) {
if (y_axis_init - y_axis > 100) {
nextTrackFlag = true;
nextTrack(random(65536));
Serial.println("Next Track");
nextTrackReleased = false;
} else if (y_axis_init - y_axis < -100) {
previousTrack();
Serial.println("Previous Track");
nextTrackReleased = false;
}
}
} while (!mfrc522.PICC_IsNewCardPresent());
// RFID Karte wurde aufgelegt
if (!mfrc522.PICC_ReadCardSerial())
return;
if (readCard(&myCard) == true) {
if (myCard.cookie == 322417479 && myCard.folder != 0 && myCard.mode != 0) {
knownCard = true;
_lastTrackFinished = 0;
numTracksInFolder = mp3.getFolderTrackCount(myCard.folder);
Serial.print(numTracksInFolder);
Serial.print(F(" Dateien in Ordner "));
Serial.println(myCard.folder);
// Hörspielmodus: eine zufällige Datei aus dem Ordner
if (myCard.mode == 1) {
Serial.println(F("Hörspielmodus -> zufälligen Track wiedergeben"));
currentTrack = random(1, numTracksInFolder + 1);
Serial.println(currentTrack);
mp3.playFolderTrack(myCard.folder, currentTrack);
}
// Album Modus: kompletten Ordner spielen
if (myCard.mode == 2) {
Serial.println(F("Album Modus -> kompletten Ordner wiedergeben"));
currentTrack = 1;
mp3.playFolderTrack(myCard.folder, currentTrack);
}
// Party Modus: Ordner in zufälliger Reihenfolge
if (myCard.mode == 3) {
Serial.println(
F("Party Modus -> Ordner in zufälliger Reihenfolge wiedergeben"));
currentTrack = random(1, numTracksInFolder + 1);
mp3.playFolderTrack(myCard.folder, currentTrack);
}
// Einzel Modus: eine Datei aus dem Ordner abspielen
if (myCard.mode == 4) {
Serial.println(
F("Einzel Modus -> eine Datei aus dem Odrdner abspielen"));
currentTrack = myCard.special;
mp3.playFolderTrack(myCard.folder, currentTrack);
}
// Hörbuch Modus: kompletten Ordner spielen und Fortschritt merken
if (myCard.mode == 5) {
Serial.println(F("Hörbuch Modus -> kompletten Ordner spielen und "
"Fortschritt merken"));
currentTrack = EEPROM.read(myCard.folder);
mp3.playFolderTrack(myCard.folder, currentTrack);
}
// Einschlaf Modus: Eine Datei abspielen
if (myCard.mode == 6) {
Serial.println(F("Einschlafmodus ist aktiv -> ein Track wird gespielt"));
currentTrack = 1;
mp3.playFolderTrack(myCard.folder, currentTrack);
}
}
// Neue Karte konfigurieren
else {
knownCard = false;
setupCard();
}
}
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
int voiceMenu(int numberOfOptions, int startMessage, int messageOffset,
bool preview = false, int previewFromFolder = 0) {
int returnValue = 0;
if (startMessage != 0)
mp3.playMp3FolderTrack(startMessage);
do {
pauseButton.read();
upButton.read();
downButton.read();
mp3.loop();
if (pauseButton.wasPressed()) {
if (returnValue != 0)
return returnValue;
delay(1000);
}
if (upButton.pressedFor(LONG_PRESS)) {
returnValue = min(returnValue + 10, numberOfOptions);
mp3.playMp3FolderTrack(messageOffset + returnValue);
delay(1000);
if (preview) {
do {
delay(10);
} while (isPlaying());
if (previewFromFolder == 0)
mp3.playFolderTrack(returnValue, 1);
else
mp3.playFolderTrack(previewFromFolder, returnValue);
}
ignoreUpButton = true;
} else if (upButton.wasReleased()) {
if (!ignoreUpButton) {
returnValue = min(returnValue + 1, numberOfOptions);
mp3.playMp3FolderTrack(messageOffset + returnValue);
delay(1000);
if (preview) {
do {
delay(10);
} while (isPlaying());
if (previewFromFolder == 0)
mp3.playFolderTrack(returnValue, 1);
else
mp3.playFolderTrack(previewFromFolder, returnValue);
}
} else
ignoreUpButton = false;
}
if (downButton.pressedFor(LONG_PRESS)) {
returnValue = max(returnValue - 10, 1);
mp3.playMp3FolderTrack(messageOffset + returnValue);
delay(1000);
if (preview) {
do {
delay(10);
} while (isPlaying());
if (previewFromFolder == 0)
mp3.playFolderTrack(returnValue, 1);
else
mp3.playFolderTrack(previewFromFolder, returnValue);
}
ignoreDownButton = true;
} else if (downButton.wasReleased()) {
if (!ignoreDownButton) {
returnValue = max(returnValue - 1, 1);
mp3.playMp3FolderTrack(messageOffset + returnValue);
delay(1000);
if (preview) {
do {
delay(10);
} while (isPlaying());
if (previewFromFolder == 0)
mp3.playFolderTrack(returnValue, 1);
else
mp3.playFolderTrack(previewFromFolder, returnValue);
}
} else
ignoreDownButton = false;
}
} while (true);
}
void resetCard() {
do {
pauseButton.read();
upButton.read();
downButton.read();
if (upButton.wasReleased() || downButton.wasReleased()) {
Serial.print(F("Abgebrochen!"));
mp3.playMp3FolderTrack(802);
return;
}
} while (!mfrc522.PICC_IsNewCardPresent());
if (!mfrc522.PICC_ReadCardSerial())
return;
Serial.print(F("Karte wird neu Konfiguriert!"));
setupCard();
}
void setupCard() {
mp3.pause();
Serial.print(F("Neue Karte konfigurieren"));
// Ordner abfragen
myCard.folder = voiceMenu(99, 300, 0, true);
// Wiedergabemodus abfragen
myCard.mode = voiceMenu(6, 310, 310);
// Hörbuchmodus -> Fortschritt im EEPROM auf 1 setzen
EEPROM.write(myCard.folder,1);
// Einzelmodus -> Datei abfragen
if (myCard.mode == 4)
myCard.special = voiceMenu(mp3.getFolderTrackCount(myCard.folder), 320, 0,
true, myCard.folder);
// Admin Funktionen
if (myCard.mode == 7)
myCard.special = voiceMenu(3, 320, 320);
// Karte ist konfiguriert -> speichern
mp3.pause();
writeCard(myCard);
}
bool readCard(nfcTagObject *nfcTag) {
bool returnValue = true;
// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
byte buffer[18];
byte size = sizeof(buffer);
// Authenticate using key A
Serial.println(F("Authenticating using key A..."));
status = (MFRC522::StatusCode)mfrc522.PCD_Authenticate(
MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
returnValue = false;
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
// Show the whole sector as it currently is
Serial.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
Serial.println();
// Read data from the block
Serial.print(F("Reading data from block "));
Serial.print(blockAddr);
Serial.println(F(" ..."));
status = (MFRC522::StatusCode)mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
returnValue = false;
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.print(F("Data in block "));
Serial.print(blockAddr);
Serial.println(F(":"));
dump_byte_array(buffer, 16);
Serial.println();
Serial.println();
uint32_t tempCookie;
tempCookie = (uint32_t)buffer[0] << 24;
tempCookie += (uint32_t)buffer[1] << 16;
tempCookie += (uint32_t)buffer[2] << 8;
tempCookie += (uint32_t)buffer[3];
nfcTag->cookie = tempCookie;
nfcTag->version = buffer[4];
nfcTag->folder = buffer[5];
nfcTag->mode = buffer[6];
nfcTag->special = buffer[7];
return returnValue;
}
void writeCard(nfcTagObject nfcTag) {
MFRC522::PICC_Type mifareType;
byte buffer[16] = {0x13, 0x37, 0xb3, 0x47, // 0x1337 0xb347 magic cookie to
// identify our nfc tags
0x01, // version 1
nfcTag.folder, // the folder picked by the user
nfcTag.mode, // the playback mode picked by the user
nfcTag.special, // track or function for admin cards
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte size = sizeof(buffer);
mifareType = mfrc522.PICC_GetType(mfrc522.uid.sak);
// Authenticate using key B
Serial.println(F("Authenticating again using key B..."));
status = (MFRC522::StatusCode)mfrc522.PCD_Authenticate(
MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
mp3.playMp3FolderTrack(401);
return;
}
// Write data to the block
Serial.print(F("Writing data into block "));
Serial.print(blockAddr);
Serial.println(F(" ..."));
dump_byte_array(buffer, 16);
Serial.println();
status = (MFRC522::StatusCode)mfrc522.MIFARE_Write(blockAddr, buffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
mp3.playMp3FolderTrack(401);
}
else
mp3.playMp3FolderTrack(400);
Serial.println();
delay(100);
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
void readAccel() {
readFrom( DATAX0, 6, _buff); //read the acceleration data from the ADXL345
// each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
// thus we are converting both bytes in to one int
x_axis = (((int)_buff[1]) << 8) | _buff[0];
y_axis = (((int)_buff[3]) << 8) | _buff[2];
z_axis = (((int)_buff[5]) << 8) | _buff[4];
if (show_Accel == true){
Serial.print("x: ");
Serial.print( x_axis );
Serial.print(" y: ");
Serial.print( y_axis );
Serial.print(" z: ");
Serial.println( z_axis );
}
if (((y_axis) < (y_axis_init + 10)) and ((y_axis) > (y_axis_init - 10)))
nextTrackReleased = true;
}
// Reads num bytes starting from address register on device in to _buff array
void readFrom(byte address, int num, byte _buff[]) {
Wire.beginTransmission(DEVICE); // start transmission to device
Wire.write(address); // sends address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(DEVICE); // start transmission to device
Wire.requestFrom(DEVICE, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) {// device may send less than requested (abnormal)
_buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); // end transmission
}
void writeTo(byte address, byte val) {
Wire.beginTransmission(DEVICE); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}