update to 1.0.3

* Fix the bug that read cannot get the right result.
* Add USB support
* Add more example.
This commit is contained in:
jimaobian
2018-08-15 13:44:47 +08:00
parent c295afa686
commit 8b4ba4f7d8
9 changed files with 626 additions and 38 deletions

View File

@@ -7,7 +7,7 @@
* @copyright GNU Lesser General Public License
*
* @author [Angelo](Angelo.qiao@dfrobot.com)
* @version V1.0
* @version V1.0.3
* @date 2016-12-07
*/
@@ -31,15 +31,12 @@ uint16_t DFRobotDFPlayerMini::calculateCheckSum(uint8_t *buffer){
}
void DFRobotDFPlayerMini::sendStack(){
if (_sending[Stack_ACK]) {
if (_sending[Stack_ACK]) { //if the ack mode is on wait until the last transmition
while (_isSending) {
delay(0);
available();
}
}
else{
delay(10);
}
#ifdef _DEBUG
Serial.println();
@@ -53,6 +50,10 @@ 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);
}
}
void DFRobotDFPlayerMini::sendStack(uint8_t command){
@@ -80,15 +81,23 @@ void DFRobotDFPlayerMini::disableACK(){
_sending[Stack_ACK] = 0x00;
}
bool DFRobotDFPlayerMini::waitAvailable(){
_isSending = true;
bool DFRobotDFPlayerMini::waitAvailable(unsigned long duration){
unsigned long timer = millis();
if (!duration) {
duration = _timeOutDuration;
}
while (!available()){
if (millis() - timer > duration) {
return false;
}
delay(0);
}
return _handleType != TimeOut;
return true;
}
bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK, bool doReset){
_serial = &stream;
if (isACK) {
enableACK();
}
@@ -96,20 +105,17 @@ bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK, bool doReset){
disableACK();
}
_serial = &stream;
if (doReset) {
_timeOutDuration += 3000;
reset();
waitAvailable();
_timeOutDuration -= 3000;
waitAvailable(2000);
delay(200);
} else {
}
else {
// assume same state as with reset(): online
_handleType = DFPlayerCardOnline;
}
return (readType() == DFPlayerCardOnline) || !isACK;
return (readType() == DFPlayerCardOnline) || (readType() == DFPlayerUSBOnline) || !isACK;
}
uint8_t DFRobotDFPlayerMini::readType(){
@@ -142,7 +148,13 @@ uint8_t DFRobotDFPlayerMini::readCommand(){
}
void DFRobotDFPlayerMini::parseStack(){
_handleCommand = *(_received + Stack_Command);
uint8_t handleCommand = *(_received + Stack_Command);
if (handleCommand == 0x41) { //handle the 0x41 ack feedback as a spcecial case, in case the pollusion of _handleCommand, _handleParameter, and _handleType.
_isSending = false;
return;
}
_handleCommand = handleCommand;
_handleParameter = arrayToUint16(_received + Stack_Parameter);
switch (_handleCommand) {
@@ -150,26 +162,35 @@ void DFRobotDFPlayerMini::parseStack(){
handleMessage(DFPlayerPlayFinished, _handleParameter);
break;
case 0x3F:
if (_handleParameter & 0x02) {
if (_handleParameter & 0x01) {
handleMessage(DFPlayerUSBOnline, _handleParameter);
}
else if (_handleParameter & 0x02) {
handleMessage(DFPlayerCardOnline, _handleParameter);
}
else if (_handleParameter & 0x03) {
handleMessage(DFPlayerCardUSBOnline, _handleParameter);
}
break;
case 0x3A:
if (_handleParameter & 0x02) {
if (_handleParameter & 0x01) {
handleMessage(DFPlayerUSBInserted, _handleParameter);
}
else if (_handleParameter & 0x02) {
handleMessage(DFPlayerCardInserted, _handleParameter);
}
break;
case 0x3B:
if (_handleParameter & 0x02) {
if (_handleParameter & 0x01) {
handleMessage(DFPlayerUSBRemoved, _handleParameter);
}
else if (_handleParameter & 0x02) {
handleMessage(DFPlayerCardRemoved, _handleParameter);
}
break;
case 0x40:
handleMessage(DFPlayerError, _handleParameter);
break;
case 0x41:
_isSending = false;
break;
case 0x3C:
case 0x3E:
case 0x42:
@@ -185,7 +206,7 @@ void DFRobotDFPlayerMini::parseStack(){
case 0x4D:
case 0x4E:
case 0x4F:
_isAvailable = true;
handleMessage(DFPlayerFeedBack, _handleParameter);
break;
default:
handleError(WrongStack);
@@ -215,7 +236,6 @@ bool DFRobotDFPlayerMini::available(){
Serial.print(F(" "));
#endif
if (_received[Stack_Header] == 0x7E) {
_isAvailable = false;
_receivedIndex ++;
}
}
@@ -247,9 +267,6 @@ bool DFRobotDFPlayerMini::available(){
if (validateStack()) {
_receivedIndex = 0;
parseStack();
if (_isAvailable && !_sending[Stack_ACK]) {
_isSending = false;
}
return _isAvailable;
}
else{
@@ -387,7 +404,12 @@ void DFRobotDFPlayerMini::disableDAC(){
int DFRobotDFPlayerMini::readState(){
sendStack(0x42);
if (waitAvailable()) {
return read();
if (readType() == DFPlayerFeedBack) {
return read();
}
else{
return -1;
}
}
else{
return -1;
@@ -404,10 +426,15 @@ int DFRobotDFPlayerMini::readVolume(){
}
}
uint8_t DFRobotDFPlayerMini::readEQ(){
int DFRobotDFPlayerMini::readEQ(){
sendStack(0x44);
if (waitAvailable()) {
return read();
if (readType() == DFPlayerFeedBack) {
return read();
}
else{
return -1;
}
}
else{
return -1;
@@ -430,7 +457,12 @@ int DFRobotDFPlayerMini::readFileCounts(uint8_t device){
}
if (waitAvailable()) {
return read();
if (readType() == DFPlayerFeedBack) {
return read();
}
else{
return -1;
}
}
else{
return -1;
@@ -452,7 +484,12 @@ int DFRobotDFPlayerMini::readCurrentFileNumber(uint8_t device){
break;
}
if (waitAvailable()) {
return read();
if (readType() == DFPlayerFeedBack) {
return read();
}
else{
return -1;
}
}
else{
return -1;
@@ -462,7 +499,12 @@ int DFRobotDFPlayerMini::readCurrentFileNumber(uint8_t device){
int DFRobotDFPlayerMini::readFileCountsInFolder(int folderNumber){
sendStack(0x4E, folderNumber);
if (waitAvailable()) {
return read();
if (readType() == DFPlayerFeedBack) {
return read();
}
else{
return -1;
}
}
else{
return -1;
@@ -472,7 +514,12 @@ int DFRobotDFPlayerMini::readFileCountsInFolder(int folderNumber){
int DFRobotDFPlayerMini::readFolderCounts(){
sendStack(0x4F);
if (waitAvailable()) {
return read();
if (readType() == DFPlayerFeedBack) {
return read();
}
else{
return -1;
}
}
else{
return -1;