From d37ee148d03c172bb49956842ad5499a3ff19602 Mon Sep 17 00:00:00 2001 From: tomyyyD Date: Fri, 2 Jun 2023 12:02:16 -0400 Subject: [PATCH 1/4] Framework --- ad7291.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ad7291.py b/ad7291.py index 7aee0cf..aba55ff 100644 --- a/ad7291.py +++ b/ad7291.py @@ -187,3 +187,13 @@ 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 the average temperature + of the ADC since boot. + """ + + pass From 16e87408a90aef040a459618b48dd8a749ff5a64 Mon Sep 17 00:00:00 2001 From: tomyyyD Date: Fri, 2 Jun 2023 14:44:41 -0400 Subject: [PATCH 2/4] Average temperature read property --- ad7291.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ad7291.py b/ad7291.py index aba55ff..68f1a26 100644 --- a/ad7291.py +++ b/ad7291.py @@ -196,4 +196,24 @@ def read_avg_temperature(self): of the ADC since boot. """ - pass + 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 << 8 + temperature += self.buf[1] + + if temperature > 4096: + return (4096 - temperature) / 4 + else: + return temperature / 4 From 2ac9339abdde685463700776e22081cbdfe35cb0 Mon Sep 17 00:00:00 2001 From: tomyyyD Date: Fri, 2 Jun 2023 14:49:25 -0400 Subject: [PATCH 3/4] More accurate comment documentation --- ad7291.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ad7291.py b/ad7291.py index 68f1a26..0d6bd5e 100644 --- a/ad7291.py +++ b/ad7291.py @@ -192,8 +192,10 @@ def read_temperature_conversion(self): def read_avg_temperature(self): """ Reads from the temperature average register. This register updates - after every temperature conversion and gets the average temperature - of the ADC since boot. + 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: From 006ee9e6a81cf803bb1567676a4f6bbe3fc8d405 Mon Sep 17 00:00:00 2001 From: tomyyyD Date: Wed, 7 Jun 2023 14:55:39 -0400 Subject: [PATCH 4/4] syntax update --- ad7291.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ad7291.py b/ad7291.py index 0d6bd5e..fab0e61 100644 --- a/ad7291.py +++ b/ad7291.py @@ -212,7 +212,7 @@ def read_avg_temperature(self): BufferError("Channel returned is not Avg Temperature channel") temperature = self.buf[0] & ((1 << 4) - 1) - temperature << 8 + temperature = temperature << 8 temperature += self.buf[1] if temperature > 4096: