mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Tidy up
This commit is contained in:
@@ -22,11 +22,13 @@ from .output_devices import (
|
|||||||
LED,
|
LED,
|
||||||
Buzzer,
|
Buzzer,
|
||||||
Motor,
|
Motor,
|
||||||
|
Robot,
|
||||||
)
|
)
|
||||||
from .boards import (
|
from .boards import (
|
||||||
TrafficLights,
|
TrafficLights,
|
||||||
PiTraffic,
|
PiTraffic
|
||||||
FishDish,
|
FishDish,
|
||||||
|
TrafficHat
|
||||||
PiLiter,
|
PiLiter,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,4 +41,4 @@ atexit.register(gpiozero_shutdown)
|
|||||||
GPIO.setmode(GPIO.BCM)
|
GPIO.setmode(GPIO.BCM)
|
||||||
GPIO.setwarnings(False)
|
GPIO.setwarnings(False)
|
||||||
|
|
||||||
__version__ = '0.3.0'
|
__version__ = '0.3.1'
|
||||||
|
|||||||
@@ -82,10 +82,8 @@ class GPIOQueue(GPIOThread):
|
|||||||
|
|
||||||
def fill(self):
|
def fill(self):
|
||||||
try:
|
try:
|
||||||
while (
|
while (not self.stopping.wait(self.sample_wait) and
|
||||||
not self.stopping.wait(self.sample_wait) and
|
len(self.queue) < self.queue.maxlen):
|
||||||
len(self.queue) < self.queue.maxlen
|
|
||||||
):
|
|
||||||
self.queue.append(self.parent._read())
|
self.queue.append(self.parent._read())
|
||||||
if self.partial:
|
if self.partial:
|
||||||
self.parent._fire_events()
|
self.parent._fire_events()
|
||||||
@@ -96,4 +94,3 @@ class GPIOQueue(GPIOThread):
|
|||||||
except ReferenceError:
|
except ReferenceError:
|
||||||
# Parent is dead; time to die!
|
# Parent is dead; time to die!
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ class WaitableInputDevice(InputDevice):
|
|||||||
|
|
||||||
def _get_when_activated(self):
|
def _get_when_activated(self):
|
||||||
return self._when_activated
|
return self._when_activated
|
||||||
|
|
||||||
def _set_when_activated(self, value):
|
def _set_when_activated(self, value):
|
||||||
if not callable(value) and value is not None:
|
if not callable(value) and value is not None:
|
||||||
raise InputDeviceError('value must be None or a function')
|
raise InputDeviceError('value must be None or a function')
|
||||||
@@ -60,10 +61,12 @@ class WaitableInputDevice(InputDevice):
|
|||||||
|
|
||||||
def _get_when_deactivated(self):
|
def _get_when_deactivated(self):
|
||||||
return self._when_deactivated
|
return self._when_deactivated
|
||||||
|
|
||||||
def _set_when_deactivated(self, value):
|
def _set_when_deactivated(self, value):
|
||||||
if not callable(value) and value is not None:
|
if not callable(value) and value is not None:
|
||||||
raise InputDeviceError('value must be None or a function')
|
raise InputDeviceError('value must be None or a function')
|
||||||
self._when_deactivated = value
|
self._when_deactivated = value
|
||||||
|
|
||||||
when_deactivated = property(_get_when_deactivated, _set_when_deactivated)
|
when_deactivated = property(_get_when_deactivated, _set_when_deactivated)
|
||||||
|
|
||||||
def _fire_events(self):
|
def _fire_events(self):
|
||||||
@@ -94,8 +97,9 @@ class DigitalInputDevice(WaitableInputDevice):
|
|||||||
super(DigitalInputDevice, self).__init__(pin, pull_up)
|
super(DigitalInputDevice, self).__init__(pin, pull_up)
|
||||||
# Yes, that's really the default bouncetime in RPi.GPIO...
|
# Yes, that's really the default bouncetime in RPi.GPIO...
|
||||||
GPIO.add_event_detect(
|
GPIO.add_event_detect(
|
||||||
self.pin, GPIO.BOTH, callback=self._fire_events,
|
self.pin, GPIO.BOTH, callback=self._fire_events,
|
||||||
bouncetime=-666 if bouncetime is None else bouncetime)
|
bouncetime=-666 if bouncetime is None else bouncetime
|
||||||
|
)
|
||||||
# Call _fire_events once to set initial state of events
|
# Call _fire_events once to set initial state of events
|
||||||
super(DigitalInputDevice, self)._fire_events()
|
super(DigitalInputDevice, self)._fire_events()
|
||||||
|
|
||||||
@@ -131,8 +135,11 @@ class SmoothedInputDevice(WaitableInputDevice):
|
|||||||
|
|
||||||
def _set_threshold(self, value):
|
def _set_threshold(self, value):
|
||||||
if not (0.0 < value < 1.0):
|
if not (0.0 < value < 1.0):
|
||||||
raise InputDeviceError('threshold must be between zero and one exclusive')
|
raise InputDeviceError(
|
||||||
|
'threshold must be between zero and one exclusive'
|
||||||
|
)
|
||||||
self._threshold = float(value)
|
self._threshold = float(value)
|
||||||
|
|
||||||
threshold = property(_get_threshold, _set_threshold)
|
threshold = property(_get_threshold, _set_threshold)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -156,8 +163,9 @@ class MotionSensor(SmoothedInputDevice):
|
|||||||
self, pin=None, queue_len=5, sample_rate=10, threshold=0.5,
|
self, pin=None, queue_len=5, sample_rate=10, threshold=0.5,
|
||||||
partial=False):
|
partial=False):
|
||||||
super(MotionSensor, self).__init__(
|
super(MotionSensor, self).__init__(
|
||||||
pin, pull_up=False, threshold=threshold,
|
pin, pull_up=False, threshold=threshold,
|
||||||
queue_len=queue_len, sample_wait=1 / sample_rate, partial=partial)
|
queue_len=queue_len, sample_wait=1 / sample_rate, partial=partial
|
||||||
|
)
|
||||||
self._queue.start()
|
self._queue.start()
|
||||||
|
|
||||||
motion_detected = _alias('is_active')
|
motion_detected = _alias('is_active')
|
||||||
@@ -174,11 +182,14 @@ class LightSensor(SmoothedInputDevice):
|
|||||||
self, pin=None, queue_len=5, charge_time_limit=0.01,
|
self, pin=None, queue_len=5, charge_time_limit=0.01,
|
||||||
threshold=0.1, partial=False):
|
threshold=0.1, partial=False):
|
||||||
super(LightSensor, self).__init__(
|
super(LightSensor, self).__init__(
|
||||||
pin, pull_up=False, threshold=threshold,
|
pin, pull_up=False, threshold=threshold,
|
||||||
queue_len=queue_len, sample_wait=0.0, partial=partial)
|
queue_len=queue_len, sample_wait=0.0, partial=partial
|
||||||
|
)
|
||||||
self._charge_time_limit = charge_time_limit
|
self._charge_time_limit = charge_time_limit
|
||||||
self._charged = Event()
|
self._charged = Event()
|
||||||
GPIO.add_event_detect(self.pin, GPIO.RISING, lambda channel: self._charged.set())
|
GPIO.add_event_detect(
|
||||||
|
self.pin, GPIO.RISING, lambda channel: self._charged.set()
|
||||||
|
)
|
||||||
self._queue.start()
|
self._queue.start()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
@@ -198,7 +209,10 @@ class LightSensor(SmoothedInputDevice):
|
|||||||
self._charged.clear()
|
self._charged.clear()
|
||||||
GPIO.setup(self.pin, GPIO.IN)
|
GPIO.setup(self.pin, GPIO.IN)
|
||||||
self._charged.wait(self.charge_time_limit)
|
self._charged.wait(self.charge_time_limit)
|
||||||
return 1.0 - min(self.charge_time_limit, time() - start) / self.charge_time_limit
|
return (
|
||||||
|
1.0 - min(self.charge_time_limit, time() - start) /
|
||||||
|
self.charge_time_limit
|
||||||
|
)
|
||||||
|
|
||||||
light_detected = _alias('is_active')
|
light_detected = _alias('is_active')
|
||||||
|
|
||||||
@@ -209,9 +223,7 @@ class LightSensor(SmoothedInputDevice):
|
|||||||
wait_for_dark = _alias('wait_for_inactive')
|
wait_for_dark = _alias('wait_for_inactive')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TemperatureSensor(W1ThermSensor):
|
class TemperatureSensor(W1ThermSensor):
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
return self.get_temperature()
|
return self.get_temperature()
|
||||||
|
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -24,7 +24,7 @@ setup(
|
|||||||
],
|
],
|
||||||
long_description=read('README.rst'),
|
long_description=read('README.rst'),
|
||||||
classifiers=[
|
classifiers=[
|
||||||
"Development Status :: 1 - Planning",
|
"Development Status :: 3 - Alpha",
|
||||||
"Intended Audience :: Education",
|
"Intended Audience :: Education",
|
||||||
"Topic :: Education",
|
"Topic :: Education",
|
||||||
"Topic :: System :: Hardware",
|
"Topic :: System :: Hardware",
|
||||||
|
|||||||
Reference in New Issue
Block a user