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