diff --git a/DFRobotDFPlayerMini.cpp b/DFRobotDFPlayerMini.cpp index 0eac2d5..672087f 100644 --- a/DFRobotDFPlayerMini.cpp +++ b/DFRobotDFPlayerMini.cpp @@ -34,7 +34,7 @@ void DFRobotDFPlayerMini::sendStack(){ if (_sending[Stack_ACK]) { //if the ack mode is on wait until the last transmition while (_isSending) { delay(0); - waitAvailable(); + waitSending(); } } @@ -50,7 +50,7 @@ void DFRobotDFPlayerMini::sendStack(){ _serial->write(_sending, DFPLAYER_SEND_LENGTH); _timeOutTimer = millis(); _isSending = _sending[Stack_ACK]; - + if (!_sending[Stack_ACK]) { //if the ack mode is off wait 10 ms after one transmition. delay(10); } @@ -95,16 +95,30 @@ bool DFRobotDFPlayerMini::waitAvailable(unsigned long duration){ return true; } +bool DFRobotDFPlayerMini::waitSending(unsigned long duration){ + unsigned long timer = millis(); + if (!duration) { + duration = _timeOutDuration; + } + while (!available() && _isSending){ + if (millis() - timer > duration) { + return handleError(TimeOut); + } + delay(0); + } + return true; +} + bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK, bool doReset){ _serial = &stream; - + if (isACK) { enableACK(); } else{ disableACK(); } - + if (doReset) { reset(); waitAvailable(2000); @@ -153,7 +167,7 @@ void DFRobotDFPlayerMini::parseStack(){ _isSending = false; return; } - + _handleCommand = handleCommand; _handleParameter = arrayToUint16(_received + Stack_Parameter); @@ -280,8 +294,8 @@ bool DFRobotDFPlayerMini::available(){ _receivedIndex++; } } - - + + return _isAvailable; } @@ -452,7 +466,7 @@ int DFRobotDFPlayerMini::readFileCounts(uint8_t device){ default: break; } - + if (waitAvailable()) { if (readType() == DFPlayerFeedBack) { return read(); diff --git a/DFRobotDFPlayerMini.h b/DFRobotDFPlayerMini.h index 32c4787..7593fc9 100644 --- a/DFRobotDFPlayerMini.h +++ b/DFRobotDFPlayerMini.h @@ -67,13 +67,13 @@ class DFRobotDFPlayerMini { Stream* _serial; - + unsigned long _timeOutTimer; unsigned long _timeOutDuration = 500; - + uint8_t _received[DFPLAYER_RECEIVED_LENGTH]; uint8_t _sending[DFPLAYER_SEND_LENGTH] = {0x7E, 0xFF, 06, 00, 01, 00, 00, 00, 00, 0xEF}; - + uint8_t _receivedIndex=0; void sendStack(); @@ -81,121 +81,123 @@ class DFRobotDFPlayerMini { void sendStack(uint8_t command, uint16_t argument); void sendStack(uint8_t command, uint8_t argumentHigh, uint8_t argumentLow); + bool waitSending(unsigned long duration = 0); + void enableACK(); void disableACK(); - + void uint16ToArray(uint16_t value,uint8_t *array); - + uint16_t arrayToUint16(uint8_t *array); - + uint16_t calculateCheckSum(uint8_t *buffer); - + void parseStack(); bool validateStack(); - + uint8_t device = DFPLAYER_DEVICE_SD; - + public: - + uint8_t _handleType; uint8_t _handleCommand; uint16_t _handleParameter; bool _isAvailable = false; bool _isSending = false; - + bool handleMessage(uint8_t type, uint16_t parameter = 0); bool handleError(uint8_t type, uint16_t parameter = 0); uint8_t readCommand(); - + bool begin(Stream& stream, bool isACK = true, bool doReset = true); - + bool waitAvailable(unsigned long duration = 0); - + bool available(); - + uint8_t readType(); - + uint16_t read(); - + void setTimeOut(unsigned long timeOutDuration); - + void next(); - + void previous(); - + void play(int fileNumber=1); - + void volumeUp(); - + void volumeDown(); - + void volume(uint8_t volume); - + void EQ(uint8_t eq); - + void loop(int fileNumber); - + void outputDevice(uint8_t device); - + void sleep(); - + void reset(); - + void start(); - + void pause(); - + void playFolder(uint8_t folderNumber, uint8_t fileNumber); - + void outputSetting(bool enable, uint8_t gain); - + void enableLoopAll(); - + void disableLoopAll(); - + void playMp3Folder(int fileNumber); - + void advertise(int fileNumber); - + void playLargeFolder(uint8_t folderNumber, uint16_t fileNumber); - + void stopAdvertise(); - + void stop(); - + void loopFolder(int folderNumber); - + void randomAll(); - + void enableLoop(); - + void disableLoop(); - + void enableDAC(); - + void disableDAC(); - + int readState(); - + int readVolume(); - + int readEQ(); - + int readFileCounts(uint8_t device); - + int readCurrentFileNumber(uint8_t device); - + int readFileCountsInFolder(int folderNumber); - + int readFileCounts(); int readFolderCounts(); - + int readCurrentFileNumber(); - + }; #endif