|
| 1 | +# SPDX-FileCopyrightText: 2021 John Furcean |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +"""I2C ANO rotary encoder with 7 segment display example.""" |
| 5 | + |
| 6 | +import board |
| 7 | +from adafruit_ht16k33 import segments |
| 8 | +from adafruit_seesaw import seesaw, rotaryio, digitalio |
| 9 | + |
| 10 | +# For use with the STEMMA connector on QT Py RP2040 |
| 11 | +# import busio |
| 12 | +# i2c = busio.I2C(board.SCL1, board.SDA1) |
| 13 | +# seesaw = seesaw.Seesaw(i2c, 0x49) |
| 14 | + |
| 15 | +i2c = board.I2C() # uses board.SCL and board.SDA |
| 16 | +# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller |
| 17 | +seesaw = seesaw.Seesaw(i2c, addr=0x49) |
| 18 | +display = segments.Seg14x4(i2c, address=0x70) |
| 19 | + |
| 20 | +seesaw_product = (seesaw.get_version() >> 16) & 0xFFFF |
| 21 | +print(f"Found product {seesaw_product}") |
| 22 | +if seesaw_product != 5740: |
| 23 | + print("Wrong firmware loaded? Expected 5740") |
| 24 | + |
| 25 | +seesaw.pin_mode(1, seesaw.INPUT_PULLUP) |
| 26 | +seesaw.pin_mode(2, seesaw.INPUT_PULLUP) |
| 27 | +seesaw.pin_mode(3, seesaw.INPUT_PULLUP) |
| 28 | +seesaw.pin_mode(4, seesaw.INPUT_PULLUP) |
| 29 | +seesaw.pin_mode(5, seesaw.INPUT_PULLUP) |
| 30 | + |
| 31 | +select = digitalio.DigitalIO(seesaw, 1) |
| 32 | +select_held = False |
| 33 | +up = digitalio.DigitalIO(seesaw, 2) |
| 34 | +up_held = False |
| 35 | +left = digitalio.DigitalIO(seesaw, 3) |
| 36 | +left_held = False |
| 37 | +down = digitalio.DigitalIO(seesaw, 4) |
| 38 | +down_held = False |
| 39 | +right = digitalio.DigitalIO(seesaw, 5) |
| 40 | +right_held = False |
| 41 | + |
| 42 | +encoder = rotaryio.IncrementalEncoder(seesaw) |
| 43 | +last_position = None |
| 44 | + |
| 45 | +buttons = [select, up, left, down, right] |
| 46 | +button_names = ["Select", "Up", "Left", "Down", "Right"] |
| 47 | +button_states = [select_held, up_held, left_held, down_held, right_held] |
| 48 | +seven_segment_names = ["SELE", " UP ", "LEFT", "DOWN", "RIGH"] |
| 49 | + |
| 50 | +while True: |
| 51 | + position = encoder.position |
| 52 | + |
| 53 | + if position != last_position: |
| 54 | + last_position = position |
| 55 | + display.print(f" {position}") |
| 56 | + print(f"Position: {position}") |
| 57 | + |
| 58 | + for b in range(5): |
| 59 | + if not buttons[b].value and button_states[b] is False: |
| 60 | + button_states[b] = True |
| 61 | + display.print(seven_segment_names[b]) |
| 62 | + print(f"{button_names[b]} button pressed") |
| 63 | + |
| 64 | + if buttons[b].value and button_states[b] is True: |
| 65 | + button_states[b] = False |
| 66 | + display.print(" ") |
| 67 | + print(f"{button_names[b]} button released") |
0 commit comments