final groupe 1
DFPlayerMini.cpp@0:62b52a8325e3, 2016-11-22 (annotated)
- Committer:
- kysiki
- Date:
- Tue Nov 22 17:00:13 2016 +0000
- Revision:
- 0:62b52a8325e3
- Child:
- 1:3c113420b97a
DFPlayerMini?????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kysiki | 0:62b52a8325e3 | 1 | /******************************************************************************* |
kysiki | 0:62b52a8325e3 | 2 | * Copyright (C) 2014 DFRobot * |
kysiki | 0:62b52a8325e3 | 3 | * * |
kysiki | 0:62b52a8325e3 | 4 | * DFPlayer_Mini_Mp3, This library provides a quite complete function for * |
kysiki | 0:62b52a8325e3 | 5 | * DFPlayer mini mp3 module. * |
kysiki | 0:62b52a8325e3 | 6 | * www.github.com/dfrobot/DFPlayer_Mini_Mp3 (github as default source provider)* |
kysiki | 0:62b52a8325e3 | 7 | * DFRobot-A great source for opensource hardware and robot. * |
kysiki | 0:62b52a8325e3 | 8 | * * |
kysiki | 0:62b52a8325e3 | 9 | * This file is part of the DFplayer_Mini_Mp3 library. * |
kysiki | 0:62b52a8325e3 | 10 | * * |
kysiki | 0:62b52a8325e3 | 11 | * DFPlayer_Mini_Mp3 is free software: you can redistribute it and/or * |
kysiki | 0:62b52a8325e3 | 12 | * modify it under the terms of the GNU Lesser General Public License as * |
kysiki | 0:62b52a8325e3 | 13 | * published by the Free Software Foundation, either version 3 of * |
kysiki | 0:62b52a8325e3 | 14 | * the License, or any later version. * |
kysiki | 0:62b52a8325e3 | 15 | * * |
kysiki | 0:62b52a8325e3 | 16 | * DFPlayer_Mini_Mp3 is distributed in the hope that it will be useful, * |
kysiki | 0:62b52a8325e3 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
kysiki | 0:62b52a8325e3 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
kysiki | 0:62b52a8325e3 | 19 | * GNU Lesser General Public License for more details. * |
kysiki | 0:62b52a8325e3 | 20 | * * |
kysiki | 0:62b52a8325e3 | 21 | * DFPlayer_Mini_Mp3 is distributed in the hope that it will be useful, * |
kysiki | 0:62b52a8325e3 | 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
kysiki | 0:62b52a8325e3 | 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
kysiki | 0:62b52a8325e3 | 24 | * GNU Lesser General Public License for more details. * |
kysiki | 0:62b52a8325e3 | 25 | * * |
kysiki | 0:62b52a8325e3 | 26 | * You should have received a copy of the GNU Lesser General Public * |
kysiki | 0:62b52a8325e3 | 27 | * License along with DFPlayer_Mini_Mp3. If not, see * |
kysiki | 0:62b52a8325e3 | 28 | * <http://www.gnu.org/licenses/>. * |
kysiki | 0:62b52a8325e3 | 29 | ******************************************************************************/ |
kysiki | 0:62b52a8325e3 | 30 | |
kysiki | 0:62b52a8325e3 | 31 | // =========================================================================== |
kysiki | 0:62b52a8325e3 | 32 | // DFPlayerMini.cpp |
kysiki | 0:62b52a8325e3 | 33 | // Nov 23 2016, kysiki |
kysiki | 0:62b52a8325e3 | 34 | // =========================================================================== |
kysiki | 0:62b52a8325e3 | 35 | // Just a simple library for DFPlayer Mini porting from DFPlayer library V2.0. |
kysiki | 0:62b52a8325e3 | 36 | // (https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299) |
kysiki | 0:62b52a8325e3 | 37 | |
kysiki | 0:62b52a8325e3 | 38 | #include "DFPlayerMini.h" |
kysiki | 0:62b52a8325e3 | 39 | #include "mbed.h" |
kysiki | 0:62b52a8325e3 | 40 | |
kysiki | 0:62b52a8325e3 | 41 | DFPlayerMini::DFPlayerMini(PinName txPin, PinName rxPin) |
kysiki | 0:62b52a8325e3 | 42 | : |
kysiki | 0:62b52a8325e3 | 43 | mp3(txPin, rxPin) |
kysiki | 0:62b52a8325e3 | 44 | { |
kysiki | 0:62b52a8325e3 | 45 | uint8_t tmp[10] = {0x7E, 0xFF, 06, 00, 00, 00, 00, 00, 00, 0xEF}; |
kysiki | 0:62b52a8325e3 | 46 | memcpy(send_buf, tmp, 10); |
kysiki | 0:62b52a8325e3 | 47 | is_reply = 0; |
kysiki | 0:62b52a8325e3 | 48 | mp3.format(8,Serial::None,1); |
kysiki | 0:62b52a8325e3 | 49 | mp3.baud(9600); |
kysiki | 0:62b52a8325e3 | 50 | } |
kysiki | 0:62b52a8325e3 | 51 | |
kysiki | 0:62b52a8325e3 | 52 | void DFPlayerMini::mp3_set_reply (uint8_t state) { |
kysiki | 0:62b52a8325e3 | 53 | is_reply = state; |
kysiki | 0:62b52a8325e3 | 54 | send_buf[4] = is_reply; |
kysiki | 0:62b52a8325e3 | 55 | } |
kysiki | 0:62b52a8325e3 | 56 | |
kysiki | 0:62b52a8325e3 | 57 | void DFPlayerMini::fill_uint16_bigend (uint8_t *thebuf, uint16_t data) { |
kysiki | 0:62b52a8325e3 | 58 | *thebuf = (uint8_t)(data>>8); |
kysiki | 0:62b52a8325e3 | 59 | *(thebuf+1) = (uint8_t)data; |
kysiki | 0:62b52a8325e3 | 60 | } |
kysiki | 0:62b52a8325e3 | 61 | |
kysiki | 0:62b52a8325e3 | 62 | //calc checksum (1~6 byte) |
kysiki | 0:62b52a8325e3 | 63 | uint16_t DFPlayerMini::mp3_get_checksum (uint8_t *thebuf) { |
kysiki | 0:62b52a8325e3 | 64 | uint16_t sum = 0; |
kysiki | 0:62b52a8325e3 | 65 | for (int i=1; i<7; i++) { |
kysiki | 0:62b52a8325e3 | 66 | sum += thebuf[i]; |
kysiki | 0:62b52a8325e3 | 67 | } |
kysiki | 0:62b52a8325e3 | 68 | return -sum; |
kysiki | 0:62b52a8325e3 | 69 | } |
kysiki | 0:62b52a8325e3 | 70 | |
kysiki | 0:62b52a8325e3 | 71 | //fill checksum to send_buf (7~8 byte) |
kysiki | 0:62b52a8325e3 | 72 | void DFPlayerMini::mp3_fill_checksum () { |
kysiki | 0:62b52a8325e3 | 73 | uint16_t checksum = mp3_get_checksum (send_buf); |
kysiki | 0:62b52a8325e3 | 74 | fill_uint16_bigend (send_buf+7, checksum); |
kysiki | 0:62b52a8325e3 | 75 | } |
kysiki | 0:62b52a8325e3 | 76 | |
kysiki | 0:62b52a8325e3 | 77 | void DFPlayerMini::send_func () { |
kysiki | 0:62b52a8325e3 | 78 | int i; |
kysiki | 0:62b52a8325e3 | 79 | for (i = 0; i < 10; i++) { |
kysiki | 0:62b52a8325e3 | 80 | mp3.putc(send_buf[i]); |
kysiki | 0:62b52a8325e3 | 81 | } |
kysiki | 0:62b52a8325e3 | 82 | } |
kysiki | 0:62b52a8325e3 | 83 | |
kysiki | 0:62b52a8325e3 | 84 | void DFPlayerMini::mp3_send_cmd (uint8_t cmd, uint16_t arg) { |
kysiki | 0:62b52a8325e3 | 85 | send_buf[3] = cmd; |
kysiki | 0:62b52a8325e3 | 86 | fill_uint16_bigend ((send_buf+5), arg); |
kysiki | 0:62b52a8325e3 | 87 | mp3_fill_checksum (); |
kysiki | 0:62b52a8325e3 | 88 | send_func (); |
kysiki | 0:62b52a8325e3 | 89 | } |
kysiki | 0:62b52a8325e3 | 90 | |
kysiki | 0:62b52a8325e3 | 91 | void DFPlayerMini::mp3_send_cmd (uint8_t cmd) { |
kysiki | 0:62b52a8325e3 | 92 | send_buf[3] = cmd; |
kysiki | 0:62b52a8325e3 | 93 | fill_uint16_bigend ((send_buf+5), 0); |
kysiki | 0:62b52a8325e3 | 94 | mp3_fill_checksum (); |
kysiki | 0:62b52a8325e3 | 95 | send_func (); |
kysiki | 0:62b52a8325e3 | 96 | } |
kysiki | 0:62b52a8325e3 | 97 | |
kysiki | 0:62b52a8325e3 | 98 | |
kysiki | 0:62b52a8325e3 | 99 | void DFPlayerMini::mp3_play_physical (uint16_t num) { |
kysiki | 0:62b52a8325e3 | 100 | mp3_send_cmd (0x03, num); |
kysiki | 0:62b52a8325e3 | 101 | } |
kysiki | 0:62b52a8325e3 | 102 | |
kysiki | 0:62b52a8325e3 | 103 | void DFPlayerMini::mp3_play_physical () { |
kysiki | 0:62b52a8325e3 | 104 | mp3_send_cmd (0x03); |
kysiki | 0:62b52a8325e3 | 105 | } |
kysiki | 0:62b52a8325e3 | 106 | |
kysiki | 0:62b52a8325e3 | 107 | void DFPlayerMini::mp3_next () { |
kysiki | 0:62b52a8325e3 | 108 | mp3_send_cmd (0x01); |
kysiki | 0:62b52a8325e3 | 109 | } |
kysiki | 0:62b52a8325e3 | 110 | |
kysiki | 0:62b52a8325e3 | 111 | void DFPlayerMini::mp3_prev () { |
kysiki | 0:62b52a8325e3 | 112 | mp3_send_cmd (0x02); |
kysiki | 0:62b52a8325e3 | 113 | } |
kysiki | 0:62b52a8325e3 | 114 | |
kysiki | 0:62b52a8325e3 | 115 | //0x06 set volume 0-30 |
kysiki | 0:62b52a8325e3 | 116 | void DFPlayerMini::mp3_set_volume (uint16_t volume) { |
kysiki | 0:62b52a8325e3 | 117 | mp3_send_cmd (0x06, volume); |
kysiki | 0:62b52a8325e3 | 118 | } |
kysiki | 0:62b52a8325e3 | 119 | |
kysiki | 0:62b52a8325e3 | 120 | //0x07 set EQ0/1/2/3/4/5 Normal/Pop/Rock/Jazz/Classic/Bass |
kysiki | 0:62b52a8325e3 | 121 | void DFPlayerMini::mp3_set_EQ (uint16_t eq) { |
kysiki | 0:62b52a8325e3 | 122 | mp3_send_cmd (0x07, eq); |
kysiki | 0:62b52a8325e3 | 123 | } |
kysiki | 0:62b52a8325e3 | 124 | |
kysiki | 0:62b52a8325e3 | 125 | //0x09 set device 1/2/3/4/5 U/SD/AUX/SLEEP/FLASH |
kysiki | 0:62b52a8325e3 | 126 | void DFPlayerMini::mp3_set_device (uint16_t device) { |
kysiki | 0:62b52a8325e3 | 127 | mp3_send_cmd (0x09, device); |
kysiki | 0:62b52a8325e3 | 128 | } |
kysiki | 0:62b52a8325e3 | 129 | |
kysiki | 0:62b52a8325e3 | 130 | void DFPlayerMini::mp3_sleep () { |
kysiki | 0:62b52a8325e3 | 131 | mp3_send_cmd (0x0a); |
kysiki | 0:62b52a8325e3 | 132 | } |
kysiki | 0:62b52a8325e3 | 133 | |
kysiki | 0:62b52a8325e3 | 134 | void DFPlayerMini::mp3_reset () { |
kysiki | 0:62b52a8325e3 | 135 | mp3_send_cmd (0x0c); |
kysiki | 0:62b52a8325e3 | 136 | } |
kysiki | 0:62b52a8325e3 | 137 | |
kysiki | 0:62b52a8325e3 | 138 | void DFPlayerMini::mp3_play () { |
kysiki | 0:62b52a8325e3 | 139 | mp3_send_cmd (0x0d); |
kysiki | 0:62b52a8325e3 | 140 | } |
kysiki | 0:62b52a8325e3 | 141 | |
kysiki | 0:62b52a8325e3 | 142 | void DFPlayerMini::mp3_pause () { |
kysiki | 0:62b52a8325e3 | 143 | mp3_send_cmd (0x0e); |
kysiki | 0:62b52a8325e3 | 144 | } |
kysiki | 0:62b52a8325e3 | 145 | |
kysiki | 0:62b52a8325e3 | 146 | void DFPlayerMini::mp3_stop () { |
kysiki | 0:62b52a8325e3 | 147 | mp3_send_cmd (0x16); |
kysiki | 0:62b52a8325e3 | 148 | } |
kysiki | 0:62b52a8325e3 | 149 | |
kysiki | 0:62b52a8325e3 | 150 | // play mp3 file in mp3 folder in your tf card |
kysiki | 0:62b52a8325e3 | 151 | void DFPlayerMini::mp3_play (uint16_t num) { |
kysiki | 0:62b52a8325e3 | 152 | mp3_send_cmd (0x12, num); |
kysiki | 0:62b52a8325e3 | 153 | } |
kysiki | 0:62b52a8325e3 | 154 | |
kysiki | 0:62b52a8325e3 | 155 | void DFPlayerMini::mp3_get_state () { |
kysiki | 0:62b52a8325e3 | 156 | mp3_send_cmd (0x42); |
kysiki | 0:62b52a8325e3 | 157 | } |
kysiki | 0:62b52a8325e3 | 158 | |
kysiki | 0:62b52a8325e3 | 159 | void DFPlayerMini::mp3_get_volume () { |
kysiki | 0:62b52a8325e3 | 160 | mp3_send_cmd (0x43); |
kysiki | 0:62b52a8325e3 | 161 | } |
kysiki | 0:62b52a8325e3 | 162 | |
kysiki | 0:62b52a8325e3 | 163 | void DFPlayerMini::mp3_get_u_sum () { |
kysiki | 0:62b52a8325e3 | 164 | mp3_send_cmd (0x47); |
kysiki | 0:62b52a8325e3 | 165 | } |
kysiki | 0:62b52a8325e3 | 166 | |
kysiki | 0:62b52a8325e3 | 167 | void DFPlayerMini::mp3_get_tf_sum () { |
kysiki | 0:62b52a8325e3 | 168 | mp3_send_cmd (0x48); |
kysiki | 0:62b52a8325e3 | 169 | } |
kysiki | 0:62b52a8325e3 | 170 | |
kysiki | 0:62b52a8325e3 | 171 | void DFPlayerMini::mp3_get_flash_sum () { |
kysiki | 0:62b52a8325e3 | 172 | mp3_send_cmd (0x49); |
kysiki | 0:62b52a8325e3 | 173 | } |
kysiki | 0:62b52a8325e3 | 174 | |
kysiki | 0:62b52a8325e3 | 175 | void DFPlayerMini::mp3_get_tf_current () { |
kysiki | 0:62b52a8325e3 | 176 | mp3_send_cmd (0x4c); |
kysiki | 0:62b52a8325e3 | 177 | } |
kysiki | 0:62b52a8325e3 | 178 | |
kysiki | 0:62b52a8325e3 | 179 | void DFPlayerMini::mp3_get_u_current () { |
kysiki | 0:62b52a8325e3 | 180 | mp3_send_cmd (0x4b); |
kysiki | 0:62b52a8325e3 | 181 | } |
kysiki | 0:62b52a8325e3 | 182 | |
kysiki | 0:62b52a8325e3 | 183 | |
kysiki | 0:62b52a8325e3 | 184 | // |
kysiki | 0:62b52a8325e3 | 185 | void DFPlayerMini::mp3_get_flash_current () { |
kysiki | 0:62b52a8325e3 | 186 | mp3_send_cmd (0x4d); |
kysiki | 0:62b52a8325e3 | 187 | } |
kysiki | 0:62b52a8325e3 | 188 | |
kysiki | 0:62b52a8325e3 | 189 | void DFPlayerMini::mp3_single_loop (uint8_t state) { |
kysiki | 0:62b52a8325e3 | 190 | mp3_send_cmd (0x19, !state); |
kysiki | 0:62b52a8325e3 | 191 | } |
kysiki | 0:62b52a8325e3 | 192 | |
kysiki | 0:62b52a8325e3 | 193 | void DFPlayerMini::mp3_single_play (uint16_t num) { |
kysiki | 0:62b52a8325e3 | 194 | mp3_play (num); |
kysiki | 0:62b52a8325e3 | 195 | wait_ms (10); |
kysiki | 0:62b52a8325e3 | 196 | mp3_single_loop (true); |
kysiki | 0:62b52a8325e3 | 197 | } |
kysiki | 0:62b52a8325e3 | 198 | |
kysiki | 0:62b52a8325e3 | 199 | void DFPlayerMini::mp3_DAC (uint8_t state) { |
kysiki | 0:62b52a8325e3 | 200 | mp3_send_cmd (0x1a, !state); |
kysiki | 0:62b52a8325e3 | 201 | } |
kysiki | 0:62b52a8325e3 | 202 | |
kysiki | 0:62b52a8325e3 | 203 | // |
kysiki | 0:62b52a8325e3 | 204 | void DFPlayerMini::mp3_random_play () { |
kysiki | 0:62b52a8325e3 | 205 | mp3_send_cmd (0x18); |
kysiki | 0:62b52a8325e3 | 206 | } |