Shimon AJISAKA / Lib_DFPlayerMini

Dependents:   InvertedPendulum2017 Lib_DFPlayerMini

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Lib_DFPlayerMini.cpp Source File

Lib_DFPlayerMini.cpp

00001 #include "Lib_DFPlayerMini.h"
00002 
00003 // constructor
00004 DFPlayerMini::DFPlayerMini( PinName busy, PinName tx, PinName rx ) : _busy(busy), _serial( tx, rx ){
00005     _func = NULL;
00006     return;
00007 }
00008 
00009 // destructor
00010 DFPlayerMini::~DFPlayerMini(){
00011     return;    
00012 }
00013 
00014 // begin
00015 void DFPlayerMini::begin(){
00016     _busy.mode( PullUp );    
00017     _serial.baud(9600);
00018     _serial.format( 8, RawSerial::None, 1 );
00019     return;   
00020 }
00021 
00022 // attach busy interrupt
00023 void DFPlayerMini::attachBusyInterrupt( BUSYFUNCPTR func ){
00024     _func   =   func;
00025     if( _func != NULL ){
00026         _busy.fall( _func );
00027     }
00028     return;
00029 }
00030 
00031 // play next track
00032 void DFPlayerMini::playNext(){
00033     _send_command( DFPLAYER_NEXT, 0x00, 0x00 );
00034     return;
00035 }
00036 
00037 // play previous track
00038 void DFPlayerMini::playPrev(){
00039     _send_command( DFPLAYER_PREV, 0x00, 0x00 );
00040     return;
00041 }
00042 
00043 // play previous track
00044 void DFPlayerMini::playNumber( uint16_t num ){
00045     _send_command( 
00046         DFPLAYER_NUM, 
00047         (uint8_t)( ( num >> 8 ) & 0xFF ), 
00048         (uint8_t)( ( num >> 0 ) & 0xFF ) 
00049     );
00050     return;
00051 }
00052 
00053 // play previous track
00054 void DFPlayerMini::playFolder( uint8_t folder, uint8_t num ){
00055     _send_command( DFPLAYER_PLAYBACKFOLDER, folder, num );
00056     return;
00057 }
00058 
00059 // increase volume
00060 void DFPlayerMini::volumePlus(){
00061     _send_command( DFPLAYER_VOL_PLUS, 0x00, 0x00 ); 
00062     return;
00063 }
00064 
00065 // decrease volume
00066 void DFPlayerMini::volumeMinus(){
00067     _send_command( DFPLAYER_VOL_MINUS, 0x00, 0x00 );    
00068     return;
00069 }
00070 
00071 // set volume
00072 void DFPlayerMini::volumeSet( uint8_t vol ){
00073     vol = ( vol > 30 ) ? 30 : vol ;
00074     _send_command( DFPLAYER_VOL_SET, 0x00, vol );
00075     return;
00076 }
00077 
00078 // send command
00079 void DFPlayerMini::_send_command( DFPLAYERMINICOM com, uint8_t param1, uint8_t param2 ){
00080     uint8_t buf[8];
00081     
00082     buf[0]  =   DFPLAYER_STX;
00083     buf[1]  =   DFPLAYER_VER;
00084     buf[2]  =   0x06;
00085     buf[3]  =   com;
00086     buf[4]  =   0x00;
00087     buf[5]  =   param1;
00088     buf[6]  =   param2;
00089     buf[7]  =   DFPLAYER_ETX;
00090     
00091     _serial.write( buf, 8 );
00092 
00093     return;    
00094 }