diff --git a/gpiozero/devices.py b/gpiozero/devices.py index 22d4048..67c90c8 100644 --- a/gpiozero/devices.py +++ b/gpiozero/devices.py @@ -25,6 +25,8 @@ class GPIODevice(object): _GPIO_THREADS = set() + + def _gpio_threads_shutdown(): while _GPIO_THREADS: for t in _GPIO_THREADS.copy(): @@ -46,4 +48,3 @@ class GPIOThread(Thread): self.stopping.set() self.join() _GPIO_THREADS.discard(self) - diff --git a/gpiozero/input_devices.py b/gpiozero/input_devices.py index 0fbec6a..4490a22 100644 --- a/gpiozero/input_devices.py +++ b/gpiozero/input_devices.py @@ -22,7 +22,8 @@ class InputDevice(GPIODevice): if pull_up: self._active_state = 0 self._inactive_state = 1 - GPIO.setup(pin, GPIO.IN, (GPIO.PUD_DOWN, GPIO.PUD_UP)[pull_up]) + pull = GPIO.PUD_UP if pull_up else GPIO.PUD_DOWN + GPIO.setup(pin, GPIO.IN, pull) @property def pull_up(self): @@ -68,25 +69,29 @@ class MotionSensor(InputDevice): def _get_sample_rate(self): return self._sample_rate + def _set_sample_rate(self, value): if value <= 0: raise InputDeviceError('sample_rate must be greater than zero') self._sample_rate = value + sample_rate = property(_get_sample_rate, _set_sample_rate) def _get_threshold(self): return self._threshold + def _set_threshold(self, value): if value < 0: raise InputDeviceError('threshold must be zero or more') self._threshold = value + threshold = property(_get_threshold, _set_threshold) def _fill_queue(self): while ( - not self._queue_thread.stopping.wait(1 / self.sample_rate) and - len(self._queue) < self._queue.maxlen - ): + not self._queue_thread.stopping.wait(1 / self.sample_rate) and + len(self._queue) < self._queue.maxlen + ): self._queue.append(self.is_active) self._queue_full.set() while not self._queue_thread.stopping.wait(1 / self.sample_rate): @@ -104,7 +109,9 @@ class LightSensor(InputDevice): self.threshold = threshold self.partial = partial 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 = deque(maxlen=queue_len) self._queue_full = Event() self._queue_thread = GPIOThread(target=self._fill_queue) @@ -124,7 +131,10 @@ class LightSensor(InputDevice): if not self.partial: self._queue_full.wait() try: - return 1.0 - (sum(self._queue) / len(self._queue)) / self.darkness_time + return ( + 1.0 - (sum(self._queue) / len(self._queue)) / + self.darkness_time + ) except ZeroDivisionError: # No data == no light return 0.0 @@ -135,14 +145,17 @@ class LightSensor(InputDevice): def _get_when_light(self): return self._when_light + def _set_when_light(self, value): if not callable(value) and value is not None: raise InputDeviceError('when_light must be None or a function') self._when_light = value + when_light = property(_get_when_light, _set_when_light) def _get_when_dark(self): return self._when_dark + def _set_when_dark(self, value): if not callable(value) and value is not None: raise InputDeviceError('when_dark must be None or a function') @@ -156,27 +169,29 @@ class LightSensor(InputDevice): def _get_darkness_time(self): return self._darkness_time + def _set_darkness_time(self, value): if value <= 0.0: raise InputDeviceError('darkness_time must be greater than zero') self._darkness_time = value # XXX Empty the queue and restart the thread + darkness_time = property(_get_darkness_time, _set_darkness_time) def _get_threshold(self): return self._threshold + def _set_threshold(self, value): if value < 0: raise InputDeviceError('threshold must be zero or more') self._threshold = value + threshold = property(_get_threshold, _set_threshold) def _fill_queue(self): try: - while ( - not self._queue_thread.stopping.is_set() and - len(self._queue) < self._queue.maxlen - ): + while (not self._queue_thread.stopping.is_set() and + len(self._queue) < self._queue.maxlen): self._queue.append(self._time_charging()) if self.partial: self._fire_events() @@ -213,9 +228,8 @@ class LightSensor(InputDevice): if self.when_dark: self.when_dark() + class TemperatureSensor(W1ThermSensor): @property def value(self): return self.get_temperature() - - diff --git a/gpiozero/output_devices.py b/gpiozero/output_devices.py index 0bdd42a..3a5840e 100644 --- a/gpiozero/output_devices.py +++ b/gpiozero/output_devices.py @@ -27,7 +27,9 @@ class LED(OutputDevice): 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): @@ -64,9 +66,7 @@ class Motor(OutputDevice): class Robot(object): def __init__(self, left=None, right=None): if not all([left, right]): - raise GPIODeviceError( - 'left and right pins must be provided' - ) + raise GPIODeviceError('left and right pins must be provided') self._left = Motor(left) self._right = Motor(right)