14 Commits
1.0.0 ... 1.0.2

Author SHA1 Message Date
jimaobian
beeba2c69d Update 2017-11-20 13:53:10 +08:00
jimaobian
cfb9428978 Merge pull request #6 from jandelgado/add_travis_ci_support
added support for travis ci
2017-11-20 13:46:35 +08:00
jimaobian
c36911d7b3 Merge pull request #5 from jandelgado/make_call_to_init_optional
Made the call to init() in DFRobotDFPlayerMini::begin() optional.
2017-11-20 13:39:01 +08:00
jimaobian
0cf620c20d Merge pull request #4 from jandelgado/adding_missing_return_statements
added missing "return" statements
2017-11-20 13:35:52 +08:00
Jan Delgado
7b5087e1af added support for travis ci 2017-06-13 20:01:49 +02:00
Jan Delgado
c0bd68111b added missing "return" statements 2017-06-13 19:08:52 +02:00
Jan Delgado
20343d62ed Made the call to init() in DFRobotDFPlayerMini::begin() optional.
Ratio: calling init() after the module is initially powered on is not necessary
and leads a) to an additional startup latency and b) (more annoying) to a loud
"plop" sound on the speaker output of the DFPlayerMini module.

By adding an optional parameter doReset to the begin() method, the user can now
decide, if the reset should be peformed.  Default is true, which is same
behaviour as now (i.e. perform reset()).
2017-06-13 19:05:51 +02:00
Angelo
31d5ec18d6 Fix compatible problem on nodeMCU (ESP8266)
add delay(0) to reset the soft wdt
2017-03-08 19:27:55 +08:00
jimaobian
6f4f402c71 Merge pull request #3 from redPanther/master
add function for query folder count + added datasheet
2017-03-08 17:34:23 +08:00
redpanther
c9f758979b add docs 2017-02-23 09:19:18 +01:00
redpanther
6111ae2018 add function to count folders on current device 2017-02-23 09:17:45 +01:00
Angelo
772d326841 Merge remote-tracking branch 'origin/master' 2016-12-13 16:53:47 +08:00
Angelo
fabbb011df update to 1.0.1 Fix the compiling error on Arduino.org IDE 2016-12-13 16:52:06 +08:00
jimaobian
9826577d39 Update README.md 2016-12-13 16:37:20 +08:00
7 changed files with 114 additions and 80 deletions

17
.travis.yml Normal file
View File

@@ -0,0 +1,17 @@
language: python
python:
- "2.7"
env:
- BOARD=uno
- BOARD=leonardo
- BOARD=esp01
install:
- python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)"
script:
- cd examples/FullFunction
- platformio ci -l ../.. FullFunction.ino --board=$BOARD
- cd ../GetStarted
- platformio ci -l ../.. getStarted.ino --board=$BOARD

View File

@@ -24,15 +24,16 @@ 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) {
delay(0);
available(); available();
} }
} }
@@ -51,7 +52,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 +60,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,20 +73,22 @@ 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; delay(0);
}
return _handleType != TimeOut;
} }
bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK){ bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK, bool doReset){
if (isACK) { if (isACK) {
enableACK(); enableACK();
} }
@@ -94,15 +97,22 @@ bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK){
} }
_serial = &stream; _serial = &stream;
_timeOutDuration += 3000;
reset(); if (doReset) {
waitAvailable(); _timeOutDuration += 3000;
_timeOutDuration -= 3000; reset();
delay(200); waitAvailable();
_timeOutDuration -= 3000;
delay(200);
} else {
// assume same state as with reset(): online
_handleType = DFPlayerCardOnline;
}
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 +122,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,9 +130,10 @@ 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;
return false;
} }
uint8_t DFRobotDFPlayerMini::readCommand(){ uint8_t DFRobotDFPlayerMini::readCommand(){
@@ -131,30 +142,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 +201,20 @@ 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()) {
delay(0);
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 +226,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 +247,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;
@@ -394,7 +406,6 @@ int DFRobotDFPlayerMini::readVolume(){
uint8_t DFRobotDFPlayerMini::readEQ(){ uint8_t DFRobotDFPlayerMini::readEQ(){
sendStack(0x44); sendStack(0x44);
while (!available());
if (waitAvailable()) { if (waitAvailable()) {
return read(); return read();
} }
@@ -458,12 +469,22 @@ int DFRobotDFPlayerMini::readFileCountsInFolder(int folderNumber){
} }
} }
int DFRobotDFPlayerMini::readFolderCounts(){
sendStack(0x4F);
if (waitAvailable()) {
return read();
}
else{
return -1;
}
}
int DFRobotDFPlayerMini::readFileCounts(){ int DFRobotDFPlayerMini::readFileCounts(){
readFileCounts(DFPLAYER_DEVICE_SD); return readFileCounts(DFPLAYER_DEVICE_SD);
} }
int DFRobotDFPlayerMini::readCurrentFileNumber(){ int DFRobotDFPlayerMini::readCurrentFileNumber(){
readCurrentFileNumber(DFPLAYER_DEVICE_SD); return readCurrentFileNumber(DFPLAYER_DEVICE_SD);
} }

View File

@@ -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,38 +92,26 @@ 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();
bool begin(Stream& stream, bool isACK = true); bool begin(Stream& stream, bool isACK = true, bool doReset = true);
bool waitAvailable(); bool waitAvailable();
bool available(); bool available();
HandleType readType(); uint8_t readType();
uint16_t read(); uint16_t read();
@@ -194,6 +186,8 @@ class DFRobotDFPlayerMini {
int readFileCountsInFolder(int folderNumber); int readFileCountsInFolder(int folderNumber);
int readFileCounts(); int readFileCounts();
int readFolderCounts();
int readCurrentFileNumber(); int readCurrentFileNumber();

View File

@@ -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.

Binary file not shown.

View File

@@ -1,11 +1,11 @@
/*************************************************** /***************************************************
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&search=mp3&description=true&product_id=1121>
*************************************************** ***************************************************
This example shows the basic function of library for DFPlayer. This example shows the basic function of library for DFPlayer.
Created 2016-12-07 Created 2014-8-28
By [Angelo qiao](Angelo.qiao@dfrobot.com) By [Angelo qiao](Angelo.qiao@dfrobot.com)
GNU Lesser General Public License. GNU Lesser General Public License.
@@ -15,7 +15,7 @@ DFPlayer - A Mini MP3 Player For Arduino
/***********Notice and Trouble shooting*************** /***********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> <>
2.This code is tested on Arduino Uno, Leonardo, Mega boards. 2.This code is tested on Arduino Uno, Leonardo, Mega boards.
****************************************************/ ****************************************************/
@@ -40,7 +40,9 @@ void setup()
Serial.println(F("Unable to begin:")); Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!")); Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!")); Serial.println(F("2.Please insert the SD card!"));
while(true); while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
} }
Serial.println(F("DFPlayer Mini online.")); Serial.println(F("DFPlayer Mini online."));

View File

@@ -1,9 +1,9 @@
name=DFRobotDFPlayerMini name=DFRobotDFPlayerMini
version=1.0.0 version=1.0.2
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=*