diff --git a/README.rst b/README.rst index a6f8b4c..2434594 100644 --- a/README.rst +++ b/README.rst @@ -70,9 +70,16 @@ Usage Example # create a thermocouple object with the above thermocouple = adafruit_max31856.MAX31856(spi, cs) - # print the temperature! + # measure the temperature! (takes approx 160ms) print(thermocouple.temperature) + # alternative (non-blocking) way to get temperature + thermocouple.initiate_one_shot_measurement() + # + # now wait for measurement to complete + while thermocouple.oneshot_pending: + pass + print(thermocouple.unpack_temperature()) Documentation ============= diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 5cc6a13..6d0af66 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -164,9 +164,13 @@ def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K): @property def temperature(self): - """The temperature of the sensor and return its value in degrees Celsius. (read-only)""" + """Measure the temperature of the sensor and wait for the result. + Return value is in degrees Celsius. (read-only)""" self._perform_one_shot_measurement() + return self.unpack_temperature() + def unpack_temperature(self) -> float: + """Reads the probe temperature from the register""" # unpack the 3-byte temperature as 4 bytes raw_temp = unpack( ">i", self._read_register(_MAX31856_LTCBH_REG, 3) + bytes([0]) @@ -182,9 +186,12 @@ def temperature(self): @property def reference_temperature(self): - """The temperature of the cold junction in degrees Celsius. (read-only)""" + """Wait to retreive temperature of the cold junction in degrees Celsius. (read-only)""" self._perform_one_shot_measurement() + return self.unpack_reference_temperature() + def unpack_reference_temperature(self) -> float: + """Reads the reference temperature from the register""" raw_read = unpack(">h", self._read_register(_MAX31856_CJTH_REG, 2))[0] # effectively shift raw_read >> 8 to convert pseudo-float @@ -264,8 +271,16 @@ def fault(self): } def _perform_one_shot_measurement(self): - - self._write_u8(_MAX31856_CJTO_REG, 0x0) + self.initiate_one_shot_measurement() + # wait for the measurement to complete + self._wait_for_oneshot() + + def initiate_one_shot_measurement(self): + """Starts a one-shot measurement and returns immediately. + A measurement takes approximately 160ms. + Check the status of the measurement with `oneshot_pending`; when it is false, + the measurement is complete and the value can be read with `unpack_temperature`. + """ # read the current value of the first config register conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0] @@ -277,7 +292,19 @@ def _perform_one_shot_measurement(self): # write it back with the new values, prompting the sensor to perform a measurement self._write_u8(_MAX31856_CR0_REG, conf_reg_0) - sleep(0.250) + @property + def oneshot_pending(self) -> bool: + """A boolean indicating the status of the one-shot flag. + A True value means the measurement is still ongoing. + A False value means measurement is complete.""" + oneshot_flag = ( + self._read_register(_MAX31856_CR0_REG, 1)[0] & _MAX31856_CR0_1SHOT + ) + return bool(oneshot_flag) + + def _wait_for_oneshot(self): + while self.oneshot_pending: + sleep(0.01) def _read_register(self, address, length): # pylint: disable=no-member