Xiaofei Qiu
/
EasyVR3_LED
EasyVR3_LED
Revision 0:13b922a701d4, committed 2015-10-22
- Comitter:
- Xiaofei
- Date:
- Thu Oct 22 18:58:41 2015 +0000
- Commit message:
- EasyVR3 LAB
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EasyVR.cpp Thu Oct 22 18:58:41 2015 +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 Thu Oct 22 18:58:41 2015 +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 22 18:58:41 2015 +0000 @@ -0,0 +1,63 @@ +#include "mbed.h" +#include "EasyVR.h" + +DigitalOut led1(LED1); +DigitalOut led4(LED4); + +EasyVR VR(p13, p14); // tx, rx +Serial pc(USBTX,USBRX); + +PwmOut red(p21); +PwmOut blue(p22); +PwmOut green(p23); + +void ledDance(); + +int main() { + char buffer=0; + + if(VR.awake()) //wake up device - needs more work and a timeout + { + led1 = 1; + } + + while (1) + { + VR.sendCmd(CMD_RECOG_SI); // Start Recognition + VR.sendArg(1); // Use Wordset 3 - the numbers 0..10 + + buffer = VR.recv(); // Receive a byte from easyVR + + if(buffer == CMD_SLEEP) // If easyVR is sleeping + { + VR.sendCmd(' '); // Send blanck to activate it + } + else + { + VR.decrypt(&buffer); // If not sleeping, decrtpt received message + pc.printf("%d\n",buffer); + } + + // if command is taken by easyVR, the LED4 will toggle + if (buffer==7) {red = 0; green = 1; blue = 0;led4=!led4;} // hello + if (buffer==6) {red = 0; green = 0; blue = 0;led4=!led4;} // stop or turn off + if (buffer==3) {led4=!led4;ledDance();} // run + if (buffer==7) led4=!led4; + wait(0.1); + } +} + +void ledDance() +{ + for(int i=0;i<5000;i++) + { + red = !green; + green = !blue; + blue = !red; + wait(0.001); + } + red = 1; + green = 0; + blue = 0; + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Oct 22 18:58:41 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/protocol.h Thu Oct 22 18:58:41 2015 +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 +