pep8 cleanup

This commit is contained in:
Ben Nuttall
2015-09-22 09:36:25 +01:00
parent 41c1bfb18f
commit ae1c31e313
3 changed files with 32 additions and 17 deletions

View File

@@ -25,6 +25,8 @@ class GPIODevice(object):
_GPIO_THREADS = set() _GPIO_THREADS = set()
def _gpio_threads_shutdown(): def _gpio_threads_shutdown():
while _GPIO_THREADS: while _GPIO_THREADS:
for t in _GPIO_THREADS.copy(): for t in _GPIO_THREADS.copy():
@@ -46,4 +48,3 @@ class GPIOThread(Thread):
self.stopping.set() self.stopping.set()
self.join() self.join()
_GPIO_THREADS.discard(self) _GPIO_THREADS.discard(self)

View File

@@ -22,7 +22,8 @@ class InputDevice(GPIODevice):
if pull_up: if pull_up:
self._active_state = 0 self._active_state = 0
self._inactive_state = 1 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 @property
def pull_up(self): def pull_up(self):
@@ -68,25 +69,29 @@ class MotionSensor(InputDevice):
def _get_sample_rate(self): def _get_sample_rate(self):
return self._sample_rate return self._sample_rate
def _set_sample_rate(self, value): def _set_sample_rate(self, value):
if value <= 0: if value <= 0:
raise InputDeviceError('sample_rate must be greater than zero') raise InputDeviceError('sample_rate must be greater than zero')
self._sample_rate = value self._sample_rate = value
sample_rate = property(_get_sample_rate, _set_sample_rate) sample_rate = property(_get_sample_rate, _set_sample_rate)
def _get_threshold(self): def _get_threshold(self):
return self._threshold return self._threshold
def _set_threshold(self, value): def _set_threshold(self, value):
if value < 0: if value < 0:
raise InputDeviceError('threshold must be zero or more') raise InputDeviceError('threshold must be zero or more')
self._threshold = value self._threshold = value
threshold = property(_get_threshold, _set_threshold) threshold = property(_get_threshold, _set_threshold)
def _fill_queue(self): def _fill_queue(self):
while ( while (
not self._queue_thread.stopping.wait(1 / self.sample_rate) and not self._queue_thread.stopping.wait(1 / self.sample_rate) and
len(self._queue) < self._queue.maxlen len(self._queue) < self._queue.maxlen
): ):
self._queue.append(self.is_active) self._queue.append(self.is_active)
self._queue_full.set() self._queue_full.set()
while not self._queue_thread.stopping.wait(1 / self.sample_rate): while not self._queue_thread.stopping.wait(1 / self.sample_rate):
@@ -104,7 +109,9 @@ class LightSensor(InputDevice):
self.threshold = threshold self.threshold = threshold
self.partial = partial self.partial = partial
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 = deque(maxlen=queue_len) self._queue = deque(maxlen=queue_len)
self._queue_full = Event() self._queue_full = Event()
self._queue_thread = GPIOThread(target=self._fill_queue) self._queue_thread = GPIOThread(target=self._fill_queue)
@@ -124,7 +131,10 @@ class LightSensor(InputDevice):
if not self.partial: if not self.partial:
self._queue_full.wait() self._queue_full.wait()
try: 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: except ZeroDivisionError:
# No data == no light # No data == no light
return 0.0 return 0.0
@@ -135,14 +145,17 @@ class LightSensor(InputDevice):
def _get_when_light(self): def _get_when_light(self):
return self._when_light return self._when_light
def _set_when_light(self, value): def _set_when_light(self, value):
if not callable(value) and value is not None: if not callable(value) and value is not None:
raise InputDeviceError('when_light must be None or a function') raise InputDeviceError('when_light must be None or a function')
self._when_light = value self._when_light = value
when_light = property(_get_when_light, _set_when_light) when_light = property(_get_when_light, _set_when_light)
def _get_when_dark(self): def _get_when_dark(self):
return self._when_dark return self._when_dark
def _set_when_dark(self, value): def _set_when_dark(self, value):
if not callable(value) and value is not None: if not callable(value) and value is not None:
raise InputDeviceError('when_dark must be None or a function') raise InputDeviceError('when_dark must be None or a function')
@@ -156,27 +169,29 @@ class LightSensor(InputDevice):
def _get_darkness_time(self): def _get_darkness_time(self):
return self._darkness_time return self._darkness_time
def _set_darkness_time(self, value): def _set_darkness_time(self, value):
if value <= 0.0: if value <= 0.0:
raise InputDeviceError('darkness_time must be greater than zero') raise InputDeviceError('darkness_time must be greater than zero')
self._darkness_time = value self._darkness_time = value
# XXX Empty the queue and restart the thread # XXX Empty the queue and restart the thread
darkness_time = property(_get_darkness_time, _set_darkness_time) darkness_time = property(_get_darkness_time, _set_darkness_time)
def _get_threshold(self): def _get_threshold(self):
return self._threshold return self._threshold
def _set_threshold(self, value): def _set_threshold(self, value):
if value < 0: if value < 0:
raise InputDeviceError('threshold must be zero or more') raise InputDeviceError('threshold must be zero or more')
self._threshold = value self._threshold = value
threshold = property(_get_threshold, _set_threshold) threshold = property(_get_threshold, _set_threshold)
def _fill_queue(self): def _fill_queue(self):
try: try:
while ( while (not self._queue_thread.stopping.is_set() and
not self._queue_thread.stopping.is_set() and len(self._queue) < self._queue.maxlen):
len(self._queue) < self._queue.maxlen
):
self._queue.append(self._time_charging()) self._queue.append(self._time_charging())
if self.partial: if self.partial:
self._fire_events() self._fire_events()
@@ -213,9 +228,8 @@ class LightSensor(InputDevice):
if self.when_dark: if self.when_dark:
self.when_dark() self.when_dark()
class TemperatureSensor(W1ThermSensor): class TemperatureSensor(W1ThermSensor):
@property @property
def value(self): def value(self):
return self.get_temperature() return self.get_temperature()

View File

@@ -27,7 +27,9 @@ class LED(OutputDevice):
def blink(self, on_time=1, off_time=1): def blink(self, on_time=1, off_time=1):
self._stop_blink() 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() self._blink_thread.start()
def _stop_blink(self): def _stop_blink(self):
@@ -64,9 +66,7 @@ class Motor(OutputDevice):
class Robot(object): class Robot(object):
def __init__(self, left=None, right=None): def __init__(self, left=None, right=None):
if not all([left, right]): if not all([left, right]):
raise GPIODeviceError( raise GPIODeviceError('left and right pins must be provided')
'left and right pins must be provided'
)
self._left = Motor(left) self._left = Motor(left)
self._right = Motor(right) self._right = Motor(right)