-
Notifications
You must be signed in to change notification settings - Fork 0
/
kk.ino
329 lines (280 loc) · 7.54 KB
/
kk.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
/**
* Karlis Kiste
*
* Arduino based, RFID-enabled jukebox.
*
* TO:
* - Sometimes a track is repeated (after button press?)
* - Prevent fwd/back on pause
* - Stop and sleep after pausing for a time (2 min?)
*
*
* BSD 3-Clause License
* Copyright (c) 2019, Sven Kissner
*/
#include "Arduino.h"
#include "SoftwareSerial.h"
#include <DFMiniMp3.h>
#include <EasyButton.h>
#include <MFRC522.h>
#include <SPI.h>
/**
* Pinout custom board (top - bottom)
* 1 - A0 - Button Play - red
* 2 - A1 - Button Fwd - or
* 3 - A2 - Button Back - br
* 4 - D4 - LED Back - ye
* 5 - D3 - LED Fwd - bl
* 6 - D2 - LED Play - gn
* 7 - GND
* 8 - GND
**/
/**
* DFPlayer, communication via SoftwareSerial
*/
#define PIN_DFP_RX 7
#define PIN_DFP_TX 8
#define PIN_DFP_BUSY 6
/**
* RFID-RC522
*/
#define PIN_SDA 10
#define PIN_RST 9
/**
* Buttons incl. LED
*/
#define BUTTON_PLAY A0
#define BUTTON_FORWARD A1
#define BUTTON_BACK A2
#define LED_PLAY 2
#define LED_FORWARD 3
#define LED_BACK 4
/*
* Global variables to keep track of Tags
*/
int oldTag = -1, newTag;
boolean rfidState;
boolean cardPresent;
boolean oldState;
boolean isSetVolume;
boolean playing;
uint16_t currentTrack;
uint16_t nTracks;
static void continuePlayback(uint16_t track);
/*
* Notifications for DFMiniMp3
*/
class Mp3Notify {
public:
static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action) {
if (source & DfMp3_PlaySources_Sd) {
Serial.print("SD Card, ");
}
if (source & DfMp3_PlaySources_Usb) {
Serial.print("USB Disk, ");
}
if (source & DfMp3_PlaySources_Flash) {
Serial.print("Flash, ");
}
Serial.println(action);
}
static void OnError(uint16_t errorCode) {
// see DfMp3_Error for code meaning
Serial.println();
Serial.print("Com Error ");
Serial.println(errorCode);
}
static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track) {
Serial.print("Play finished for #");
Serial.println(track);
continuePlayback(track);
}
static void OnPlaySourceOnline(DfMp3_PlaySources source) {
PrintlnSourceAction(source, "online");
}
static void OnPlaySourceInserted(DfMp3_PlaySources source) {
PrintlnSourceAction(source, "inserted");
}
static void OnPlaySourceRemoved(DfMp3_PlaySources source) {
PrintlnSourceAction(source, "removed");
}
};
MFRC522 rfidReader(PIN_SDA, PIN_RST);
SoftwareSerial softSerial(PIN_DFP_RX, PIN_DFP_TX);
DFMiniMp3<SoftwareSerial, Mp3Notify> player(softSerial);
/*
* All valid UIDs, one for each folder.
*/
byte validUids[][4] = {
//{0xB6, 0xC8, 0x10, 0x1A}, // Tag 0 (Card)
{0x04, 0x4E, 0x9D, 0x1A}, //
{0x04, 0x2F, 0x9D, 0x1A}, //
{0x04, 0x29, 0x9D, 0x1A}, //
{0x04, 0x17, 0x9D, 0x1A}, //
{0x04, 0x23, 0x9D, 0x1A}, //
};
/*
* Total number of tags/valid UIDs.
*/
int nFolders = sizeof(validUids)/sizeof(byte);
/*
* Buttons and their callbacks
*/
EasyButton buttonPlay(BUTTON_PLAY);
EasyButton buttonForward(BUTTON_FORWARD);
EasyButton buttonBack(BUTTON_BACK);
uint16_t longpress = 2500;
void onButtonPressPlay() {
// ignore if volume is beeing set
if (isSetVolume) {
isSetVolume = false;
return;
}
if (digitalRead(PIN_DFP_BUSY)) {
player.start();
playing = true;
} else {
player.pause();
playing = false;
}
}
void onButtonPressBack() {
if (buttonPlay.isPressed()) {
isSetVolume = true;
player.decreaseVolume();
Serial.print("Vol-- | Vol: ");
Serial.println(player.getVolume());
} else {
if (currentTrack == 1) {
return;
}
player.playFolderTrack(oldTag, --currentTrack);
}
}
void onButtonPressForward() {
if (buttonPlay.isPressed()) {
isSetVolume = true;
if (player.getVolume() > 16) {
Serial.print("Max. Vol: ");
Serial.println(player.getVolume());
return;
}
player.increaseVolume();
Serial.print("Vol++ | Vol: ");
Serial.println(player.getVolume());
} else {
if (currentTrack == nTracks) {
return;
}
continuePlayback(0);
}
}
void onButtonPressedForPlay() {}
void onButtonPressedForForward() {}
void onButtonPressedForBack() {}
/**
* We need to manually start the next track on onPlayFinished().
* The callback fires twice for some reason, catch duplicates.
*/
uint16_t lastTrack;
static void continuePlayback(uint16_t track) {
if ((track == lastTrack) && track != 0) {
return;
}
lastTrack = track;
player.playFolderTrack(oldTag, ++currentTrack);
}
void setup() {
buttonPlay.begin();
buttonForward.begin();
buttonBack.begin();
buttonPlay.onPressed(onButtonPressPlay);
buttonForward.onPressed(onButtonPressForward);
buttonBack.onPressed(onButtonPressBack);
// buttonPlay.onPressedFor(longpress, onButtonPressedForPlay);
// buttonForward.onPressedFor(longpress, onButtonPressedForForward);
// buttonBack.onPressedFor(longpress, onButtonPressedForBack);
pinMode(LED_PLAY, OUTPUT);
pinMode(LED_FORWARD, OUTPUT);
pinMode(LED_BACK, OUTPUT);
pinMode(PIN_DFP_BUSY, INPUT);
softSerial.begin(9600);
SPI.begin();
rfidReader.PCD_Init();
Serial.begin(9600);
Serial.println("Initializing...");
player.begin();
player.setVolume(8); //Set volume value. From 0 to 30
}
void loop() {
// light when pressing buttons
digitalWrite(LED_PLAY, !digitalRead(BUTTON_PLAY));
digitalWrite(LED_FORWARD, !digitalRead(BUTTON_FORWARD));
digitalWrite(LED_BACK, !digitalRead(BUTTON_BACK));
player.loop();
buttonPlay.read();
buttonForward.read();
buttonBack.read();
cardPresent = rfidReader.PICC_IsNewCardPresent();
rfidState = rfidReader.PICC_ReadCardSerial();
/*
* PICC_IsNewCardPresent() and PICC_ReadCardSerial() return false every
* 2nd iteration even if a tag is present. oldState keeps track of two
* consecutive "false" which should indicate that a tag has indeed been
* removed. Great stuff.
*/
if (!cardPresent && !rfidState && oldState) {
oldState = false;
} else if (!cardPresent && !rfidState && !oldState) {
oldState = true;
player.pause();
} else if (cardPresent && rfidState) {
oldState = true;
newTag = checkUID(rfidReader.uid.uidByte);
// new card detected
if ((newTag > -1) && (newTag != oldTag)) {
blinkTagFound();
Serial.print("Detected new valid tag #");
Serial.println(newTag);
player.pause();
currentTrack = 1;
nTracks = player.getFolderTrackCount(newTag);
Serial.println(nTracks);
player.playFolderTrack(newTag, currentTrack);
oldTag = newTag;
playing = true;
// same card as before
} else if (newTag == oldTag){
if (playing) {
player.start();
}
} else {
Serial.println("Unknown or invalid Tag.");
}
}
player.loop();
}
void blinkTagFound() {
digitalWrite(LED_BACK, HIGH);
delay(150);
digitalWrite(LED_PLAY, HIGH);
delay(150);
digitalWrite(LED_FORWARD, HIGH);
delay(150);
digitalWrite(LED_BACK, LOW);
delay(150);
digitalWrite(LED_PLAY, LOW);
delay(150);
digitalWrite(LED_FORWARD, LOW);
}
int checkUID(byte uid[8]) {
for (int i = 0; i < nFolders; i++) {
if ((uid[0] == validUids[i][0]) &&
(uid[1] == validUids[i][1]) &&
(uid[2] == validUids[i][2]) &&
(uid[3] == validUids[i][3])) {
return i+1;
}
}
return -1;
}