Add events to all input devices

Fairly major tidy up of the hierarchy as well. There's now a trivial
base class: InputDevice which simply permits reading of state.
WaitableInputDevice descends from this and introduces waitable events
and callbacks, and provides a hook for calling them but needs further
machinery to activate that hook.

DigitalInputDevice (crap name?) descends from WaitableInputDevice and
uses the standard RPi.GPIO callback mechanisms to handle events. This is
intended for use with trivial on/off devices with predictably small
bounce times.

Next is SmoothedInputDevice (crap name?) which also descends from
WaitableInputDevice. This includes a background threaded queue which
constantly monitors the state of the device and provides a running mean
of its state. This is compared to a threshold for determining active /
inactive state. This is intended for use with on/off devices that
"jitter" a lot and for which a running average is therefore appropriate
or for devices which provide an effectively analog readout (like
charging capacitor timings).

MonitorSensor and LightSensor now descend from SmoothedInputDevice, and
Button descends from DigitalInputDevice. All "concrete" classes provide
event aliases appropriate to their function (e.g. when_dark,
when_pressed, etc.)
This commit is contained in:
Dave Jones
2015-09-22 10:37:55 +01:00
parent 4d7f9eeeb3
commit b1913e5e39
3 changed files with 235 additions and 182 deletions

View File

@@ -72,7 +72,6 @@ Alternatively:
```python
from gpiozero import LED
from time import sleep
red = LED(2)
red.blink(1, 1)
@@ -118,7 +117,7 @@ from gpiozero import Button
button = Button(4)
button.wait_for_input()
button.wait_for_press()
print("Button was pressed")
```
@@ -127,44 +126,54 @@ Run a function every time the button is pressed:
```python
from gpiozero import Button
def hello(pin):
print("Button was pressed")
def warning():
print("Don't push the button!")
button = Button(4)
button.add_callback(hello)
button.when_pressed = warning
```
### Motion Sensor
Detect motion:
Detect motion and light an LED when it's detected:
```python
from gpiozero import MotionSensor
from gpiozero import MotionSensor, LED
pir = MotionSensor(5)
led = LED(16)
while True:
if pir.motion_detected:
print("Motion detected")
pir.when_motion = led.on
pir.when_no_motion = led.off
```
### Light Sensor
Retrieve light sensor value:
Wait for light and dark:
```python
from time import sleep
from gpiozero import LightSensor
sensor = LightSensor(18)
while True:
sensor.wait_for_light()
print("It's light! :)")
sensor.wait_for_dark()
print("It's dark :(")
```
Run a function when the light changes:
```python
from gpiozero import LightSensor, LED
sensor = LightSensor(18)
led = LED(16)
sensor.when_dark = led.on
sensor.when_light = led.off
while True:
sleep(1)
```
### Temperature Sensor
@@ -196,3 +205,4 @@ sleep(5)
left_motor.off()
right_motor.off()
```