From 58b0b4839363bcbb94141d30ec318c6b752146de Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 20 Dec 2019 21:40:57 +0100 Subject: [PATCH] Added gpio.cleanup at the end of get_measurement in distance sensor plugin --- .../plugins/gpio/sensor/distance/__init__.py | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/platypush/plugins/gpio/sensor/distance/__init__.py b/platypush/plugins/gpio/sensor/distance/__init__.py index 1096eac06..48ddf5534 100644 --- a/platypush/plugins/gpio/sensor/distance/__init__.py +++ b/platypush/plugins/gpio/sensor/distance/__init__.py @@ -24,33 +24,21 @@ class GpioSensorDistancePlugin(GpioPlugin, GpioSensorPlugin): :type echo_pin: int """ - import RPi.GPIO as gpio GpioPlugin.__init__(self, *args, **kwargs) - self.trigger_pin = trigger_pin self.echo_pin = echo_pin self._is_probing = False + def _init_gpio(self): + import RPi.GPIO as gpio gpio.setmode(self.mode) gpio.setup(self.trigger_pin, gpio.OUT) gpio.setup(self.echo_pin, gpio.IN) - - self.logger.info('Resetting trigger sensor on GPIO pin {}'.format(self.trigger_pin)) gpio.output(self.trigger_pin, False) - - @action - def get_measurement(self): - """ - Extends :func:`.GpioSensorPlugin.get_measurement` - - :returns: Distance measurement as a scalar (in mm): - """ - + def _get_data(self): import RPi.GPIO as gpio - gpio.setmode(gpio.BCM) - gpio.setup(self.trigger_pin, gpio.OUT) - gpio.setup(self.echo_pin, gpio.IN) + self._init_gpio() gpio.output(self.trigger_pin, True) time.sleep(0.00001) # 1 us pulse to trigger echo measurement @@ -88,4 +76,19 @@ class GpioSensorDistancePlugin(GpioPlugin, GpioSensorPlugin): return round(pulse_duration * 171500.0, 2) + @action + def get_measurement(self): + """ + Extends :func:`.GpioSensorPlugin.get_measurement` + + :returns: Distance measurement as a scalar (in mm): + """ + import RPi.GPIO as gpio + + try: + return self._get_data() + finally: + gpio.cleanup() + + # vim:sw=4:ts=4:et: