You are reading a single comment by @Jofo and its replies. Click here to read the full conversation.
  • To follow up, here is the inlineC version, which actually is fast enough for my purpose! I am still interested in hardware support, maybe more power-efficient or leaving cpu for other tasks, but this could be good enough anyway. Super-cool that we can combine js and c this way, so thanks for that.

    var c = E.compiledC(`
    // void encA(bool)
    // void encB(bool)
    // int getEnc()
    const int stable[] = {0,-1,1,0,1,0,0,-1,-1,0, 0,1, 0,1, -1, 0};
    volatile bool encoder_raw[] = {false, false};
    volatile int counter = 0;
    volatile int prev_state = -1;
    void encoder_callback(int AB, bool value){
        encoder_raw[AB]=value;
        int cur_state = encoder_raw[0] << 1 | encoder_raw[1];
        if(prev_state < 0) prev_state = cur_state;
        counter += stable[prev_state << 2 | cur_state];
        prev_state=cur_state;
    };
    void encA(bool state){ encoder_callback(0,state); }
    void encB(bool state){ encoder_callback(1,state); }
    int getEnc() {
     int r = counter;
     return r;
    }
    `);
    setWatch(c.encA, D25, {repeat:true, edge:"both", irq:true});
    setWatch(c.encB, D26, {repeat:true, edge:"both", irq:true});
    
    // Every .1 seconds, report back the value
    setInterval(function() {
      print(c.getEnc());
    }, 100);
    
  • Tue 2019.08.20

    Thank you @Jofo for the additional detail, we now may follow along more easily.

    Surprised, and glad you were able to get the inline 'C' working quickly. My first attempts took me longer. Nothing appears out of the ordinary for the Javascript snippet. Proper use of setWatch() and not embedded with the setInterval() function.

    This is most likely understood, so forgive me if this is too obvious. I use a technique to allow a function call in the WebIDE Left-Hand console window to stop processing as at times Ctrl+C isn't always as responsive when updating at 100msec. L9 could be modified to:

    var intervalID = {};
    
    function ci() { clearInterval( intervalID ); }
    
    L9:  intervalID = setInterval(function() {
    


    ' it works if I rotate slowly (~4s per turn), but skips counts if i turn faster'

    Your post reminded me of another with an encoder issue, several days ago, and a link there shows a solution Gordon proposed that should be fast enough with Javascript and setWatch() data: options attribute.

    See the link 'Proper technique needed to nest a setWatch()' in #27 there

    post #22 and #27 and with encoder image #30
    http://forum.espruino.com/comments/14857­261/

     

    'I am still interested in hardware support, maybe more power-efficient'

    I'll continue to read up from your links, in the mean time I'll defer to others. . . .

About

Avatar for Jofo @Jofo started