You are reading a single comment by @DrAzzy and its replies. Click here to read the full conversation.
  • I've got it working, but it's hideous. There's gotta be a better way to do this...

    Interfaces with an EasyVR voice recognition module.

    
    /* Copyright (C) 2015 Spence Konde. See the file LICENSE for copying permission. */
      /*
    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.onWatch);
      this.vrstate=-1;
      this.stsr='o';
      this.rcvv="";
    }
    
    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.onWatch=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=evr.sts_idx[data];
    		//console.log("serial lookup: "+temp);
    		if (temp[0]) {
    			evr.stsr=data;
    			evr.ser.print(' ');
    		} else {
    			eval(temp[1]);
    		}
    	} else {
    		console.log("data");
    		evr.rcvv+=data;
    		if (evr.rcvv.length>=evr.sts_idx[evr.stsr][­0]){
    		    console.log("running callback "+evr.sts_idx[evr.stsr][1]);
    			eval(evr.sts_idx[evr.stsr][1]);
    			evr.rcvv="";
    			evr.stsr='o';
    		} else {
    		    console.log("need more data");
    			evr.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);"],
    	"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(t­ype,to) {
    	this.sts_idx.o[1]="evr.startRec("+type+"­,"+to+");";
    	this.timeout(to);
    	if (to) {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.char­arg(evr.rcvv));";
    	this.sts_idx.t[1]="evr.sts_idx.r[1]='';e­vr.sts_idx.t[1]='';evr.onTimeout(evr.vrs­tate);";
    	this.sendCmd('d',type);
    	this.vrstate=type;
    };
    
    
    EasyVR.prototype.sendCmd=function(cmd,ar­g) {
    	//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);"],
    	"r":[1,"console.log('STS_RESULT');"]
    	};
    	this.sendCmd('b');
    };
    EasyVR.prototype.timeout=function(arg) {
    	this.sendCmd('o',arg);
    };
    
    
    

    Of course it depends on the object being called evr.

    
    var ocm=function(menu,option) {
      console.log("menu:"+menu+" option: "+option);
      if (menu==1&&option==2) {
        return {type:2,timeout:10};
      } else {
        if (menu==2) {
          if (option==0) {
            digitalWrite([LED1,LED2,LED3],2);
          } else if(option==1) {
            digitalWrite([LED1,LED2,LED3],4);
          } else {
            
            digitalWrite([LED1,LED2,LED3],1);
          }
        }
        return {type:1,timeout:0};
      }
      
    };
    
    var otm=function(){
      this.setRecognize(1,0);
    };
    
    
    
    Serial4.setup(9600,{tx:C10,rx:C11});
        
    var evr=require("easyvr").connect(Serial4,oc­m,otm,otm);
    
    
    
    
About

Avatar for DrAzzy @DrAzzy started