diff --git a/ad7291.py b/ad7291.py index 7aee0cf..fab0e61 100644 --- a/ad7291.py +++ b/ad7291.py @@ -187,3 +187,35 @@ def read_temperature_conversion(self): return (4096 - temperature)/4 else: return temperature/4 + + @property + def read_avg_temperature(self): + """ + Reads from the temperature average register. This register updates + after every temperature conversion and gets a moving average of the ADC + + The sensor uses the equation: + AVG = 0.875 * (Previous Average Result) + 0.125 * (Current Result) + """ + + if not self.settings >> 7: + BufferError("Temperature sensor not enabled") + + self.buf[0] = _T_SENSE_AVERAGE_RESULT + with self.i2c_device as i2c: + i2c.write(self.buf, end=1) + + i2c.readinto(self.buf, end=2) + + channel = (self.buf[0] >> 4) & ((1 << 4) - 1) + if channel != 9: + BufferError("Channel returned is not Avg Temperature channel") + + temperature = self.buf[0] & ((1 << 4) - 1) + temperature = temperature << 8 + temperature += self.buf[1] + + if temperature > 4096: + return (4096 - temperature) / 4 + else: + return temperature / 4