-
• #2
Does it actually output an error message, or it just doesn't work as you expect?
-
• #3
Error is:
Uncaught SyntaxError: Got ':' expected EOF at line 1 col 46 ...-n)/57;var b;if(a>r&&a<s){a:{for(b in d)if(d[b].active&&a>d[... ^
-
• #4
Hmm. If it's complaining about
if(a>r&&a<s){a:{...
that does look a bit iffy.I wonder if because I switched to using ES6 minification (to allow numbers like
0b0101
to be handled nicely), it's creating some crazy minified ES6 code too :) -
• #5
Got it running by moving code-part of functions getArea and between into calcGesture. Now it is this:
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 = "",i; if(d > min && d < max){ for(i in areas){ if(areas[i].active){ if(d > areas[i].min && d < areas[i].max){ a = i; } } } 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 = ""; } } 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);});
looks like Espruino does not like the construction to abort the loop.
a:{for(b in d)if(d[b].active&&a>d[b].min&&a<d[b].max)break a;b=""}
-
• #6
Ahh. Labeled break statements. Espruino doesn't support those at the moment - I'm surprised the minifier added them in though!
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.