From d9024aa56445fad0d49b2c0fe13f1e997fae386f Mon Sep 17 00:00:00 2001 From: CamDavidsonPilon Date: Wed, 17 Jul 2024 12:13:26 -0400 Subject: [PATCH] vendoring TMP library --- CHANGELOG.md | 1 + pioreactor/background_jobs/monitor.py | 2 +- .../background_jobs/temperature_automation.py | 2 +- pioreactor/utils/adcs.py | 5 ++- pioreactor/utils/temps.py | 32 +++++++++++++++++++ pioreactor/whoami.py | 4 --- requirements/requirements_worker.txt | 1 - setup.py | 1 - 8 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 pioreactor/utils/temps.py diff --git a/CHANGELOG.md b/CHANGELOG.md index e150958f..be8e6f6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - `unit()` can be used in mqtt fetch expressions. Example: `unit():stirring:target_rpm` is identical to `::stirring:target_rpm`. The latter can be seen as a shortened version of the former. - experiment profiles can have a `description` in the `job` field (i.e. at the same level as `actions`). - Updated Raspberry Pi OS image to 2024-07-04. + - Vendoring the TMP1075 library, which also fixes the RPi5 error. #### Breaking changes diff --git a/pioreactor/background_jobs/monitor.py b/pioreactor/background_jobs/monitor.py index 6181e78d..0e211948 100644 --- a/pioreactor/background_jobs/monitor.py +++ b/pioreactor/background_jobs/monitor.py @@ -380,7 +380,7 @@ def check_heater_pcb_temperature(self) -> None: from pioreactor.utils.mock import MockTMP1075 as TMP1075 else: try: - from TMP1075 import TMP1075 # type: ignore + from pioreactor.utils.temps import TMP1075 # type: ignore except ImportError: # leader-only is a worker? self.logger.debug( diff --git a/pioreactor/background_jobs/temperature_automation.py b/pioreactor/background_jobs/temperature_automation.py index 0dbe987a..494fab92 100644 --- a/pioreactor/background_jobs/temperature_automation.py +++ b/pioreactor/background_jobs/temperature_automation.py @@ -117,7 +117,7 @@ def __init__( if whoami.is_testing_env(): from pioreactor.utils.mock import MockTMP1075 as TMP1075 else: - from TMP1075 import TMP1075 # type: ignore + from pioreactor.utils.temps import TMP1075 # type: ignore self.heater_duty_cycle = 0.0 self.pwm = self.setup_pwm() diff --git a/pioreactor/utils/adcs.py b/pioreactor/utils/adcs.py index b64c4f4d..01bdad27 100644 --- a/pioreactor/utils/adcs.py +++ b/pioreactor/utils/adcs.py @@ -2,7 +2,7 @@ # adc abstraction from __future__ import annotations -import busio # type: ignore +from busio import I2C # type: ignore from pioreactor import exc from pioreactor import hardware @@ -51,7 +51,6 @@ def __init__(self) -> None: super().__init__() from adafruit_ads1x15.analog_in import AnalogIn # type: ignore - from busio import I2C # type: ignore from adafruit_ads1x15.ads1115 import ADS1115 as ADS # type: ignore self.analog_in: dict[int, AnalogIn] = {} @@ -94,7 +93,7 @@ def read_from_channel(self, channel: pt.AdcChannel) -> pt.AnalogValue: class Pico_ADC(_ADC): def __init__(self) -> None: # set up i2c connection to hardware.ADC - self.i2c = busio.I2C(hardware.SCL, hardware.SDA) + self.i2c = I2C(hardware.SCL, hardware.SDA) def read_from_channel(self, channel: pt.AdcChannel) -> pt.AnalogValue: assert 0 <= channel <= 3 diff --git a/pioreactor/utils/temps.py b/pioreactor/utils/temps.py new file mode 100644 index 00000000..198916a8 --- /dev/null +++ b/pioreactor/utils/temps.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +from __future__ import annotations + +from adafruit_bus_device.i2c_device import I2CDevice +from busio import I2C # type: ignore + +from pioreactor import hardware + + +class TMP1075: + """ + Driver for the TI TMP1075 temperature sensor. + See datasheet: http://www.ti.com/lit/ds/symlink/tmp1075.pdf + + """ + + TEMP_REGISTER = bytearray([0x00]) + CONFIG_REGISTER = bytearray([0x01]) + + def __init__(self, address: int = 0x4F): + comm_port = (I2C(hardware.SCL, hardware.SDA),) + self.i2c = I2CDevice(comm_port, address) + + def get_temperature(self) -> float: + b = bytearray(2) + self.i2c.write_then_readinto(self.TEMP_REGISTER, b) + return ((b[0] << 4) + (b[1] >> 4)) * 0.0625 + + @property + def temperature(self): + """alias for get_temperature""" + return self.get_temperature() diff --git a/pioreactor/whoami.py b/pioreactor/whoami.py index 93301d31..867f8746 100644 --- a/pioreactor/whoami.py +++ b/pioreactor/whoami.py @@ -148,10 +148,6 @@ def am_I_leader() -> bool: return get_unit_name() == leader_hostname -def am_I_active_worker() -> bool: - return is_active(get_unit_name()) - - def am_I_a_worker() -> bool: from pioreactor.pubsub import get_from_leader diff --git a/requirements/requirements_worker.txt b/requirements/requirements_worker.txt index 55a401be..ee351b63 100644 --- a/requirements/requirements_worker.txt +++ b/requirements/requirements_worker.txt @@ -13,6 +13,5 @@ pyftdi==0.55.4 pyserial==3.5 pyusb==1.2.1 rpi_hardware_pwm==0.2.1 -TMP1075==0.2.1 typing_extensions==4.12.2 wheel==0.41.2 diff --git a/setup.py b/setup.py index d5cb88cb..9099834f 100644 --- a/setup.py +++ b/setup.py @@ -53,7 +53,6 @@ "pyserial==3.5", "pyusb==1.2.1", "rpi_hardware_pwm==0.2.1", - "TMP1075==0.2.1", "typing_extensions==4.12.2", "wheel==0.41.2", ]