Simplified motion sensor interface by removing unneeded absolute vector

This commit is contained in:
Fabio Manganiello 2019-09-24 00:13:02 +02:00
parent 3da6c41f21
commit d6de1189e5
2 changed files with 8 additions and 31 deletions

View file

@ -13,26 +13,12 @@ class SensorMotionPwm3901Backend(SensorBackend):
_default_poll_seconds = 0.01 _default_poll_seconds = 0.01
def __init__(self, absolute=True, relative=True, **kwargs): def __init__(self, **kwargs):
"""
:param absolute: Enable absolute motion sensor events (default: true)
:param relative: Enable relative motion sensor events (default: true)
"""
enabled_sensors = {
'motion_rel_x': relative,
'motion_rel_y': relative,
'motion_rel_mod': relative,
'motion_abs_x': absolute,
'motion_abs_y': absolute,
'motion_abs_mod': absolute,
}
if 'poll_seconds' not in kwargs: if 'poll_seconds' not in kwargs:
# noinspection PyTypeChecker # noinspection PyTypeChecker
kwargs['poll_seconds'] = self._default_poll_seconds kwargs['poll_seconds'] = self._default_poll_seconds
super().__init__(plugin='gpio.sensor.motion.pwm3901', enabled_sensors=enabled_sensors, **kwargs) super().__init__(plugin='gpio.sensor.motion.pwm3901', **kwargs)
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -45,7 +45,6 @@ class GpioSensorMotionPwm3901Plugin(GpioSensorPlugin):
super().__init__(**kwargs) super().__init__(**kwargs)
self.spi_port = spi_port self.spi_port = spi_port
self._sensor = None self._sensor = None
(self.x, self.y) = (0, 0)
try: try:
if isinstance(rotation, int): if isinstance(rotation, int):
@ -82,28 +81,20 @@ class GpioSensorMotionPwm3901Plugin(GpioSensorPlugin):
:returns: dict. Example:: :returns: dict. Example::
output = { output = {
"motion_rel_x": 0, # Detected relative motion vector X-coord "motion_x": 3, # Detected motion vector X-coord
"motion_rel_y": 1, # Detected relative motion vector Y-coord "motion_y": 4, # Detected motion vector Y-coord
"motion_abs_x": 3, # Detected absolute motion vector X-coord "motion_mod": 5 # Detected motion vector module
"motion_abs_y": 3, # Detected absolute motion vector Y-coord
"motion_rel_mod": 1, # Detected relative motion vector module
"motion_abs_mod": 5 # Detected absolute motion vector module
} }
""" """
sensor = self._get_sensor() sensor = self._get_sensor()
x, y = sensor.get_motion() x, y = sensor.get_motion()
self.x += x
self.y += y
return { return {
'motion_rel_x': x, 'motion_x': x,
'motion_rel_y': y, 'motion_y': y,
'motion_abs_x': self.x, 'motion_mod': math.sqrt(x * x + y * y),
'motion_abs_y': self.y,
'motion_rel_mod': math.sqrt(x * x + y * y),
'motion_abs_mod': math.sqrt(self.x * self.x + self.y * self.y),
} }