Skip to content

Commit

Permalink
Only create one instance of sensor_module for ADS1x15 (#286)
Browse files Browse the repository at this point in the history
* Only create one instance of sensor_module for ADS1x15

* Reformat dict comprehension
  • Loading branch information
shbatm authored Oct 13, 2022
1 parent 86fe402 commit 3bfa1b7
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions mqtt_io/modules/sensor/ads1x15.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
ADS1x15 analog to digital converters
"""

from typing import cast

from ...types import CerberusSchemaType, ConfigType, SensorValueType
Expand All @@ -20,7 +19,7 @@
empty=False,
allowed=SENSOR_TYPES,
),
"pin": dict(type="integer", required=True, empty=False, allowed=[0, 1, 2, 3]),
"pins": dict(type="list", required=True, empty=False, allowed=[0, 1, 2, 3]),
"gain": dict(
type="integer",
required=False,
Expand All @@ -44,6 +43,13 @@ class Sensor(GenericSensor):
allowed=["value", "voltage"],
default="value",
),
"pin": dict(
type="integer",
required=True,
empty=False,
allowed=[0, 1, 2, 3],
default=0,
),
}

def setup_module(self) -> None:
Expand All @@ -70,15 +76,18 @@ def setup_module(self) -> None:
self.i2c, gain=self.config["gain"], address=self.config["chip_addr"]
)

# Create single-ended input on channel 0
self.chan = AnalogIn(self.ads, self.config["pin"])
# Create single-ended input for each pin in config
self.channels = {pin: AnalogIn(self.ads, pin) for pin in self.config["pins"]}

def get_value(self, sens_conf: ConfigType) -> SensorValueType:
"""
Get the value or voltage from the sensor
"""
sens_type = sens_conf["type"]
data = dict(value=self.chan.value, voltage=self.chan.voltage)
data = dict(
value=self.channels[sens_conf["pin"]].value,
voltage=self.channels[sens_conf["pin"]].voltage,
)
return cast(
float,
data[sens_type],
Expand Down

0 comments on commit 3bfa1b7

Please sign in to comment.