Skip to content

Commit 66be365

Browse files
authored
Merge pull request #1 from BlitzCityDIY/main
Adding library and example
2 parents 7c0778e + 0c2dffb commit 66be365

File tree

7 files changed

+184
-45
lines changed

7 files changed

+184
-45
lines changed

adafruit_ads7830.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

adafruit_ads7830/ads7830.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
:py:class:`~adafruit_ads7830.ads7830.ADS7830`
6+
================================================================================
7+
8+
CircuitPython driver for the ADS7830 analog to digital converter
9+
10+
11+
* Author(s): Liz Clark
12+
13+
Implementation Notes
14+
--------------------
15+
16+
**Hardware:**
17+
18+
* `Adafruit ADS7830 8-Channel 8-Bit ADC with I2C <https://www.adafruit.com/product/5836>`_
19+
20+
**Software and Dependencies:**
21+
22+
* Adafruit CircuitPython firmware for the supported boards:
23+
https://circuitpython.org/downloads
24+
25+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
26+
"""
27+
28+
from adafruit_bus_device.i2c_device import I2CDevice
29+
from micropython import const
30+
31+
try:
32+
import typing # pylint: disable=unused-import
33+
from busio import I2C
34+
except ImportError:
35+
pass
36+
37+
__version__ = "0.0.0+auto.0"
38+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADS7830.git"
39+
40+
_I2C_ADDR = const(0x48)
41+
42+
43+
class ADS7830:
44+
"""Adafruit ADS7830 ADC driver"""
45+
46+
# Single channel selection list
47+
_CHANNEL_SELECTION = [
48+
0x08, # SINGLE_CH0
49+
0x0C, # SINGLE_CH1
50+
0x09, # SINGLE_CH2
51+
0x0D, # SINGLE_CH3
52+
0x0A, # SINGLE_CH4
53+
0x0E, # SINGLE_CH5
54+
0x0B, # SINGLE_CH6
55+
0x0F, # SINGLE_CH7
56+
]
57+
# Differential channel selection list
58+
_DIFF_CHANNEL_SELECTION = [
59+
0x00, # DIFF_CH0_CH1
60+
0x04, # DIFF_CH1_CH0
61+
0x01, # DIFF_CH2_CH3
62+
0x05, # DIFF_CH3_CH2
63+
0x02, # DIFF_CH4_CH5
64+
0x06, # DIFF_CH5_CH4
65+
0x03, # DIFF_CH6_CH7
66+
0x07, # DIFF_CH7_CH6
67+
]
68+
69+
# pylint: disable=too-many-arguments
70+
def __init__(
71+
self,
72+
i2c: I2C,
73+
address: int = _I2C_ADDR,
74+
differential_mode: bool = False,
75+
int_ref_power_down: bool = False,
76+
adc_power_down: bool = False,
77+
) -> None:
78+
"""Initialization over I2C
79+
80+
:param int address: I2C address (default 0x48)
81+
:param bool differential_mode: Select differential vs. single mode
82+
:param bool int_ref_power_down: Power down internal reference after sampling
83+
:param bool adc_power_down: Power down ADC after sampling
84+
"""
85+
self.i2c_device = I2CDevice(i2c, address)
86+
_pd = 0
87+
if not int_ref_power_down:
88+
_pd |= 2
89+
if not adc_power_down:
90+
_pd |= 1
91+
self.power_down = _pd
92+
self.differential_mode = differential_mode
93+
94+
def read(self, channel: int) -> int:
95+
"""ADC value
96+
Scales the 8-bit ADC value to a 16-bit value
97+
98+
:param int channel: Channel (0-7)
99+
:return: Scaled ADC value or raise an exception if read failed
100+
:rtype: int
101+
"""
102+
if channel > 7:
103+
raise ValueError("Invalid channel: must be 0-7")
104+
if self.differential_mode:
105+
command_byte = self._DIFF_CHANNEL_SELECTION[channel // 2]
106+
else:
107+
command_byte = self._CHANNEL_SELECTION[channel]
108+
command_byte <<= 4
109+
command_byte |= self.power_down << 2
110+
111+
with self.i2c_device as i2c:
112+
try:
113+
# Buffer to store the read ADC value
114+
adc_value = bytearray(1)
115+
i2c.write_then_readinto(bytearray([command_byte]), adc_value)
116+
# Scale the 8-bit value to 16-bit
117+
return adc_value[0] << 8
118+
except Exception as error:
119+
raise RuntimeError(f"Failed to read value: {error}") from error

adafruit_ads7830/analog_in.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
:py:class:`~adafruit_ads7830.analog_in.AnalogIn`
7+
======================================================
8+
AnalogIn for ADC readings.
9+
10+
* Author(s): Liz Clark
11+
12+
"""
13+
14+
from adafruit_ads7830.ads7830 import ADS7830
15+
16+
17+
class AnalogIn:
18+
"""AnalogIn Mock Implementation for ADC Reads.
19+
20+
:param ADS7830 adc: The ADC object.
21+
:param int pin: Required pin for reading.
22+
"""
23+
24+
def __init__(self, adc: ADS7830, pin: int) -> None:
25+
if not isinstance(adc, ADS7830):
26+
raise ValueError("ADC object is from the ADS7830 class.")
27+
self._adc = adc
28+
self._pin = pin
29+
30+
@property
31+
def value(self) -> int:
32+
"""Returns the value of an ADC pin as an integer in the range [0, 65535]."""
33+
result = self._adc.read(self._pin)
34+
return result

docs/api.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7-
.. automodule:: adafruit_ads7830
7+
.. automodule:: adafruit_ads7830.ads7830
8+
:members:
9+
10+
.. automodule:: adafruit_ads7830.analog_in
811
:members:

docs/conf.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
"sphinx.ext.todo",
2424
]
2525

26-
# TODO: Please Read!
27-
# Uncomment the below if you use native CircuitPython modules such as
28-
# digitalio, micropython and busio. List the modules you use. Without it, the
29-
# autodoc module docs will fail to generate with a warning.
30-
autodoc_mock_imports = ["board", "busio", "micropython"]
26+
autodoc_mock_imports = [
27+
"micropython",
28+
"busio",
29+
"adafruit_bus_device",
30+
]
3131

3232
autodoc_preserve_defaults = True
3333

3434

3535
intersphinx_mapping = {
36-
"python": ("https://docs.python.org/3", None),"BusDevice": ("https://docs.circuitpython.org/projects/busdevice/en/latest/", None),
37-
"Register": ("https://docs.circuitpython.org/projects/register/en/latest/", None),
36+
"python": ("https://docs.python.org/3", None),
37+
"BusDevice": ("https://docs.circuitpython.org/projects/busdevice/en/latest/", None),
3838
"CircuitPython": ("https://docs.circuitpython.org/en/latest/", None),
3939
}
4040

examples/ads7830_simpletest.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2-
# SPDX-FileCopyrightText: Copyright (c) 2023 Liz Clark for Adafruit Industries
3-
#
4-
# SPDX-License-Identifier: Unlicense
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# Simple demo to read analog input on channel 0
5+
6+
import time
7+
import board
8+
import adafruit_ads7830.ads7830 as ADC
9+
from adafruit_ads7830.analog_in import AnalogIn
10+
11+
i2c = board.I2C()
12+
13+
# Initialize ADS7830
14+
adc = ADC.ADS7830(i2c)
15+
chan = AnalogIn(adc, 0)
16+
17+
while True:
18+
print(f"ADC channel 0 = {chan.value}")
19+
time.sleep(0.1)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dynamic = ["dependencies", "optional-dependencies"]
4141
[tool.setuptools]
4242
# TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER,
4343
# CHANGE `py_modules = ['...']` TO `packages = ['...']`
44-
py-modules = ["adafruit_ads7830"]
44+
packages = ["adafruit_ads7830"]
4545

4646
[tool.setuptools.dynamic]
4747
dependencies = {file = ["requirements.txt"]}

0 commit comments

Comments
 (0)