-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton_handler.py
More file actions
41 lines (33 loc) · 1.11 KB
/
Copy pathbutton_handler.py
File metadata and controls
41 lines (33 loc) · 1.11 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
from machine import Pin
#Imports dictionary that defines what pins can be used for inputs
from gpioDicts import inputsDict
'''
push button Interface
init:
pushSwitch(input pin number)
external methods:
buttonPressed(no args) - checks state of pin, if high then returns true
NOTE: Only pins in the inputsDict can be assigned for buttons
'''
class pushButton:
def __init__ (self, pin):
self.pin = pin
try:
#check if pin is available for Button by checking dictionary of GPIOs
if pin in inputsDict :
self.button = Pin(pin,Pin.IN,Pin.PULL_UP)
else:
raise OSError
except OSError:
print('GPIO', pin,'not suitable for Input')
print('Choose from:', inputsDict)
def buttonPressed(self):
pressed = False
#using a pull-up resistor, so input is 1 by default and must test if is 0, which shows button is pressed
if self.button.value() == 0:
pressed = True
return pressed
def buttonInterrupt (self):
self.interrupt = self.button.irq(trigger = Pin.IRQ_FALLING, handler = buttonHandler)
def buttonHandler (self,pin):
print("Button press")