Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ads1x15.py #329

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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