Skip to content

Commit

Permalink
Update ads1x15.py (#329)
Browse files Browse the repository at this point in the history
* Update ads1x15.py

Avoids race condition when reading a channel

* Update ads1x15.py

Avoids race condition when reading a channel

* Update ads1x15.py

Formatting with Lint
  • Loading branch information
maxthebuch authored Jan 2, 2024
1 parent 2bb2f94 commit 390cc52
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions mqtt_io/modules/sensor/ads1x15.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
ADS1x15 analog to digital converters
"""
import threading

from typing import cast

from ...types import CerberusSchemaType, ConfigType, SensorValueType
Expand Down Expand Up @@ -78,15 +80,21 @@ def setup_module(self) -> None:
# Create single-ended input for each pin in config
self.channels = {pin: AnalogIn(self.ads, pin) for pin in self.config["pins"]}

# initialize mutex lock
self.lock = threading.Lock()

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.channels[sens_conf["pin"]].value,
voltage=self.channels[sens_conf["pin"]].voltage,
)
# acquire the lock
with self.lock:
sens_type = sens_conf["type"]
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 390cc52

Please sign in to comment.