Skip to content

Prevent timeout when waiting for ACK #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions DFRobotDFPlayerMini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -153,7 +167,7 @@ void DFRobotDFPlayerMini::parseStack(){
_isSending = false;
return;
}

_handleCommand = handleCommand;
_handleParameter = arrayToUint16(_received + Stack_Parameter);

Expand Down Expand Up @@ -280,8 +294,8 @@ bool DFRobotDFPlayerMini::available(){
_receivedIndex++;
}
}


return _isAvailable;
}

Expand Down Expand Up @@ -452,7 +466,7 @@ int DFRobotDFPlayerMini::readFileCounts(uint8_t device){
default:
break;
}

if (waitAvailable()) {
if (readType() == DFPlayerFeedBack) {
return read();
Expand Down
110 changes: 56 additions & 54 deletions DFRobotDFPlayerMini.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,135 +67,137 @@

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();
void sendStack(uint8_t command);
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