use interrupts on C-level

Posted on
  • I'm working on a native class to recognize gestures.
    Input will be a distance sensor HC-SR04.
    To recognize a gesture something like setWatch is used.
    Its running up to the point that an interupt is recognized, but there are no args that would help to assign the interupt to a parent.
    Any idea ?

    void gotInterrupt(int n, ... ){ jsWarn("got it %j",n); }
    void jswrap_Gesture_scan(JsVar *parent){
      JsGesture gest;
      if(!getFromVar(&gest,parent)) return;
      IOEventFlags exti = EV_NONE;
      if (!jsiIsWatchingPin(gest.data.echo))
        exti = jshPinWatch(gest.data.echo, true);
        // disable event callbacks by default
      if (exti) {
    	jshSetEventCallback(exti, 0);
    	JsVar *fn = jsvNewNativeFunction((void (*)(void))gotInterrupt, JSWAT_VOID|JSWAT_THIS_ARG|(JSWAT_JSVAR<<­JSWAT_BITS));
        if (!fn) return;
        jshSetEventCallback(exti, (JshEventCallbackCallback)jsvGetNativeFu­nctionPtr(fn));
    	jsvUnLock(fn);
      }
    }
    
  • I'm afraid there isn't anything at the moment - it'd just be a matter of having a static variable, of multiple functions if you wanted to allow multiple instantiation...

    Also, there's no need for jsvNewNativeFunction... you can just do:

    void gotInterrupt(int n, ... ){ jsWarn("got it %j",n); }
    void jswrap_Gesture_scan(JsVar *parent){
      JsGesture gest;
      if(!getFromVar(&gest,parent)) return;
      IOEventFlags exti = EV_NONE;
      if (!jsiIsWatchingPin(gest.data.echo))
        exti = jshPinWatch(gest.data.echo, true);
        // disable event callbacks by default
      if (exti) {
        jshSetEventCallback(exti, (JshEventCallbackCallback)gotInterrupt);­
      }
    }
    

    So I guess a multi-function might look like:

    JsVar *parents[4]  {0,0,0,0};
    void gotInterrupt1(int n, ... ){ /* parents[0] */ }
    void gotInterrupt2(int n, ... ){ /* parents[1] */ }
    void gotInterrupt3(int n, ... ){ /* parents[2] */ }
    void gotInterrupt4(int n, ... ){ /* parents[3] */ }
    const JshEventCallbackCallback gotInterrupts[4] = { gotInterrupt1, gotInterrupt2, gotInterrupt3, gotInterrupt4 };
    
    void jswrap_Gesture_scan(JsVar *parent){
      JsGesture gest;
      if(!getFromVar(&gest,parent)) return;
      IOEventFlags exti = EV_NONE;
      if (!jsiIsWatchingPin(gest.data.echo))
        exti = jshPinWatch(gest.data.echo, true);
      if (exti) {
        int n = 0;
        while (parents[n]) n++;
        if (n>3) {
         jsExceptionHere(JSET_ERROR, "Too many watches!");
         return;
        }
        jsvUnLock(parents[n]);
        parents[n] = jsvLockAgain(parent);
        jshSetEventCallback(exti, (JshEventCallbackCallback)gotInterrupts[­n]);
      }
    }
    
  • @gordon, thanks for the feedback. In my knowledge, this would be a first step solution and would solve my needs.
    On a longer term, there are some questions, like what happens on something like clearwatch ?
    Would it be a better option to change definition of jshEventCallbacks in jsdevices to a struct, having a JshEventCallbackCallback and a JsVar for parent (or something else, I cannot even imagine right now) ?
    As far as I can see, there are only 5 locations, where JshEventCallbacks is used.
    Adding something like set jshSetEventJsVar to set and an if-statement in jshPushIOWatchEvent which would add this to callback ?
    I'm pretty sure, it is doable, but would it be helpful ?
    Lat not least it would slow down, would this be acceptable ?

  • what happens on something like clearwatch ?

    As far as I know, clearWatch wouldn't stop those watches (there's a list of JS watches stored in a JsVar).

    Would it be a better option to change definition of jshEventCallbacks in jsdevices to a struct

    Maybe. I've been trying to avoid things like that though - there are 16 watch targets so with a 32 bit pointer that's 64 bytes of RAM that's then not available to the user. Not a big deal on bigger parts, but when 99.9% of users wouldn't use it, it seems like a bit of a waste (esp on the smaller parts).

    I don't think it'd slow anything down noticably - so really it's just whether it's a good idea memory-wise.

    Another quick solution would be to just pass the EXTI number into the function as an argument, and then you could work out where everything went yourself (since jshPinWatch will return the EXTI when called).

  • Hmm, understand.
    Passing EXTI into a function, would this be adding (channel-EV_EXTI0) to calling callback in jshPushIOWatchEvent (jsdevives.c) ?

    Thinking loud:
    I could add the list of parents to Gesture source, so that only guys using this have to pay the price (in bytes) ?
    Or an array of internal parameters and replace internal structure with an entry in this array, which would make setVar and getFromVar needless ?

    Hmm, anyway, next weeks are reserved for family and vacations.

  • Yes - but I'd just pass channel in directly, then it's up to whatever library to do channel-EV_EXTI0 if needed (but at least you're comparing JsEventFlags with JsEventFlags).

    I'm not 100% sure I understand what you mean - but if you're only going to use one instance of Gesture then yes, definitely statically allocate it.

    In Espruino I don't do this because I only want to use RAM when it's needed - but for you, you're only compiling in Gesture when you need it so you might as well statically allocate.

  • use RAM when it's needed only, good point.

    Quick first test with changes:

    typedef void(*JshEventCallbackCallback)(bool state,int exti); //changed by jumjum
    
    jshEventCallbacks[channel-EV_EXTI0](stat­e,channel); //changed by jumjum
    

    It works prefect, tested with SetWatch and Gesture itself(very early version).

    Just got some angry eyes from family, so its better to stop this now and take care on infrastructure for vacations like Laptop, CamCorder, Radio, GPS, cellphone, several charger, SD-cards, Camera, .........
    How could we live without any of this in previous millennium ;-)

  • :) glad it's working - I'll see about committing that.

    How you have good holidays!

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

use interrupts on C-level

Posted by Avatar for JumJum @JumJum

Actions