Ameba_Arduino 发表于 2016-6-3 11:33:46

【Ameba Arduino】实战:用Ameba打造Wi-Fi控制的MP3播放器

本帖最后由 Ameba_Arduino 于 2016-6-3 11:33 编辑

转载申明:原文连接http://makerpro.cc/2016/04/building-wifi-control-mp3player-by-ameba/

为了让Ameba能够广泛运用,本文作者(赖桑)hack了十几年前市面上常见的MP3播放器,进一步将MP3改装成可以通过WiFi点歌的设备,这样的系统出了运用在娱乐上,也能用子啊家庭安防上,而且价格只需要几百台币(5台币等于1元RMB)就可以获取这样的模组,既简单又方便。

视频连接:
http://www.tudou.com/programs/view/ipIK84zAruU/
材料准备
§Ameba x1
§Grove-Serial MP3 Player
§Grove-LCD RGB Backlight
§MicroSD卡
§杜邦线若干

范例说明




所有模组都用Ameba的5v供电,建议在5V和GND之间,接一个电解质电容,以免有USB供电不稳,发送掉电的情况。

代码清单

#include "rgb_lcd.h"
#include "WiFiServer1.h"

#define mp3Serial1

#define PORT 5000

char ssid[] = "AndroidAP1B3F";   // the name of your network
char pwd[] = "t101419008";      //The password of your AP
int status = WL_IDLE_STATUS;   // the Wifi radio's status

WiFiServer1 server(PORT);
WiFiClient1 client;

int playIndex = 1;
int volumn = 0x0E;
int playMode = 1;

rgb_lcd lcd;

const int colorR = 0;
const int colorG = 0;
const int colorB = 128;

void setup() {
// put your setup code here, to run once:
mp3.begin(9600);
mp3.setTimeout(500);

Serial.begin(9600);
delay(100);
if (true ==SetPlayMode(0x01))
    Serial.println("Set The Play Mode to 0x01, Single Loop Mode.");
else
    Serial.println("Playmode Set Error");

PauseOnOffCurrentMusic();

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
lcd.print("Ameba MP3 player");

status = WiFi1.begin(ssid, pwd);
if ( status != WL_CONNECTED) {
    lcd.print("    No WiFi   ");
    // don't do anything else:
    while(true);
}
else {
    delay(3000);
    IPAddress ip = WiFi1.localIP();
    lcd.setCursor(0, 1);
    lcd.print("S");      //Show server IP
    lcd.print(ip);
    server.begin();
}

delay(1000);
}

char buffer;

void loop() {
// put your main code here, to run repeatedly:
// check the network connection once every 10 seconds:
// listen for incoming clients
int n;

client = server.available();

if(client)
{
    lcd.setCursor(0, 0);
    lcd.print("C               ");
    lcd.setCursor(1, 0);
    lcd.print(client.get_address());
    lcd.setRGB(128, 128, 0);

    if(client.available())
    {
      n = client.read((uint8_t*)(&buffer), sizeof(buffer));
      n = server.write(buffer, n);
      Serial.println(buffer);
      
      switch(buffer)
      {
      case 'P':
      case 'p':
          playIndex = buffer - 0x30;
          SetMusicPlay(highByte(playIndex), lowByte(playIndex));
          delay(1000);
          break;
      case 'M':
      case 'm':
          playMode = buffer - 0x30;
          SetPlayMode(playMode);
          delay(1000);
          break;
      case '+':
          volumn++;
          SetVolume(volumn);
          break;
      case '-':
          volumn--;
          SetVolume(volumn);
          break;
      }
      buffer = buffer = buffer = 0;
      
      client.stop();
    }
}
}

//Set the music index to play, the index is decided by the input sequence
//of the music;
//hbyte: the high byte of the index;
//lbyte: the low byte of the index;
boolean SetMusicPlay(uint8_t hbyte,uint8_t lbyte)
{
    mp3.write(0x7E);
    mp3.write(0x04);
    mp3.write(0xA0);
    mp3.write(hbyte);
    mp3.write(lbyte);
    mp3.write(0x7E);
    delay(10);
    while(mp3.available())
    {
      if (0xA0==mp3.read())
      return true;
      else
      return false;
    }
}
// Pause on/offthe current music
boolean PauseOnOffCurrentMusic(void)
{
    mp3.write(0x7E);
    mp3.write(0x02);
    mp3.write(0xA3);
    mp3.write(0x7E);
    delay(10);
    while(mp3.available())
    {
      if (0xA3==mp3.read())
      return true;
      else
      return false;
    }
}

//Set the volume, the range is 0x00 to 0x1F
boolean SetVolume(uint8_t volume)
{
    mp3.write(0x7E);
    Mp3.write(0x03);
    mp3.write(0xa7);
    mp3.write(volume);
    mp3.write(0x7e);
    delay(10);
    while(mp3.available())
    {
      if (0xa7==mp3.read())
      return true;
      else
      return false;
    }
}

Boolean setplaymode(uint8_t playmode)
{
    if (((playmode==0x00)|(playmode==0x01)|(playmode==0x02)|(playmode==0x03))==false)
    {
      serial.println("playmode parameter error! ");
      return false;
    }
    mp3.write(0x7e);
    mp3.write(0x03);
    mp3.write(0xa9);
    mp3.write(playmode);
    mp3.write(0x7e);
    delay(10);
    while(mp3.available())    {
      if (0xa9==mp3.read())
      return true;
      else
      return false;
    }
}


需要用到的Grove LCD RGB backlight库文件:

基本解说:
Grove LCD RGB Backlight与Ameba间,透过I2C通讯,而GroveSerial MP3 player与Ameba以serial通讯。另外,Serial MP3 player上面主要是一个WT5001的芯片,这颗芯片就是当年MP3风行一时的主流,常被用于MP3的主芯片,同时也具有SPI及SD卡存取功能。
程序一开以serial1来跟MP3 player建立通讯,并且以protocol跟WT5001通讯。系统会先以single mode的方式发出SD卡上的一首歌曲。当系统登入WiFi后,会在LCD上显示当前取得的IP,目前程序固定port为5000.当使用者通过像是Telnet等终端登录Ameba之后,就可以舒服对应的资源,字串,控制MP3的歌曲播出效果。
结语
透过无线通讯控制音效发出,这样的功能本身就常见于目前日常生活的娱乐设备上,这次运用Ameba打造的WiFi控制MP3,只要所录制的MP3档案具有警示音效,就能再出门的时候当做警报器,甚至用于电话上的语音留言的发出。
而Amabe的下一代版本,将会特别强调影音串流管理效果,事实上,由于Ameba是以ARM Cortex系列为基础进行开发的,这样的效果是可以办到的。我个人非常期待新一代具有影音串流管理的Ameba,到时候再写文档与大家分享。

关于更多介绍请访问官方主页:http://ameba.realsil.com.cn/
QQ交流群:Ameba Arduino 技术交流群(184666894)
论坛技术贴:
【新品推荐】Ameba Arduino全功能IoT解决方案
【Ameba Arduino】入手教程之一:开发环境搭建
【Ameba Arduino】入门教程之二:5分钟玩转小车无线控制
【Ameba Arduino】入门教程之三: Wi-Fi之初体验
【Ameba Arduino】入门教程之四:Ameba WebServer LED 控制
【Ameba Arduino】入门教程之五:给无线小车添加“眼睛”
【Ameba Arduino】入门教程之六:无线小车进阶控制---六轴姿态控制

页: [1]
查看完整版本: 【Ameba Arduino】实战:用Ameba打造Wi-Fi控制的MP3播放器