找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6206|回复: 3
打印 上一主题 下一主题
收起左侧

BY8001语音模块ARDUINO范例程序

[复制链接]
跳转到指定楼层
楼主
BY8001语音模块ARDUINO范例
一共四个例程

单片机源程序如下:
  1. // BY8001.cpp, BY8001 Mp3 Player Function Library
  2. // Date: June 2015
  3. // Author: borland of Arduino forum
  4. // No affiliation with Balway Electronics Technology or Shenzhen Electronics Technology Ltd.
  5. // Released in public domain

  6. #include "Arduino.h"
  7. #include "BY8001.h"

  8. BY8001::BY8001() {
  9.    sendBuffer[0] = 0x7E;
  10.    sendBuffer[6] = 0xEF;
  11. }

  12. void BY8001::setup(Stream& serial){
  13.    mp3Serial = &serial;
  14. }

  15. void BY8001::sendCommand(byte cmd){
  16.    sendBuffer[1] = 0x03; // length
  17.    sendBuffer[2] = cmd;
  18.    fillCheckCode();
  19.    sendBuffer[4] = 0xEF;
  20.    txFunc();
  21. }

  22. void BY8001::sendCommand(byte cmd, byte pmtr){
  23.    sendBuffer[1] = 0x04;  // length
  24.    sendBuffer[2] = cmd;
  25.    sendBuffer[3] = pmtr;
  26.    fillCheckCode();
  27.    sendBuffer[5] = 0xEF;
  28.    txFunc();
  29. }

  30. void BY8001::sendCommand(byte cmd, word pmtr){
  31.    sendBuffer[1] = 0x05;  // length
  32.    sendBuffer[2] = cmd;
  33.    sendBuffer[3] = highByte(pmtr); sendBuffer[4] = lowByte(pmtr);
  34.    fillCheckCode();
  35.    txFunc();
  36. }

  37. void BY8001::sendCommand(byte cmd, byte pmtr1, byte pmtr2){
  38.    sendBuffer[1] = 0x05;  // length
  39.    sendBuffer[2] = cmd;
  40.    sendBuffer[3] = pmtr1; sendBuffer[4] = pmtr2;
  41.    fillCheckCode();
  42.    txFunc();
  43. }

  44. void BY8001::fillCheckCode(){
  45.   byte checkCode = getCheckCode();
  46.   sendBuffer[(int)sendBuffer[1]] = checkCode;
  47. }

  48. byte BY8001::getCheckCode(){
  49.   byte count = 0;
  50.   int j = sendBuffer[1];
  51.   for(int i=1; i<j; i++){
  52.    count = count ^ sendBuffer[i]; // XOR checksum or check code
  53.   }
  54.   return count;
  55. }

  56. void BY8001::txFunc(){
  57.   while ( mp3Serial->available() >0 ) mp3Serial->read();
  58.   mp3Serial->write(sendBuffer, (int)sendBuffer[1] + 2);
  59. }

  60. void BY8001::rxFunc(int length){
  61.   // delay 130ms
  62.   unsigned long mTimer = millis(); while (millis() - mTimer < 130) {}
  63.   mp3Serial->readBytes(receiveBuffer, length);
  64. }

  65. word BY8001::getReply(int length, int offset){
  66.   rxFunc(length);
  67.   word value = strtoul(receiveBuffer + offset, NULL, 16);
  68.   return value;
  69. }

  70. // User Command Functions:
  71. void BY8001::play(){
  72.   sendCommand(0x01);
  73. }

  74. void BY8001::pause(){
  75.   sendCommand(0x02);
  76. }

  77. void BY8001::nextTrack(){
  78.   sendCommand(0x03);
  79. }

  80. void BY8001::previousTrack(){
  81.   sendCommand(0x04);
  82. }

  83. void BY8001::increaseVolume(){
  84.   sendCommand(0x05);
  85. }

  86. void BY8001::decreaseVolume(){
  87.   sendCommand(0x06);
  88. }

  89. void BY8001::toggleStandbyMode(){
  90.   sendCommand(0x07);
  91. }

  92. void BY8001::resetModule(){
  93.   sendCommand(0x09);
  94. }

  95. void BY8001::fastForward(){
  96.   sendCommand(0x0A);
  97. }

  98. void BY8001::fastRewind(){
  99.   sendCommand(0x0B);
  100. }

  101. void BY8001::stopPlayback(){
  102.   sendCommand(0x0E);
  103. }

  104. void BY8001::setVolume(int volume){
  105.   sendCommand(0x31, (byte) volume);
  106.   // delay 100ms
  107.   unsigned long mTimer = millis(); while (millis() - mTimer < 100) {}
  108. }
  109. void BY8001::setEqualizerProfile(int equalizerProfile){
  110.   sendCommand(0x32,(byte) equalizerProfile );
  111.   // delay 100ms
  112.   unsigned long mTimer = millis(); while (millis() - mTimer < 100) {}
  113. }

  114. void BY8001::setLoopPlaybackMode(int loopPlaybackMode){
  115.   sendCommand(0x33,(byte) loopPlaybackMode );
  116.   // delay 100ms
  117.   unsigned long mTimer = millis(); while (millis() - mTimer < 100) {}
  118. }

  119. void BY8001::switchFolder(int swDirection){
  120.   sendCommand(0x34, (byte) swDirection);
  121. }

  122. void BY8001::switchDevice(int device){
  123.   sendCommand(0x35,(byte) device );
  124. }

  125. void BY8001::playTrackByIndexNumber(word trackNumber){
  126.   sendCommand(0x41, trackNumber);
  127. }

  128. void BY8001::playTrackFromFolder(int folderNumber, int trackNumber){
  129. sendCommand(0x42, (byte) folderNumber, (byte) trackNumber);
  130. }

  131. void BY8001::playInsertionSpot(int trackNumber){
  132. sendCommand(0x43, (byte) trackNumber);
  133. }

  134. void BY8001::playInsertionFromFolder(int folderNumber, int trackNumber){
  135. sendCommand(0x44, (byte) folderNumber, (byte) trackNumber);
  136. }

  137. // User Query Commands:
  138. byte BY8001::getPlaybackStatus(){
  139.   sendCommand(0x10);
  140.   byte currentStatus = getReply(8,0);
  141.   return currentStatus;
  142. }

  143. byte BY8001::getVolumeSetting(){
  144.   sendCommand(0x11);
  145.   byte volume = getReply(8,0);
  146.   return volume;
  147. }

  148. byte BY8001::getEqualizerProfile(){
  149.   sendCommand(0x12);
  150.   word profile = getReply(8,0);
  151.   return (byte)profile;
  152. }

  153. byte BY8001::getLoopPlaybackMode(){
  154.   sendCommand(0x13);
  155.   byte loopPlaybackMode = getReply(8,0);
  156.   return loopPlaybackMode;
  157. }

  158. char* BY8001::getFirmwareVersion(){
  159.   sendCommand(0x14);
  160.   rxFunc(4);
  161.   String versionReturned = receiveBuffer;
  162.   char * fwVersion = new char[5];
  163.   versionReturned = versionReturned.substring(0,4);
  164.   versionReturned.toCharArray(fwVersion,5);
  165.   return fwVersion;
  166. }

  167. word BY8001::getNumberOfTracksTF(){
  168.   sendCommand(0x15);
  169.   word numberOfTracksTF = getReply(8,0);
  170.   return numberOfTracksTF;
  171. }

  172. word BY8001::getNumberOfTracksUSB(){
  173.   sendCommand(0x16);
  174.   word numberOfTracksUSB = getReply(8,0);
  175.   return numberOfTracksUSB;
  176. }

  177. byte BY8001::getCurrentPlaybackDevice(){
  178.   sendCommand(0x18);
  179.   byte device = getReply(8,0);
  180.   return device;
  181. }

  182. word BY8001::getCurrentTrackTF(){
  183.   sendCommand(0x19);
  184.   word currentTrackTF = getReply(8,0);
  185.   return currentTrackTF;
  186. }

  187. word BY8001::getCurrentTrackUSB(){
  188.   sendCommand(0x1A);
  189.   word currentTrackUSB = getReply(8,0);
  190.   return currentTrackUSB;
  191. }

  192. word BY8001::getElapsedTrackPlaybackTime(){
  193.   sendCommand(0x1C);
  194.   word elapsedTrackPlaybackTime = getReply(8,2);
  195.   return elapsedTrackPlaybackTime;
  196. }

  197. word BY8001::getTotalTrackPlaybackTime(){
  198.   sendCommand(0x1D);
  199.   word totalTrackPlaybackTime = getReply(8,2);
  200.   return totalTrackPlaybackTime;
  201. }

  202. char* BY8001::getFileNameCurrentTrack(){
  203.   // delay 130ms
  204.   unsigned long mTimer = millis(); while (millis() - mTimer < 130) {}
  205.   sendCommand(0x1E);
  206.   rxFunc(15);
  207.   String nameReturned = receiveBuffer;
  208.   char * name = new char[10];
  209.   nameReturned = nameReturned.substring(0,15);
  210.   if (nameReturned.endsWith("\r\n") == true) {
  211.     nameReturned = nameReturned.substring(2,10);
  212.   }
  213.   else {
  214.     nameReturned = nameReturned.substring(0,8);
  215.   }
  216.   nameReturned.toCharArray(name,9);
  217.   return name;
  218. }

  219. word BY8001::getNumberOfTracksCurrentFolder(){
  220. sendCommand(0x1F);
  221. word numberTracksCurrentFolder = getReply(8,2);
  222. return numberTracksCurrentFolder;
  223. }
复制代码

所有资料51hei提供下载:
BY8001.rar (17.5 KB, 下载次数: 61)



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:230841 发表于 2020-7-30 14:46 | 只看该作者
谢谢分享!这是个好东西,刚好现在用得到!
回复

使用道具 举报

板凳
ID:968978 发表于 2021-12-13 20:17 | 只看该作者
谢谢楼主了,也在找呢,终于找到了,非常感谢了。
回复

使用道具 举报

地板
ID:138707 发表于 2023-7-29 18:58 | 只看该作者
谢谢楼主了,也在找呢,终于找到了,非常感谢了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表