-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFPlayerCore.h
More file actions
270 lines (200 loc) · 4.39 KB
/
DFPlayerCore.h
File metadata and controls
270 lines (200 loc) · 4.39 KB
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
/*
* DFPlayerCore.h
* Core class of DFPlayer.
*
* @author Nikolai Tikhonov aka Dragon_Knight <dubki4132@mail.ru>, https://vk.com/globalzone_edev
* @licenses MIT https://opensource.org/licenses/MIT
* @repo https://github.com/Dragon-Knight/DFPlayer
*/
#ifndef DFPlayerCore_h
#define DFPlayerCore_h
#include <Arduino.h>
#include <SoftwareSerial.h>
enum DFPlayerSourse
{
DFPLAYER_SOURSE_NONE,
DFPLAYER_SOURSE_ROOT,
DFPLAYER_SOURSE_NUM,
DFPLAYER_SOURSE_MP3,
DFPLAYER_SOURSE_ADVERT
};
enum DFPlayerEqualizer
{
DFPLAYER_EQUALIZER_NORMAL = 0,
DFPLAYER_EQUALIZER_POP = 1,
DFPLAYER_EQUALIZER_ROCK = 2,
DFPLAYER_EQUALIZER_JAZZ = 3,
DFPLAYER_EQUALIZER_CLASSIC = 4,
DFPLAYER_EQUALIZER_BASS = 5
};
class DFPlayerCore
{
public:
DFPlayerCore(SoftwareSerial &serial, uint8_t busyPin) : _serial(&serial), _busyPin(busyPin)
{
pinMode(this->_busyPin, INPUT);
this->_cooldownTime = 0;
return;
}
DFPlayerCore(SoftwareSerial &&serial, uint8_t busyPin) = delete;
void Begin()
{
this->_serial->begin(9600);
this->_serial->stopListening();
this->Reset();
return;
}
//////////////////// COMMANDS ////////////////////
void PlayNext() const
{
this->SendData(0x01, 0x00, 0x00, 32);
return;
}
void PlayPrevious() const
{
this->SendData(0x02, 0x00, 0x00, 32);
return;
}
void PlayROOT(uint16_t track) const
{
this->SendData(0x03, highByte(track), lowByte(track), 32);
return;
}
void VolumeUp() const
{
this->SendData(0x04, 0x00, 0x00, 32);
return;
}
void VolumeDown() const
{
this->SendData(0x05, 0x00, 0x00, 32);
return;
}
void SetVolume(uint8_t volume) const
{
this->SendData(0x06, 0x00, volume, 32);
return;
}
void SetEQ(DFPlayerEqualizer eq) const
{
this->SendData(0x07, 0x00, (uint8_t)eq, 32);
return;
}
void PlaybackMode(uint8_t mode) const // По докам тупо повтор трека 1-3000, только откуда?
{
this->SendData(0x08, 0x00, mode, 32);
return;
}
// 0x09
void Sleep() const
{
this->SendData(0x0A, 0x00, 0x00, 128);
return;
}
// 0x0B
void Reset() const
{
this->SendData(0x0C, 0x00, 0x00, 1024);
return;
}
void Play() const
{
this->SendData(0x0D, 0x00, 0x00, 32);
return;
}
void Pause() const
{
this->SendData(0x0E, 0x00, 0x00, 32);
return;
}
void PlayNUM(uint8_t folder, uint8_t track) const
{
this->SendData(0x0F, folder, track, 32);
return;
}
// 0x10
// 0x11
void PlayMP3(uint16_t track) const
{
this->SendData(0x12, highByte(track), lowByte(track), 32);
return;
}
void PlayADVERT(uint16_t track) const
{
this->SendData(0x13, highByte(track), lowByte(track), 32);
return;
}
// 0x14
void StopAdvert() const
{
this->SendData(0x15, 0x00, 0x00, 32);
return;
}
void Stop() const
{
this->SendData(0x16, 0x00, 0x00, 32);
return;
}
// 0x17
// 0x18
// 0x19
// 0x1A
//////////////////////////////////////////////////
//////////////////// UTILITY ////////////////////
void PlayBySourse(uint16_t param1, uint8_t param2, DFPlayerSourse source)
{
switch(source)
{
case DFPLAYER_SOURSE_ROOT:
{
this->PlayROOT(param1);
break;
}
case DFPLAYER_SOURSE_NUM:
{
this->PlayNUM(param1, param2);
break;
}
case DFPLAYER_SOURSE_MP3:
{
this->PlayMP3(param1);
break;
}
case DFPLAYER_SOURSE_ADVERT:
{
this->PlayADVERT(param1);
break;
}
default:
{
break;
}
}
return;
}
bool IsBusy() const
{
return !digitalRead(this->_busyPin);
}
bool IsReadyReceive() const
{
return (millis() > this->_cooldownTime);
}
//////////////////////////////////////////////////
private:
void SendData(byte command, byte msb, byte lsb, uint16_t timeout)
{
byte data[10] = {0x7E, 0xFF, 0x06, command, 0x00, msb, lsb, 0x00, 0x00, 0xEF};
uint16_t crc = 0 - (0xFF + 0x06 + command + 0x00 + msb + lsb);
data[7] = highByte(crc);
data[8] = lowByte(crc);
while(this->IsReadyReceive() == false){ }
this->_serial->write(data, 10);
this->_cooldownTime = millis() + timeout;
return;
}
SoftwareSerial *_serial;
uint8_t _busyPin;
uint32_t _cooldownTime;
};
#endif