From f5487296768b386a4b06931fd8f8b0a4e76aff8e Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Mon, 14 Sep 2015 15:54:54 +0100 Subject: [PATCH] Add MotionSensor --- README.md | 1 + gpio_components/input_devices.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 078fe34..a9daedb 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,4 @@ Abstraction classes for everyday GPIO components, based on RPi.GPIO - LED - Buzzer - Button +- MotionSensor diff --git a/gpio_components/input_devices.py b/gpio_components/input_devices.py index 3155a4a..40a31b2 100644 --- a/gpio_components/input_devices.py +++ b/gpio_components/input_devices.py @@ -37,5 +37,15 @@ class Button(InputDevice): pass +class MotionSensor(InputDevice): + def _is_active_with_pause(self): + sleep(0.1) + return self.is_active() + + def motion_detected(self): + n = 20 + return sum(self._is_active_with_pause() for i in range(n)) > n/2 + + class InputDeviceError(Exception): pass