feat: 全量同步 254 个常用的 Arduino 扩展库文件
This commit is contained in:
@@ -0,0 +1,537 @@
|
||||
/*!
|
||||
* @file DFRobotDFPlayerMini.cpp
|
||||
* @brief DFPlayer - An Arduino Mini MP3 Player From DFRobot
|
||||
* @n Header file for DFRobot's DFPlayer
|
||||
*
|
||||
* @copyright [DFRobot]( http://www.dfrobot.com ), 2016
|
||||
* @copyright GNU Lesser General Public License
|
||||
*
|
||||
* @author [Angelo](Angelo.qiao@dfrobot.com)
|
||||
* @version V1.0.3
|
||||
* @date 2016-12-07
|
||||
*/
|
||||
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
|
||||
void DFRobotDFPlayerMini::setTimeOut(unsigned long timeOutDuration){
|
||||
_timeOutDuration = timeOutDuration;
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::uint16ToArray(uint16_t value, uint8_t *array){
|
||||
*array = (uint8_t)(value>>8);
|
||||
*(array+1) = (uint8_t)(value);
|
||||
}
|
||||
|
||||
uint16_t DFRobotDFPlayerMini::calculateCheckSum(uint8_t *buffer){
|
||||
uint16_t sum = 0;
|
||||
for (int i=Stack_Version; i<Stack_CheckSum; i++) {
|
||||
sum += buffer[i];
|
||||
}
|
||||
return -sum;
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::sendStack(){
|
||||
if (_sending[Stack_ACK]) { //if the ack mode is on wait until the last transmition
|
||||
while (_isSending) {
|
||||
delay(0);
|
||||
available();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
Serial.println();
|
||||
Serial.print(F("sending:"));
|
||||
for (int i=0; i<DFPLAYER_SEND_LENGTH; i++) {
|
||||
Serial.print(_sending[i],HEX);
|
||||
Serial.print(F(" "));
|
||||
}
|
||||
Serial.println();
|
||||
#endif
|
||||
_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){
|
||||
sendStack(command, 0);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::sendStack(uint8_t command, uint16_t argument){
|
||||
_sending[Stack_Command] = command;
|
||||
uint16ToArray(argument, _sending+Stack_Parameter);
|
||||
uint16ToArray(calculateCheckSum(_sending), _sending+Stack_CheckSum);
|
||||
sendStack();
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::sendStack(uint8_t command, uint8_t argumentHigh, uint8_t argumentLow){
|
||||
uint16_t buffer = argumentHigh;
|
||||
buffer <<= 8;
|
||||
sendStack(command, buffer | argumentLow);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::enableACK(){
|
||||
_sending[Stack_ACK] = 0x01;
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::disableACK(){
|
||||
_sending[Stack_ACK] = 0x00;
|
||||
}
|
||||
|
||||
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 true;
|
||||
}
|
||||
|
||||
bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK, bool doReset){
|
||||
_serial = &stream;
|
||||
|
||||
if (isACK) {
|
||||
enableACK();
|
||||
}
|
||||
else{
|
||||
disableACK();
|
||||
}
|
||||
|
||||
if (doReset) {
|
||||
reset();
|
||||
waitAvailable(2000);
|
||||
delay(200);
|
||||
}
|
||||
else {
|
||||
// assume same state as with reset(): online
|
||||
_handleType = DFPlayerCardOnline;
|
||||
}
|
||||
|
||||
return (readType() == DFPlayerCardOnline) || (readType() == DFPlayerUSBOnline) || !isACK;
|
||||
}
|
||||
|
||||
uint8_t DFRobotDFPlayerMini::readType(){
|
||||
_isAvailable = false;
|
||||
return _handleType;
|
||||
}
|
||||
|
||||
uint16_t DFRobotDFPlayerMini::read(){
|
||||
_isAvailable = false;
|
||||
return _handleParameter;
|
||||
}
|
||||
|
||||
bool DFRobotDFPlayerMini::handleMessage(uint8_t type, uint16_t parameter){
|
||||
_receivedIndex = 0;
|
||||
_handleType = type;
|
||||
_handleParameter = parameter;
|
||||
_isAvailable = true;
|
||||
return _isAvailable;
|
||||
}
|
||||
|
||||
bool DFRobotDFPlayerMini::handleError(uint8_t type, uint16_t parameter){
|
||||
handleMessage(type, parameter);
|
||||
_isSending = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t DFRobotDFPlayerMini::readCommand(){
|
||||
_isAvailable = false;
|
||||
return _handleCommand;
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::parseStack(){
|
||||
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) {
|
||||
case 0x3D:
|
||||
handleMessage(DFPlayerPlayFinished, _handleParameter);
|
||||
break;
|
||||
case 0x3F:
|
||||
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 & 0x01) {
|
||||
handleMessage(DFPlayerUSBInserted, _handleParameter);
|
||||
}
|
||||
else if (_handleParameter & 0x02) {
|
||||
handleMessage(DFPlayerCardInserted, _handleParameter);
|
||||
}
|
||||
break;
|
||||
case 0x3B:
|
||||
if (_handleParameter & 0x01) {
|
||||
handleMessage(DFPlayerUSBRemoved, _handleParameter);
|
||||
}
|
||||
else if (_handleParameter & 0x02) {
|
||||
handleMessage(DFPlayerCardRemoved, _handleParameter);
|
||||
}
|
||||
break;
|
||||
case 0x40:
|
||||
handleMessage(DFPlayerError, _handleParameter);
|
||||
break;
|
||||
case 0x3C:
|
||||
case 0x3E:
|
||||
case 0x42:
|
||||
case 0x43:
|
||||
case 0x44:
|
||||
case 0x45:
|
||||
case 0x46:
|
||||
case 0x47:
|
||||
case 0x48:
|
||||
case 0x49:
|
||||
case 0x4B:
|
||||
case 0x4C:
|
||||
case 0x4D:
|
||||
case 0x4E:
|
||||
case 0x4F:
|
||||
handleMessage(DFPlayerFeedBack, _handleParameter);
|
||||
break;
|
||||
default:
|
||||
handleError(WrongStack);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t DFRobotDFPlayerMini::arrayToUint16(uint8_t *array){
|
||||
uint16_t value = *array;
|
||||
value <<=8;
|
||||
value += *(array+1);
|
||||
return value;
|
||||
}
|
||||
|
||||
bool DFRobotDFPlayerMini::validateStack(){
|
||||
return calculateCheckSum(_received) == arrayToUint16(_received+Stack_CheckSum);
|
||||
}
|
||||
|
||||
bool DFRobotDFPlayerMini::available(){
|
||||
while (_serial->available()) {
|
||||
delay(0);
|
||||
if (_receivedIndex == 0) {
|
||||
_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) {
|
||||
_receivedIndex ++;
|
||||
}
|
||||
}
|
||||
else{
|
||||
_received[_receivedIndex] = _serial->read();
|
||||
#ifdef _DEBUG
|
||||
Serial.print(_received[_receivedIndex],HEX);
|
||||
Serial.print(F(" "));
|
||||
#endif
|
||||
switch (_receivedIndex) {
|
||||
case Stack_Version:
|
||||
if (_received[_receivedIndex] != 0xFF) {
|
||||
return handleError(WrongStack);
|
||||
}
|
||||
break;
|
||||
case Stack_Length:
|
||||
if (_received[_receivedIndex] != 0x06) {
|
||||
return handleError(WrongStack);
|
||||
}
|
||||
break;
|
||||
case Stack_End:
|
||||
#ifdef _DEBUG
|
||||
Serial.println();
|
||||
#endif
|
||||
if (_received[_receivedIndex] != 0xEF) {
|
||||
return handleError(WrongStack);
|
||||
}
|
||||
else{
|
||||
if (validateStack()) {
|
||||
_receivedIndex = 0;
|
||||
parseStack();
|
||||
return _isAvailable;
|
||||
}
|
||||
else{
|
||||
return handleError(WrongStack);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
_receivedIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
if (_isSending && (millis()-_timeOutTimer>=_timeOutDuration)) {
|
||||
return handleError(TimeOut);
|
||||
}
|
||||
|
||||
return _isAvailable;
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::next(){
|
||||
sendStack(0x01);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::previous(){
|
||||
sendStack(0x02);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::play(int fileNumber){
|
||||
sendStack(0x03, fileNumber);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::volumeUp(){
|
||||
sendStack(0x04);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::volumeDown(){
|
||||
sendStack(0x05);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::volume(uint8_t volume){
|
||||
sendStack(0x06, volume);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::EQ(uint8_t eq) {
|
||||
sendStack(0x07, eq);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::loop(int fileNumber) {
|
||||
sendStack(0x08, fileNumber);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::outputDevice(uint8_t device) {
|
||||
sendStack(0x09, device);
|
||||
delay(200);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::sleep(){
|
||||
sendStack(0x0A);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::reset(){
|
||||
sendStack(0x0C);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::start(){
|
||||
sendStack(0x0D);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::pause(){
|
||||
sendStack(0x0E);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::playFolder(uint8_t folderNumber, uint8_t fileNumber){
|
||||
sendStack(0x0F, folderNumber, fileNumber);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::outputSetting(bool enable, uint8_t gain){
|
||||
sendStack(0x10, enable, gain);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::enableLoopAll(){
|
||||
sendStack(0x11, 0x01);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::disableLoopAll(){
|
||||
sendStack(0x11, 0x00);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::playMp3Folder(int fileNumber){
|
||||
sendStack(0x12, fileNumber);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::advertise(int fileNumber){
|
||||
sendStack(0x13, fileNumber);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::playLargeFolder(uint8_t folderNumber, uint16_t fileNumber){
|
||||
sendStack(0x14, (((uint16_t)folderNumber) << 12) | fileNumber);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::stopAdvertise(){
|
||||
sendStack(0x15);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::stop(){
|
||||
sendStack(0x16);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::loopFolder(int folderNumber){
|
||||
sendStack(0x17, folderNumber);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::randomAll(){
|
||||
sendStack(0x18);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::enableLoop(){
|
||||
sendStack(0x19, 0x00);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::disableLoop(){
|
||||
sendStack(0x19, 0x01);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::enableDAC(){
|
||||
sendStack(0x1A, 0x00);
|
||||
}
|
||||
|
||||
void DFRobotDFPlayerMini::disableDAC(){
|
||||
sendStack(0x1A, 0x01);
|
||||
}
|
||||
|
||||
int DFRobotDFPlayerMini::readState(){
|
||||
sendStack(0x42);
|
||||
if (waitAvailable()) {
|
||||
if (readType() == DFPlayerFeedBack) {
|
||||
return read();
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int DFRobotDFPlayerMini::readVolume(){
|
||||
sendStack(0x43);
|
||||
if (waitAvailable()) {
|
||||
return read();
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int DFRobotDFPlayerMini::readEQ(){
|
||||
sendStack(0x44);
|
||||
if (waitAvailable()) {
|
||||
if (readType() == DFPlayerFeedBack) {
|
||||
return read();
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int DFRobotDFPlayerMini::readFileCounts(uint8_t device){
|
||||
switch (device) {
|
||||
case DFPLAYER_DEVICE_U_DISK:
|
||||
sendStack(0x47);
|
||||
break;
|
||||
case DFPLAYER_DEVICE_SD:
|
||||
sendStack(0x48);
|
||||
break;
|
||||
case DFPLAYER_DEVICE_FLASH:
|
||||
sendStack(0x49);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (waitAvailable()) {
|
||||
if (readType() == DFPlayerFeedBack) {
|
||||
return read();
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int DFRobotDFPlayerMini::readCurrentFileNumber(uint8_t device){
|
||||
switch (device) {
|
||||
case DFPLAYER_DEVICE_U_DISK:
|
||||
sendStack(0x4B);
|
||||
break;
|
||||
case DFPLAYER_DEVICE_SD:
|
||||
sendStack(0x4C);
|
||||
break;
|
||||
case DFPLAYER_DEVICE_FLASH:
|
||||
sendStack(0x4D);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (waitAvailable()) {
|
||||
if (readType() == DFPlayerFeedBack) {
|
||||
return read();
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int DFRobotDFPlayerMini::readFileCountsInFolder(int folderNumber){
|
||||
sendStack(0x4E, folderNumber);
|
||||
if (waitAvailable()) {
|
||||
if (readType() == DFPlayerFeedBack) {
|
||||
return read();
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int DFRobotDFPlayerMini::readFolderCounts(){
|
||||
sendStack(0x4F);
|
||||
if (waitAvailable()) {
|
||||
if (readType() == DFPlayerFeedBack) {
|
||||
return read();
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int DFRobotDFPlayerMini::readFileCounts(){
|
||||
return readFileCounts(DFPLAYER_DEVICE_SD);
|
||||
}
|
||||
|
||||
int DFRobotDFPlayerMini::readCurrentFileNumber(){
|
||||
return readCurrentFileNumber(DFPLAYER_DEVICE_SD);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
/*!
|
||||
* @file DFRobotDFPlayerMini.h
|
||||
* @brief DFPlayer - An Arduino Mini MP3 Player From DFRobot
|
||||
* @n Header file for DFRobot's DFPlayer
|
||||
*
|
||||
* @copyright [DFRobot]( http://www.dfrobot.com ), 2016
|
||||
* @copyright GNU Lesser General Public License
|
||||
*
|
||||
* @author [Angelo](Angelo.qiao@dfrobot.com)
|
||||
* @version V1.0.3
|
||||
* @date 2016-12-07
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#ifndef DFRobotDFPlayerMini_cpp
|
||||
#define DFRobotDFPlayerMini_cpp
|
||||
|
||||
|
||||
#define DFPLAYER_EQ_NORMAL 0
|
||||
#define DFPLAYER_EQ_POP 1
|
||||
#define DFPLAYER_EQ_ROCK 2
|
||||
#define DFPLAYER_EQ_JAZZ 3
|
||||
#define DFPLAYER_EQ_CLASSIC 4
|
||||
#define DFPLAYER_EQ_BASS 5
|
||||
|
||||
#define DFPLAYER_DEVICE_U_DISK 1
|
||||
#define DFPLAYER_DEVICE_SD 2
|
||||
#define DFPLAYER_DEVICE_AUX 3
|
||||
#define DFPLAYER_DEVICE_SLEEP 4
|
||||
#define DFPLAYER_DEVICE_FLASH 5
|
||||
|
||||
#define DFPLAYER_RECEIVED_LENGTH 10
|
||||
#define DFPLAYER_SEND_LENGTH 10
|
||||
|
||||
//#define _DEBUG
|
||||
|
||||
#define TimeOut 0
|
||||
#define WrongStack 1
|
||||
#define DFPlayerCardInserted 2
|
||||
#define DFPlayerCardRemoved 3
|
||||
#define DFPlayerCardOnline 4
|
||||
#define DFPlayerPlayFinished 5
|
||||
#define DFPlayerError 6
|
||||
#define DFPlayerUSBInserted 7
|
||||
#define DFPlayerUSBRemoved 8
|
||||
#define DFPlayerUSBOnline 9
|
||||
#define DFPlayerCardUSBOnline 10
|
||||
#define DFPlayerFeedBack 11
|
||||
|
||||
#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;
|
||||
|
||||
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);
|
||||
|
||||
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
|
||||
@@ -0,0 +1,20 @@
|
||||
# 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.
|
||||
|
||||
Created 2016-12-07
|
||||
By [Angelo qiao](Angelo.qiao@dfrobot.com)
|
||||
|
||||
GNU Lesser General Public License.
|
||||
See <http://www.gnu.org/licenses/> for details.
|
||||
All above must be included in any redistribution
|
||||
|
||||
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.
|
||||
@@ -0,0 +1,126 @@
|
||||
/***************************************************
|
||||
DFPlayer - A Mini MP3 Player For Arduino
|
||||
<https://www.dfrobot.com/product-1121.html>
|
||||
|
||||
***************************************************
|
||||
This example shows the basic function of library for DFPlayer.
|
||||
|
||||
Created 2016-12-07
|
||||
By [Angelo qiao](Angelo.qiao@dfrobot.com)
|
||||
|
||||
GNU Lesser General Public License.
|
||||
See <http://www.gnu.org/licenses/> for details.
|
||||
All above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
/***********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.
|
||||
****************************************************/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
|
||||
DFRobotDFPlayerMini myDFPlayer;
|
||||
void printDetail(uint8_t type, int value);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial1.begin(9600);
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println();
|
||||
Serial.println(F("DFRobot DFPlayer Mini Demo"));
|
||||
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
|
||||
|
||||
if (!myDFPlayer.begin(Serial1)) { //Use softwareSerial to communicate with mp3.
|
||||
Serial.println(F("Unable to begin:"));
|
||||
Serial.println(F("1.Please recheck the connection!"));
|
||||
Serial.println(F("2.Please insert the SD card!"));
|
||||
while(true){
|
||||
delay(0); // Code to compatible with ESP8266 watch dog.
|
||||
}
|
||||
}
|
||||
Serial.println(F("DFPlayer Mini online."));
|
||||
|
||||
myDFPlayer.volume(10); //Set volume value. From 0 to 30
|
||||
myDFPlayer.play(1); //Play the first mp3
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static unsigned long timer = millis();
|
||||
|
||||
if (millis() - timer > 3000) {
|
||||
timer = millis();
|
||||
myDFPlayer.next(); //Play next mp3 every 3 second.
|
||||
}
|
||||
|
||||
if (myDFPlayer.available()) {
|
||||
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
|
||||
}
|
||||
}
|
||||
|
||||
void printDetail(uint8_t type, int value){
|
||||
switch (type) {
|
||||
case TimeOut:
|
||||
Serial.println(F("Time Out!"));
|
||||
break;
|
||||
case WrongStack:
|
||||
Serial.println(F("Stack Wrong!"));
|
||||
break;
|
||||
case DFPlayerCardInserted:
|
||||
Serial.println(F("Card Inserted!"));
|
||||
break;
|
||||
case DFPlayerCardRemoved:
|
||||
Serial.println(F("Card Removed!"));
|
||||
break;
|
||||
case DFPlayerCardOnline:
|
||||
Serial.println(F("Card Online!"));
|
||||
break;
|
||||
case DFPlayerUSBInserted:
|
||||
Serial.println("USB Inserted!");
|
||||
break;
|
||||
case DFPlayerUSBRemoved:
|
||||
Serial.println("USB Removed!");
|
||||
break;
|
||||
case DFPlayerPlayFinished:
|
||||
Serial.print(F("Number:"));
|
||||
Serial.print(value);
|
||||
Serial.println(F(" Play Finished!"));
|
||||
break;
|
||||
case DFPlayerError:
|
||||
Serial.print(F("DFPlayerError:"));
|
||||
switch (value) {
|
||||
case Busy:
|
||||
Serial.println(F("Card not found"));
|
||||
break;
|
||||
case Sleeping:
|
||||
Serial.println(F("Sleeping"));
|
||||
break;
|
||||
case SerialWrongStack:
|
||||
Serial.println(F("Get Wrong Stack"));
|
||||
break;
|
||||
case CheckSumNotMatch:
|
||||
Serial.println(F("Check Sum Not Match"));
|
||||
break;
|
||||
case FileIndexOut:
|
||||
Serial.println(F("File Index Out of Bound"));
|
||||
break;
|
||||
case FileMismatch:
|
||||
Serial.println(F("Cannot Find File"));
|
||||
break;
|
||||
case Advertise:
|
||||
Serial.println(F("In Advertise"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/***************************************************
|
||||
DFPlayer - A Mini MP3 Player For Arduino
|
||||
<https://www.dfrobot.com/product-1121.html>
|
||||
|
||||
***************************************************
|
||||
This example shows the basic function of library for DFPlayer.
|
||||
|
||||
Created 2016-12-07
|
||||
By [Angelo qiao](Angelo.qiao@dfrobot.com)
|
||||
|
||||
GNU Lesser General Public License.
|
||||
See <http://www.gnu.org/licenses/> for details.
|
||||
All above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
/***********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.
|
||||
****************************************************/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "SoftwareSerial.h"
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
|
||||
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
|
||||
DFRobotDFPlayerMini myDFPlayer;
|
||||
void printDetail(uint8_t type, int value);
|
||||
|
||||
void setup()
|
||||
{
|
||||
mySoftwareSerial.begin(9600);
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println();
|
||||
Serial.println(F("DFRobot DFPlayer Mini Demo"));
|
||||
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
|
||||
|
||||
if (!myDFPlayer.begin(mySoftwareSerial, false)) { //Use softwareSerial to communicate with mp3.
|
||||
Serial.println(F("Unable to begin:"));
|
||||
Serial.println(F("1.Please recheck the connection!"));
|
||||
Serial.println(F("2.Please insert the SD card!"));
|
||||
while(true){
|
||||
delay(0); // Code to compatible with ESP8266 watch dog.
|
||||
}
|
||||
}
|
||||
Serial.println(F("DFPlayer Mini online."));
|
||||
|
||||
myDFPlayer.volume(10); //Set volume value. From 0 to 30
|
||||
myDFPlayer.play(1); //Play the first mp3
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static unsigned long timer = millis();
|
||||
|
||||
if (millis() - timer > 3000) {
|
||||
timer = millis();
|
||||
myDFPlayer.next(); //Play next mp3 every 3 second.
|
||||
}
|
||||
|
||||
if (myDFPlayer.available()) {
|
||||
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
|
||||
}
|
||||
}
|
||||
|
||||
void printDetail(uint8_t type, int value){
|
||||
switch (type) {
|
||||
case TimeOut:
|
||||
Serial.println(F("Time Out!"));
|
||||
break;
|
||||
case WrongStack:
|
||||
Serial.println(F("Stack Wrong!"));
|
||||
break;
|
||||
case DFPlayerCardInserted:
|
||||
Serial.println(F("Card Inserted!"));
|
||||
break;
|
||||
case DFPlayerCardRemoved:
|
||||
Serial.println(F("Card Removed!"));
|
||||
break;
|
||||
case DFPlayerCardOnline:
|
||||
Serial.println(F("Card Online!"));
|
||||
break;
|
||||
case DFPlayerUSBInserted:
|
||||
Serial.println("USB Inserted!");
|
||||
break;
|
||||
case DFPlayerUSBRemoved:
|
||||
Serial.println("USB Removed!");
|
||||
break;
|
||||
case DFPlayerPlayFinished:
|
||||
Serial.print(F("Number:"));
|
||||
Serial.print(value);
|
||||
Serial.println(F(" Play Finished!"));
|
||||
break;
|
||||
case DFPlayerError:
|
||||
Serial.print(F("DFPlayerError:"));
|
||||
switch (value) {
|
||||
case Busy:
|
||||
Serial.println(F("Card not found"));
|
||||
break;
|
||||
case Sleeping:
|
||||
Serial.println(F("Sleeping"));
|
||||
break;
|
||||
case SerialWrongStack:
|
||||
Serial.println(F("Get Wrong Stack"));
|
||||
break;
|
||||
case CheckSumNotMatch:
|
||||
Serial.println(F("Check Sum Not Match"));
|
||||
break;
|
||||
case FileIndexOut:
|
||||
Serial.println(F("File Index Out of Bound"));
|
||||
break;
|
||||
case FileMismatch:
|
||||
Serial.println(F("Cannot Find File"));
|
||||
break;
|
||||
case Advertise:
|
||||
Serial.println(F("In Advertise"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/***************************************************
|
||||
DFPlayer - A Mini MP3 Player For Arduino
|
||||
<https://www.dfrobot.com/product-1121.html>
|
||||
|
||||
***************************************************
|
||||
This example shows the basic function of library for DFPlayer.
|
||||
|
||||
Created 2016-12-07
|
||||
By [Angelo qiao](Angelo.qiao@dfrobot.com)
|
||||
|
||||
GNU Lesser General Public License.
|
||||
See <http://www.gnu.org/licenses/> for details.
|
||||
All above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
/***********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.
|
||||
****************************************************/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "SoftwareSerial.h"
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
|
||||
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
|
||||
DFRobotDFPlayerMini myDFPlayer;
|
||||
void printDetail(uint8_t type, int value);
|
||||
|
||||
void setup()
|
||||
{
|
||||
mySoftwareSerial.begin(9600);
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println();
|
||||
Serial.println(F("DFRobot DFPlayer Mini Demo"));
|
||||
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
|
||||
|
||||
if (!myDFPlayer.begin(mySoftwareSerial, true, false)) { //Use softwareSerial to communicate with mp3.
|
||||
Serial.println(F("Unable to begin:"));
|
||||
Serial.println(F("1.Please recheck the connection!"));
|
||||
Serial.println(F("2.Please insert the SD card!"));
|
||||
while(true){
|
||||
delay(0); // Code to compatible with ESP8266 watch dog.
|
||||
}
|
||||
}
|
||||
Serial.println(F("DFPlayer Mini online."));
|
||||
|
||||
myDFPlayer.volume(10); //Set volume value. From 0 to 30
|
||||
myDFPlayer.play(1); //Play the first mp3
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static unsigned long timer = millis();
|
||||
|
||||
if (millis() - timer > 3000) {
|
||||
timer = millis();
|
||||
myDFPlayer.next(); //Play next mp3 every 3 second.
|
||||
}
|
||||
|
||||
if (myDFPlayer.available()) {
|
||||
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
|
||||
}
|
||||
}
|
||||
|
||||
void printDetail(uint8_t type, int value){
|
||||
switch (type) {
|
||||
case TimeOut:
|
||||
Serial.println(F("Time Out!"));
|
||||
break;
|
||||
case WrongStack:
|
||||
Serial.println(F("Stack Wrong!"));
|
||||
break;
|
||||
case DFPlayerCardInserted:
|
||||
Serial.println(F("Card Inserted!"));
|
||||
break;
|
||||
case DFPlayerCardRemoved:
|
||||
Serial.println(F("Card Removed!"));
|
||||
break;
|
||||
case DFPlayerCardOnline:
|
||||
Serial.println(F("Card Online!"));
|
||||
break;
|
||||
case DFPlayerUSBInserted:
|
||||
Serial.println("USB Inserted!");
|
||||
break;
|
||||
case DFPlayerUSBRemoved:
|
||||
Serial.println("USB Removed!");
|
||||
break;
|
||||
case DFPlayerPlayFinished:
|
||||
Serial.print(F("Number:"));
|
||||
Serial.print(value);
|
||||
Serial.println(F(" Play Finished!"));
|
||||
break;
|
||||
case DFPlayerError:
|
||||
Serial.print(F("DFPlayerError:"));
|
||||
switch (value) {
|
||||
case Busy:
|
||||
Serial.println(F("Card not found"));
|
||||
break;
|
||||
case Sleeping:
|
||||
Serial.println(F("Sleeping"));
|
||||
break;
|
||||
case SerialWrongStack:
|
||||
Serial.println(F("Get Wrong Stack"));
|
||||
break;
|
||||
case CheckSumNotMatch:
|
||||
Serial.println(F("Check Sum Not Match"));
|
||||
break;
|
||||
case FileIndexOut:
|
||||
Serial.println(F("File Index Out of Bound"));
|
||||
break;
|
||||
case FileMismatch:
|
||||
Serial.println(F("Cannot Find File"));
|
||||
break;
|
||||
case Advertise:
|
||||
Serial.println(F("In Advertise"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
/***************************************************
|
||||
DFPlayer - A Mini MP3 Player For Arduino
|
||||
<https://www.dfrobot.com/product-1121.html>
|
||||
|
||||
***************************************************
|
||||
This example shows the all the function of library for DFPlayer.
|
||||
|
||||
Created 2016-12-07
|
||||
By [Angelo qiao](Angelo.qiao@dfrobot.com)
|
||||
|
||||
GNU Lesser General Public License.
|
||||
See <http://www.gnu.org/licenses/> for details.
|
||||
All above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
/***********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.
|
||||
****************************************************/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "SoftwareSerial.h"
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
|
||||
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
|
||||
DFRobotDFPlayerMini myDFPlayer;
|
||||
void printDetail(uint8_t type, int value);
|
||||
|
||||
void setup()
|
||||
{
|
||||
mySoftwareSerial.begin(9600);
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println();
|
||||
Serial.println(F("DFRobot DFPlayer Mini Demo"));
|
||||
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
|
||||
|
||||
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
|
||||
Serial.println(F("Unable to begin:"));
|
||||
Serial.println(F("1.Please recheck the connection!"));
|
||||
Serial.println(F("2.Please insert the SD card!"));
|
||||
while(true);
|
||||
}
|
||||
Serial.println(F("DFPlayer Mini online."));
|
||||
|
||||
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
|
||||
|
||||
//----Set volume----
|
||||
myDFPlayer.volume(10); //Set volume value (0~30).
|
||||
myDFPlayer.volumeUp(); //Volume Up
|
||||
myDFPlayer.volumeDown(); //Volume Down
|
||||
|
||||
//----Set different EQ----
|
||||
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
|
||||
// myDFPlayer.EQ(DFPLAYER_EQ_POP);
|
||||
// myDFPlayer.EQ(DFPLAYER_EQ_ROCK);
|
||||
// myDFPlayer.EQ(DFPLAYER_EQ_JAZZ);
|
||||
// myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC);
|
||||
// myDFPlayer.EQ(DFPLAYER_EQ_BASS);
|
||||
|
||||
//----Set device we use SD as default----
|
||||
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_U_DISK);
|
||||
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
|
||||
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_AUX);
|
||||
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_SLEEP);
|
||||
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_FLASH);
|
||||
|
||||
//----Mp3 control----
|
||||
// myDFPlayer.sleep(); //sleep
|
||||
// myDFPlayer.reset(); //Reset the module
|
||||
// myDFPlayer.enableDAC(); //Enable On-chip DAC
|
||||
// myDFPlayer.disableDAC(); //Disable On-chip DAC
|
||||
// myDFPlayer.outputSetting(true, 15); //output setting, enable the output and set the gain to 15
|
||||
|
||||
//----Mp3 play----
|
||||
myDFPlayer.next(); //Play next mp3
|
||||
delay(1000);
|
||||
myDFPlayer.previous(); //Play previous mp3
|
||||
delay(1000);
|
||||
myDFPlayer.play(1); //Play the first mp3
|
||||
delay(1000);
|
||||
myDFPlayer.loop(1); //Loop the first mp3
|
||||
delay(1000);
|
||||
myDFPlayer.pause(); //pause the mp3
|
||||
delay(1000);
|
||||
myDFPlayer.start(); //start the mp3 from the pause
|
||||
delay(1000);
|
||||
myDFPlayer.playFolder(15, 4); //play specific mp3 in SD:/15/004.mp3; Folder Name(1~99); File Name(1~255)
|
||||
delay(1000);
|
||||
myDFPlayer.enableLoopAll(); //loop all mp3 files.
|
||||
delay(1000);
|
||||
myDFPlayer.disableLoopAll(); //stop loop all mp3 files.
|
||||
delay(1000);
|
||||
myDFPlayer.playMp3Folder(4); //play specific mp3 in SD:/MP3/0004.mp3; File Name(0~65535)
|
||||
delay(1000);
|
||||
myDFPlayer.advertise(3); //advertise specific mp3 in SD:/ADVERT/0003.mp3; File Name(0~65535)
|
||||
delay(1000);
|
||||
myDFPlayer.stopAdvertise(); //stop advertise
|
||||
delay(1000);
|
||||
myDFPlayer.playLargeFolder(2, 999); //play specific mp3 in SD:/02/004.mp3; Folder Name(1~10); File Name(1~1000)
|
||||
delay(1000);
|
||||
myDFPlayer.loopFolder(5); //loop all mp3 files in folder SD:/05.
|
||||
delay(1000);
|
||||
myDFPlayer.randomAll(); //Random play all the mp3.
|
||||
delay(1000);
|
||||
myDFPlayer.enableLoop(); //enable loop.
|
||||
delay(1000);
|
||||
myDFPlayer.disableLoop(); //disable loop.
|
||||
delay(1000);
|
||||
|
||||
//----Read imformation----
|
||||
Serial.println(myDFPlayer.readState()); //read mp3 state
|
||||
Serial.println(myDFPlayer.readVolume()); //read current volume
|
||||
Serial.println(myDFPlayer.readEQ()); //read EQ setting
|
||||
Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
|
||||
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
|
||||
Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static unsigned long timer = millis();
|
||||
|
||||
if (millis() - timer > 3000) {
|
||||
timer = millis();
|
||||
myDFPlayer.next(); //Play next mp3 every 3 second.
|
||||
}
|
||||
|
||||
if (myDFPlayer.available()) {
|
||||
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
|
||||
}
|
||||
}
|
||||
|
||||
void printDetail(uint8_t type, int value){
|
||||
switch (type) {
|
||||
case TimeOut:
|
||||
Serial.println(F("Time Out!"));
|
||||
break;
|
||||
case WrongStack:
|
||||
Serial.println(F("Stack Wrong!"));
|
||||
break;
|
||||
case DFPlayerCardInserted:
|
||||
Serial.println(F("Card Inserted!"));
|
||||
break;
|
||||
case DFPlayerCardRemoved:
|
||||
Serial.println(F("Card Removed!"));
|
||||
break;
|
||||
case DFPlayerCardOnline:
|
||||
Serial.println(F("Card Online!"));
|
||||
break;
|
||||
case DFPlayerUSBInserted:
|
||||
Serial.println("USB Inserted!");
|
||||
break;
|
||||
case DFPlayerUSBRemoved:
|
||||
Serial.println("USB Removed!");
|
||||
break;
|
||||
case DFPlayerPlayFinished:
|
||||
Serial.print(F("Number:"));
|
||||
Serial.print(value);
|
||||
Serial.println(F(" Play Finished!"));
|
||||
break;
|
||||
case DFPlayerError:
|
||||
Serial.print(F("DFPlayerError:"));
|
||||
switch (value) {
|
||||
case Busy:
|
||||
Serial.println(F("Card not found"));
|
||||
break;
|
||||
case Sleeping:
|
||||
Serial.println(F("Sleeping"));
|
||||
break;
|
||||
case SerialWrongStack:
|
||||
Serial.println(F("Get Wrong Stack"));
|
||||
break;
|
||||
case CheckSumNotMatch:
|
||||
Serial.println(F("Check Sum Not Match"));
|
||||
break;
|
||||
case FileIndexOut:
|
||||
Serial.println(F("File Index Out of Bound"));
|
||||
break;
|
||||
case FileMismatch:
|
||||
Serial.println(F("Cannot Find File"));
|
||||
break;
|
||||
case Advertise:
|
||||
Serial.println(F("In Advertise"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/***************************************************
|
||||
DFPlayer - A Mini MP3 Player For Arduino
|
||||
<https://www.dfrobot.com/product-1121.html>
|
||||
|
||||
***************************************************
|
||||
This example shows the basic function of library for DFPlayer.
|
||||
|
||||
Created 2016-12-07
|
||||
By [Angelo qiao](Angelo.qiao@dfrobot.com)
|
||||
|
||||
GNU Lesser General Public License.
|
||||
See <http://www.gnu.org/licenses/> for details.
|
||||
All above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
/***********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.
|
||||
****************************************************/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "SoftwareSerial.h"
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
|
||||
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
|
||||
DFRobotDFPlayerMini myDFPlayer;
|
||||
void printDetail(uint8_t type, int value);
|
||||
|
||||
void setup()
|
||||
{
|
||||
mySoftwareSerial.begin(9600);
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println();
|
||||
Serial.println(F("DFRobot DFPlayer Mini Demo"));
|
||||
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
|
||||
|
||||
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
|
||||
Serial.println(F("Unable to begin:"));
|
||||
Serial.println(F("1.Please recheck the connection!"));
|
||||
Serial.println(F("2.Please insert the SD card!"));
|
||||
while(true){
|
||||
delay(0); // Code to compatible with ESP8266 watch dog.
|
||||
}
|
||||
}
|
||||
Serial.println(F("DFPlayer Mini online."));
|
||||
|
||||
myDFPlayer.volume(10); //Set volume value. From 0 to 30
|
||||
myDFPlayer.play(1); //Play the first mp3
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static unsigned long timer = millis();
|
||||
|
||||
if (millis() - timer > 3000) {
|
||||
timer = millis();
|
||||
myDFPlayer.next(); //Play next mp3 every 3 second.
|
||||
}
|
||||
|
||||
if (myDFPlayer.available()) {
|
||||
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
|
||||
}
|
||||
}
|
||||
|
||||
void printDetail(uint8_t type, int value){
|
||||
switch (type) {
|
||||
case TimeOut:
|
||||
Serial.println(F("Time Out!"));
|
||||
break;
|
||||
case WrongStack:
|
||||
Serial.println(F("Stack Wrong!"));
|
||||
break;
|
||||
case DFPlayerCardInserted:
|
||||
Serial.println(F("Card Inserted!"));
|
||||
break;
|
||||
case DFPlayerCardRemoved:
|
||||
Serial.println(F("Card Removed!"));
|
||||
break;
|
||||
case DFPlayerCardOnline:
|
||||
Serial.println(F("Card Online!"));
|
||||
break;
|
||||
case DFPlayerUSBInserted:
|
||||
Serial.println("USB Inserted!");
|
||||
break;
|
||||
case DFPlayerUSBRemoved:
|
||||
Serial.println("USB Removed!");
|
||||
break;
|
||||
case DFPlayerPlayFinished:
|
||||
Serial.print(F("Number:"));
|
||||
Serial.print(value);
|
||||
Serial.println(F(" Play Finished!"));
|
||||
break;
|
||||
case DFPlayerError:
|
||||
Serial.print(F("DFPlayerError:"));
|
||||
switch (value) {
|
||||
case Busy:
|
||||
Serial.println(F("Card not found"));
|
||||
break;
|
||||
case Sleeping:
|
||||
Serial.println(F("Sleeping"));
|
||||
break;
|
||||
case SerialWrongStack:
|
||||
Serial.println(F("Get Wrong Stack"));
|
||||
break;
|
||||
case CheckSumNotMatch:
|
||||
Serial.println(F("Check Sum Not Match"));
|
||||
break;
|
||||
case FileIndexOut:
|
||||
Serial.println(F("File Index Out of Bound"));
|
||||
break;
|
||||
case FileMismatch:
|
||||
Serial.println(F("Cannot Find File"));
|
||||
break;
|
||||
case Advertise:
|
||||
Serial.println(F("In Advertise"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/***************************************************
|
||||
DFPlayer - A Mini MP3 Player For Arduino
|
||||
<https://www.dfrobot.com/product-1121.html>
|
||||
|
||||
***************************************************
|
||||
This example shows the basic function of library for DFPlayer.
|
||||
|
||||
Created 2016-12-07
|
||||
Modified 2018-08-15
|
||||
By [Angelo qiao](Angelo.qiao@dfrobot.com)
|
||||
|
||||
GNU Lesser General Public License.
|
||||
See <http://www.gnu.org/licenses/> for details.
|
||||
All above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
/***********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.
|
||||
****************************************************/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "SoftwareSerial.h"
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
|
||||
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
|
||||
DFRobotDFPlayerMini myDFPlayer;
|
||||
void printDetail(uint8_t type, int value);
|
||||
|
||||
void setup()
|
||||
{
|
||||
mySoftwareSerial.begin(9600);
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println();
|
||||
Serial.println(F("DFRobot DFPlayer Mini Demo"));
|
||||
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
|
||||
|
||||
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
|
||||
Serial.println(F("Unable to begin:"));
|
||||
Serial.println(F("1.Please recheck the connection!"));
|
||||
Serial.println(F("2.Please insert the SD card or USB drive!"));
|
||||
while(true){
|
||||
delay(0); // Code to compatible with ESP8266 watch dog.
|
||||
}
|
||||
}
|
||||
Serial.println(F("DFPlayer Mini online."));
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static unsigned long timer = millis();
|
||||
|
||||
if (millis() - timer > 3000) {
|
||||
timer = millis();
|
||||
|
||||
int value;
|
||||
|
||||
// value = myDFPlayer.readState(); //read mp3 state
|
||||
// value = myDFPlayer.readVolume(); //read current volume
|
||||
// value = myDFPlayer.readEQ(); //read EQ setting
|
||||
// value = myDFPlayer.readFileCounts(); //read all file counts in SD card
|
||||
// value = myDFPlayer.readCurrentFileNumber(); //read current play file number
|
||||
value = myDFPlayer.readFileCountsInFolder(3); //read file counts in folder SD:/03
|
||||
|
||||
if (value == -1) { //Error while Reading.
|
||||
printDetail(myDFPlayer.readType(), myDFPlayer.read());
|
||||
}
|
||||
else{ //Successfully get the result.
|
||||
Serial.println(value);
|
||||
}
|
||||
}
|
||||
|
||||
if (myDFPlayer.available()) {
|
||||
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
|
||||
}
|
||||
}
|
||||
|
||||
void printDetail(uint8_t type, int value){
|
||||
switch (type) {
|
||||
case TimeOut:
|
||||
Serial.println(F("Time Out!"));
|
||||
break;
|
||||
case WrongStack:
|
||||
Serial.println(F("Stack Wrong!"));
|
||||
break;
|
||||
case DFPlayerCardInserted:
|
||||
Serial.println(F("Card Inserted!"));
|
||||
break;
|
||||
case DFPlayerCardRemoved:
|
||||
Serial.println(F("Card Removed!"));
|
||||
break;
|
||||
case DFPlayerCardOnline:
|
||||
Serial.println(F("Card Online!"));
|
||||
break;
|
||||
case DFPlayerUSBInserted:
|
||||
Serial.println("USB Inserted!");
|
||||
break;
|
||||
case DFPlayerUSBRemoved:
|
||||
Serial.println("USB Removed!");
|
||||
break;
|
||||
case DFPlayerPlayFinished:
|
||||
Serial.print(F("Number:"));
|
||||
Serial.print(value);
|
||||
Serial.println(F(" Play Finished!"));
|
||||
break;
|
||||
case DFPlayerError:
|
||||
Serial.print(F("DFPlayerError:"));
|
||||
switch (value) {
|
||||
case Busy:
|
||||
Serial.println(F("Card not found"));
|
||||
break;
|
||||
case Sleeping:
|
||||
Serial.println(F("Sleeping"));
|
||||
break;
|
||||
case SerialWrongStack:
|
||||
Serial.println(F("Get Wrong Stack"));
|
||||
break;
|
||||
case CheckSumNotMatch:
|
||||
Serial.println(F("Check Sum Not Match"));
|
||||
break;
|
||||
case FileIndexOut:
|
||||
Serial.println(F("File Index Out of Bound"));
|
||||
break;
|
||||
case FileMismatch:
|
||||
Serial.println(F("Cannot Find File"));
|
||||
break;
|
||||
case Advertise:
|
||||
Serial.println(F("In Advertise"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
name=DFRobotDFPlayerMini
|
||||
version=1.0.5
|
||||
author=DFRobot
|
||||
maintainer=Angelo <angelo.qiao@dfrobot.com>
|
||||
sentence=Driver for DFPlayer Mini from DFRobot
|
||||
paragraph=Easy-to-use and reliable library for DFPlayer Mini
|
||||
category=Device Control
|
||||
url=https://github.com/DFRobot/DFRobotDFPlayerMini
|
||||
architectures=*
|
||||
Reference in New Issue
Block a user