Skip to content

Commit f62b324

Browse files
authored
feat: implement device reset logic (#4)
* feat: implement device reset logic * test: verify the device reset logic (multi-byte read/write) * chore: inject copier template 'pypackage-template'
1 parent afd37ba commit f62b324

23 files changed

+1428
-327
lines changed

.copier/answers_pypackage.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
# Changes here will be overwritten by Copier; NEVER EDIT MANUALLY
3+
#
4+
# You can use `pdm run copier update --defaults` to update the template
5+
# files using the answers stored in this file.
6+
# (see https://copier.readthedocs.io/en/stable/updating/ for details)
7+
8+
_commit: v0.1.3
9+
_src_path: https://github.com/feeph/pypackage-template
10+
author_email: 55798703+feeph@users.noreply.github.com
11+
author_name: Feeph Aifeimei
12+
default_branch: master
13+
is_typed: true
14+
issuetracker_url: https://github.com/feeph/libads1xxx-python/issues
15+
max_line_length: 250
16+
package_description: library for the ADS1xxx family of I²C analog-to-digital converters
17+
package_name: ads1xxx
18+
package_namespace: feeph
19+
python_constraint: '>=3.10,<3.13'
20+
repository_name: libads1xxx-python
21+
repository_url: https://github.com/feeph/libads1xxx-python

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# repository and assigned a permission level first. Otherwise they will
77
# be underlined with red squiggly lines when looking at the file in the
88
# WebGUI and it won't work.
9-
# -> https://github.com/feeph/libads1xxx-python/settings/access
9+
# (GitHub -> Repository -> Settings -> Collaborators)
1010
#
1111

1212
# define default ownership
File renamed without changes.

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
# documentation can be found at https://pre-commit.com/
44
#
55
# perform once after cloning the repository:
6-
# pipx install pre-commit
7-
# pre-commit install --allow-missing-config --hook-type pre-commit
8-
# pre-commit install --allow-missing-config --hook-type commit-msg
9-
# pre-commit install --allow-missing-config --hook-type post-commit
10-
# pre-commit install --allow-missing-config --hook-type pre-push
6+
# scripts/prepare_repository
117
#
128
# if you want to trigger pre-commit manually:
139
# pre-commit run
10+
# pre-commit run --all-files
1411
#
1512
repos:
1613
- repo: https://github.com/pre-commit/pre-commit-hooks
1714
rev: v2.3.0
1815
hooks:
16+
- id: check-merge-conflict
17+
args: [--assume-in-merge]
1918
- id: check-json
19+
- id: check-toml
2020
- id: check-yaml
2121
- id: end-of-file-fixer
2222
- id: trailing-whitespace

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# libads1xxx-python
22

3+
[![Copier](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/copier-org/copier/master/img/badge/badge-grayscale-inverted-border-purple.json)](https://github.com/copier-org/copier)
34
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)
45
[![pdm-managed](https://img.shields.io/endpoint?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Fpdm-project%2F.github%2Fbadge.json)](https://pdm-project.org)
56
[![tox](https://img.shields.io/badge/tox-ab79d2)](https://tox.wiki/)
@@ -12,7 +13,7 @@ library for the ADS1xxx family of I²C analog-to-digital converters
1213

1314
## Bugs & Features
1415

15-
Please submit bugs and request features on the [issue tracker]( https://github.com/feeph/libads1xxx-python/issues).
16+
Please submit bugs and request features on the [issue tracker](https://github.com/feeph/libads1xxx-python/issues).
1617

1718
Contributions are always welcome.
1819

docs/CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ tox
5252
### use the demo script
5353

5454
```SHELL
55-
pdm run scripts/demonstrator.py
56-
pdm run scripts/demonstrator.py -v -i 2
55+
pdm run examples/demonstrator.py
56+
pdm run examples/demonstrator.py -v -i 2
5757
```

feeph/ads1xxx/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python3
22
"""
3+
ADS1xxx family of I²C analog-to-digital converters
34
"""
45

56
# the following imports are provided for user convenience
67
# flake8: noqa: F401
7-
from feeph.ads1xxx.component import Component
8-
from feeph.ads1xxx.functions import function1
8+
from feeph.ads1xxx.ads1115 import Ads1115, Ads1115Config

feeph/ads1xxx/ads1115.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python3
2+
"""
3+
ADS111x - Ultra-Small, Low-Power, I2C-Compatible, 860-SPS, 16-Bit ADCs
4+
With Internal Reference, Oscillator, and Programmable Comparator
5+
6+
datasheet: https://www.ti.com/lit/ds/symlink/ads1115.pdf
7+
"""
8+
9+
import logging
10+
11+
# module busio provides no type hints
12+
import busio # type: ignore
13+
from attrs import define
14+
from feeph.i2c import BurstHandler
15+
16+
from feeph.ads1xxx.ads111x import Ads111x
17+
18+
LH = logging.getLogger('feeph.ads1xxx')
19+
20+
21+
@define
22+
class Ads1115Config:
23+
"""
24+
The 16-bit Config register is used to control the operating mode, input
25+
selection, data rate, full-scale range, and comparator modes.
26+
"""
27+
# fmt: off
28+
OSSA: int # 0b#..._...._...._.... status or single shot start
29+
IMUX: int # 0b.###_...._...._.... input multiplexer configuration
30+
PGA: int # 0b...._###._...._.... programmable gain amplifier
31+
MODE: int # 0b...._...#_...._.... operating mode
32+
DR: int # 0b...._...._###._.... data rate
33+
COMP_MOD: int # 0b...._...._...#_.... comparator mode
34+
COMP_POL: int # 0b...._...._...._#... comparator polarity
35+
COMP_LAT: int # 0b...._...._...._.#.. latching comparator
36+
COMP_QUE: int # 0b...._...._...._..## comparator queue & disable
37+
# fmt: on
38+
39+
40+
DEFAULTS = {
41+
0x01: 0x8583,
42+
0x02: 0x8000,
43+
0x03: 0x7FFF,
44+
}
45+
46+
47+
class Ads1115(Ads111x):
48+
# 0x00 - conversion register (2 bytes, ro, default: 0x0000)
49+
# 0x01 - config register (2 bytes, rw, default: 0x8583)
50+
# 0x10 - lo_thresh register (2 bytes, rw, default: 0x0080)
51+
# 0x11 - hi_thresh register (2 bytes, rw, default: 0xFF7F)
52+
53+
def __init__(self, i2c_bus: busio.I2C):
54+
self._i2c_bus = i2c_bus
55+
self._i2c_adr = 0x48 # the I²C bus address is hardcoded
56+
57+
def get_config(self) -> Ads1115Config:
58+
with BurstHandler(i2c_bus=self._i2c_bus, i2c_adr=self._i2c_adr) as bh:
59+
value = bh.read_register(0x01, byte_count=2)
60+
params = {
61+
"OSSA": value & 0b1000_0000_0000_0000,
62+
"IMUX": value & 0b0111_0000_0000_0000,
63+
"PGA": value & 0b0000_1110_0000_0000,
64+
"MODE": value & 0b0000_0001_0000_0000,
65+
"DR": value & 0b0000_0000_1110_0000,
66+
"COMP_MOD": value & 0b0000_0000_0001_0000,
67+
"COMP_POL": value & 0b0000_0000_0000_1000,
68+
"COMP_LAT": value & 0b0000_0000_0000_0100,
69+
"COMP_QUE": value & 0b0000_0000_0000_0011,
70+
}
71+
return Ads1115Config(**params)
72+
73+
def set_config(self, config: Ads1115Config):
74+
value = 0b0000_0000_0000_0000
75+
value &= config.OSSA
76+
value &= config.IMUX
77+
value &= config.PGA
78+
value &= config.MODE
79+
value &= config.DR
80+
value &= config.COMP_MOD
81+
value &= config.COMP_POL
82+
value &= config.COMP_LAT
83+
value &= config.COMP_QUE
84+
with BurstHandler(i2c_bus=self._i2c_bus, i2c_adr=self._i2c_adr) as bh:
85+
bh.write_register(0x01, value, byte_count=2)
86+
87+
def reset_device_registers(self):
88+
with BurstHandler(i2c_bus=self._i2c_bus, i2c_adr=self._i2c_adr) as bh:
89+
for register, value in DEFAULTS.items():
90+
bh.write_register(register, value, byte_count=2)
91+
92+
# ---------------------------------------------------------------------
93+
94+
def get_measurement(self) -> int:
95+
return 0
96+
97+
# ---------------------------------------------------------------------

feeph/ads1xxx/ads111x.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
"""
3+
abstract base class for ADS1113, ADS1114 and ADS1115
4+
"""
5+
6+
import logging
7+
from abc import ABC, abstractmethod
8+
9+
LH = logging.getLogger('feeph.ads1xxx')
10+
11+
12+
class Ads111x(ABC):
13+
"""
14+
15+
"""
16+
17+
@abstractmethod
18+
def get_measurement(self) -> int:
19+
...
20+
21+
@abstractmethod
22+
def reset_device_registers(self):
23+
...

0 commit comments

Comments
 (0)