-
Notifications
You must be signed in to change notification settings - Fork 2
Adding library and example #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
20c11b5
Adding library and example
BlitzCityDIY f80fc15
redo API with analog_in
BlitzCityDIY 101784f
Update pyproject.toml
BlitzCityDIY 6cbac31
Update ads7830_simpletest.py
BlitzCityDIY f574919
move power down modes to init
BlitzCityDIY 7f8d06d
fix docstring
BlitzCityDIY 1045b8b
remove power down list
BlitzCityDIY f550889
update param definition
BlitzCityDIY 0c2dffb
change to temporary variable
BlitzCityDIY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 Liz Clark for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: MIT | ||
""" | ||
:py:class:`~adafruit_ads7830.ads7830.ADS7830` | ||
================================================================================ | ||
|
||
CircuitPython driver for the ADS7830 analog to digital converter | ||
|
||
|
||
* Author(s): Liz Clark | ||
|
||
Implementation Notes | ||
-------------------- | ||
|
||
**Hardware:** | ||
|
||
* `Adafruit ADS7830 8-Channel 8-Bit ADC with I2C <https://www.adafruit.com/product/5836>`_ | ||
|
||
**Software and Dependencies:** | ||
|
||
* Adafruit CircuitPython firmware for the supported boards: | ||
https://circuitpython.org/downloads | ||
|
||
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice | ||
""" | ||
|
||
from adafruit_bus_device.i2c_device import I2CDevice | ||
from micropython import const | ||
|
||
try: | ||
import typing # pylint: disable=unused-import | ||
from busio import I2C | ||
except ImportError: | ||
pass | ||
|
||
__version__ = "0.0.0+auto.0" | ||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADS7830.git" | ||
|
||
_I2C_ADDR = const(0x48) | ||
|
||
|
||
class ADS7830: | ||
"""Adafruit ADS7830 ADC driver""" | ||
|
||
_POWER_DOWN_MODES = [ | ||
(True, True), # power down ref and adc | ||
(True, False), # power down ref and not adc | ||
(False, True), # power down adc and not ref | ||
(False, False), # do not power down ref or adc | ||
] | ||
# Single channel selection list | ||
_CHANNEL_SELECTION = [ | ||
0x08, # SINGLE_CH0 | ||
0x0C, # SINGLE_CH1 | ||
0x09, # SINGLE_CH2 | ||
0x0D, # SINGLE_CH3 | ||
0x0A, # SINGLE_CH4 | ||
0x0E, # SINGLE_CH5 | ||
0x0B, # SINGLE_CH6 | ||
0x0F, # SINGLE_CH7 | ||
] | ||
# Differential channel selection list | ||
_DIFF_CHANNEL_SELECTION = [ | ||
0x00, # DIFF_CH0_CH1 | ||
0x04, # DIFF_CH1_CH0 | ||
0x01, # DIFF_CH2_CH3 | ||
0x05, # DIFF_CH3_CH2 | ||
0x02, # DIFF_CH4_CH5 | ||
0x06, # DIFF_CH5_CH4 | ||
0x03, # DIFF_CH6_CH7 | ||
0x07, # DIFF_CH7_CH6 | ||
] | ||
|
||
# pylint: disable=too-many-arguments | ||
def __init__( | ||
self, | ||
i2c: I2C, | ||
address: int = _I2C_ADDR, | ||
diff_mode: bool = False, | ||
int_ref_pd: bool = False, | ||
adc_pd: bool = False, | ||
) -> None: | ||
"""Initialization over I2C | ||
|
||
:param int address: I2C address (default 0x48) | ||
:param bool diff_mode: Select differential vs. single mode | ||
:param bool int_ref_pd: Power down mode for internal reference (defaults to False) | ||
:param bool adc_pd: Power down mode for ADC (defaults to False) | ||
""" | ||
self.i2c_device = I2CDevice(i2c, address) | ||
_pd = (int_ref_pd, adc_pd) | ||
self.power_down = self._POWER_DOWN_MODES.index(_pd) | ||
self.differential_mode = diff_mode | ||
|
||
def read(self, channel: int) -> int: | ||
"""ADC value | ||
Scales the 8-bit ADC value to a 16-bit value | ||
|
||
:param int channel: Channel (0-7) | ||
:return: Scaled ADC value or raise an exception if read failed | ||
:rtype: int | ||
""" | ||
if channel > 7: | ||
raise ValueError("Invalid channel: must be 0-7") | ||
if self.differential_mode: | ||
command_byte = self._DIFF_CHANNEL_SELECTION[channel // 2] | ||
else: | ||
command_byte = self._CHANNEL_SELECTION[channel] | ||
command_byte <<= 4 | ||
command_byte |= self.power_down << 2 | ||
|
||
with self.i2c_device as i2c: | ||
try: | ||
# Buffer to store the read ADC value | ||
adc_value = bytearray(1) | ||
i2c.write_then_readinto(bytearray([command_byte]), adc_value) | ||
# Scale the 8-bit value to 16-bit | ||
return adc_value[0] << 8 | ||
except Exception as error: | ||
raise RuntimeError(f"Failed to read value: {error}") from error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
:py:class:`~adafruit_ads7830.analog_in.AnalogIn` | ||
====================================================== | ||
AnalogIn for ADC readings. | ||
|
||
* Author(s): Liz Clark | ||
|
||
""" | ||
|
||
from adafruit_ads7830.ads7830 import ADS7830 | ||
|
||
|
||
class AnalogIn: | ||
"""AnalogIn Mock Implementation for ADC Reads. | ||
|
||
:param ADS7830 adc: The ADC object. | ||
:param int pin: Required pin for reading. | ||
""" | ||
|
||
def __init__(self, adc: ADS7830, pin: int) -> None: | ||
if not isinstance(adc, ADS7830): | ||
raise ValueError("ADC object is from the ADS7830 class.") | ||
self._adc = adc | ||
self._pin = pin | ||
|
||
@property | ||
def value(self) -> int: | ||
"""Returns the value of an ADC pin as an integer in the range [0, 65535].""" | ||
result = self._adc.read(self._pin) | ||
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 Liz Clark for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries | ||
# SPDX-License-Identifier: MIT | ||
|
||
# Simple demo to read analog input on channel 0 | ||
|
||
import time | ||
import board | ||
import adafruit_ads7830.ads7830 as ADC | ||
from adafruit_ads7830.analog_in import AnalogIn | ||
|
||
i2c = board.I2C() | ||
|
||
# Initialize ADS7830 | ||
adc = ADC.ADS7830(i2c) | ||
chan = AnalogIn(adc, 0) | ||
|
||
while True: | ||
print(f"ADC channel 0 = {chan.value}") | ||
time.sleep(0.1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.