mirror of
https://github.com/DFRobot/DFRobotDFPlayerMini.git
synced 2026-03-16 21:46:49 +01:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
772d326841 | ||
|
|
fabbb011df | ||
|
|
9826577d39 |
@@ -24,14 +24,14 @@ void DFRobotDFPlayerMini::uint16ToArray(uint16_t value, uint8_t *array){
|
|||||||
|
|
||||||
uint16_t DFRobotDFPlayerMini::calculateCheckSum(uint8_t *buffer){
|
uint16_t DFRobotDFPlayerMini::calculateCheckSum(uint8_t *buffer){
|
||||||
uint16_t sum = 0;
|
uint16_t sum = 0;
|
||||||
for (int i=Stack::Version; i<Stack::CheckSum; i++) {
|
for (int i=Stack_Version; i<Stack_CheckSum; i++) {
|
||||||
sum += buffer[i];
|
sum += buffer[i];
|
||||||
}
|
}
|
||||||
return -sum;
|
return -sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DFRobotDFPlayerMini::sendStack(){
|
void DFRobotDFPlayerMini::sendStack(){
|
||||||
if (_sending[Stack::ACK]) {
|
if (_sending[Stack_ACK]) {
|
||||||
while (_isSending) {
|
while (_isSending) {
|
||||||
available();
|
available();
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ void DFRobotDFPlayerMini::sendStack(){
|
|||||||
#endif
|
#endif
|
||||||
_serial->write(_sending, DFPLAYER_SEND_LENGTH);
|
_serial->write(_sending, DFPLAYER_SEND_LENGTH);
|
||||||
_timeOutTimer = millis();
|
_timeOutTimer = millis();
|
||||||
_isSending = _sending[Stack::ACK];
|
_isSending = _sending[Stack_ACK];
|
||||||
}
|
}
|
||||||
|
|
||||||
void DFRobotDFPlayerMini::sendStack(uint8_t command){
|
void DFRobotDFPlayerMini::sendStack(uint8_t command){
|
||||||
@@ -59,9 +59,9 @@ void DFRobotDFPlayerMini::sendStack(uint8_t command){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DFRobotDFPlayerMini::sendStack(uint8_t command, uint16_t argument){
|
void DFRobotDFPlayerMini::sendStack(uint8_t command, uint16_t argument){
|
||||||
_sending[Stack::Command] = command;
|
_sending[Stack_Command] = command;
|
||||||
uint16ToArray(argument, _sending+Stack::Parameter);
|
uint16ToArray(argument, _sending+Stack_Parameter);
|
||||||
uint16ToArray(calculateCheckSum(_sending), _sending+Stack::CheckSum);
|
uint16ToArray(calculateCheckSum(_sending), _sending+Stack_CheckSum);
|
||||||
sendStack();
|
sendStack();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,17 +72,17 @@ void DFRobotDFPlayerMini::sendStack(uint8_t command, uint8_t argumentHigh, uint8
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DFRobotDFPlayerMini::enableACK(){
|
void DFRobotDFPlayerMini::enableACK(){
|
||||||
_sending[Stack::ACK] = 0x01;
|
_sending[Stack_ACK] = 0x01;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DFRobotDFPlayerMini::disableACK(){
|
void DFRobotDFPlayerMini::disableACK(){
|
||||||
_sending[Stack::ACK] = 0x00;
|
_sending[Stack_ACK] = 0x00;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DFRobotDFPlayerMini::waitAvailable(){
|
bool DFRobotDFPlayerMini::waitAvailable(){
|
||||||
_isSending = true;
|
_isSending = true;
|
||||||
while (!available());
|
while (!available());
|
||||||
return _handleType != HandleType::TimeOut;
|
return _handleType != TimeOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK){
|
bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK){
|
||||||
@@ -102,7 +102,7 @@ bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK){
|
|||||||
return (readType() == DFPlayerCardOnline) || !isACK;
|
return (readType() == DFPlayerCardOnline) || !isACK;
|
||||||
}
|
}
|
||||||
|
|
||||||
HandleType DFRobotDFPlayerMini::readType(){
|
uint8_t DFRobotDFPlayerMini::readType(){
|
||||||
_isAvailable = false;
|
_isAvailable = false;
|
||||||
return _handleType;
|
return _handleType;
|
||||||
}
|
}
|
||||||
@@ -112,7 +112,7 @@ uint16_t DFRobotDFPlayerMini::read(){
|
|||||||
return _handleParameter;
|
return _handleParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DFRobotDFPlayerMini::handleMessage(HandleType type, uint16_t parameter){
|
bool DFRobotDFPlayerMini::handleMessage(uint8_t type, uint16_t parameter){
|
||||||
_receivedIndex = 0;
|
_receivedIndex = 0;
|
||||||
_handleType = type;
|
_handleType = type;
|
||||||
_handleParameter = parameter;
|
_handleParameter = parameter;
|
||||||
@@ -120,7 +120,7 @@ bool DFRobotDFPlayerMini::handleMessage(HandleType type, uint16_t parameter){
|
|||||||
return _isAvailable;
|
return _isAvailable;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DFRobotDFPlayerMini::handleError(HandleType type, uint16_t parameter){
|
bool DFRobotDFPlayerMini::handleError(uint8_t type, uint16_t parameter){
|
||||||
handleMessage(type, parameter);
|
handleMessage(type, parameter);
|
||||||
_isSending = false;
|
_isSending = false;
|
||||||
}
|
}
|
||||||
@@ -131,30 +131,30 @@ uint8_t DFRobotDFPlayerMini::readCommand(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DFRobotDFPlayerMini::parseStack(){
|
void DFRobotDFPlayerMini::parseStack(){
|
||||||
_handleCommand = *(_received + Stack::Command);
|
_handleCommand = *(_received + Stack_Command);
|
||||||
_handleParameter = arrayToUint16(_received + Stack::Parameter);
|
_handleParameter = arrayToUint16(_received + Stack_Parameter);
|
||||||
|
|
||||||
switch (_handleCommand) {
|
switch (_handleCommand) {
|
||||||
case 0x3D:
|
case 0x3D:
|
||||||
handleMessage(HandleType::DFPlayerPlayFinished, _handleParameter);
|
handleMessage(DFPlayerPlayFinished, _handleParameter);
|
||||||
break;
|
break;
|
||||||
case 0x3F:
|
case 0x3F:
|
||||||
if (_handleParameter & 0x02) {
|
if (_handleParameter & 0x02) {
|
||||||
handleMessage(HandleType::DFPlayerCardOnline, _handleParameter);
|
handleMessage(DFPlayerCardOnline, _handleParameter);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 0x3A:
|
case 0x3A:
|
||||||
if (_handleParameter & 0x02) {
|
if (_handleParameter & 0x02) {
|
||||||
handleMessage(HandleType::DFPlayerCardInserted, _handleParameter);
|
handleMessage(DFPlayerCardInserted, _handleParameter);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 0x3B:
|
case 0x3B:
|
||||||
if (_handleParameter & 0x02) {
|
if (_handleParameter & 0x02) {
|
||||||
handleMessage(HandleType::DFPlayerCardRemoved, _handleParameter);
|
handleMessage(DFPlayerCardRemoved, _handleParameter);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 0x40:
|
case 0x40:
|
||||||
handleMessage(HandleType::DFPlayerError, _handleParameter);
|
handleMessage(DFPlayerError, _handleParameter);
|
||||||
break;
|
break;
|
||||||
case 0x41:
|
case 0x41:
|
||||||
_isSending = false;
|
_isSending = false;
|
||||||
@@ -190,19 +190,19 @@ uint16_t DFRobotDFPlayerMini::arrayToUint16(uint8_t *array){
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool DFRobotDFPlayerMini::validateStack(){
|
bool DFRobotDFPlayerMini::validateStack(){
|
||||||
return calculateCheckSum(_received) == arrayToUint16(_received+Stack::CheckSum);
|
return calculateCheckSum(_received) == arrayToUint16(_received+Stack_CheckSum);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DFRobotDFPlayerMini::available(){
|
bool DFRobotDFPlayerMini::available(){
|
||||||
while (_serial->available()) {
|
while (_serial->available()) {
|
||||||
if (_receivedIndex == 0) {
|
if (_receivedIndex == 0) {
|
||||||
_received[Stack::Header] = _serial->read();
|
_received[Stack_Header] = _serial->read();
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
Serial.print(F("received:"));
|
Serial.print(F("received:"));
|
||||||
Serial.print(_received[_receivedIndex],HEX);
|
Serial.print(_received[_receivedIndex],HEX);
|
||||||
Serial.print(F(" "));
|
Serial.print(F(" "));
|
||||||
#endif
|
#endif
|
||||||
if (_received[Stack::Header] == 0x7E) {
|
if (_received[Stack_Header] == 0x7E) {
|
||||||
_isAvailable = false;
|
_isAvailable = false;
|
||||||
_receivedIndex ++;
|
_receivedIndex ++;
|
||||||
}
|
}
|
||||||
@@ -214,17 +214,17 @@ bool DFRobotDFPlayerMini::available(){
|
|||||||
Serial.print(F(" "));
|
Serial.print(F(" "));
|
||||||
#endif
|
#endif
|
||||||
switch (_receivedIndex) {
|
switch (_receivedIndex) {
|
||||||
case Stack::Version:
|
case Stack_Version:
|
||||||
if (_received[_receivedIndex] != 0xFF) {
|
if (_received[_receivedIndex] != 0xFF) {
|
||||||
return handleError(WrongStack);
|
return handleError(WrongStack);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Stack::Length:
|
case Stack_Length:
|
||||||
if (_received[_receivedIndex] != 0x06) {
|
if (_received[_receivedIndex] != 0x06) {
|
||||||
return handleError(WrongStack);
|
return handleError(WrongStack);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Stack::End:
|
case Stack_End:
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
Serial.println();
|
Serial.println();
|
||||||
#endif
|
#endif
|
||||||
@@ -235,7 +235,7 @@ bool DFRobotDFPlayerMini::available(){
|
|||||||
if (validateStack()) {
|
if (validateStack()) {
|
||||||
_receivedIndex = 0;
|
_receivedIndex = 0;
|
||||||
parseStack();
|
parseStack();
|
||||||
if (_isAvailable && !_sending[Stack::ACK]) {
|
if (_isAvailable && !_sending[Stack_ACK]) {
|
||||||
_isSending = false;
|
_isSending = false;
|
||||||
}
|
}
|
||||||
return _isAvailable;
|
return _isAvailable;
|
||||||
|
|||||||
@@ -35,26 +35,30 @@
|
|||||||
|
|
||||||
//#define _DEBUG
|
//#define _DEBUG
|
||||||
|
|
||||||
enum HandleType{
|
#define TimeOut 0
|
||||||
TimeOut,
|
#define WrongStack 1
|
||||||
WrongStack,
|
#define DFPlayerCardInserted 2
|
||||||
DFPlayerCardInserted,
|
#define DFPlayerCardRemoved 3
|
||||||
DFPlayerCardRemoved,
|
#define DFPlayerCardOnline 4
|
||||||
DFPlayerCardOnline,
|
#define DFPlayerPlayFinished 5
|
||||||
DFPlayerPlayFinished,
|
#define DFPlayerError 6
|
||||||
DFPlayerError
|
|
||||||
};
|
|
||||||
|
|
||||||
enum DFPlayerErrorType{
|
#define Busy 1
|
||||||
Busy = 1,
|
#define Sleeping 2
|
||||||
Sleeping,
|
#define SerialWrongStack 3
|
||||||
SerialWrongStack,
|
#define CheckSumNotMatch 4
|
||||||
CheckSumNotMatch,
|
#define FileIndexOut 5
|
||||||
FileIndexOut,
|
#define FileMismatch 6
|
||||||
FileMismatch,
|
#define Advertise 7
|
||||||
Advertise
|
|
||||||
};
|
|
||||||
|
|
||||||
|
#define Stack_Header 0
|
||||||
|
#define Stack_Version 1
|
||||||
|
#define Stack_Length 2
|
||||||
|
#define Stack_Command 3
|
||||||
|
#define Stack_ACK 4
|
||||||
|
#define Stack_Parameter 5
|
||||||
|
#define Stack_CheckSum 7
|
||||||
|
#define Stack_End 9
|
||||||
|
|
||||||
class DFRobotDFPlayerMini {
|
class DFRobotDFPlayerMini {
|
||||||
Stream* _serial;
|
Stream* _serial;
|
||||||
@@ -88,28 +92,16 @@ class DFRobotDFPlayerMini {
|
|||||||
|
|
||||||
uint8_t device = DFPLAYER_DEVICE_SD;
|
uint8_t device = DFPLAYER_DEVICE_SD;
|
||||||
|
|
||||||
|
|
||||||
enum Stack{
|
|
||||||
Header = 0,
|
|
||||||
Version = 1,
|
|
||||||
Length = 2,
|
|
||||||
Command = 3,
|
|
||||||
ACK = 4,
|
|
||||||
Parameter = 5,
|
|
||||||
CheckSum = 7,
|
|
||||||
End = 9
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
HandleType _handleType;
|
uint8_t _handleType;
|
||||||
uint8_t _handleCommand;
|
uint8_t _handleCommand;
|
||||||
uint16_t _handleParameter;
|
uint16_t _handleParameter;
|
||||||
bool _isAvailable = false;
|
bool _isAvailable = false;
|
||||||
bool _isSending = false;
|
bool _isSending = false;
|
||||||
|
|
||||||
bool handleMessage(HandleType type, uint16_t parameter = 0);
|
bool handleMessage(uint8_t type, uint16_t parameter = 0);
|
||||||
bool handleError(HandleType type, uint16_t parameter = 0);
|
bool handleError(uint8_t type, uint16_t parameter = 0);
|
||||||
|
|
||||||
uint8_t readCommand();
|
uint8_t readCommand();
|
||||||
|
|
||||||
@@ -119,7 +111,7 @@ class DFRobotDFPlayerMini {
|
|||||||
|
|
||||||
bool available();
|
bool available();
|
||||||
|
|
||||||
HandleType readType();
|
uint8_t readType();
|
||||||
|
|
||||||
uint16_t read();
|
uint16_t read();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# DFPlayer - A Mini MP3 Player For Arduino
|
# DFPlayer - A Mini MP3 Player For Arduino
|
||||||
DFPlayer - A Mini MP3 Player For Arduino
|
|
||||||
|
|
||||||
|
|
||||||
|
DFPlayer - A Mini MP3 Player For Arduino
|
||||||
https://www.dfrobot.com/index.php?route=product/product&product_id=1121
|
https://www.dfrobot.com/index.php?route=product/product&product_id=1121
|
||||||
|
|
||||||
This example shows the all the function of library for DFPlayer.
|
This example shows the all the function of library for DFPlayer.
|
||||||
@@ -16,5 +17,4 @@ Notice and Trouble shooting
|
|||||||
|
|
||||||
1.Connection and Diagram can be found here
|
1.Connection and Diagram can be found here
|
||||||
https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram
|
https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram
|
||||||
|
2.This code is tested on Arduino Uno, Leonardo, Mega boards.
|
||||||
2.This code is tested on Arduino Uno, Leonardo, Mega boards.
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
name=DFRobotDFPlayerMini
|
name=DFRobotDFPlayerMini
|
||||||
version=1.0.0
|
version=1.0.1
|
||||||
author=DFRobot
|
author=DFRobot
|
||||||
maintainer=Angelo <angelo.qiao@dfrobot.com>
|
maintainer=Angelo <angelo.qiao@dfrobot.com>
|
||||||
sentence=Driver for DFPlayer Mini from DFRobot
|
sentence=Driver for DFPlayer Mini from DFRobot
|
||||||
paragraph=Easy-to-use and reliable library for DFPlayer Mini
|
paragraph=Easy-to-use and reliable library for DFPlayer Mini
|
||||||
category=Sensors
|
category=Sensors
|
||||||
url=https://github.com/DFRobot/DFRobotDFPlayerMini
|
url=https://github.com/DFRobot/DFRobotDFPlayerMini
|
||||||
architectures=*
|
architectures=*
|
||||||
|
|||||||
Reference in New Issue
Block a user