Skip to content

Commit 533bf3e

Browse files
authored
fix: fix example scripts and related issues (#13)
1 parent 38e5f05 commit 533bf3e

File tree

16 files changed

+100
-28
lines changed

16 files changed

+100
-28
lines changed

examples/01_singleshot.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
pdm run examples/01_singleshot.py
1717
"""
1818

19+
import logging
1920
import time
2021

2122
import board # type: ignore
@@ -25,7 +26,11 @@
2526

2627
from feeph.ads1xxx import DRS, UNIT, Ads1113, Ads1113Config
2728

29+
LH = logging.getLogger("main")
30+
2831
if __name__ == '__main__':
32+
logging.basicConfig(format='%(levelname).1s: %(message)s', level=logging.INFO)
33+
2934
i2c_bus = busio.I2C(scl=board.SCL, sda=board.SDA)
3035
ads1x13 = Ads1113(i2c_bus=i2c_bus)
3136

@@ -38,17 +43,17 @@
3843
# It is recommended to provide a configuration at least once in order
3944
# to ensure the device uses the expected configuration.
4045
value1 = ads1x13.get_ssc_measurement(config=my_config)
41-
print("value1: {value1}µV")
46+
print(f"value1: {value1}µV")
4247

4348
# take a second measurement
4449
# if no configuration is provided we continue to use the previous one
4550
value2 = ads1x13.get_ssc_measurement()
46-
print("value2: {value1}µV")
51+
print(f"value2: {value2}µV")
4752

4853
# convert from MicroVolt to Volt
4954
# This might skew the value due to conversion to float!
50-
value3 = float(value1) / (1000 * 1000)
51-
print("value3: {value2:0.6f}V")
55+
value3 = ads1x13.get_ssc_measurement() / (1000 * 1000)
56+
print(f"value3: {value3:0.6f}V")
5257

5358
# show the raw value
5459
# range:
@@ -57,16 +62,15 @@
5762
# ADS101x: 15 steps (..., 15, 30, 45, ...)
5863
# ADS111x: 1 step (..., 1, 2, 3, 4, ...)
5964
value4 = ads1x13.get_ssc_measurement(unit=UNIT.STEPS)
60-
print("value4: {value1}")
65+
print(f"value4: {value4}")
6166

6267
print('-' * 80)
6368

6469
# take multiple measurements and alternate between 2 configurations
6570
config1 = Ads1113Config(drs=DRS.MODE4)
6671
config2 = Ads1113Config(drs=DRS.MODE5)
67-
while True:
68-
value4 = ads1x13.get_ssc_measurement(config=config1)
69-
print("value4: {value1}µV")
70-
value5 = ads1x13.get_ssc_measurement(config=config2)
71-
print("value5: {value1}µV")
72+
for i in range(1, 10):
73+
value5 = ads1x13.get_ssc_measurement(config=config1) / (1000 * 1000)
74+
value6 = ads1x13.get_ssc_measurement(config=config2) / (1000 * 1000)
75+
print(f"#{i:} value5: {value5:.03f}V, value6: {value6:.03f}V")
7276
time.sleep(1)

examples/02_continuous.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
pdm run examples/02_continuous.py
2222
"""
2323

24+
import logging
2425
import time
2526

2627
import board # type: ignore
@@ -30,22 +31,27 @@
3031

3132
from feeph.ads1xxx import DOM, DRS, Ads1113, Ads1113Config
3233

34+
LH = logging.getLogger("main")
35+
3336
if __name__ == '__main__':
37+
logging.basicConfig(format='%(levelname).1s: %(message)s', level=logging.INFO)
38+
3439
i2c_bus = busio.I2C(scl=board.SCL, sda=board.SDA)
3540
ads1x13 = Ads1113(i2c_bus=i2c_bus)
3641

3742
# create a configuration
3843
# - enable continuous conversion mode
3944
# - set desired data rate
4045
# on ADS1x14/ADS1x15: configure comparator (clat, cmod, cpol & cque)
46+
LH.info("Configuring for continuous conversion mode.")
4147
my_config = Ads1113Config(dom=DOM.CCM, drs=DRS.MODE2)
4248

4349
if ads1x13.configure(config=my_config):
4450
while True:
4551
# on an ADS111x in MODE2 there should be a new sample
4652
# every 0.5 seconds (ADS101x is much faster)
4753
value = ads1x13.get_ccm_measurement()
48-
print("value: {value1}µV")
54+
print(f"value: {value}µV")
4955
time.sleep(0.5)
5056
else:
5157
print("Unable to configure ADC.")

examples/03_comparator.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
pdm run examples/03_comparator.py
1717
"""
1818

19+
import logging
1920
import time
2021

2122
import board # type: ignore
@@ -25,7 +26,11 @@
2526

2627
from feeph.ads1xxx import DOM, DRS, Ads1114, Ads1114Config
2728

29+
LH = logging.getLogger("main")
30+
2831
if __name__ == '__main__':
32+
logging.basicConfig(format='%(levelname).1s: %(message)s', level=logging.INFO)
33+
2934
i2c_bus = busio.I2C(scl=board.SCL, sda=board.SDA)
3035
ads1x14 = Ads1114(i2c_bus=i2c_bus)
3136

@@ -40,7 +45,7 @@
4045
# on an ADS111x in MODE2 there should be a new sample
4146
# every 0.5 seconds (ADS101x is much faster)
4247
value = ads1x14.get_ccm_measurement()
43-
print("value: {value1}µV")
48+
print(f"value: {value}µV")
4449
time.sleep(0.5)
4550
else:
4651
print("Unable to configure ADC.")

examples/04_amplifier.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@
1010
pdm run examples/04_amplifier.py
1111
"""
1212

13+
import logging
14+
1315
import board # type: ignore
1416

1517
# module busio and board provide no type hints
1618
import busio # type: ignore
1719

1820
from feeph.ads1xxx import PGA, Ads1114, Ads1114Config
1921

22+
LH = logging.getLogger("main")
23+
2024
if __name__ == '__main__':
25+
logging.basicConfig(format='%(levelname).1s: %(message)s', level=logging.INFO)
26+
2127
i2c_bus = busio.I2C(scl=board.SCL, sda=board.SDA)
2228
ads1x14 = Ads1114(i2c_bus=i2c_bus)
2329

@@ -30,6 +36,6 @@
3036
config2 = Ads1114Config(pga=PGA.MODE4) # FSR = ±0.512V
3137

3238
value1 = ads1x14.get_ssc_measurement(config=config1)
33-
print("value1: {value1}µV") # 63µV resolution
39+
print(f"value1: {value1}µV") # 63µV resolution
3440
value2 = ads1x14.get_ssc_measurement(config=config2)
35-
print("value2: {value2}µV") # 16µV resolution
41+
print(f"value2: {value2}µV") # 16µV resolution

examples/05_multiplexer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
pdm run examples/05_multiplexer.py
1111
"""
1212

13+
import logging
1314
import time
1415

1516
import board # type: ignore
@@ -19,7 +20,11 @@
1920

2021
from feeph.ads1xxx import MUX, PGA, Ads1115, Ads1115Config
2122

23+
LH = logging.getLogger("main")
24+
2225
if __name__ == '__main__':
26+
logging.basicConfig(format='%(levelname).1s: %(message)s', level=logging.INFO)
27+
2328
i2c_bus = busio.I2C(scl=board.SCL, sda=board.SDA)
2429
ads1x15 = Ads1115(i2c_bus=i2c_bus)
2530

@@ -35,7 +40,6 @@
3540
# switch between both configurations and take measurements
3641
while True:
3742
value1 = ads1x15.get_ssc_measurement(config=config_abs)
38-
print("value1: {value1}µV")
3943
value2 = ads1x15.get_ssc_measurement(config=config_dif)
40-
print("value2: {value1}µV")
44+
print(f"value1: {value1}µV, value2: {value1}µV")
4145
time.sleep(1)

feeph/ads1xxx/ads1x1x.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,17 @@ def reset_device_registers(self):
6767
continue
6868
bh.write_register(register, value, byte_count=2)
6969

70-
def configure(self, config: Ads1x1xConfig):
70+
def configure(self, config: Ads1x1xConfig) -> bool:
71+
# order is important!
7172
with BurstHandler(i2c_bus=self._i2c_bus, i2c_adr=self._i2c_adr) as bh:
72-
bh.write_register(0x01, config.as_uint16(), byte_count=2)
73+
# 1) reset to a defined state and disable alerting
74+
bh.write_register(0x01, 0x0583, byte_count=2)
75+
# 2) configure thresholds
7376
bh.write_register(0x10, config.get_atlo(unit=UNIT.STEPS), byte_count=2)
7477
bh.write_register(0x11, config.get_athi(unit=UNIT.STEPS), byte_count=2)
78+
# 3) update config register
79+
bh.write_register(0x01, config.as_uint16(), byte_count=2)
80+
return True
7581

7682
def get_ssc_measurement(self, config: Ads1x1xConfig | None = None, unit: UNIT = UNIT.MICRO) -> int:
7783
with BurstHandler(i2c_bus=self._i2c_bus, i2c_adr=self._i2c_adr) as bh:
@@ -84,19 +90,22 @@ def get_ssc_measurement(self, config: Ads1x1xConfig | None = None, unit: UNIT =
8490
config_atlo = config.get_atlo(unit=UNIT.STEPS)
8591
config_athi = config.get_athi(unit=UNIT.STEPS)
8692
if config_uint & DOM.SSM.value:
87-
bh.write_register(0x01, config_uint | SSC.START.value, byte_count=2)
93+
# 1) reset to a defined state and disable alerting
94+
bh.write_register(0x01, 0x0583, byte_count=2)
95+
# 2) configure thresholds
8896
if config_atlo is not None:
8997
bh.write_register(0x10, config_atlo, byte_count=2)
9098
if config_athi is not None:
9199
bh.write_register(0x11, config_athi, byte_count=2)
100+
# 3) update config register
101+
bh.write_register(0x01, config_uint | SSC.START.value, byte_count=2)
92102
# TODO wait until measurement is ready
93103
# (0b0..._...._...._.... -> 0b1..._...._...._....)
94104
step = bh.read_register(0x00, byte_count=2)
95105
if unit == UNIT.MICRO:
96106
if self._has_pga:
97107
pga_setting = config_uint & 0b0000_1110_0000_0000
98108
for pga_mode in PGA:
99-
LH.warning("get_ssc_measurement(): %s -> %d", pga_mode.name, pga_mode.value)
100109
if pga_setting == pga_mode.value:
101110
return convert_step_to_microvolts(step, pga_mode)
102111
else:

pdm.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ attrs==23.2.0 \
3131
binho-host-adapter==0.1.6 \
3232
--hash=sha256:1e6da7a84e208c13b5f489066f05774bff1d593d0f5bf1ca149c2b8e83eae856 \
3333
--hash=sha256:f71ca176c1e2fc1a5dce128beb286da217555c6c7c805f2ed282a6f3507ec277
34-
feeph-i2c==0.7.1 \
35-
--hash=sha256:a7d2d8bf785c18e8ce3205fc7a3533e6eeac1b563ad45dde7978c525499bd945 \
36-
--hash=sha256:ee522bb4640f546da82c1948dd3c5a165af060104006022559afc861a8cd2a5b
34+
feeph-i2c==0.7.3 \
35+
--hash=sha256:5a1937c4fcc4a7820e64b4195077d8a36e99430382b0e5dc291ed79ac053e53c \
36+
--hash=sha256:f5cf7c7090861fe9cc6c385bb41e22f7fb97f5e6896c013ce4ab7115939246ef
3737
gpiod==2.2.1 \
3838
--hash=sha256:51ba8e0e92e9b986b1d7aa4071617b3948d4fc69bfdef68e7d8b94aec8e56e54 \
3939
--hash=sha256:5fd077dc8933eeb1be2da78980aa4c53812119a6773d73060c7b5a1e30886373 \

scripts/run_tests

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ if [[ -n $FILES ]] ; then
2323
pdm run mypy $FILES
2424
fi
2525

26+
pdm run pytest --cov=feeph.ads1xxx --cov-report=term-missing tests/
27+
2628
echo "----------------------------------------------------------------------"
2729
echo "If you reached this far you passed. Congratulations!"
2830
echo "----------------------------------------------------------------------"

tests/test_ads1013.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,15 @@ def test_ssc_measurement_conflict2(self):
122122

123123
# ---------------------------------------------------------------------
124124

125+
def test_configure_verify_return_value(self):
126+
config = sut.Ads1013Config(dom=sut.DOM.CCM)
127+
# -----------------------------------------------------------------
128+
# -----------------------------------------------------------------
129+
self.assertTrue(self.ads1013.configure(config=config))
130+
125131
def test_configure(self):
126132
config = sut.Ads1013Config(dom=sut.DOM.CCM)
127-
self.ads1013.configure(config=config)
133+
self.assertTrue(self.ads1013.configure(config=config))
128134
# -----------------------------------------------------------------
129135
# -----------------------------------------------------------------
130136
with BurstHandler(i2c_bus=self.i2c_bus, i2c_adr=self.i2c_adr) as bh:

0 commit comments

Comments
 (0)