Dmitry Bursov / DFPlayerMini

Dependents:   pinball pinball-sensZero

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DFPlayerMini.cpp Source File

DFPlayerMini.cpp

00001 /*******************************************************************************
00002  * Copyright (C) 2014 DFRobot                              *
00003  *                                         *
00004  * DFPlayer_Mini_Mp3, This library provides a quite complete function for      * 
00005  * DFPlayer mini mp3 module.                                                   *
00006  * www.github.com/dfrobot/DFPlayer_Mini_Mp3 (github as default source provider)*
00007  *  DFRobot-A great source for opensource hardware and robot.                  *
00008  *                                                                             *
00009  * This file is part of the DFplayer_Mini_Mp3 library.                         *
00010  *                                                                             *
00011  * DFPlayer_Mini_Mp3 is free software: you can redistribute it and/or          *
00012  * modify it under the terms of the GNU Lesser General Public License as       *
00013  * published by the Free Software Foundation, either version 3 of              *
00014  * the License, or any later version.                                          *
00015  *                                                                             *
00016  * DFPlayer_Mini_Mp3 is distributed in the hope that it will be useful,        *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00019  * GNU Lesser General Public License for more details.                         *
00020  *                                                                             *
00021  * DFPlayer_Mini_Mp3 is distributed in the hope that it will be useful,        *
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00024  * GNU Lesser General Public License for more details.                         *
00025  *                                                                             *
00026  * You should have received a copy of the GNU Lesser General Public            *
00027  * License along with DFPlayer_Mini_Mp3. If not, see                           *
00028  * <http://www.gnu.org/licenses/>.                                             *
00029  ******************************************************************************/
00030  
00031 // ===========================================================================
00032 // DFPlayerMini.cpp
00033 // Nov 23 2016, kysiki
00034 // ===========================================================================
00035 // Just a simple library for DFPlayer Mini porting from DFPlayer library V2.0.
00036 // (https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299)
00037 
00038 #include "DFPlayerMini.h"
00039 #include "mbed.h"
00040 
00041 DFPlayerMini::DFPlayerMini(PinName txPin, PinName rxPin)
00042 : 
00043     mp3(txPin, rxPin)
00044 {
00045     uint8_t tmp[10] = {0x7E, 0xFF, 06, 00, 00, 00, 00, 00, 00, 0xEF};
00046     memcpy(send_buf, tmp, 10);
00047     is_reply = 0;
00048     mp3.format(8,Serial::None,1);
00049     mp3.baud(9600);
00050 }
00051 
00052 void DFPlayerMini::mp3_set_reply (uint8_t state) {
00053     is_reply = state;
00054     send_buf[4] = is_reply;
00055 }
00056 
00057 void DFPlayerMini::fill_uint16_bigend (uint8_t *thebuf, uint16_t data) {
00058     *thebuf =   (uint8_t)(data>>8);
00059     *(thebuf+1) =   (uint8_t)data;
00060 }
00061 
00062 //calc checksum (1~6 byte)
00063 uint16_t DFPlayerMini::mp3_get_checksum (uint8_t *thebuf) {
00064     uint16_t sum = 0;
00065     for (int i=1; i<7; i++) {
00066         sum += thebuf[i];
00067     }
00068     return -sum;
00069 }
00070 
00071 //fill checksum to send_buf (7~8 byte)
00072 void DFPlayerMini::mp3_fill_checksum () {
00073     uint16_t checksum = mp3_get_checksum (send_buf);
00074     fill_uint16_bigend (send_buf+7, checksum);
00075 }
00076 
00077 void DFPlayerMini::send_func () {
00078     int i;
00079     for (i = 0; i < 10; i++) {
00080         mp3.putc(send_buf[i]);
00081     } 
00082 }
00083 
00084 void DFPlayerMini::mp3_send_cmd (uint8_t cmd, uint16_t arg) {
00085     send_buf[3] = cmd;
00086     fill_uint16_bigend ((send_buf+5), arg);
00087     mp3_fill_checksum ();
00088     send_func ();
00089 }
00090 
00091 void DFPlayerMini::mp3_send_cmd (uint8_t cmd) {
00092     send_buf[3] = cmd;
00093     fill_uint16_bigend ((send_buf+5), 0);
00094     mp3_fill_checksum ();
00095     send_func ();
00096 }
00097 
00098 
00099 void DFPlayerMini::mp3_play_physical (uint16_t num) {
00100     mp3_send_cmd (0x03, num);
00101 }
00102 
00103 void DFPlayerMini::mp3_play_physical () {
00104     mp3_send_cmd (0x03);
00105 }
00106 
00107 void DFPlayerMini::mp3_next () {
00108     mp3_send_cmd (0x01);
00109 }
00110 
00111 void DFPlayerMini::mp3_prev () {
00112     mp3_send_cmd (0x02);
00113 }
00114 
00115 //0x06 set volume 0-30
00116 void DFPlayerMini::mp3_set_volume (uint16_t volume) {
00117     mp3_send_cmd (0x06, volume);
00118 }
00119 
00120 //0x07 set EQ0/1/2/3/4/5    Normal/Pop/Rock/Jazz/Classic/Bass
00121 void DFPlayerMini::mp3_set_EQ (uint16_t eq) {
00122     mp3_send_cmd (0x07, eq);
00123 }
00124 
00125 //0x09 set device 1/2/3/4/5 U/SD/AUX/SLEEP/FLASH
00126 void DFPlayerMini::mp3_set_device (uint16_t device) {
00127     mp3_send_cmd (0x09, device);
00128 }
00129 
00130 void DFPlayerMini::mp3_sleep () {
00131     mp3_send_cmd (0x0a);
00132 }
00133 
00134 void DFPlayerMini::mp3_reset () {
00135     mp3_send_cmd (0x0c);
00136 }
00137 
00138 void DFPlayerMini::mp3_play () {
00139     mp3_send_cmd (0x0d);
00140 }
00141 
00142 void DFPlayerMini::mp3_pause () {
00143     mp3_send_cmd (0x0e);
00144 }
00145 
00146 void DFPlayerMini::mp3_stop () {
00147     mp3_send_cmd (0x16);
00148 }
00149 
00150 // play mp3 file in mp3 folder in your tf card
00151 void DFPlayerMini::mp3_play (uint16_t num) {
00152     mp3_send_cmd (0x12, num);
00153 }
00154 
00155 void DFPlayerMini::mp3_get_state () {
00156     mp3_send_cmd (0x42);
00157 }
00158 
00159 void DFPlayerMini::mp3_get_volume () {
00160     mp3_send_cmd (0x43);
00161 }
00162 
00163 void DFPlayerMini::mp3_get_u_sum () {
00164     mp3_send_cmd (0x47);
00165 }
00166 
00167 void DFPlayerMini::mp3_get_tf_sum () {
00168     mp3_send_cmd (0x48);
00169 }
00170 
00171 void DFPlayerMini::mp3_get_flash_sum () {
00172     mp3_send_cmd (0x49);
00173 }
00174 
00175 void DFPlayerMini::mp3_get_tf_current () {
00176     mp3_send_cmd (0x4c);
00177 }
00178 
00179 void DFPlayerMini::mp3_get_u_current () {
00180     mp3_send_cmd (0x4b);
00181 }
00182 
00183 
00184 //
00185 void DFPlayerMini::mp3_get_flash_current () {
00186     mp3_send_cmd (0x4d);
00187 }
00188 
00189 void DFPlayerMini::mp3_single_loop (uint8_t state) {
00190     mp3_send_cmd (0x19, !state);
00191 }
00192 
00193 void DFPlayerMini::mp3_single_play (uint16_t num) {
00194     mp3_play (num);
00195     wait_ms (10);
00196     mp3_single_loop (true); 
00197 }
00198 
00199 void DFPlayerMini::mp3_DAC (uint8_t state) {
00200     mp3_send_cmd (0x1a, !state);
00201 }
00202 
00203 //
00204 void DFPlayerMini::mp3_random_play () {
00205     mp3_send_cmd (0x18);
00206 }