Precision parameter added to accelerometer plugin

This commit is contained in:
Fabio Manganiello 2018-09-06 02:47:47 +02:00
parent 98a8874084
commit 5d1fd3a02f

View file

@ -16,12 +16,15 @@ class GpioSensorAccelerometerPlugin(GpioSensorPlugin):
* ``Adafruit_Python_GPIO`` (``pip install Adafruit_Python_GPIO``) * ``Adafruit_Python_GPIO`` (``pip install Adafruit_Python_GPIO``)
""" """
def __init__(self, g=4, *args, **kwargs): def __init__(self, g=4, precision=None, *args, **kwargs):
""" """
Only LIS3DH in I2C mode is currently supported: https://learn.adafruit.com/assets/59080. Only LIS3DH in I2C mode is currently supported: https://learn.adafruit.com/assets/59080.
:param g: Accelerometer range as a multiple of G - can be 2G, 4G, 8G or 16G :param g: Accelerometer range as a multiple of G - can be 2G, 4G, 8G or 16G
:type g: int :type g: int
:param precision: If set, the position values will be rounded to the specified number of decimal digits (default: no rounding)
:type precision: int
""" """
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -38,6 +41,7 @@ class GpioSensorAccelerometerPlugin(GpioSensorPlugin):
else: else:
raise RuntimeError('Invalid G range: {}'.format(g)) raise RuntimeError('Invalid G range: {}'.format(g))
self.precision = precision
self.sensor = LIS3DH() self.sensor = LIS3DH()
self.sensor.setRange(self.g) self.sensor.setRange(self.g)
@ -50,11 +54,12 @@ class GpioSensorAccelerometerPlugin(GpioSensorPlugin):
:returns: The sensor's current position as a dictionary with the three components (x,y,z) in degrees, each between -90 and 90 :returns: The sensor's current position as a dictionary with the three components (x,y,z) in degrees, each between -90 and 90
""" """
return { values = [
'x': self.sensor.getX()*100, (pos*100 if self.precision is None else round(pos*100, self.precision))
'y': self.sensor.getY()*100, for pos in (self.sensor.getX(), self.sensor.getY(), self.sensor.getZ())
'z': self.sensor.getZ()*100 ]
}
return { 'x': values[0], 'y': values[1], 'z': values[2] }
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: