Skip to content

Commit

Permalink
Add button class
Browse files Browse the repository at this point in the history
  • Loading branch information
irvyncornejo committed Jan 25, 2023
1 parent 7912f4f commit f161735
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion raspberry-pico/rpi-gpio-pico/gpiopico/input_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,36 @@ class LM35(AnalogicInputs):
pass

class Button:
def __init__(self, pin) -> None:
def __init__(
self,
pin: int,
show_value: bool = False
) -> None:
self._input = Pin(pin, Pin.IN, Pin.PULL_UP)
self._when_pressed = None
self._show_value = show_value


@property
def when_pressed(self):
return self._when_pressed

@when_pressed.setter
def when_pressed(self, callback):
if (
type(callback).__name__ == 'function' or
type(callback).__name__ == 'bound_method'
):
self._when_pressed = callback
else:
raise ValueError('callback will be a function or bound_method')

def check_state(self):
_value = self._input.value()
if self._when_pressed and _value == 0:
self._when_pressed
(print(_value) if self._show_value else None)
sleep(0.3)

class PIR:
def __init__(
Expand Down

0 comments on commit f161735

Please sign in to comment.