xdrv_14_mp3.ino 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. xdrv_14_mp3.ino - MP3 support for Sonoff-Tasmota
  3. Copyright (C) 2018 gemu2015, mike2nl and Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. --------------------------------------------------------------------------------------------
  15. Version yyyymmdd Action Description
  16. --------------------------------------------------------------------------------------------
  17. 1.0.0.4 20181003 added - MP3Reset command in case that the player do rare things
  18. - and needs a reset, the default volume will be set again too
  19. added - MP3_CMD_RESET_VALUE for the player reset function
  20. cleaned - some comments and added function text header
  21. fixed - missing void's in function calls
  22. added - MP3_CMD_DAC command to switch off/on the dac outputs
  23. tested - works with MP3Device 1 = USB STick, or MP3Device 2 = SD-Card
  24. - after power and/or reset the SD-Card(2) is the default device
  25. - DAC looks working too on a headset. Had no amplifier for test
  26. ---
  27. 1.0.0.3 20180915 added - select device for SD-Card or USB Stick, default will be SD-Card
  28. tested - works by MP3Device 1 = USB STick, or MP3Device 2 = SD-Card
  29. - after power and/or reset the SD-Card(2) is the default device
  30. ---
  31. 1.0.0.2 20180912 added - again some if-commands to switch() because of new commands
  32. ---
  33. 1.0.0.1 20180911 added - command eq (equalizer 0..5)
  34. tested - works in console with MP3EQ 1, the value can be 0..5
  35. added - USB device selection via command in console
  36. tested - looks like it is working
  37. erased - code for USB device about some errors, will be added in a next release
  38. ---
  39. 1.0.0.1 20180910 changed - command real MP3Stop in place of pause/stop used in the original version
  40. changed - the command MP3Play e.g. 001 to MP3Track e.g. 001,
  41. added - new normal command MP3Play and MP3Pause
  42. ---
  43. 1.0.0.0 20180907 merged - by arendst
  44. changed - the driver name from xdrv_91_mp3.ino to xdrv_14_mp3.ino
  45. ---
  46. 0.9.0.3 20180906 request - Pull Request
  47. changed - if-commands to switch() for faster response
  48. ---
  49. 0.9.0.2 20180906 cleaned - source code for faster reading
  50. ---
  51. 0.9.0.1 20180905 added - #include <TasmotaSerial.h> because compiler error (Arduino IDE v1.8.5)
  52. ---
  53. 0.9.0.0 20180901 started - further development by mike2nl - https://github.com/mike2nl/Sonoff-Tasmota
  54. base - code base from gemu2015 ;-) - https://github.com/gemu2015/Sonoff-Tasmota
  55. forked - from arendst/tasmota - https://github.com/arendst/Sonoff-Tasmota
  56. */
  57. #ifdef USE_MP3_PLAYER
  58. /*********************************************************************************************\
  59. * MP3 control for RB-DFR-562 DFRobot mini MP3 player
  60. * https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299
  61. \*********************************************************************************************/
  62. #define XDRV_14 14
  63. #include <TasmotaSerial.h>
  64. TasmotaSerial *MP3Player;
  65. /*********************************************************************************************\
  66. * constants
  67. \*********************************************************************************************/
  68. #define D_CMND_MP3 "MP3"
  69. const char S_JSON_MP3_COMMAND_NVALUE[] PROGMEM = "{\"" D_CMND_MP3 "%s\":%d}";
  70. const char S_JSON_MP3_COMMAND[] PROGMEM = "{\"" D_CMND_MP3 "%s\"}";
  71. const char kMP3_Commands[] PROGMEM = "Track|Play|Pause|Stop|Volume|EQ|Device|Reset|DAC";
  72. /*********************************************************************************************\
  73. * enumerationsines
  74. \*********************************************************************************************/
  75. enum MP3_Commands { // commands useable in console or rules
  76. CMND_MP3_TRACK, // MP3Track 001...255
  77. CMND_MP3_PLAY, // MP3Play, after pause or normal start to play
  78. CMND_MP3_PAUSE, // MP3Pause
  79. CMND_MP3_STOP, // MP3Stop, real stop, original version was pause function
  80. CMND_MP3_VOLUME, // MP3Volume 0..100
  81. CMND_MP3_EQ, // MP3EQ 0..5
  82. CMND_MP3_DEVICE, // sd-card: 02, usb-stick: 01
  83. CMND_MP3_RESET, // MP3Reset, a fresh and default restart
  84. CMND_MP3_DAC }; // set dac, 1=off, 0=on, DAC is turned on (0) by default
  85. /*********************************************************************************************\
  86. * command defines
  87. \*********************************************************************************************/
  88. #define MP3_CMD_RESET_VALUE 0 // mp3 reset command value
  89. // player commands
  90. #define MP3_CMD_TRACK 0x03 // specify playback of a track, e.g. MP3Track 003
  91. #define MP3_CMD_PLAY 0x0d // Play, works as a normal play on a real MP3 Player, starts at 001.mp3 file on the selected device
  92. #define MP3_CMD_PAUSE 0x0e // Pause, was original designed as stop, see data sheet
  93. #define MP3_CMD_STOP 0x16 // Stop, it's a real stop now, in the original version it was a pause command
  94. #define MP3_CMD_VOLUME 0x06 // specifies the volume and means a console input as 0..100
  95. #define MP3_CMD_EQ 0x07 // specify EQ(0/1/2/3/4/5), 0:Normal, 1:Pop, 2:Rock, 3:Jazz, 4:Classic, 5:Bass
  96. #define MP3_CMD_DEVICE 0x09 // specify playback device, USB=1, SD-Card=2, default is 2 also after reset or power down/up
  97. #define MP3_CMD_RESET 0x0C // send a reset command to start fresh
  98. #define MP3_CMD_DAC 0x1A // activate or deactivate the DAC output for an external amplifier, DAC is turned on by default
  99. /*********************************************************************************************\
  100. * calculate the checksum
  101. * starts with cmd[1] with a length of 6 bytes
  102. \*********************************************************************************************/
  103. uint16_t MP3_Checksum(uint8_t *array)
  104. {
  105. uint16_t checksum = 0;
  106. for (uint8_t i = 0; i < 6; i++) {
  107. checksum += array[i];
  108. }
  109. checksum = checksum^0xffff;
  110. return (checksum+1);
  111. }
  112. /*********************************************************************************************\
  113. * init player
  114. * define serial tx port fixed with 9600 baud
  115. \*********************************************************************************************/
  116. void MP3PlayerInit(void) {
  117. MP3Player = new TasmotaSerial(-1, pin[GPIO_MP3_DFR562]);
  118. // start serial communication fixed to 9600 baud
  119. if (MP3Player->begin(9600)) {
  120. MP3Player->flush();
  121. delay(1000);
  122. MP3_CMD(MP3_CMD_RESET, MP3_CMD_RESET_VALUE); // reset the player to defaults
  123. delay(3000);
  124. MP3_CMD(MP3_CMD_VOLUME, MP3_VOLUME); // after reset set volume depending on the entry in the my_user_config.h
  125. }
  126. return;
  127. }
  128. /*********************************************************************************************\
  129. * create the MP3 commands payload, and send it via serial interface to the MP3 player
  130. * data length is 6 = 6 bytes [FF 06 09 00 00 00] but not counting the start, end, and verification.
  131. * {start byte, version, length, command, feedback, para MSB, para LSB, chks MSB, chks LSB, end byte};
  132. * {cmd[0] , cmd[1] , cmd[2], cmd[3] , cmd[4] , cmd[5] , cmd[6] , cmd[7] , cmd[8] , cmd[9] };
  133. * {0x7e , 0xff , 6 , 0 , 0/1 , 0 , 0 , 0 , 0 , 0xef };
  134. \*********************************************************************************************/
  135. void MP3_CMD(uint8_t mp3cmd,uint16_t val) {
  136. uint8_t i = 0;
  137. uint8_t cmd[10] = {0x7e,0xff,6,0,0,0,0,0,0,0xef}; // fill array
  138. cmd[3] = mp3cmd; // mp3 command value
  139. cmd[4] = 0; // feedback, 1=yes, 0=no, yet not use
  140. cmd[5] = val>>8; // data value, shift 8 byte right
  141. cmd[6] = val; // data value low byte
  142. uint16_t chks = MP3_Checksum(&cmd[1]); // see calculate the checksum
  143. cmd[7] = chks>>8; // checksum. shift 8 byte right
  144. cmd[8] = chks; // checksum low byte
  145. MP3Player->write(cmd, sizeof(cmd)); // write mp3 data array to player
  146. delay(1000);
  147. if (mp3cmd == MP3_CMD_RESET) {
  148. MP3_CMD(MP3_CMD_VOLUME, MP3_VOLUME); // after reset set volume depending on the entry in the my_user_config.h
  149. }
  150. return;
  151. }
  152. /*********************************************************************************************\
  153. * check the MP3 commands
  154. \*********************************************************************************************/
  155. boolean MP3PlayerCmd(void) {
  156. char command[CMDSZ];
  157. boolean serviced = true;
  158. uint8_t disp_len = strlen(D_CMND_MP3);
  159. if (!strncasecmp_P(XdrvMailbox.topic, PSTR(D_CMND_MP3), disp_len)) { // prefix
  160. int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic + disp_len, kMP3_Commands);
  161. switch (command_code) {
  162. case CMND_MP3_TRACK:
  163. case CMND_MP3_VOLUME:
  164. case CMND_MP3_EQ:
  165. case CMND_MP3_DEVICE:
  166. case CMND_MP3_DAC:
  167. // play a track, set volume, select EQ, sepcify file device
  168. if (XdrvMailbox.data_len > 0) {
  169. if (command_code == CMND_MP3_TRACK) { MP3_CMD(MP3_CMD_TRACK, XdrvMailbox.payload); }
  170. if (command_code == CMND_MP3_VOLUME) { MP3_CMD(MP3_CMD_VOLUME, XdrvMailbox.payload * 30 / 100); }
  171. if (command_code == CMND_MP3_EQ) { MP3_CMD(MP3_CMD_EQ, XdrvMailbox.payload); }
  172. if (command_code == CMND_MP3_DEVICE) { MP3_CMD(MP3_CMD_DEVICE, XdrvMailbox.payload); }
  173. if (command_code == CMND_MP3_DAC) { MP3_CMD(MP3_CMD_DAC, XdrvMailbox.payload); }
  174. }
  175. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_MP3_COMMAND_NVALUE, command, XdrvMailbox.payload);
  176. break;
  177. case CMND_MP3_PLAY:
  178. case CMND_MP3_PAUSE:
  179. case CMND_MP3_STOP:
  180. case CMND_MP3_RESET:
  181. // play or re-play after pause, pause, stop,
  182. if (command_code == CMND_MP3_PLAY) { MP3_CMD(MP3_CMD_PLAY, 0); }
  183. if (command_code == CMND_MP3_PAUSE) { MP3_CMD(MP3_CMD_PAUSE, 0); }
  184. if (command_code == CMND_MP3_STOP) { MP3_CMD(MP3_CMD_STOP, 0); }
  185. if (command_code == CMND_MP3_RESET) { MP3_CMD(MP3_CMD_RESET, 0); }
  186. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_MP3_COMMAND, command, XdrvMailbox.payload);
  187. break;
  188. default:
  189. // else for Unknown command
  190. serviced = false;
  191. break;
  192. }
  193. }
  194. return serviced;
  195. }
  196. /*********************************************************************************************\
  197. * Interface
  198. \*********************************************************************************************/
  199. boolean Xdrv14(byte function)
  200. {
  201. boolean result = false;
  202. if (pin[GPIO_MP3_DFR562] < 99) {
  203. switch (function) {
  204. case FUNC_PRE_INIT:
  205. MP3PlayerInit(); // init and start communication
  206. break;
  207. case FUNC_COMMAND:
  208. result = MP3PlayerCmd(); // return result from mp3 player command
  209. break;
  210. }
  211. }
  212. return result;
  213. }
  214. #endif // USE_MP3_PLAYER