This repository was archived by the owner on Jan 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushbutton.py
More file actions
74 lines (61 loc) · 2.54 KB
/
pushbutton.py
File metadata and controls
74 lines (61 loc) · 2.54 KB
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
71
72
73
74
from machine import Pin
from time import ticks_ms, ticks_diff
class MultiplePB:
"Make multiple click function on a single PB"
def __init__(self, pin, pull, sprint=0):
self.pin = pin
self.pull = pull
self.print = sprint
self.is_pressed = 0
self.pressed = 0
self.pressed_time = 0
self.pressed_trigger = 1000
self.trig = 0
self.trig_time = 0
self.trig_hold = 50
self.wait_hold = 250
self.firstclick = 0
self.output = 0
def state(self):
# Set internal pullup or pulldown resistor
if self.pull==1:
pb = Pin(self.pin, Pin.IN, Pin.PULL_UP)
self.is_pressed = not pb.value()
else:
pb = Pin(self.pin, Pin.IN, Pin.PULL_DOWN)
self.is_pressed = pb.value()
# Sensing the button state if pressed
if self.is_pressed and not self.pressed:
self.pressed = 1
self.pressed_time = ticks_ms()
if self.print: print('Button %s Pressed!' % self)
# Sensing the button state if released
if not self.is_pressed and self.pressed:
self.pressed = 0
interval = ticks_diff(ticks_ms(),self.pressed_time)
if interval >= self.pressed_trigger:
self.output = 3 # LONG PRESSED == 3
else:
if self.firstclick == 1:
self.output = 2 # DOUBLE CLICKED == 2
self.firstclick = 0
else:
self.firstclick = 1
self.wait_secondclick = ticks_ms()
if self.firstclick:
if ticks_diff(ticks_ms(),self.wait_secondclick) >= self.wait_hold:
self.firstclick = 0
self.output = 1 # SINGLE PRESSED == 1
# Start timer for triggered output delay
if (self.output>0) and (self.trig==0):
self.trig = 1
self.trig_time = ticks_ms()
if self.print: print('Button %s State = %d' % (self, self.output))
# Set the outpul LOW or 0 after given set time
if (self.output>0) and (self.trig==1):
if (ticks_diff(ticks_ms(),self.trig_time)) >= self.trig_hold:
self.trig = 0
self.output = 0
if self.print: print('Button %s State = %d\n' % (self, self.output))
# Send the output
return self.output