-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotary_irq_esp.py
71 lines (54 loc) · 1.97 KB
/
rotary_irq_esp.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
# The MIT License (MIT)
# Copyright (c) 2020 Mike Teachman
# https://opensource.org/licenses/MIT
# Platform-specific MicroPython code for the rotary encoder module
# ESP8266/ESP32 implementation
# Documentation:
# https://github.com/MikeTeachman/micropython-rotary
"""
from machine import Pin
from rotary import Rotary
class RotaryIRQ(Rotary):
"""Start"""
def __init__(
self,
pin_num_clk,
pin_num_dt,
min_val=0,
max_val=10,
reverse=False,
range_mode=Rotary.RANGE_UNBOUNDED,
pull_up=False
):
super().__init__(min_val, max_val, reverse, range_mode)
if pull_up:
self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP)
self._pin_dt = Pin(pin_num_dt, Pin.IN, Pin.PULL_UP)
else:
self._pin_clk = Pin(pin_num_clk, Pin.IN)
self._pin_dt = Pin(pin_num_dt, Pin.IN)
self._enable_clk_irq(self._process_rotary_pins)
self._enable_dt_irq(self._process_rotary_pins)
def _enable_clk_irq(self, callback=None):
# self._pin_clk.init(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=callback)
self._pin_clk.init(trigger=Pin.IRQ_ANYEDGE, handler=callback)
def _enable_dt_irq(self, callback=None):
# self._pin_dt.init(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=callback)
self._pin_dt.init(trigger=Pin.IRQ_ANYEDGE, handler=callback)
def _disable_clk_irq(self):
self._pin_clk.init(handler=None)
def _disable_dt_irq(self):
self._pin_dt.init(handler=None)
def _hal_get_clk_value(self):
return self._pin_clk.value()
def _hal_get_dt_value(self):
return self._pin_dt.value()
def _hal_enable_irq(self):
self._enable_clk_irq(self._process_rotary_pins)
self._enable_dt_irq(self._process_rotary_pins)
def _hal_disable_irq(self):
self._disable_clk_irq()
self._disable_dt_irq()
def _hal_close(self):
self._hal_disable_irq()