I'm developing a module to recognize simple gestures based on hc-sr04.
Code runs fine, but minified(simple optimization) runs into error, somewhere around calcGesture.
function gesture(trig,echo){
function Empty(){}
function gestureArea(min,max){this.min = min;this.max = max;this.active = true;}
function trigger(){ digitalPulse(trig, 1, 0.01); }
var riseTime = 0,fallTime = 0,iv,ivDef = 50,clkDef = 0.15;
var min = 10,max = 100,actArea = "",prevArea = "",lastEnter = 0;
var callback = {enter:Empty,leave:Empty,click:Empty,move:Empty};
var areas = {};
this.setRange = function(mn,mx){min = mn;max = mx;};
this.setCallback = function(t,f){callback[t] = f;};
this.addArea = function(name,min,max){ areas[name] = new gestureArea(min,max); };
this.setAreaStatus = function(name,status){ areas[name].active = status; };
this.start = function(t){
ivDef = t || 50;
clkDef = ivDef * 3 / 1000;
iv = setInterval(function(){trigger();},ivDef);};
this.stop = function(){clearInterval(iv);};
function calcGesture(d){
var a;
if(between(min,max,d)){
a = getArea(d);
if(a){
if(actArea === a){
a = areas[a];
callback.move("move",actArea,d,(d - a.min) / (a.max - a.min) * 100);
}
else{
if(actArea !== ""){ callback.leave("leave",actArea,d); }
callback.enter("enter",a,d);
lastEnter = fallTime; prevArea = actArea;actArea = a;
}
}
else{prevArea = actArea;actArea = "";}
}
else{
if(actArea){
callback.leave("leave",actArea,d);
if((fallTime - lastEnter) < clkDef){ callback.click("click",actArea,d); }
}
prevArea = actArea; actArea = "";
}
}
function getArea(d){
for(var a in areas){
if(areas[a].active){
if(between(areas[a].min,areas[a].max,d)){ return a; }
}
}
return "";
}
function between(min,max,value){return (value>min && value<max);}
setWatch(function(e){riseTime = e.time;},echo,{repeat:true, edge:'rising'});
setWatch(function(e){
fallTime = e.time;
calcGesture(((fallTime - riseTime) * 1000000)/ 57);
},echo,{repeat:true, edge:'falling'});
}
var g = new gesture(A0,A1);
g.addArea("bottom",10,20);
g.addArea("middle",30,40);
g.addArea("top",50,60);
//g.setCallback("enter",function(a,t,d){console.log(a,t,d);});
//g.setCallback("leave",function(a,t,d){console.log(a,t,d);});
g.setCallback("click",function(a,t,d){console.log(a,t,d);});
//g.setCallback("move",function(a,t,d,p){console.log(a,t,d,p);});
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.
I'm developing a module to recognize simple gestures based on hc-sr04.
Code runs fine, but minified(simple optimization) runs into error, somewhere around calcGesture.