pimoroni-pico/micropython/examples/encoder/item_selector.py

40 wiersze
1.2 KiB
Python
Czysty Zwykły widok Historia

2022-05-12 13:45:20 +00:00
import gc
2022-05-12 15:15:40 +00:00
from encoder import Encoder
# from encoder import REVERSED_DIR
2022-05-12 13:45:20 +00:00
"""
A demonstration of a rotary encoder being used to
select items based on its physical position.
This requires that the encoder is positioned in the same
2022-05-12 15:15:40 +00:00
direction at the start of every program run (e.g. upwards).
2022-05-12 13:45:20 +00:00
"""
# Free up hardware resources ahead of creating a new Encoder
gc.collect()
# Create an encoder on the 3 ADC pins, using PIO 0 and State Machine 0
PIN_A = 26 # The A channel pin
PIN_B = 28 # The B channel pin
PIN_C = 27 # The common pin
2022-05-12 15:15:40 +00:00
enc = Encoder(0, 0, (PIN_A, PIN_B), PIN_C)
2022-05-12 13:45:20 +00:00
# Uncomment the below line (and the top import) to reverse the counting direction
2022-05-12 15:15:40 +00:00
# enc.direction(REVERSED_DIR)
2022-05-12 13:45:20 +00:00
# A list of items, up to the encoder's counts_per_rev
ITEMS = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Black", "White"]
last_step = -1
# Loop forever
while True:
step = enc.step()
if step != last_step:
if step < len(ITEMS):
2022-05-12 15:15:40 +00:00
print(step, "/", int(enc.counts_per_rev()) - 1, ": ", ITEMS[step], sep="")
2022-05-12 13:45:20 +00:00
else:
2022-05-12 15:15:40 +00:00
print(step, "/", int(enc.counts_per_rev()) - 1, ": ", "Undefined", sep="")
2022-05-12 13:45:20 +00:00
last_step = step