• Verified (and enhanced) code creates this console output:

     1v80 Copyright 2015 G.Williams
    >echo(0);
    =undefined
    949377234.41604518890
    scan si: 0 - di: 0 - s: 0
    949377234.41604518890: Button # 0 pressed: pwr toggle
    4.81315994262
    scan si: 0 - di: 0 - s: 0
    scan si: 0 - di: 1 - s: 0
    949377241.43724060058: Button # 2 pressed: temp down
    -1.82220757007
    2.95375728607
    scan si: 0 - di: 0 - s: 0
    scan si: 0 - di: 1 - s: 0
    scan si: 0 - di: 2 - s: 0
    949377246.60013008117: Button # 4 pressed: fan toggle
    6.82493495941
    scan si: 0 - di: 0 - s: 0
    scan si: 0 - di: 1 - s: 0
    949377255.63546943664: Button # 2 pressed: temp down
    78.17845439910
    scan si: 1 - di: 0 - s: 0
    scan si: 1 - di: 1 - s: 0
    949377336.02310276031: Button # 3 pressed: temp up
    -1.92238414287
    2.93543815612
    scan si: 1 - di: 0 - s: 0
    949377341.16769027709: Button # 1 pressed: N/C
    0.59415245056
    scan si: 1 - di: 0 - s: 0
    scan si: 1 - di: 1 - s: 0
    scan si: 1 - di: 2 - s: 0
    949377343.96973037719: Button # 5 pressed: N/C
    > 
    

    Noteworthy:

    1. Code needed to discard multiple physical and logical press events within the debounce time: happening while pressing temp down and temp up the first time (tp < np, a negative value, this_press_time < nextallowedpress time) - in lines 11. and 25.

    2. Scanning is optimized - and simplified - and so the result: 'first' (lowest) detected pressed button (number) is used. If multi-pressings are not (yet) detected.

    3. Enhancements to think about are:

      • detection of multiple button presses and optionally allow / disallow
      • press and hold button
      • provide press (button down) and release (button up) information in either same or
        separate callbacks.
      • press-'fumblin' (timely overlapping of presses of multiple/different buttons

    4. You can use any number of driving and sensing pins. Using Espruino directly - driving and sensing pins each using a GPIO pin - is 'expensive'. Using some SPI or I2C driven port yields more pins for other things... but requires this extra chip and wiring...

    5. Was a fun exercise - with some serious challenges as every HI / input (and output) UI has.

    6. Useful for marketed and DIY matrix key pads.

    7. Practically module-ready...

About

Avatar for allObjects @allObjects started