From 40e31bf38f964af75ba1c313fca95e97ae74cacd Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Sun, 2 Jul 2023 15:07:59 +0700 Subject: [PATCH 1/4] Change value property to return full range of 0 - 65535. --- adafruit_mcp3xxx/analog_in.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/adafruit_mcp3xxx/analog_in.py b/adafruit_mcp3xxx/analog_in.py index 21fbeca..089bbb8 100755 --- a/adafruit_mcp3xxx/analog_in.py +++ b/adafruit_mcp3xxx/analog_in.py @@ -14,7 +14,7 @@ The ADC chips supported by this library do not use negative numbers. If the resulting differential read is less than 0, then the returned integer value (and voltage value) is ``0``. If for some reason the voltage on a channel is greater than the reference voltage or - less than 0, then the returned integer value is ``65472`` or ``0`` respectively. + less than 0, then the returned integer value is ``65535`` or ``0`` respectively. """ @@ -54,15 +54,11 @@ def __init__( @property def value(self) -> int: - """Returns the value of an ADC pin as an integer. Due to 10-bit accuracy of the chip, the - returned values range [0, 65472].""" - return ( - self._mcp.read(self._pin_setting, is_differential=self.is_differential) << 6 - ) + """Returns the value of an ADC pin as an integer in the range [0, 65535].""" + return int(self._mcp.read(self._pin_setting, is_differential=self.is_differential) * (65535 / 1023)) @property def voltage(self) -> float: """Returns the voltage from the ADC pin as a floating point value. Due to the 10-bit - accuracy of the chip, returned values range from 0 to (``reference_voltage`` * - 65472 / 65535)""" - return (self.value * self._mcp.reference_voltage) / 65535 + accuracy of the chip, returned values range from 0 to ``reference_voltage``.""" + return self.value * self._mcp.reference_voltage / 65535 From 0a44e063783794ff648bfe9070aa8841bf765b18 Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Sun, 2 Jul 2023 15:15:58 +0700 Subject: [PATCH 2/4] Ran pre-commit. --- adafruit_mcp3xxx/analog_in.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_mcp3xxx/analog_in.py b/adafruit_mcp3xxx/analog_in.py index 089bbb8..081d018 100755 --- a/adafruit_mcp3xxx/analog_in.py +++ b/adafruit_mcp3xxx/analog_in.py @@ -55,7 +55,10 @@ def __init__( @property def value(self) -> int: """Returns the value of an ADC pin as an integer in the range [0, 65535].""" - return int(self._mcp.read(self._pin_setting, is_differential=self.is_differential) * (65535 / 1023)) + return int( + self._mcp.read(self._pin_setting, is_differential=self.is_differential) + * (65535 / 1023) + ) @property def voltage(self) -> float: From 14964a80a59b8259695d4fff5773fdf3f9853ab1 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 21 Sep 2023 15:10:36 -0400 Subject: [PATCH 3/4] use bit-shifting and or-ing to cover full range --- adafruit_mcp3xxx/analog_in.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_mcp3xxx/analog_in.py b/adafruit_mcp3xxx/analog_in.py index 081d018..d0e1483 100755 --- a/adafruit_mcp3xxx/analog_in.py +++ b/adafruit_mcp3xxx/analog_in.py @@ -55,9 +55,10 @@ def __init__( @property def value(self) -> int: """Returns the value of an ADC pin as an integer in the range [0, 65535].""" - return int( - self._mcp.read(self._pin_setting, is_differential=self.is_differential) - * (65535 / 1023) + # Initial result is only 10 bits. + result = int(self._mcp.read(self._pin_setting, is_differential=self.is_differential)) + # Stretch to 16 bits and cover full range. + return (result << 6) | (result >> 4) ) @property From e6c7f27c3c27f36c527dba951d752e27f45a8258 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 21 Sep 2023 15:38:07 -0400 Subject: [PATCH 4/4] fix typo --- adafruit_mcp3xxx/analog_in.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adafruit_mcp3xxx/analog_in.py b/adafruit_mcp3xxx/analog_in.py index d0e1483..781b577 100755 --- a/adafruit_mcp3xxx/analog_in.py +++ b/adafruit_mcp3xxx/analog_in.py @@ -56,10 +56,11 @@ def __init__( def value(self) -> int: """Returns the value of an ADC pin as an integer in the range [0, 65535].""" # Initial result is only 10 bits. - result = int(self._mcp.read(self._pin_setting, is_differential=self.is_differential)) + result = int( + self._mcp.read(self._pin_setting, is_differential=self.is_differential) + ) # Stretch to 16 bits and cover full range. return (result << 6) | (result >> 4) - ) @property def voltage(self) -> float: