Skip to content

Commit

Permalink
Add new module for sensor adxl345 (#223)
Browse files Browse the repository at this point in the history
* Add module adxl345
* Add ADXL345 to README.md
---------
Co-authored-by: Benji <46675043+BenjiU@users.noreply.github.com>
  • Loading branch information
birdie1 authored Aug 2, 2023
1 parent d7e07f5 commit 7872a2f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Hardware support is provided by specific GPIO, Sensor and Stream modules. It's e
- INA219 DC current sensor (`ina219`)
- LM75 temperature sensor (`lm75`)
- MCP3008 analog to digital converter (`mcp3008`)
- ADXl345 3-axis accelerometer up to ±16g (`adxl345`)

### Streams

Expand Down
68 changes: 68 additions & 0 deletions mqtt_io/modules/sensor/adxl345.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
ADXL345 Digital Accelerometer Sensor
Mandatory:
- chip_addr
Optional:
- output_g (set True if output in g). default:m*s²
Output:
- x (in m*s²)
- y (in m*s²)
- z (in m*s²)
"""

from json import dumps
from typing import cast

from ...types import CerberusSchemaType, ConfigType, SensorValueType
from . import GenericSensor

REQUIREMENTS = ("adxl345",)
CONFIG_SCHEMA: CerberusSchemaType = {
"chip_addr": dict(type="integer", required=True, empty=False),
"output_g": dict(type="boolean", required=False, empty=False),
}


class Sensor(GenericSensor):
"""
Implementation of Sensor class for the ADXL345 sensor.
"""

SENSOR_SCHEMA: CerberusSchemaType = {
"type": dict(
type="string",
required=False,
empty=False,
default="all",
allowed=["all", "x", "y", "z"],
)
}

def setup_module(self) -> None:
# pylint: disable=import-outside-toplevel,attribute-defined-outside-init
# pylint: disable=import-error,no-member
from adxl345 import ADXL345 # type: ignore

self.i2c_addr: int = self.config["chip_addr"]
self.adxl345 = ADXL345(self.i2c_addr)

def get_value(self, sens_conf: ConfigType) -> SensorValueType:
sens_type = sens_conf["type"]

if "output_g" in self.config and self.config["output_g"]:
all_axes = self.adxl345.get_axes(True)
else:
all_axes = self.adxl345.get_axes()

return cast(
float,
dict(
x=all_axes["x"],
y=all_axes["y"],
z=all_axes["z"],
all_axes=dumps(all_axes),
)[sens_type],
)

0 comments on commit 7872a2f

Please sign in to comment.