-
Notifications
You must be signed in to change notification settings - Fork 5
/
encoderLib.py
38 lines (31 loc) · 1.04 KB
/
encoderLib.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
31
32
33
34
35
36
37
38
from machine import Pin
from machine import Timer
class encoder:
# Init variables
encoder_clk_prev = False
i = 0
def __init__(self, clk_pin, dt_pin):
# Configure the rotary encoder pins and interupt
self.clk = Pin(clk_pin, Pin.IN, Pin.PULL_UP)
self.dt = Pin(dt_pin, Pin.IN, Pin.PULL_UP)
tim = Timer(-1)
tim.init( # Timer to run self.update every 5ms
period=5,
mode=Timer.PERIODIC,
callback=self.update
)
def getValue(self):
return(self.i) # Return rotary encoder value
# Non blocking delay of 5ms
def update(self, p):
# Read the rotary encoder pins
self.encoder_clk = self.clk.value()
self.encoder_dt = self.dt.value()
# If rotary encoder rotated
if not self.encoder_clk and self.encoder_clk_prev:
# Get direction of rotation
if self.encoder_dt:
self.i += 1
else:
self.i -= 1
self.encoder_clk_prev = self.encoder_clk