Skip to content

Commit 9f298e9

Browse files
committed
program correction of unit limit and unit op90, op180
1 parent 8ec31a6 commit 9f298e9

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

m5stack/libs/unit/limit.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
from machine import Pin
12
from hardware import Button
23

4+
SWITCH = 1
5+
COUNTER = 2
36

4-
def LIMITUnit(port):
5-
return Button(port[0])
7+
class LIMITUnit(Button):
8+
def __init__(self, port, active_low=True, type=SWITCH):
9+
self.count_value = 0
10+
self.curr_status = 0
11+
self.prev_status = 0 if active_low else 1
12+
self.pin = Pin(port[0], Pin.IN, Pin.PULL_UP)
13+
super().__init__(port[0], active_low=active_low)
14+
if type != SWITCH:
15+
self.pin.irq(handler=self._cb_irq, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING))
16+
17+
def _cb_irq(self, arg):
18+
self.curr_status = self.pin.value()
19+
if self.curr_status != self.prev_status:
20+
self.count_value += 1
21+
self.prev_status = self.curr_status
22+
23+
def count_reset(self):
24+
self.count_value = 0

m5stack/libs/unit/op.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
from machine import Pin
2+
from hardware import Button
23

34
SWITCH = 1
45
COUNTER = 2
56

6-
7-
class OPUnit:
8-
def __init__(self, port, type=SWITCH):
7+
class OPUnit(Button):
8+
def __init__(self, port, active_low=True, type=SWITCH):
99
self.count_value = 0
10+
self.curr_status = 0
11+
self.prev_status = 0 if active_low else 1
1012
self.pin = Pin(port[0], Pin.IN, Pin.PULL_UP)
13+
super().__init__(port[0], active_low=active_low)
1114
if type != SWITCH:
12-
self.pin.irq(handler=self._cb_irq, trigger=Pin.IRQ_RISING)
13-
14-
@property
15-
def get_value(self):
16-
return self.pin.value()
15+
self.pin.irq(handler=self._cb_irq, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING))
1716

1817
def _cb_irq(self, arg):
19-
self.count_value += 1
18+
self.curr_status = self.pin.value()
19+
if self.curr_status != self.prev_status:
20+
self.count_value += 1
21+
self.prev_status = self.curr_status
2022

2123
def count_reset(self):
2224
self.count_value = 0

0 commit comments

Comments
 (0)