• ...continued...

    The MCP23017 I happened to get are I2C models, stamped: MCP23017 E/SP. Not as familiar to me as SPI, it gave me some grieve on top of the challenge what the universality of the chip already posed to me, especially the re-config from 16 bit to 8 bit mode (and of course also some lousy wiring flaws and coding typos made me burn some night oil).

    The code is not the most leanest, but having implemented a touch UI, I knew about the needs and challenges to meet. Therefore, the 'Kpd' module (or class) supports the use of multiple types of callbacks: callback on key up (preferred), callback on key down, and both. It even supports a polling mode if you want to live an Arduino loop and lifecycle...

    The code also supports bouncy hardware and clumsy key press and releases behavior.

    Example output - with key number first in line - from provided sample callback look like this:

    _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v94 Copyright 2016 G.Williams
    >
    =undefined
    0 .     up at  946686483.38686466217  after  0.22292900085
    5 .     up at  946686484.37534046173  after  0.17869758605
    9 .     up at  946686487.18709087371  after  0.13181495666
    7 .     up at  946686513.53536319732  after  0.06225681304
    9 .     up at  946686515.38583564758  after  0.04290485382
    1 .     up at  946686517.51007080078  after  0.04504585266
    2 .     up at  946686519.69350910186  after  0.04776573181
    

    and:

    6 ... down at  946691897.44143199920
    8 ... down at  946691898.60526180267
    2 ... down at  946691898.89713573455
    

    and both down AND up callbacks:

    4 ... down at  946692008.95299339294
    4 .     up at  946692009.08904552459  after  0.13605213165
    5 ... down at  946692010.02036190032
    5 .     up at  946692010.13677501678  after  0.11641311645
    5 ... down at  946692014.20031738281
    5 .     up at  946692014.23853874206  after  0.03822135925
    2 ... down at  946692016.39242458343
    2 .     up at  946692016.42747020721  after  0.03504562377
    1 ... down at  946692021.18497848510
    1 .     up at  946692021.22566699981  after  0.04068851470
    >
    

    Usage with no callbacks - ciao Arduino - can look like this:

      var kpd = new Kpd().connect(i2a,pxa,pxi,pxr).enable();­ // ciao Arduino
      var lastUpT = kpd.upT;
      setInterval(function(){
        if ( ! kpd.down && (kpd.upT !== lastUpT)) {
            console.log(kpd.key," up at ",(lastUpT = kpd.upT));
      } },50);
    

    and related output:

    7  up at  946692250.16881752014
    6  up at  946692252.47182941436
    6  up at  946692253.89186477661
    8  up at  946692255.25380229949
    6  up at  946692264.55666446685
    

    Even though the interval driven polling / Arduino loop is not the most efficient, it is though already a lot more efficient that doing just every thing with polling / in loop.

    As mentioned earlier, the implementation could be leaner (.enable() and ```.disable() can be removed when arm part of .enable is put into connect), including more efficient to the point that also the key up is detected with interrupts and setWatch. Eventually I will get 'there'. Also, it would be nice to have a 'key repeat' callback and related configuration... *...I'm thinking about it.... (help / enhancement contributions are always welcome...).

About

Avatar for allObjects @allObjects started