mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Merge branch 'master' into when-active-when-inactive
Conflicts: gpiozero/devices.py gpiozero/input_devices.py
This commit is contained in:
@@ -8,7 +8,7 @@ from .devices import (
|
||||
_gpio_threads_shutdown,
|
||||
GPIODeviceError,
|
||||
GPIODevice,
|
||||
)
|
||||
)
|
||||
from .input_devices import (
|
||||
InputDeviceError,
|
||||
InputDevice,
|
||||
@@ -23,6 +23,12 @@ from .output_devices import (
|
||||
Buzzer,
|
||||
Motor,
|
||||
)
|
||||
from .boards import (
|
||||
TrafficLights,
|
||||
PiTraffic,
|
||||
FishDish,
|
||||
PiLiter,
|
||||
)
|
||||
|
||||
|
||||
def gpiozero_shutdown():
|
||||
|
||||
76
gpiozero/boards.py
Normal file
76
gpiozero/boards.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from .input_devices import Button
|
||||
from .output_devices import LED, Buzzer
|
||||
from .devices import GPIODeviceError
|
||||
|
||||
|
||||
class TrafficLights(object):
|
||||
def __init__(self, red=None, amber=None, green=None):
|
||||
if not all([red, amber, green]):
|
||||
raise GPIODeviceError('Red, Amber and Green pins must be provided')
|
||||
|
||||
self.red = LED(red)
|
||||
self.amber = LED(amber)
|
||||
self.green = LED(green)
|
||||
self._leds = (self.red, self.amber, self.green)
|
||||
|
||||
def on(self):
|
||||
for led in self._leds:
|
||||
led.on()
|
||||
|
||||
def off(self):
|
||||
for led in self._leds:
|
||||
led.off()
|
||||
|
||||
def blink(self, on_time=1, off_time=1):
|
||||
for led in self._leds:
|
||||
led.blink(on_time, off_time)
|
||||
|
||||
|
||||
class FishDish(TrafficLights):
|
||||
def __init__(self):
|
||||
red, amber, green = (9, 22, 4)
|
||||
super(FishDish, self).__init__(red, amber, green)
|
||||
self.buzzer = Buzzer(8)
|
||||
self.button = Button(pin=7, pull_up=False)
|
||||
self._all = tuple(list(self._leds) + [self.buzzer])
|
||||
|
||||
def on(self):
|
||||
for thing in self._all:
|
||||
thing.on()
|
||||
|
||||
def off(self):
|
||||
for thing in self._all:
|
||||
thing.off()
|
||||
|
||||
def lights_on(self):
|
||||
super.on()
|
||||
|
||||
def lights_off(self):
|
||||
super.off()
|
||||
|
||||
|
||||
class TrafficHat(FishDish):
|
||||
def __init__(self):
|
||||
red, amber, green = (24, 23, 22)
|
||||
super(PiTraffic, self).__init__(red, amber, green)
|
||||
self.buzzer = Buzzer(5)
|
||||
self.button = Button(25)
|
||||
self._all = tuple(list(self._leds) + [self.buzzer])
|
||||
|
||||
|
||||
class PiLiter(object):
|
||||
def __init__(self):
|
||||
leds = (4, 17, 27, 18, 22, 23, 24, 25)
|
||||
self._leds = tuple([LED(led) for led in leds])
|
||||
|
||||
def on(self):
|
||||
for led in self._leds:
|
||||
led.on()
|
||||
|
||||
def off(self):
|
||||
for led in self._leds:
|
||||
led.off()
|
||||
|
||||
def blink(self, on_time=1, off_time=1):
|
||||
for led in self._leds:
|
||||
led.blink(on_time, off_time)
|
||||
@@ -33,6 +33,8 @@ class GPIODevice(object):
|
||||
|
||||
|
||||
_GPIO_THREADS = set()
|
||||
|
||||
|
||||
def _gpio_threads_shutdown():
|
||||
while _GPIO_THREADS:
|
||||
for t in _GPIO_THREADS.copy():
|
||||
@@ -95,4 +97,3 @@ class GPIOQueue(GPIOThread):
|
||||
# Parent is dead; time to die!
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@@ -128,6 +128,7 @@ class SmoothedInputDevice(WaitableInputDevice):
|
||||
|
||||
def _get_threshold(self):
|
||||
return self._threshold
|
||||
|
||||
def _set_threshold(self, value):
|
||||
if not (0.0 < value < 1.0):
|
||||
raise InputDeviceError('threshold must be between zero and one exclusive')
|
||||
@@ -208,6 +209,7 @@ class LightSensor(SmoothedInputDevice):
|
||||
wait_for_dark = _alias('wait_for_inactive')
|
||||
|
||||
|
||||
|
||||
class TemperatureSensor(W1ThermSensor):
|
||||
@property
|
||||
def value(self):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from RPi import GPIO
|
||||
from time import sleep
|
||||
|
||||
from .devices import GPIODeviceError, GPIODevice, GPIOThread
|
||||
|
||||
@@ -24,9 +25,11 @@ class LED(OutputDevice):
|
||||
super(LED, self).__init__(pin)
|
||||
self._blink_thread = None
|
||||
|
||||
def blink(self, on_time, off_time):
|
||||
def blink(self, on_time=1, off_time=1):
|
||||
self._stop_blink()
|
||||
self._blink_thread = GPIOThread(target=self._blink_led, args=(on_time, off_time))
|
||||
self._blink_thread = GPIOThread(
|
||||
target=self._blink_led, args=(on_time, off_time)
|
||||
)
|
||||
self._blink_thread.start()
|
||||
|
||||
def _stop_blink(self):
|
||||
@@ -58,3 +61,35 @@ class Buzzer(OutputDevice):
|
||||
|
||||
class Motor(OutputDevice):
|
||||
pass
|
||||
|
||||
|
||||
class Robot(object):
|
||||
def __init__(self, left=None, right=None):
|
||||
if not all([left, right]):
|
||||
raise GPIODeviceError('left and right pins must be provided')
|
||||
|
||||
self._left = Motor(left)
|
||||
self._right = Motor(right)
|
||||
|
||||
def left(self, seconds=None):
|
||||
self._left.on()
|
||||
if seconds is not None:
|
||||
sleep(seconds)
|
||||
self._left.off()
|
||||
|
||||
def right(self, seconds=None):
|
||||
self._right.on()
|
||||
if seconds is not None:
|
||||
sleep(seconds)
|
||||
self._right.off()
|
||||
|
||||
def forwards(self, seconds=None):
|
||||
self.left()
|
||||
self.right()
|
||||
if seconds is not None:
|
||||
sleep(seconds)
|
||||
self.stop()
|
||||
|
||||
def stop(self):
|
||||
self._left.off()
|
||||
self._right.off()
|
||||
|
||||
Reference in New Issue
Block a user