-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcbusswitch.py
60 lines (46 loc) · 2.09 KB
/
cbusswitch.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
# cbusswitch.py
import time
import uasyncio as asyncio
from machine import Pin
from micropython import const
import logger
INTERRUPT_GUARD_TIME = const(20)
class cbusswitch:
def __init__(self, pin_number: int):
self.logger = logger.logger()
self.pin = Pin(pin_number, Pin.IN, Pin.PULL_UP)
self.state = self.pin.value()
self.previous_state = self.state
self.previous_state_change_at = time.ticks_ms()
self.previous_state_duration = 0
self.state_changed = False
self.num_interrupts = 0
self.last_interrupt_time = time.ticks_ms()
self.tsf = asyncio.ThreadSafeFlag()
self.switch_changed_state_flag = None
self.pin.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=lambda x: self.tsf.set())
asyncio.create_task(self.interrupt_handler())
def interrupt_handler(self) -> None:
while True:
await self.tsf.wait()
if time.ticks_diff(time.ticks_ms(), self.last_interrupt_time) > INTERRUPT_GUARD_TIME:
self.last_interrupt_time = time.ticks_ms()
self.num_interrupts += 1
self.state = self.pin.value()
if self.state != self.previous_state:
self.state_changed = True
self.previous_state = self.state
self.previous_state_duration = time.ticks_diff(time.ticks_ms(), self.previous_state_change_at)
self.previous_state_change_at = time.ticks_ms()
# self.logger.log(f"switch state changed, state = {self.state}, last duration = {self.previous_state_duration}")
if self.switch_changed_state_flag is not None:
self.switch_changed_state_flag.set()
else:
self.state_changed = False
def is_pressed(self) -> bool:
return self.state == 0
def current_state_duration(self) -> int:
return time.ticks_diff(time.ticks_ms(), self.previous_state_change_at)
def reset(self) -> None:
self.state = 1
self.state_changed = False