Serial.on() issues... #723
Replies: 13 comments
-
Posted at 2015-03-30 by DrAzzy 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.
Of course it depends on the object being called evr.
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @gfwilliams I think this is actually exactly the same issue as you'd have with To solve it you'd define a variable in your function's scope and then use that:
Possibly There's some stuff on StackOverflow about it, and a neat solution would be to use |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @allObjects DrAzzy, sorry to respond late on your first post... was out all day on water whale watching... Your initial approach is just fine... the problem with JS is that
As you notice, you make a copy This is sufficient if you do not need to call In case you need to reference Nice 'concidence' to me after just looking up Lambda_calculus on, section of Lambda calculus and programming languages - Anonymous functions: it describes almost to a tee the language teachings and sequence and thinking about algorithms and data structures I was exposed to : Pascal (S) - 'brain child' of ALGOL 60 - Smalltalk - Javascript. In same section under 'more recent' C++11 is mentioned. Java is still missing... (explains 'needs Edit' comment). All other languages I got used to use were like noise on the road side.
This will fix this. ...nice word play (at least in the eyes - and ears - of a non native English speaker...). On the Web you find What frameworks - as mentioned in many forum entries mention - are doing with the bind is essentially what is done in line 6: make sure the function is called in the context of the object, so it is more a method invocation of an object then just a function call. It is binding the function to the object context. Unfortunately, most of all the good things that come with the frameworks are just too much bulky for resource frugal Espruino... just like V8 was to be useful. And even more unfortunate, that more an more Web entries under Javascript give solutions for problems not in Javascript but in frameworks, and Javascript core knowledge shrinks to being lost. Java and 'real object oriented language' proponents will call this still hideous. But liking almost all languages, I appreciate the freedom I get with Javascript for a very small price... over (m)any other languages. Javascript got the Lambda calculus right in its initial release - when still called Lifescript and not even called Javascript yet, where it took Java 8 major releases to get there. Agreed: you could do Javascript-similar things with Java too - much more safely - but also much much much more hideously... Way more 'hideousness' than what is needed to make Javascript to have what Java has in regard of inheritance and polymorphism / class hierarchy). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @gfwilliams I've just added
I'm not sure which is better - the |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @allObjects Nice solution the 'bind()'... the use of global 'evr' still makes making EasyVR a class a joke though... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @allObjects With the newly implemented bind and the just to implement
@gordon, if there are a few bytes left to be 'wasted' on Object.getPrototypeOf(object), that would be cool too. @gordon - sorry, again - from your knowledge of the internals of the bind() implementation, what is the memory footprint compared to creating an anonymous function instead? MSDN describes bind() as creating a function with same body on the bound object... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @allObjects Thinking through what an implementation of getPrototypeOf() or getClass() (='the function object') could entail, it may not be easily done... Shims or fillins do that by creating an extra instance variable and assign the constructor function - or class (function). Since it is in the constructor itself, it is nothing wrong to 'globally' reference it in there. Taking the same approach for referencing the constructor function - self in itself - and use the (now) available .bind(), simplifies above approach to this:
@drazzy, renaming onWatch (vs. onTimeout) to onData would go in synch with serial.onData()... ;-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by DrAzzy Thanks, Gordon and allObjects. I'll update tonight and try out bind(). That will make the code a lot more readable - can I get rid of the global reference entirely with:
I'd really like to get rid of it, since it does defeat the purpose of the class. yeah, onData() makes a lot more sense than onWatch(). The variable naming there is very dubious overall, and needs to be cleaned up. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @allObjects If It - your final solution to get rid of all globals, including the 'class' - ... If It Were A Snake, It Would Have Bit Me. Just don't get why it did't. - :( |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-31 by DrAzzy Lucky thing this isn't python then ;-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-31 by DrAzzy
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. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-31 by @gfwilliams Hopefully it's not a big deal to fix the eval... However can you not just define functions rather than strings? You can call them with
It's not too bad. One memory unit for the function itself, one per parameter, plus one for It's going to be a bit better than an anonymous function - check it out with |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-31 by @allObjects @gordon, thanks for the comparison of .bind() vs anonymous function in app. @drazzy, eval() has the smell of a Trojan Horse... Any option to avoid it is useful... Since you do not 'grow' - increase the functionality - of the code, think of your 'pre-defined source code expressions' as prebuilt functions... held in an arrays and object properties (just as you already hold the expressions now). You can then even think of a plug-in mechanism: pairs of voice recognizable commands and execution functions... a cool thing... (and I guess, will in the end also having this thing talking back to you... audibly...) Since a while I have on my desk laying around some external audio player modules that I wanted to connect, but I'm bound into something else right now. = I know that Espruino can play wave forms and do that too, but I want offload that part. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2015-03-29 by DrAzzy
How do I make
this
work here?This seems to be set to... an object containing everything. How do I get it to be the object that the function is a property of?
Secondly - with same code, try doing:
evr.onWatch=function(){console.log("roar!!!");} //where evr is an instance of that object
then provoke another serial data event. The callback isn't updated. How can I make it call the function I pointed it at, instead of remembering what said function happened to be when I initially set up the callback? Is this behavior intended?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions