Removing Touchscreen Handler

Posted on
  • Hi,

    it might just be a bit late today, but how do I remove a callback for the Touchscreen that I have set in the connect method? I see that it creates a watch. Can that be referenced?

    (i could reference another function from there, but i wonder if I ever get rid of that original connect handler)

    Stev

  • I think it's just setting a watch, so if you figure out the watch number, you can clear it with clearWatch().

    Unfortunately, as you can see in the module code, they don't keep record of the watches - that would be easy enough to change though http://www.espruino.com/modules/ADS7843.­js

  • Hey. Yes, i guess changing the module code is they best way to go. I tried to figure out the watch id before. Cumbersome.

    Thanks.

    Stev

  • Yes - At the moment you're a bit stuck.

    Why do you want to remove it though? Can't the callback just return? I can't remember specifically about that module, but by repeatedly connecting and disconnecting you would probably be re-initialising the chip each time.

  • I just was playing around. Now that the board seems to work, I'd like build some touch UI on top of it and that were my first steps last night, when I realized that calling the connect function again would result in adding watches, not deleting the former. Just wanted to get rid of the older ones and see where I would put the switching of "active" screen areas.

    You're probably right about the re-init, so I'll try to put the picking behind that one callback.

    I still seem to have to re-polish my JS skills. Right now I always end up with code copied into handlers when I just want to pass a reference to a function and I still have no idea on how to access/create "top-level"/global vars from within a handler. Is there a name/reference for the Espruino top level object (the one I get with "this" when in the shell)? Or maybe that's just the wrong pattern to communicate between handlers and set up a system.

    BTW: Also I see a growing number of "LCD = {}" entries when I look into the var with dump(). I can do multiple "delete LCD" to get rid of them, but neither do I know where the come from, nor how multiples of them can exist be created in the first place.

    So. Arrived at the JS level, at last :)

  • I'm surprised you can't get at global vars within a handler normally - stuff like setWatch/setInterval will set this to be the global object, but just accessing a variable within a function will access the global version if a local one doesn't exist.

    As far as callbacks, if you do:

    function touch(x,y) {
      ...
    }
    require("Touchscreen").connect(touch);
    

    Then you can redefine the touch function, either by just entering it again, or by typing edit("touch").

    Shame about the LCD={} stuff. It's a bug in the LCD implementation (normal Espruino doesn't come with the LCD object, so I didn't spot it). I believe I just fixed it, so the next compile from Git should sort it out.

  • Good to know about 'this' in the timers/handlers.
    I am really just sorting things out right now and sometimes don't know if it's JS-specific or specific to Espruino's environment. Like how to create a top level var from within another object's method... which I probably shouldn't, anyway.
    I guess, I look at that object-tree and I am missing the reference to its root...

  • Yes, it's probably an oversight really. In the browser you've got window, and we should really have something similar. The 'root' is of type Hardware, so really we should have a hardware variable that can be accessed.

    However, you can create a global variable just like:

    function foo() {
      global = "Hello";
      var local = "World";
    }
    

    It works even for objects too - so you've actually got to be careful about using var in functions of you end up creating a lot of global stuff :)

  • As far as callbacks, if you do:
    function touch(x,y) {
    ...
    }
    require("Touchscreen").connect(touch);
    Then you can redefine the touch function, either by just entering it again, or by
    typing edit("touch").

    Hm. That's exactly what's not working (and the problem I meant when mentioning not being clear when copying function code vs referencing functions).

    When I do this like in your example above, I can edit the touch function, but it will not have any effect, as the original one seems to be "submitted" and stays active.

    If I instead use a double reference like

    function touch(x,y) {doTouch(x,y);}
    function doTouch(x,y) {console.log(x+"/"+y);}
    require("Touchscreen").connect(touch);
    

    , I can redefine doTouch effectively using edit().

    That's where I stumbled and wondered if I somehow could delete the original callback.

  • Ahh, that's interesting. I just checked and it seems that if you redefine the function with:

    function touch(x,y) {
    ...
    }
    

    It should work (I didn't try this on the touchscreen, but I tried with setInterval, and something that looks a bit like the touchscreen handler).

    However if you try edit then it does:

    touch = function(x,y) { 
    ...
    };
    

    And that doesn't work. I'll try and fix that (what edit does) in the next version though. Unfortunately I believe that touch = function() { ... } actually does the correct thing in not updating the function, only the pointer.

  • Funny. As I usually use the functional notation. Only since working with the touchscreen code I used the direct function declaration because the touchscreen examples were written that way :)

    Notation-wise it might make sense to differentiate them (named function vs function var with anonymous function), but I don't know what JSs spec actually says.

    I just remember that in a coding context/script, direct function declarations are parsed befor the actual script execution while the var = function(){} notation is a executed as part of the normal execution sequence (making the former being present/declared for code positioned before the function declaration, while the latter wouldn't).

    Also, in espruino, when I do a

    touch
    

    i'll get

    =function (x,y) { ... }
    

    in both cases. And both objects can be combined with additional methods.

    Just was that thing that left me confuse about the copying vs. referencing idea for function(object)s and wanting to be able to delete that callback . Because once you screwed it...

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Removing Touchscreen Handler

Posted by Avatar for Stev @Stev

Actions