-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.py
30 lines (25 loc) · 953 Bytes
/
components.py
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
from microcontroller import Pin
from rotaryio import IncrementalEncoder
class Encoder:
BUTTON_COUNT = 2 # only rotary click buttons are counted
def __init__(self, pin_a: Pin, pin_b: Pin, divisor: int = 4):
self.encoder = IncrementalEncoder(
pin_a=pin_a,
pin_b=pin_b,
divisor=divisor
)
self.last_position = self.encoder.position
def get_click_amount(self):
"""
returns amount of clicks made with encoder since last position check
- negative values describe left turn direction
- positive values describe right turn direction
"""
position = self.encoder.position
if position == self.last_position:
return 0
click_amount: int = abs(position - self.last_position)
if position < self.last_position:
click_amount *= -1
self.last_position = position
return click_amount