Skip to content

Commit 14964a8

Browse files
authored
use bit-shifting and or-ing to cover full range
1 parent 0a44e06 commit 14964a8

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

adafruit_mcp3xxx/analog_in.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ def __init__(
5555
@property
5656
def value(self) -> int:
5757
"""Returns the value of an ADC pin as an integer in the range [0, 65535]."""
58-
return int(
59-
self._mcp.read(self._pin_setting, is_differential=self.is_differential)
60-
* (65535 / 1023)
58+
# Initial result is only 10 bits.
59+
result = int(self._mcp.read(self._pin_setting, is_differential=self.is_differential))
60+
# Stretch to 16 bits and cover full range.
61+
return (result << 6) | (result >> 4)
6162
)
6263

6364
@property

0 commit comments

Comments
 (0)