forked from pimoroni/inventorhatmini-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio_encoder.py
More file actions
30 lines (21 loc) · 911 Bytes
/
gpio_encoder.py
File metadata and controls
30 lines (21 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import time
from inventorhatmini import InventorHATMini, GPIO_1, GPIO_2
# from ioexpander.common import REVERSED_DIR
"""
An example of how to read a rotary encoder connected to Inventor HAT Mini's GPIO pins.
Press "User" to exit the program.
"""
# Constants
CHANNEL = 3 # The encoder channel to use (1 and 2 are used by Inventor's motor connectors)
# Create a new InventorHATMini
board = InventorHATMini(init_leds=False)
# Create an Encoder object using two GPIO pins
enc = board.encoder_from_gpio_pins(CHANNEL, GPIO_1, GPIO_2)
# Uncomment the below line (and the top imports) to
# reverse the counting direction of the encoder
# enc.direction(REVERSED_DIR)
# Read the encoder until the user button is pressed
while not board.switch_pressed():
# Print out the current step and turn of the rotary encoder
print("Step = ", enc.step(), ", Turn = ", enc.turn(), sep="")
time.sleep(0.1)