Skip to content
Open
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
32 changes: 32 additions & 0 deletions ad7291.py
Original file line number Diff line number Diff line change
Expand Up @@ -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