-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3tf16p.h
164 lines (151 loc) · 3.66 KB
/
mp3tf16p.h
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
// Class Helper for the DFRobotDFPlayerMini library
//
#include "DFRobotDFPlayerMini.h"
#define MP3_ERROR_ONLY 1
#define MP3_ALL_MESSAGE 2
class MP3Player
{
private:
void statusOnSerial(uint8_t type, int value);
void waitPlayIsTerminated(void);
int p_RX;
int p_TX;
public:
DFRobotDFPlayerMini player;
MP3Player(int RX, int TX);
~MP3Player();
void playTrackNumber(int trackNumber, int volume, boolean waitPlayTerminated = true);
boolean playCompleted(void);
void initialize(void);
int serialPrintStatus(int errorOnly);
};
MP3Player::MP3Player(int RX, int TX)
{
p_TX = TX;
p_RX = RX;
}
MP3Player::~MP3Player()
{
}
void MP3Player::initialize(void)
{
Serial2.begin(9600, SERIAL_8N1, p_RX, p_TX);
Serial.println(F("Initializing MP3Player ..."));
if (!player.begin(Serial2, true, false))
{
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
return;
}
player.volume(10);
Serial.println(F("MP3Player online."));
}
void MP3Player::playTrackNumber(int trackNumber, int volume, boolean waitPlayTerminated)
{
player.volume(volume);
player.play(trackNumber);
if (waitPlayTerminated)
{
waitPlayIsTerminated();
}
}
void MP3Player::waitPlayIsTerminated(void)
{
while (!playCompleted())
{
}
}
boolean MP3Player::playCompleted(void)
{
if (player.available())
{
return player.readType() == DFPlayerPlayFinished;
}
return false;
}
// Print the detail message from DFPlayer to handle different errors and states.
int MP3Player::serialPrintStatus(int verbose)
{
if (player.available())
{
uint8_t type = player.readType();
int value = player.read();
if (verbose == MP3_ERROR_ONLY)
{
if (type == DFPlayerError)
{
statusOnSerial(type, value);
}
}
else
{
statusOnSerial(type, value);
}
if (type == DFPlayerError)
{
return value;
}
else
{
return 0;
}
}
}
void MP3Player::statusOnSerial(uint8_t type, int value)
{
switch (type)
{
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value)
{
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}