diff --git a/DFRobotDFPlayerMini.cpp b/DFRobotDFPlayerMini.cpp index 33a0167..1fea3dc 100644 --- a/DFRobotDFPlayerMini.cpp +++ b/DFRobotDFPlayerMini.cpp @@ -24,14 +24,14 @@ void DFRobotDFPlayerMini::uint16ToArray(uint16_t value, uint8_t *array){ uint16_t DFRobotDFPlayerMini::calculateCheckSum(uint8_t *buffer){ uint16_t sum = 0; - for (int i=Stack::Version; iwrite(_sending, DFPLAYER_SEND_LENGTH); _timeOutTimer = millis(); - _isSending = _sending[Stack::ACK]; + _isSending = _sending[Stack_ACK]; } 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){ - _sending[Stack::Command] = command; - uint16ToArray(argument, _sending+Stack::Parameter); - uint16ToArray(calculateCheckSum(_sending), _sending+Stack::CheckSum); + _sending[Stack_Command] = command; + uint16ToArray(argument, _sending+Stack_Parameter); + uint16ToArray(calculateCheckSum(_sending), _sending+Stack_CheckSum); sendStack(); } @@ -72,17 +72,17 @@ void DFRobotDFPlayerMini::sendStack(uint8_t command, uint8_t argumentHigh, uint8 } void DFRobotDFPlayerMini::enableACK(){ - _sending[Stack::ACK] = 0x01; + _sending[Stack_ACK] = 0x01; } void DFRobotDFPlayerMini::disableACK(){ - _sending[Stack::ACK] = 0x00; + _sending[Stack_ACK] = 0x00; } bool DFRobotDFPlayerMini::waitAvailable(){ _isSending = true; while (!available()); - return _handleType != HandleType::TimeOut; + return _handleType != TimeOut; } bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK){ @@ -102,7 +102,7 @@ bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK){ return (readType() == DFPlayerCardOnline) || !isACK; } -HandleType DFRobotDFPlayerMini::readType(){ +uint8_t DFRobotDFPlayerMini::readType(){ _isAvailable = false; return _handleType; } @@ -112,7 +112,7 @@ uint16_t DFRobotDFPlayerMini::read(){ return _handleParameter; } -bool DFRobotDFPlayerMini::handleMessage(HandleType type, uint16_t parameter){ +bool DFRobotDFPlayerMini::handleMessage(uint8_t type, uint16_t parameter){ _receivedIndex = 0; _handleType = type; _handleParameter = parameter; @@ -120,7 +120,7 @@ bool DFRobotDFPlayerMini::handleMessage(HandleType type, uint16_t parameter){ return _isAvailable; } -bool DFRobotDFPlayerMini::handleError(HandleType type, uint16_t parameter){ +bool DFRobotDFPlayerMini::handleError(uint8_t type, uint16_t parameter){ handleMessage(type, parameter); _isSending = false; } @@ -131,30 +131,30 @@ uint8_t DFRobotDFPlayerMini::readCommand(){ } void DFRobotDFPlayerMini::parseStack(){ - _handleCommand = *(_received + Stack::Command); - _handleParameter = arrayToUint16(_received + Stack::Parameter); + _handleCommand = *(_received + Stack_Command); + _handleParameter = arrayToUint16(_received + Stack_Parameter); switch (_handleCommand) { case 0x3D: - handleMessage(HandleType::DFPlayerPlayFinished, _handleParameter); + handleMessage(DFPlayerPlayFinished, _handleParameter); break; case 0x3F: if (_handleParameter & 0x02) { - handleMessage(HandleType::DFPlayerCardOnline, _handleParameter); + handleMessage(DFPlayerCardOnline, _handleParameter); } break; case 0x3A: if (_handleParameter & 0x02) { - handleMessage(HandleType::DFPlayerCardInserted, _handleParameter); + handleMessage(DFPlayerCardInserted, _handleParameter); } break; case 0x3B: if (_handleParameter & 0x02) { - handleMessage(HandleType::DFPlayerCardRemoved, _handleParameter); + handleMessage(DFPlayerCardRemoved, _handleParameter); } break; case 0x40: - handleMessage(HandleType::DFPlayerError, _handleParameter); + handleMessage(DFPlayerError, _handleParameter); break; case 0x41: _isSending = false; @@ -190,19 +190,19 @@ uint16_t DFRobotDFPlayerMini::arrayToUint16(uint8_t *array){ } bool DFRobotDFPlayerMini::validateStack(){ - return calculateCheckSum(_received) == arrayToUint16(_received+Stack::CheckSum); + return calculateCheckSum(_received) == arrayToUint16(_received+Stack_CheckSum); } bool DFRobotDFPlayerMini::available(){ while (_serial->available()) { if (_receivedIndex == 0) { - _received[Stack::Header] = _serial->read(); + _received[Stack_Header] = _serial->read(); #ifdef _DEBUG Serial.print(F("received:")); Serial.print(_received[_receivedIndex],HEX); Serial.print(F(" ")); #endif - if (_received[Stack::Header] == 0x7E) { + if (_received[Stack_Header] == 0x7E) { _isAvailable = false; _receivedIndex ++; } @@ -214,17 +214,17 @@ bool DFRobotDFPlayerMini::available(){ Serial.print(F(" ")); #endif switch (_receivedIndex) { - case Stack::Version: + case Stack_Version: if (_received[_receivedIndex] != 0xFF) { return handleError(WrongStack); } break; - case Stack::Length: + case Stack_Length: if (_received[_receivedIndex] != 0x06) { return handleError(WrongStack); } break; - case Stack::End: + case Stack_End: #ifdef _DEBUG Serial.println(); #endif @@ -235,7 +235,7 @@ bool DFRobotDFPlayerMini::available(){ if (validateStack()) { _receivedIndex = 0; parseStack(); - if (_isAvailable && !_sending[Stack::ACK]) { + if (_isAvailable && !_sending[Stack_ACK]) { _isSending = false; } return _isAvailable; diff --git a/DFRobotDFPlayerMini.h b/DFRobotDFPlayerMini.h index 0ebffeb..897db0e 100644 --- a/DFRobotDFPlayerMini.h +++ b/DFRobotDFPlayerMini.h @@ -35,26 +35,30 @@ //#define _DEBUG -enum HandleType{ - TimeOut, - WrongStack, - DFPlayerCardInserted, - DFPlayerCardRemoved, - DFPlayerCardOnline, - DFPlayerPlayFinished, - DFPlayerError -}; +#define TimeOut 0 +#define WrongStack 1 +#define DFPlayerCardInserted 2 +#define DFPlayerCardRemoved 3 +#define DFPlayerCardOnline 4 +#define DFPlayerPlayFinished 5 +#define DFPlayerError 6 -enum DFPlayerErrorType{ - Busy = 1, - Sleeping, - SerialWrongStack, - CheckSumNotMatch, - FileIndexOut, - FileMismatch, - Advertise -}; +#define Busy 1 +#define Sleeping 2 +#define SerialWrongStack 3 +#define CheckSumNotMatch 4 +#define FileIndexOut 5 +#define FileMismatch 6 +#define Advertise 7 +#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 { Stream* _serial; @@ -88,28 +92,16 @@ class DFRobotDFPlayerMini { 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: - HandleType _handleType; + uint8_t _handleType; uint8_t _handleCommand; uint16_t _handleParameter; bool _isAvailable = false; bool _isSending = false; - bool handleMessage(HandleType type, uint16_t parameter = 0); - bool handleError(HandleType type, uint16_t parameter = 0); + bool handleMessage(uint8_t type, uint16_t parameter = 0); + bool handleError(uint8_t type, uint16_t parameter = 0); uint8_t readCommand(); @@ -119,7 +111,7 @@ class DFRobotDFPlayerMini { bool available(); - HandleType readType(); + uint8_t readType(); uint16_t read(); diff --git a/README.md b/README.md index 2feb6f9..bfe250b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # 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 This example shows the all the function of library for DFPlayer. @@ -16,5 +15,4 @@ Notice and Trouble shooting 1.Connection and Diagram can be found here https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram - -2.This code is tested on Arduino Uno, Leonardo, Mega boards. \ No newline at end of file +2.This code is tested on Arduino Uno, Leonardo, Mega boards. diff --git a/library.properties b/library.properties index a93d16b..602e12c 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=DFRobotDFPlayerMini -version=1.0.0 +version=1.0.1 author=DFRobot maintainer=Angelo 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 url=https://github.com/DFRobot/DFRobotDFPlayerMini architectures=*