/*
This module interfaces with EasyVR voice recognition
onCom is called with two arguments, the option group, and the command returned.
onTimeout is called with one argument, the option group.
*/
exports.connect = function(serial,onCm,onTo,onEr) {
return new EasyVR(serial,onCm,onTo,onEr);
};
function EasyVR(ser,onCm,onTo,onEr) {
this.ser = ser;
this.onCommand=onCm;
this.onTimeout=onTo;
this.onErr=onEr;
this.ser.on('data',this.onData.bind(this));
this.stop();
this.vrstate=-1;
this.stsr='o';
this.rcvv="";
this.tout=0;
}
EasyVR.prototype.argchar=function(val) {
if (val<-1 || val > 31) {throw "Bad arg";}
return String.fromCharCode(0x41+val);
};
EasyVR.prototype.chararg=function(chr) {
return chr.charCodeAt(0)-0x41;
};
EasyVR.prototype.onData=function(data) {
//this=evr;
console.log("serial data watch: "+data);
var rcv=data.charCodeAt(0);
console.log("serial data: "+rcv);
if (rcv>0x60) {
console.log("status");
var temp=this.sts_idx[data];
//console.log("serial lookup: "+temp);
if (temp[0]) {
this.stsr=data;
this.ser.print(' ');
} else {
eval(temp[1]);
}
} else {
console.log("data");
this.rcvv+=data;
if (this.rcvv.length>=this.sts_idx[this.stsr][0]){
console.log("running callback "+this.sts_idx[this.stsr][1]);
eval(this.sts_idx[this.stsr][1]);
this.rcvv="";
this.stsr='o';
} else {
console.log("need more data");
this.ser.print(' ');
}
}
};
EasyVR.prototype.sts_idx={
"o":[0,"console.log('STS_SUCCESS');"],
"t":[0,"console.log('STS_TIMEOUT');"],
"v":[0,"console.log('STS_INVALID');"],
"i":[0,"console.log('STS_INTERR');"],
"e":[2,"console.log('STS_ERROR '+evr.rcvv);evr.onErr(evr.rcvv);"],
"s":[1,"console.log('STS_SIMILAR '+evr.rcvv);evr.onErr(evr.rcvv);"],
"r":[1,"console.log('STS_RESULT');"]
};
EasyVR.prototype.onResult=function(r) {
console.log("onresult");
console.log(r);
var rt = this.onCommand(this.vrstate,r);
if (rt.type!==undefined) {
this.stop();
this.setRecognize(rt.type,rt.timeout);
}
};
EasyVR.prototype.setRecognize=function(type,to) {
if (this.tout) {
clearTimeout(this.tout);
this.tout=0;
}
this.sts_idx.o[1]="evr.startRec("+type+","+to+");";
this.timeout(to);
if (to) {this.tout=setTimeout("eval(evr.sts_idx.t[1])",to*1000+1000);}
};
EasyVR.prototype.startRec=function(type,timeout){
this.sts_idx.o[1]="";
this.sts_idx.r[1]="evr.onResult(evr.chararg(evr.rcvv));";
this.sts_idx.t[1]="evr.sts_idx.r[1]='';evr.sts_idx.t[1]='';evr.onTimeout(evr.vrstate);";
this.sendCmd('d',type);
this.vrstate=type;
};
EasyVR.prototype.sendCmd=function(cmd,arg) {
//lastCmd=[cmd,arg];
this.ser.print(cmd);
console.log("Sending command: "+cmd);
if (arg!==undefined){console.log("With arg: "+this.argchar(arg));this.ser.print(this.argchar(arg));}
};
EasyVR.prototype.stop=function(){
this.sts_idx={
"o":[0,"console.log('STS_SUCCESS');"],
"t":[0,"console.log('STS_TIMEOUT');"],
"v":[0,"console.log('STS_INVALID');"],
"i":[0,"console.log('STS_INTERR');"],
"e":[2,"console.log('STS_ERROR '+evr.rcvv);evr.onErr(evr.rcvv);"],
"s":[1,"console.log('STS_SIMILAR '+evr.rcvv);evr.onErr(evr.rcvv);"],
"r":[1,"console.log('STS_RESULT');"]
};
this.sendCmd('b');
};
EasyVR.prototype.timeout=function(arg) {
this.sendCmd('o',arg);
};
EasyVR.prototype.setStrict=function(arg) {
this.sendCmd('v',E.clip(arg,1,5));
};
var ocm=function(menu,option) {
console.log("menu:"+menu+" option: "+option);
if (menu==0) {
if (option==0) {
console.log("LIGHTS ON");
//do lights on calls
} else if (option==1) {
console.log("LIGHTS OFF");
//do lights off calls
} else if (option==2) {
console.log("SWITCH :");
digitalWrite(LED1,1);
return {type:2,timeout:15};
} else if (option==3) {
console.log("DESK :");
digitalWrite(LED1,1);
return {type:3,timeout:15};
} else if (option==4) {
console.log("NIXIE :");
digitalWrite(LED1,1);
return {type:4,timeout:15};
}
} else {
if (menu==2) { // toggle a fargo or RF controlled device
console.log("toggle device "+option);
} else if (menu==3) { // control desk lamp
console.log("desk lamp "+option);
} else if (menu==4) { // control nixie clock
console.log("control nixie clock "+option);
}
digitalWrite(LED1,0);
return {type:1,timeout:0};
}
};
var otm=function(){
digitalWrite(LED1,0);
this.setRecognize(1,0);
};
Serial4.setup(9600,{tx:C10,rx:C11});
var evr=require("easyvr").connect(Serial4,ocm,otm,otm);
Still needs to be cleaned up (and of course, code to do those actions written) , the code is really awkward right now... But it does seem to work, and I will be able to talk to my room, and it will be awesome.
Still needs the global, for the evals, due to issue #513.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Still needs to be cleaned up (and of course, code to do those actions written) , the code is really awkward right now... But it does seem to work, and I will be able to talk to my room, and it will be awesome.
Still needs the global, for the evals, due to issue #513.