Skip to content

Commit

Permalink
ads1x1x: fix voltage default value, rename continous to continuous an…
Browse files Browse the repository at this point in the history
…d try to be more accurate on the sample timing

Signed-off-by: Konstantin Koch <korsarnek@gmail.com>
  • Loading branch information
korsarNek committed Oct 12, 2024
1 parent 030df02 commit 15f2a93
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/Config_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -4867,7 +4867,7 @@ chip: ADS1115
#mode: single
# Default value is single. Turn off the chip after a single reading or keep
# it running. Turning off saves power but turning it back on will be slower.
# Options are single and continous.
# Options are single and continuous.
#samples_per_second: 128
# Default value is 128. The amount of samples that the ADC can provide per
# second. A lower value makes the samples more accurate, but it takes longer
Expand Down
22 changes: 10 additions & 12 deletions klippy/extras/ads1x1x.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def isADS111X(chip):
0x0A00: 0.256 / 2047.0 # +/-0.256V range = Gain 16
}
ADS1X1X_MODE = {
'continous': 0x0000, # Continuous conversion mode
'continuous': 0x0000, # Continuous conversion mode
'single': 0x0100 # Power-down single-shot mode
}

Expand All @@ -130,9 +130,6 @@ def isADS111X(chip):
'860': 0x00e0 # 860 samples per second
}

ADS101X_CONVERSION_DELAY = 0.002
ADS111X_CONVERSION_DELAY = 0.008

ADS1X1X_COMPARATOR_MODE = {
'TRADITIONAL': 0x0000, # Traditional comparator with hysteresis
'WINDOW': 0x0010 # Window comparator
Expand Down Expand Up @@ -188,7 +185,7 @@ def __init__(self, config):
self._ppins = self._printer.lookup_object("pins")
self._ppins.register_chip(self.name, self)

self.pga = config.getchoice('pga', ADS1X1X_PGA, 'V4.096')
self.pga = config.getchoice('pga', ADS1X1X_PGA, '4.096V')
self.mode = config.getchoice('mode', ADS1X1X_MODE, 'single')
# Comparators are not implemented, they would only be useful if the
# alert pin is used, which we haven't made configurable.
Expand Down Expand Up @@ -229,7 +226,8 @@ def _handle_ready(self):

def is_ready(self):
config = self._read_register(ADS1X1X_REG_POINTER['CONFIG'])
return bool((config & ADS1X1X_REG_CONFIG['OS_MASK']) == ADS1X1X_OS['OS_IDLE'])
return bool((config & ADS1X1X_REG_CONFIG['OS_MASK']) == \
ADS1X1X_OS['OS_IDLE'])

def sample(self, sensor):
with self._mutex:
Expand All @@ -238,13 +236,13 @@ def sample(self, sensor):
try:
self._write_register(ADS1X1X_REG_POINTER['CONFIG'],
pin_object.config)
delay = 0
if isADS101X(self.chip):
delay = ADS101X_CONVERSION_DELAY
else:
delay = ADS111X_CONVERSION_DELAY
# The report time is 1 / sample_count * 4 to account for the 4
# possible inputs. So sample_count 16 on 1 input will result
# in 4 samples per second.
delay = 1 / self.samples_per_second_numeric
self._reactor.pause(self._reactor.monotonic() + delay)
while not self.is_ready():
self._reactor.pause(self._reactor.monotonic() + delay)
self._reactor.pause(self._reactor.monotonic() + 0.001)
sample = self._read_register(ADS1X1X_REG_POINTER['CONVERSION'])
except Exception:
logging.exception("ADS1X1X: error while sampling")
Expand Down

0 comments on commit 15f2a93

Please sign in to comment.