Draft of a queue implementation for MotionSensor

This is a quick *untested* implementation of a background threaded
queue for `MotionSensor`. The optional *queue_len* is the number of
values to store in the queue, *sample_rate* is the number of times per
second to query the motion sensor, and if *partial* is False (the
default) then the `motion_detected` property won't return anything until
the queue has been filled.
This commit is contained in:
Dave Jones
2015-09-15 16:02:16 +01:00
parent 90aed0e925
commit 065b5aa40d

View File

@@ -1,3 +1,9 @@
from __future__ import division
from time import sleep
from threading import Thread, Event
from collections import deque
from RPi import GPIO from RPi import GPIO
from w1thermsensor import W1ThermSensor from w1thermsensor import W1ThermSensor
@@ -36,14 +42,34 @@ class Button(InputDevice):
class MotionSensor(InputDevice): class MotionSensor(InputDevice):
def _is_active_with_pause(self): def __init__(self, pin=None, queue_len=20, sample_rate=10, partial=False):
sleep(0.1) super(MotionSensor, self).__init__(pin)
return self.is_active self._sample_rate = sample_rate
self._partial = partial
self._queue = deque(maxlen=queue_len)
self._queue_full = Event()
self._terminated = False
self._queue_thread = Thread(target=self._fill_queue)
self._queue_thread.start()
def __del__(self):
self._terminated = True
self._queue_thread.join()
def _fill_queue(self):
while not self._terminated and len(self._queue) < self._queue.maxlen:
self._queue.append(self.is_active)
sleep(1 / self._sample_rate)
self._queue_full.set()
while not self._terminated:
self._queue.append(self.is_active)
sleep(1 / self._sample_rate)
@property @property
def motion_detected(self): def motion_detected(self):
n = 20 if not self._partial:
return sum(self._is_active_with_pause() for i in range(n)) > n/2 self._queue_full.wait()
return sum(self._queue) > (len(self._queue) / 2)
class LightSensor(object): class LightSensor(object):