"""Encoder and button test for the airband bridge (rev B). MicroPython on a Pico 2. Run it from the Mac, with the Pico on USB and the 12 V supply on: uvx mpremote cp bridge/firmware/lcd.py :lcd.py + run bridge/firmware/encoder_test.py The LCD shows, live: Enc +0 SW:up one per click, up one way and down the other; SW:DN while the knob is pushed B1:- B2:- B3:- a * replaces the - while that button is held It runs until the Pico is reset or another program is started. Every change is also printed on the Mac. Wiring, from bridge/README.md: encoder SW = GP0, A = GP1, B = GP2; buttons B1-B3 = GP8-GP10. All switch to ground and use the Pico's own pull-ups. """ from machine import Pin import time from lcd import Lcd, wait_for_power ENC_SW, ENC_A, ENC_B = 0, 1, 2 BUTTONS = (8, 9, 10) STEPS_PER_CLICK = 4 # this encoder makes four electrical steps between detents # One entry per (previous A,B state, new A,B state): +1, -1, or 0 for no move or a skipped step. STEP = (0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0) a = Pin(ENC_A, Pin.IN, Pin.PULL_UP) b = Pin(ENC_B, Pin.IN, Pin.PULL_UP) sw = Pin(ENC_SW, Pin.IN, Pin.PULL_UP) buttons = [Pin(g, Pin.IN, Pin.PULL_UP) for g in BUTTONS] count = 0 state = (a.value() << 1) | b.value() def turned(_pin): global count, state new = (a.value() << 1) | b.value() count += STEP[(state << 2) | new] state = new def main(): lcd = Lcd() if not wait_for_power(lcd): print('PROBLEM: no 5 V at the LCD. Switch on the 12 V supply and run this again.') return lcd.start() a.irq(turned, Pin.IRQ_RISING | Pin.IRQ_FALLING) b.irq(turned, Pin.IRQ_RISING | Pin.IRQ_FALLING) print('running: turn the knob, push it, press the buttons') shown = None while True: now = ((count + 2) // STEPS_PER_CLICK, sw.value(), tuple(p.value() for p in buttons)) if now != shown: shown = now n, s, bs = now row0 = 'Enc {:+5d} SW:{}'.format(n, 'up' if s else 'DN') row1 = ' '.join('B{}:{}'.format(i + 1, '-' if v else '*') for i, v in enumerate(bs)) lcd.write(0, row0) lcd.write(1, row1) print(row0, '|', row1) time.sleep_ms(20) main()