diff --git a/README.rst b/README.rst index 1e11321..090dc66 100644 --- a/README.rst +++ b/README.rst @@ -67,24 +67,24 @@ Single Ended .. code-block:: python import time + import board - import busio - import adafruit_ads1x15.ads1015 as ADS - from adafruit_ads1x15.analog_in import AnalogIn + + from adafruit_ads1x15 import ADS1015, AnalogIn, ads1x15 # Create the I2C bus - i2c = busio.I2C(board.SCL, board.SDA) + i2c = board.I2C() # Create the ADC object using the I2C bus - ads = ADS.ADS1015(i2c) + ads = ADS1015(i2c) # Create single-ended input on channel 0 - chan = AnalogIn(ads, ADS.P0) + chan = AnalogIn(ads, ads1x15.Pin.A0) # Create differential input between channel 0 and 1 - #chan = AnalogIn(ads, ADS.P0, ADS.P1) + # chan = AnalogIn(ads, ads1x15.Pin.A0, ads1x15.Pin.A1) - print("{:>5}\t{:>5}".format('raw', 'v')) + print("{:>5}\t{:>5}".format("raw", "v")) while True: print("{:>5}\t{:>5.3f}".format(chan.value, chan.voltage)) diff --git a/adafruit_ads1x15/__init__.py b/adafruit_ads1x15/__init__.py index e69de29..2b33abe 100644 --- a/adafruit_ads1x15/__init__.py +++ b/adafruit_ads1x15/__init__.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: 2018 Carter Nelson for Adafruit Industries +# SPDX-FileCopyrightText: 2025 Asadullah Shaikh +# +# SPDX-License-Identifier: MIT + +""" +`adafruit_ads1x15` +==================================================== + +Support for the ADS1x15 series of analog-to-digital converters. + +* Author(s): Carter Nelson +""" + +from .ads1015 import ADS1015 +from .ads1115 import ADS1115 +from .analog_in import AnalogIn + +__all__ = ["ADS1015", "ADS1115", "AnalogIn"] diff --git a/adafruit_ads1x15/ads1015.py b/adafruit_ads1x15/ads1015.py index 666735e..e999b05 100644 --- a/adafruit_ads1x15/ads1015.py +++ b/adafruit_ads1x15/ads1015.py @@ -33,16 +33,6 @@ 3300: 0x00C0, } -# Pins -P0 = 0 -"""Analog Pin 0""" -P1 = 1 -"""Analog Pin 1""" -P2 = 2 -"""Analog Pin 2""" -P3 = 3 -"""Analog Pin 3""" - class ADS1015(ADS1x15): """Class for the ADS1015 12 bit ADC.""" diff --git a/adafruit_ads1x15/ads1115.py b/adafruit_ads1x15/ads1115.py index 0933f24..0a919ea 100644 --- a/adafruit_ads1x15/ads1115.py +++ b/adafruit_ads1x15/ads1115.py @@ -34,16 +34,6 @@ 860: 0x00E0, } -# Pins -P0 = 0 -"""Analog Pin 0""" -P1 = 1 -"""Analog Pin 1""" -P2 = 2 -"""Analog Pin 2""" -P3 = 3 -"""Analog Pin 3""" - class ADS1115(ADS1x15): """Class for the ADS1115 16 bit ADC.""" diff --git a/adafruit_ads1x15/ads1x15.py b/adafruit_ads1x15/ads1x15.py index 573b9e7..4d7d155 100644 --- a/adafruit_ads1x15/ads1x15.py +++ b/adafruit_ads1x15/ads1x15.py @@ -23,12 +23,8 @@ from typing import Dict, List, Optional from busio import I2C - from microcontroller import Pin except ImportError: - # define Pin to avoid the error: - # def read(self, pin: Pin, is_differential: bool = False) -> int: - # NameError: name 'Pin' is not defined - Pin = None + pass _ADS1X15_DEFAULT_ADDRESS = const(0x48) _ADS1X15_POINTER_CONVERSION = const(0x00) @@ -54,6 +50,19 @@ } +class Pin: + """An enum-like class representing possible ADC pins.""" + + A0 = 0 + """Analog Pin 0""" + A1 = 1 + """Analog Pin 1""" + A2 = 2 + """Analog Pin 2""" + A3 = 3 + """Analog Pin 3""" + + class Mode: """An enum-like class representing possible ADC operating modes.""" @@ -127,7 +136,7 @@ class ADS1x15: def __init__( self, - i2c: I2C, + i2c: "I2C", gain: float = 1, data_rate: Optional[int] = None, mode: int = Mode.SINGLE, @@ -312,7 +321,7 @@ def comparator_latch(self, comp_latch: int) -> None: if self.initialized: self._write_config() - def read(self, pin: Pin) -> int: + def read(self, pin: Optional[int]) -> int: """I2C Interface for ADS1x15-based ADCs reads. :param ~microcontroller.Pin pin: individual or differential pin. @@ -332,7 +341,7 @@ def _conversion_value(self, raw_adc: int) -> int: """ raise NotImplementedError("Subclass must implement _conversion_value function!") - def _read(self, pin: Pin) -> int: + def _read(self, pin: Optional[int]) -> int: """Perform an ADC read. Returns the signed integer result of the read.""" # Immediately return conversion register result if in CONTINUOUS mode # and pin has not changed diff --git a/examples/ads1x15_ads1115_simpletest.py b/examples/ads1x15_ads1115_simpletest.py index 6ebe7d5..daa5d7b 100644 --- a/examples/ads1x15_ads1115_simpletest.py +++ b/examples/ads1x15_ads1115_simpletest.py @@ -4,24 +4,22 @@ import time import board -import busio -import adafruit_ads1x15.ads1115 as ADS -from adafruit_ads1x15.analog_in import AnalogIn +from adafruit_ads1x15 import ADS1115, AnalogIn, ads1x15 # Create the I2C bus -i2c = busio.I2C(board.SCL, board.SDA) +i2c = board.I2C() # Create the ADC object using the I2C bus -ads = ADS.ADS1115(i2c) +ads = ADS1115(i2c) # you can specify an I2C adress instead of the default 0x48 # ads = ADS.ADS1115(i2c, address=0x49) # Create single-ended input on channel 0 -chan = AnalogIn(ads, ADS.P0) +chan = AnalogIn(ads, ads1x15.Pin.A0) # Create differential input between channel 0 and 1 -# chan = AnalogIn(ads, ADS.P0, ADS.P1) +# chan = AnalogIn(ads, ads1x15.Pin.A0, ads1x15.Pin.A1) print("{:>5}\t{:>5}".format("raw", "v")) diff --git a/examples/ads1x15_comparator_example.py b/examples/ads1x15_comparator_example.py index 03cdaea..c71719b 100644 --- a/examples/ads1x15_comparator_example.py +++ b/examples/ads1x15_comparator_example.py @@ -4,40 +4,35 @@ import time import board -import busio import countio -import adafruit_ads1x15.ads1015 as ADS - -# import adafruit_ads1x15.ads1115 as ADS -from adafruit_ads1x15.ads1x15 import Comp_Latch, Comp_Mode, Comp_Polarity, Mode -from adafruit_ads1x15.analog_in import AnalogIn +from adafruit_ads1x15 import ADS1015, AnalogIn, ads1x15 # Create the I2C bus -i2c = busio.I2C(board.SCL, board.SDA) +i2c = board.I2C() # Create the ADS object -ads = ADS.ADS1015(i2c) +ads = ADS1015(i2c) # ads = ADS.ADS1115(i2c) -# Create a single-ended channel on Pin 0 +# Create a single-ended channel on Pin A0 # Max counts for ADS1015 = 2047 # ADS1115 = 32767 -chan = AnalogIn(ads, ADS.P0) +chan = AnalogIn(ads, ads1x15.Pin.A0) # Create Interrupt-driven input to track comparator changes int_pin = countio.Counter(board.GP9, edge=countio.Edge.RISE) # Set ADC to continuously read new data -ads.mode = Mode.CONTINUOUS +ads.mode = ads1x15.Mode.CONTINUOUS # Set comparator to assert after 1 ADC conversion ads.comparator_queue_length = 1 # Set comparator to use traditional threshold instead of window -ads.comparator_mode = Comp_Mode.TRADITIONAL +ads.comparator_mode = ads1x15.Comp_Mode.TRADITIONAL # Set comparator output to de-assert if readings no longer above threshold -ads.comparator_latch = Comp_Latch.NONLATCHING +ads.comparator_latch = ads1x15.Comp_Latch.NONLATCHING # Set comparator output to logic LOW when asserted -ads.comparator_polarity = Comp_Polarity.ACTIVE_LOW +ads.comparator_polarity = ads1x15.Comp_Polarity.ACTIVE_LOW # Gain should be explicitly set to ensure threshold values are calculated correctly ads.gain = 1 # Set comparator low threshold to 2V diff --git a/examples/ads1x15_fast_read.py b/examples/ads1x15_fast_read.py index af04072..3cbc5b0 100644 --- a/examples/ads1x15_fast_read.py +++ b/examples/ads1x15_fast_read.py @@ -6,9 +6,7 @@ import board import busio -import adafruit_ads1x15.ads1015 as ADS -from adafruit_ads1x15.ads1x15 import Mode -from adafruit_ads1x15.analog_in import AnalogIn +from adafruit_ads1x15 import ADS1015, AnalogIn, ads1x15 # Data collection setup RATE = 3300 @@ -21,18 +19,18 @@ i2c = busio.I2C(board.SCL, board.SDA, frequency=1000000) # Create the ADC object using the I2C bus -ads = ADS.ADS1015(i2c) +ads = ADS1015(i2c) # Create single-ended input on channel 0 -chan0 = AnalogIn(ads, ADS.P0) +chan = AnalogIn(ads, ads1x15.Pin.A0) # ADC Configuration -ads.mode = Mode.CONTINUOUS +ads.mode = ads1x15.Mode.CONTINUOUS ads.data_rate = RATE # First ADC channel read in continuous mode configures device # and waits 2 conversion cycles -_ = chan0.value +_ = chan.value sample_interval = 1.0 / ads.data_rate @@ -51,7 +49,7 @@ pass # Read conversion value for ADC channel - data[i] = chan0.value + data[i] = chan.value # Loop timing time_last_sample = time.monotonic() diff --git a/examples/ads1x15_gain_example.py b/examples/ads1x15_gain_example.py index 9acf8d0..7b87b06 100644 --- a/examples/ads1x15_gain_example.py +++ b/examples/ads1x15_gain_example.py @@ -4,23 +4,20 @@ import time import board -import busio -# import adafruit_ads1x15.ads1015 as ADS -import adafruit_ads1x15.ads1115 as ADS -from adafruit_ads1x15.analog_in import AnalogIn +from adafruit_ads1x15 import ADS1115, AnalogIn, ads1x15 # Create the I2C bus -i2c = busio.I2C(board.SCL, board.SDA) +i2c = board.I2C() # Create the ADS object # ads = ADS.ADS1015(i2c) -ads = ADS.ADS1115(i2c) +ads = ADS1115(i2c) -# Create a single-ended channel on Pin 0 +# Create a single-ended channel on Pin A0 # Max counts for ADS1015 = 2047 # ADS1115 = 32767 -chan = AnalogIn(ads, ADS.P0) +chan = AnalogIn(ads, ads1x15.Pin.A0) # The ADS1015 and ADS1115 both have the same gain options. # diff --git a/examples/ads1x15_simpletest.py b/examples/ads1x15_simpletest.py index 041d978..c00b2a9 100644 --- a/examples/ads1x15_simpletest.py +++ b/examples/ads1x15_simpletest.py @@ -4,22 +4,20 @@ import time import board -import busio -import adafruit_ads1x15.ads1015 as ADS -from adafruit_ads1x15.analog_in import AnalogIn +from adafruit_ads1x15 import ADS1015, AnalogIn, ads1x15 # Create the I2C bus -i2c = busio.I2C(board.SCL, board.SDA) +i2c = board.I2C() # Create the ADC object using the I2C bus -ads = ADS.ADS1015(i2c) +ads = ADS1015(i2c) # Create single-ended input on channel 0 -chan = AnalogIn(ads, ADS.P0) +chan = AnalogIn(ads, ads1x15.Pin.A0) # Create differential input between channel 0 and 1 -# chan = AnalogIn(ads, ADS.P0, ADS.P1) +# chan = AnalogIn(ads, ads1x15.Pin.A0, ads1x15.Pin.A1) print("{:>5}\t{:>5}".format("raw", "v"))