Audio

Dependencies:   Lib_DFPlayerMini

Committer:
ackerden
Date:
Wed May 05 15:24:10 2021 +0000
Revision:
3:df913b0f0d54
Parent:
2:b25892ce36c5
Audio;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bluefish 0:6e015ec7e3a7 1 #include "Lib_DFPlayerMini.h"
bluefish 0:6e015ec7e3a7 2
bluefish 1:e64636383d8c 3 // constructor
bluefish 0:6e015ec7e3a7 4 DFPlayerMini::DFPlayerMini( PinName busy, PinName tx, PinName rx ) : _busy(busy), _serial( tx, rx ){
bluefish 0:6e015ec7e3a7 5 _func = NULL;
bluefish 0:6e015ec7e3a7 6 return;
bluefish 0:6e015ec7e3a7 7 }
bluefish 0:6e015ec7e3a7 8
bluefish 1:e64636383d8c 9 // destructor
bluefish 0:6e015ec7e3a7 10 DFPlayerMini::~DFPlayerMini(){
bluefish 0:6e015ec7e3a7 11 return;
bluefish 0:6e015ec7e3a7 12 }
bluefish 0:6e015ec7e3a7 13
bluefish 1:e64636383d8c 14 // begin
bluefish 0:6e015ec7e3a7 15 void DFPlayerMini::begin(){
bluefish 0:6e015ec7e3a7 16 _busy.mode( PullUp );
ackerden 2:b25892ce36c5 17 _serial.set_baud(9600);
ackerden 2:b25892ce36c5 18 _serial.set_format( 8, BufferedSerial::None, 1 );
bluefish 0:6e015ec7e3a7 19 return;
bluefish 0:6e015ec7e3a7 20 }
bluefish 0:6e015ec7e3a7 21
bluefish 1:e64636383d8c 22 // attach busy interrupt
bluefish 0:6e015ec7e3a7 23 void DFPlayerMini::attachBusyInterrupt( BUSYFUNCPTR func ){
bluefish 0:6e015ec7e3a7 24 _func = func;
bluefish 0:6e015ec7e3a7 25 if( _func != NULL ){
bluefish 0:6e015ec7e3a7 26 _busy.fall( _func );
bluefish 0:6e015ec7e3a7 27 }
bluefish 0:6e015ec7e3a7 28 return;
bluefish 0:6e015ec7e3a7 29 }
bluefish 0:6e015ec7e3a7 30
bluefish 0:6e015ec7e3a7 31 // play next track
bluefish 0:6e015ec7e3a7 32 void DFPlayerMini::playNext(){
bluefish 0:6e015ec7e3a7 33 _send_command( DFPLAYER_NEXT, 0x00, 0x00 );
bluefish 0:6e015ec7e3a7 34 return;
bluefish 0:6e015ec7e3a7 35 }
bluefish 0:6e015ec7e3a7 36
bluefish 0:6e015ec7e3a7 37 // play previous track
bluefish 0:6e015ec7e3a7 38 void DFPlayerMini::playPrev(){
bluefish 0:6e015ec7e3a7 39 _send_command( DFPLAYER_PREV, 0x00, 0x00 );
bluefish 0:6e015ec7e3a7 40 return;
bluefish 0:6e015ec7e3a7 41 }
bluefish 0:6e015ec7e3a7 42
bluefish 0:6e015ec7e3a7 43 // play previous track
bluefish 0:6e015ec7e3a7 44 void DFPlayerMini::playNumber( uint16_t num ){
bluefish 0:6e015ec7e3a7 45 _send_command(
bluefish 0:6e015ec7e3a7 46 DFPLAYER_NUM,
bluefish 0:6e015ec7e3a7 47 (uint8_t)( ( num >> 8 ) & 0xFF ),
bluefish 0:6e015ec7e3a7 48 (uint8_t)( ( num >> 0 ) & 0xFF )
bluefish 0:6e015ec7e3a7 49 );
bluefish 0:6e015ec7e3a7 50 return;
bluefish 0:6e015ec7e3a7 51 }
bluefish 0:6e015ec7e3a7 52
bluefish 0:6e015ec7e3a7 53 // play previous track
bluefish 0:6e015ec7e3a7 54 void DFPlayerMini::playFolder( uint8_t folder, uint8_t num ){
bluefish 0:6e015ec7e3a7 55 _send_command( DFPLAYER_PLAYBACKFOLDER, folder, num );
bluefish 0:6e015ec7e3a7 56 return;
bluefish 0:6e015ec7e3a7 57 }
bluefish 0:6e015ec7e3a7 58
bluefish 0:6e015ec7e3a7 59 // increase volume
bluefish 0:6e015ec7e3a7 60 void DFPlayerMini::volumePlus(){
bluefish 0:6e015ec7e3a7 61 _send_command( DFPLAYER_VOL_PLUS, 0x00, 0x00 );
bluefish 0:6e015ec7e3a7 62 return;
bluefish 0:6e015ec7e3a7 63 }
bluefish 0:6e015ec7e3a7 64
bluefish 0:6e015ec7e3a7 65 // decrease volume
bluefish 0:6e015ec7e3a7 66 void DFPlayerMini::volumeMinus(){
bluefish 0:6e015ec7e3a7 67 _send_command( DFPLAYER_VOL_MINUS, 0x00, 0x00 );
bluefish 0:6e015ec7e3a7 68 return;
bluefish 0:6e015ec7e3a7 69 }
bluefish 0:6e015ec7e3a7 70
bluefish 1:e64636383d8c 71 // set volume
bluefish 0:6e015ec7e3a7 72 void DFPlayerMini::volumeSet( uint8_t vol ){
bluefish 0:6e015ec7e3a7 73 vol = ( vol > 30 ) ? 30 : vol ;
bluefish 0:6e015ec7e3a7 74 _send_command( DFPLAYER_VOL_SET, 0x00, vol );
bluefish 0:6e015ec7e3a7 75 return;
bluefish 0:6e015ec7e3a7 76 }
bluefish 0:6e015ec7e3a7 77
bluefish 1:e64636383d8c 78 // send command
bluefish 0:6e015ec7e3a7 79 void DFPlayerMini::_send_command( DFPLAYERMINICOM com, uint8_t param1, uint8_t param2 ){
bluefish 0:6e015ec7e3a7 80 uint8_t buf[8];
bluefish 0:6e015ec7e3a7 81
bluefish 0:6e015ec7e3a7 82 buf[0] = DFPLAYER_STX;
bluefish 0:6e015ec7e3a7 83 buf[1] = DFPLAYER_VER;
bluefish 0:6e015ec7e3a7 84 buf[2] = 0x06;
bluefish 0:6e015ec7e3a7 85 buf[3] = com;
bluefish 0:6e015ec7e3a7 86 buf[4] = 0x00;
bluefish 0:6e015ec7e3a7 87 buf[5] = param1;
bluefish 0:6e015ec7e3a7 88 buf[6] = param2;
bluefish 0:6e015ec7e3a7 89 buf[7] = DFPLAYER_ETX;
bluefish 0:6e015ec7e3a7 90
bluefish 0:6e015ec7e3a7 91 _serial.write( buf, 8 );
bluefish 0:6e015ec7e3a7 92
bluefish 0:6e015ec7e3a7 93 return;
bluefish 0:6e015ec7e3a7 94 }