Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem Speaker TextLCD mbed wave_player
Fork of musicplayer by
Revision 2:84f06fb95343, committed 2016-03-14
- Comitter:
- ychang79
- Date:
- Mon Mar 14 23:31:18 2016 +0000
- Parent:
- 1:45d8f6557ff8
- Commit message:
- hello
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Mon Mar 14 23:31:18 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/EasyVR.cpp Mon Mar 14 23:31:18 2016 +0000
@@ -0,0 +1,59 @@
+#pragma once
+#include "EasyVR.h"
+
+EasyVR::EasyVR(PinName tx,PinName rx):_easyVR(tx,rx)
+{
+}
+
+EasyVR::~EasyVR()
+{
+}
+
+void EasyVR::sendCmd(uint8_t c)
+{
+ _easyVR.putc(c);
+ wait(0.001);
+}
+
+void EasyVR::sendArg(int8_t c)
+{
+ _easyVR.putc(c + ARG_ZERO);
+ wait(0.001);
+}
+
+int8_t EasyVR::recv(int8_t time)
+{
+ while(!_easyVR.readable() && time != 0)
+ {
+ if(time>0)
+ time--;
+ wait(0.001);
+ }
+ return _easyVR.getc();
+}
+
+void EasyVR::decrypt(char* arg)
+{
+ *arg = *arg - ARG_ZERO;
+}
+
+bool EasyVR::awake(int timeOut)
+{
+ bool isAwake=0;
+
+ while (timeOut>0)
+ {
+ _easyVR.putc(CMD_BREAK);
+ if(_easyVR.getc()==CMD_TIMEOUT)
+ {
+ isAwake = 1;
+ }
+
+ if(timeOut>0)
+ {
+ timeOut--;
+ }
+ wait(0.001);
+ }
+ return isAwake;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/EasyVR.h Mon Mar 14 23:31:18 2016 +0000
@@ -0,0 +1,22 @@
+#pragma once
+#include "mbed.h"
+#include "protocol.h"
+class EasyVR
+{
+public:
+ EasyVR(PinName tx,PinName rx);
+ ~EasyVR();
+
+ void sendCmd(uint8_t);
+ void sendArg(int8_t);
+
+ int8_t recv(int8_t timeOut = 1);
+ void decrypt(char*);
+
+ bool awake(int timeOut = 100);
+
+
+private:
+ Serial _easyVR;
+
+};
\ No newline at end of file
--- a/main.cpp Mon Mar 17 18:48:13 2014 +0000
+++ b/main.cpp Mon Mar 14 23:31:18 2016 +0000
@@ -1,7 +1,8 @@
#include "mbed.h"
+#include "EasyVR.h"
#include "SDFileSystem.h"
#include "wave_player.h"
-#include "TextLCD.h"
+#include "uLCD_4DGL.h"
#include "PinDetect.h"
#include "Speaker.h"
#include <vector>
@@ -13,24 +14,27 @@
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
+EasyVR VR(p28, p27); // tx, rx
+Serial pc(USBTX,USBRX);
+
+PwmOut red(p21);
+PwmOut blue(p22);
+PwmOut green(p23);
+
+PinDetect pb3(p26); // pb for pause
+
using namespace std;
-SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
-TextLCD lcd(p9, p10, p11, p12, p13, p14); // rs, e, d4-d7
-DigitalIn sdDetect(p17); // Set up a pin for SD Card Detect
+SDFileSystem sd(p5, p6, p7, p15, "sd"); //SD card
+uLCD_4DGL lcd(p9, p10, p8);
+DigitalIn sdDetect(p17); // SD Card Detect
-PinDetect pb1(p28); // pb forup shift
-PinDetect pb2(p29); // pb fordown shift
-PinDetect pb3(p30); // pb for pause
-PinDetect pb4(p27); // pb for volume or play
-
-
-AnalogOut DACout(p18); //set up speaker
+AnalogOut DACout(p18); //speaker
wave_player waver(&DACout); //set up wave player library
int pos = 0; // index of the song
-int vol = 0; // volume controller
+int vol = 0; // volume
-bool playing = false; //variable for pause/play since we only have 1 pb for that
+bool playing = false; //variable for pause
vector<string> filenames; //filenames are stored in a vector string
void read_file_names(char *dir) // function that reads in file names from sd cards
{
@@ -42,10 +46,11 @@
filenames.push_back(string(dirp->d_name));
}
}
-//interrupt handler for pb1
-void pb1_hit_callback (void)
+
+//incrementing music list
+void music_increment (void)
{
- // it checks for the total number of songs in the sd card..then increments the index until it reaches the last one, then resets to 0
+ // counts number of songs in the sd card, then increments the index until it reaches the last one, then resets to 0
int l = filenames.size();
if (pos < (l-1)) {
pos++;
@@ -56,12 +61,14 @@
unsigned index = songname.find(".wav");
songname = songname.substr(0,index);
lcd.cls();
- lcd.printf(songname.c_str()); //it clears screen and then sets the new index song in the lcd display
+ lcd.printf(songname.c_str()); //clears screen then sets the new index song in the lcd display
}
-//interrupt handler for pb2
-void pb2_hit_callback (void)
+
+
+//decrement music list
+void decrement_music (void)
{
- //does opposite of pb1..moves the index down..from 2nd song to 1st song via changing the index number and then calling in the vector
+ //dmoves the index down..from 2nd song to 1st song via changing the index number
int l = filenames.size();
if (pos > 0) {
pos--;
@@ -74,8 +81,10 @@
lcd.cls();
lcd.printf(songname.c_str());
}
-//interrupt handler for 3rd pushbutton
-void pb3_hit_callback (void)
+
+
+//play-stop music
+void play_stop (void)
{
//this interrupt handler changes the play to pause mode or vice versa
//this is done using the boolean playing
@@ -90,48 +99,60 @@
lcd.printf(songname.c_str());
}
}
-//interrupt handler for pb4
-void pb4_hit_callback (void){
- // this pb changes the volume by lowering the volume until it reaches 0. then it resets to the max volume
- // the volume range has been divided into 16 possible ranges. and hence, it toggles through those 16 values
- // this only changes the variable vol, which is then used in the wave player file to actually adjust the volume
- vol = (vol+1) % 16;
+
+//high volume
+void vol_high (void){
+ vol = (vol+2) % 16;
+}
+
+//low volume
+void vol_low (void){
+ vol = (vol-2) % 16;
+}
+
+void ledDance()
+{
+ for(int i=0;i<1000;i++)
+ {
+ red = !green;
+ green = !blue;
+ blue = !red;
+ wait(0.001);
+ }
+ red = 1;
+ green = 1;
+ blue = 1;
+
}
int main()
{
+ char buffer=0;
+
+ if(VR.awake()) //wake up device - needs more work and a timeout
+ {
+ myled = 1;
+ }
+
sdDetect.mode(PullUp);
wait(.1);
//wait after pulling up the sd card,
// read file names into vector of strings
- pb1.mode(PullUp);
- pb2.mode(PullUp);
pb3.mode(PullUp);
- pb4.mode(PullUp);
// Delay for initial pullup to take effect
wait(.01);
// Setup Interrupt callback functions for a pb hit
- pb1.attach_deasserted(&pb1_hit_callback);
- pb2.attach_deasserted(&pb2_hit_callback);
- pb3.attach_deasserted(&pb3_hit_callback);
- pb4.attach_deasserted(&pb4_hit_callback);
+ pb3.attach_deasserted(&play_stop);
// Start sampling pb inputs using interrupts
- pb1.setSampleFrequency();
- pb2.setSampleFrequency();
pb3.setSampleFrequency();
- pb4.setSampleFrequency();
- lcd.cls();
- //detects whethere there is a SD card or not.. if not then it prints and informs the user
- while(sdDetect ==0) {
- lcd.locate(0,0);
- lcd.printf("Insert SD Card");
- wait(.5);
- }
+
lcd.cls();
wait(.5);
sd.disk_initialize();
read_file_names("/sd/Music");
- while(1) {
+
+ while (1)
+ {
//while pb3 is low, then we can start playing the song
while(playing == true) { //we have 2 while loops..one while loop makes sure the music player is always on, the other one is for the song
string songname = filenames[pos];
@@ -144,5 +165,30 @@
waver.play(wave_file); //plays the music file
fclose(wave_file);
}
+
+ VR.sendCmd(CMD_RECOG_SI); // Start Recognition
+ VR.sendArg(2); // Use Wordset 2 command 0-5
+
+ buffer = VR.recv(); // Receive frequency in byte from easyVR
+
+ if(buffer == CMD_SLEEP) // If easyVR is sleeping, then activate
+ {
+ VR.sendCmd(' ');
+ }
+ else
+ {
+ VR.decrypt(&buffer); // If not sleeping, then continue received message
+ pc.printf("%d\n",buffer);
+ }
+
+ // if command is taken by easyVR, the LED4 will toggle
+ if (buffer==1) {red = 0; green = 0; blue = 1;music_increment();} // right: move song
+ if (buffer==0) {red = 1; green = 0; blue = 0;decrement_music();} // left: move song
+ if (buffer==4) {myled4=!myled4;ledDance();play_stop();} // foward: play music
+ if (buffer==2) {red = 1; green = 1; blue = 0;vol_high();} // up: volume high
+ if (buffer==3) {red = 0; green = 1; blue = 1;vol_low();} // down: volum low
+ wait(0.1);
}
+
+
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/protocol.h Mon Mar 14 23:31:18 2016 +0000 @@ -0,0 +1,59 @@ +#ifndef PROTOCOL_H +#define PROTOCOL_H + +#define CMD_BREAK 'b' // abort recog or ping +#define CMD_SLEEP 's' // go to power down +#define CMD_KNOB 'k' // set si knob <1> +#define CMD_MIC_DIST 'k' // set microphone (<1>=-1) distance <2> +#define CMD_LEVEL 'v' // set sd level <1> +#define CMD_LANGUAGE 'l' // set si language <1> +#define CMD_TIMEOUT 'o' // set timeout <1> +#define CMD_RECOG_SI 'i' // do si recog from ws <1> +#define CMD_TRAIN_SD 't' // train sd command at group <1> pos <2> +#define CMD_GROUP_SD 'g' // insert new command at group <1> pos <2> +#define CMD_UNGROUP_SD 'u' // remove command at group <1> pos <2> +#define CMD_RECOG_SD 'd' // do sd recog at group <1> (0 = trigger mixed si/sd) +#define CMD_ERASE_SD 'e' // reset command at group <1> pos <2> +#define CMD_NAME_SD 'n' // label command at group <1> pos <2> with length <3> name <4-n> +#define CMD_COUNT_SD 'c' // get command count for group <1> +#define CMD_DUMP_SD 'p' // read command data at group <1> pos <2> +#define CMD_MASK_SD 'm' // get active group mask +#define CMD_RESETALL 'r' // reset all commands and groups +#define CMD_ID 'x' // get version id +#define CMD_DELAY 'y' // set transmit delay <1> (log scale) +#define CMD_BAUDRATE 'a' // set baudrate <1> (bit time, 1=>115200) +#define CMD_QUERY_IO 'q' // configure, read or write I/O pin <1> of type <2> +#define CMD_PLAY_SX 'w' // wave table entry <1-2> (10-bit) playback at volume <3> +#define CMD_PLAY_DTMF 'w' // play (<1>=-1) dial tone <2> for duration <3> +#define CMD_DUMP_SX 'h' // dump wave table entries +#define CMD_DUMP_SI 'z' // dump si settings for ws <1> (or total ws count if -1) +#define CMD_SEND_SN 'j' // send sonicnet token with bits <1> index <2-3> at time <4-5> +#define CMD_RECV_SN 'f' // receive sonicnet token with bits <1> rejection <2> timeout <3-4> + +#define STS_MASK 'k' // mask of active groups <1-8> +#define STS_COUNT 'c' // count of commands <1> (or number of ws <1>) +#define STS_AWAKEN 'w' // back from power down mode +#define STS_DATA 'd' // provide training <1>, conflict <2>, command label <3-35> (counted string) +#define STS_ERROR 'e' // signal error code <1-2> +#define STS_INVALID 'v' // invalid command or argument +#define STS_TIMEOUT 't' // timeout expired +#define STS_INTERR 'i' // back from aborted recognition (see 'break') +#define STS_SUCCESS 'o' // no errors status +#define STS_RESULT 'r' // recognised sd command <1> - training similar to sd <1> +#define STS_SIMILAR 's' // recognised si <1> (in mixed si/sd) - training similar to si <1> +#define STS_OUT_OF_MEM 'm' // no more available commands (see 'group') +#define STS_ID 'x' // provide version id <1> +#define STS_PIN 'p' // return pin state <1> +#define STS_TABLE_SX 'h' // table entries count <1-2> (10-bit), table name <3-35> (counted string) +#define STS_GRAMMAR 'z' // si grammar: flags <1>, word count <2>, labels... <3-35> (n counted strings) +#define STS_TOKEN 'f' // received sonicnet token <1-2> + +// protocol arguments are in the range 0x40 (-1) to 0x60 (+31) inclusive +#define ARG_MIN 0x40 +#define ARG_MAX 0x60 +#define ARG_ZERO 0x41 + +#define ARG_ACK 0x20 // to read more status arguments + +#endif //PROTOCOL_H +
--- a/wave_player.lib Mon Mar 17 18:48:13 2014 +0000 +++ b/wave_player.lib Mon Mar 14 23:31:18 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/sarthakjaiswal/code/wave_player/#bd0772f13bc8 +https://developer.mbed.org/users/ychang79/code/wave_player/#bd0772f13bc8
