From d21fd6410013fc692b13e8209bd88394de7dc1b4 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 24 Sep 2019 01:17:13 +0200 Subject: [PATCH] Added motion_events_per_sec measurement --- platypush/plugins/gpio/sensor/motion/pwm3901.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/platypush/plugins/gpio/sensor/motion/pwm3901.py b/platypush/plugins/gpio/sensor/motion/pwm3901.py index 5870fbaf..97ccb890 100644 --- a/platypush/plugins/gpio/sensor/motion/pwm3901.py +++ b/platypush/plugins/gpio/sensor/motion/pwm3901.py @@ -1,5 +1,6 @@ import enum import math +import time # noinspection PyUnresolvedReferences,PyPackageRequirements from pmw3901 import PMW3901, BG_CS_FRONT_BCM, BG_CS_BACK_BCM @@ -45,6 +46,8 @@ class GpioSensorMotionPwm3901Plugin(GpioSensorPlugin): super().__init__(**kwargs) self.spi_port = spi_port self._sensor = None + self._events_per_sec = {} + self.x, self.y = (0, 0) try: if isinstance(rotation, int): @@ -84,6 +87,7 @@ class GpioSensorMotionPwm3901Plugin(GpioSensorPlugin): "motion_x": 3, # Detected motion vector X-coord "motion_y": 4, # Detected motion vector Y-coord "motion_mod": 5 # Detected motion vector module + "motion_events_per_sec": 7 # Number of motion events detected in the last second } """ @@ -97,10 +101,20 @@ class GpioSensorMotionPwm3901Plugin(GpioSensorPlugin): self._sensor = None return {}, str(e) + secs = int(time.time()) + if (x, y) != (self.x, self.y): + (self.x, self.y) = (x, y) + + if secs not in self._events_per_sec: + self._events_per_sec = {secs: 1} + else: + self._events_per_sec[secs] += 1 + return { 'motion_x': x, 'motion_y': y, 'motion_mod': math.sqrt(x * x + y * y), + 'motion_events_per_sec': self._events_per_sec[secs], }