File tree Expand file tree Collapse file tree 2 files changed +32
-11
lines changed Expand file tree Collapse file tree 2 files changed +32
-11
lines changed Original file line number Diff line number Diff line change
1
+ from machine import Pin
1
2
from hardware import Button
2
3
4
+ SWITCH = 1
5
+ COUNTER = 2
3
6
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
Original file line number Diff line number Diff line change 1
1
from machine import Pin
2
+ from hardware import Button
2
3
3
4
SWITCH = 1
4
5
COUNTER = 2
5
6
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 ):
9
9
self .count_value = 0
10
+ self .curr_status = 0
11
+ self .prev_status = 0 if active_low else 1
10
12
self .pin = Pin (port [0 ], Pin .IN , Pin .PULL_UP )
13
+ super ().__init__ (port [0 ], active_low = active_low )
11
14
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 ))
17
16
18
17
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
20
22
21
23
def count_reset (self ):
22
24
self .count_value = 0
You can’t perform that action at this time.
0 commit comments