Skip to content

Commit 09f7128

Browse files
authored
Merge pull request #101 from pantheraleo-7/main
re-export symbols (classes, constants) to accessible & ergonomic locations
2 parents 648ad4b + fcc4fa3 commit 09f7128

File tree

10 files changed

+74
-80
lines changed

10 files changed

+74
-80
lines changed

README.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,24 @@ Single Ended
6767
.. code-block:: python
6868
6969
import time
70+
7071
import board
71-
import busio
72-
import adafruit_ads1x15.ads1015 as ADS
73-
from adafruit_ads1x15.analog_in import AnalogIn
72+
73+
from adafruit_ads1x15 import ADS1015, AnalogIn, ads1x15
7474
7575
# Create the I2C bus
76-
i2c = busio.I2C(board.SCL, board.SDA)
76+
i2c = board.I2C()
7777
7878
# Create the ADC object using the I2C bus
79-
ads = ADS.ADS1015(i2c)
79+
ads = ADS1015(i2c)
8080
8181
# Create single-ended input on channel 0
82-
chan = AnalogIn(ads, ADS.P0)
82+
chan = AnalogIn(ads, ads1x15.Pin.A0)
8383
8484
# Create differential input between channel 0 and 1
85-
#chan = AnalogIn(ads, ADS.P0, ADS.P1)
85+
# chan = AnalogIn(ads, ads1x15.Pin.A0, ads1x15.Pin.A1)
8686
87-
print("{:>5}\t{:>5}".format('raw', 'v'))
87+
print("{:>5}\t{:>5}".format("raw", "v"))
8888
8989
while True:
9090
print("{:>5}\t{:>5.3f}".format(chan.value, chan.voltage))

adafruit_ads1x15/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: 2018 Carter Nelson for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2025 Asadullah Shaikh <github.com/pantheraleo-7>
3+
#
4+
# SPDX-License-Identifier: MIT
5+
6+
"""
7+
`adafruit_ads1x15`
8+
====================================================
9+
10+
Support for the ADS1x15 series of analog-to-digital converters.
11+
12+
* Author(s): Carter Nelson
13+
"""
14+
15+
from .ads1015 import ADS1015
16+
from .ads1115 import ADS1115
17+
from .analog_in import AnalogIn
18+
19+
__all__ = ["ADS1015", "ADS1115", "AnalogIn"]

adafruit_ads1x15/ads1015.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@
3333
3300: 0x00C0,
3434
}
3535

36-
# Pins
37-
P0 = 0
38-
"""Analog Pin 0"""
39-
P1 = 1
40-
"""Analog Pin 1"""
41-
P2 = 2
42-
"""Analog Pin 2"""
43-
P3 = 3
44-
"""Analog Pin 3"""
45-
4636

4737
class ADS1015(ADS1x15):
4838
"""Class for the ADS1015 12 bit ADC."""

adafruit_ads1x15/ads1115.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@
3434
860: 0x00E0,
3535
}
3636

37-
# Pins
38-
P0 = 0
39-
"""Analog Pin 0"""
40-
P1 = 1
41-
"""Analog Pin 1"""
42-
P2 = 2
43-
"""Analog Pin 2"""
44-
P3 = 3
45-
"""Analog Pin 3"""
46-
4737

4838
class ADS1115(ADS1x15):
4939
"""Class for the ADS1115 16 bit ADC."""

adafruit_ads1x15/ads1x15.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@
2323
from typing import Dict, List, Optional
2424

2525
from busio import I2C
26-
from microcontroller import Pin
2726
except ImportError:
28-
# define Pin to avoid the error:
29-
# def read(self, pin: Pin, is_differential: bool = False) -> int:
30-
# NameError: name 'Pin' is not defined
31-
Pin = None
27+
pass
3228

3329
_ADS1X15_DEFAULT_ADDRESS = const(0x48)
3430
_ADS1X15_POINTER_CONVERSION = const(0x00)
@@ -54,6 +50,19 @@
5450
}
5551

5652

53+
class Pin:
54+
"""An enum-like class representing possible ADC pins."""
55+
56+
A0 = 0
57+
"""Analog Pin 0"""
58+
A1 = 1
59+
"""Analog Pin 1"""
60+
A2 = 2
61+
"""Analog Pin 2"""
62+
A3 = 3
63+
"""Analog Pin 3"""
64+
65+
5766
class Mode:
5867
"""An enum-like class representing possible ADC operating modes."""
5968

@@ -312,10 +321,10 @@ def comparator_latch(self, comp_latch: int) -> None:
312321
if self.initialized:
313322
self._write_config()
314323

315-
def read(self, pin: Pin) -> int:
324+
def read(self, pin: int) -> int:
316325
"""I2C Interface for ADS1x15-based ADCs reads.
317326
318-
:param ~microcontroller.Pin pin: individual or differential pin.
327+
:param int pin: individual or differential pin.
319328
:param bool is_differential: single-ended or differential read.
320329
"""
321330
return self._read(pin)
@@ -332,7 +341,7 @@ def _conversion_value(self, raw_adc: int) -> int:
332341
"""
333342
raise NotImplementedError("Subclass must implement _conversion_value function!")
334343

335-
def _read(self, pin: Pin) -> int:
344+
def _read(self, pin: int) -> int:
336345
"""Perform an ADC read. Returns the signed integer result of the read."""
337346
# Immediately return conversion register result if in CONTINUOUS mode
338347
# and pin has not changed

examples/ads1x15_ads1115_simpletest.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@
44
import time
55

66
import board
7-
import busio
87

9-
import adafruit_ads1x15.ads1115 as ADS
10-
from adafruit_ads1x15.analog_in import AnalogIn
8+
from adafruit_ads1x15 import ADS1115, AnalogIn, ads1x15
119

1210
# Create the I2C bus
13-
i2c = busio.I2C(board.SCL, board.SDA)
11+
i2c = board.I2C()
1412

1513
# Create the ADC object using the I2C bus
16-
ads = ADS.ADS1115(i2c)
14+
ads = ADS1115(i2c)
1715
# you can specify an I2C adress instead of the default 0x48
1816
# ads = ADS.ADS1115(i2c, address=0x49)
1917

2018
# Create single-ended input on channel 0
21-
chan = AnalogIn(ads, ADS.P0)
19+
chan = AnalogIn(ads, ads1x15.Pin.A0)
2220

2321
# Create differential input between channel 0 and 1
24-
# chan = AnalogIn(ads, ADS.P0, ADS.P1)
22+
# chan = AnalogIn(ads, ads1x15.Pin.A0, ads1x15.Pin.A1)
2523

2624
print("{:>5}\t{:>5}".format("raw", "v"))
2725

examples/ads1x15_comparator_example.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,35 @@
44
import time
55

66
import board
7-
import busio
87
import countio
98

10-
import adafruit_ads1x15.ads1015 as ADS
11-
12-
# import adafruit_ads1x15.ads1115 as ADS
13-
from adafruit_ads1x15.ads1x15 import Comp_Latch, Comp_Mode, Comp_Polarity, Mode
14-
from adafruit_ads1x15.analog_in import AnalogIn
9+
from adafruit_ads1x15 import ADS1015, AnalogIn, ads1x15
1510

1611
# Create the I2C bus
17-
i2c = busio.I2C(board.SCL, board.SDA)
12+
i2c = board.I2C()
1813

1914
# Create the ADS object
20-
ads = ADS.ADS1015(i2c)
15+
ads = ADS1015(i2c)
2116
# ads = ADS.ADS1115(i2c)
2217

23-
# Create a single-ended channel on Pin 0
18+
# Create a single-ended channel on Pin A0
2419
# Max counts for ADS1015 = 2047
2520
# ADS1115 = 32767
26-
chan = AnalogIn(ads, ADS.P0)
21+
chan = AnalogIn(ads, ads1x15.Pin.A0)
2722

2823
# Create Interrupt-driven input to track comparator changes
2924
int_pin = countio.Counter(board.GP9, edge=countio.Edge.RISE)
3025

3126
# Set ADC to continuously read new data
32-
ads.mode = Mode.CONTINUOUS
27+
ads.mode = ads1x15.Mode.CONTINUOUS
3328
# Set comparator to assert after 1 ADC conversion
3429
ads.comparator_queue_length = 1
3530
# Set comparator to use traditional threshold instead of window
36-
ads.comparator_mode = Comp_Mode.TRADITIONAL
31+
ads.comparator_mode = ads1x15.Comp_Mode.TRADITIONAL
3732
# Set comparator output to de-assert if readings no longer above threshold
38-
ads.comparator_latch = Comp_Latch.NONLATCHING
33+
ads.comparator_latch = ads1x15.Comp_Latch.NONLATCHING
3934
# Set comparator output to logic LOW when asserted
40-
ads.comparator_polarity = Comp_Polarity.ACTIVE_LOW
35+
ads.comparator_polarity = ads1x15.Comp_Polarity.ACTIVE_LOW
4136
# Gain should be explicitly set to ensure threshold values are calculated correctly
4237
ads.gain = 1
4338
# Set comparator low threshold to 2V

examples/ads1x15_fast_read.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import board
77
import busio
88

9-
import adafruit_ads1x15.ads1015 as ADS
10-
from adafruit_ads1x15.ads1x15 import Mode
11-
from adafruit_ads1x15.analog_in import AnalogIn
9+
from adafruit_ads1x15 import ADS1015, AnalogIn, ads1x15
1210

1311
# Data collection setup
1412
RATE = 3300
@@ -21,18 +19,18 @@
2119
i2c = busio.I2C(board.SCL, board.SDA, frequency=1000000)
2220

2321
# Create the ADC object using the I2C bus
24-
ads = ADS.ADS1015(i2c)
22+
ads = ADS1015(i2c)
2523

2624
# Create single-ended input on channel 0
27-
chan0 = AnalogIn(ads, ADS.P0)
25+
chan = AnalogIn(ads, ads1x15.Pin.A0)
2826

2927
# ADC Configuration
30-
ads.mode = Mode.CONTINUOUS
28+
ads.mode = ads1x15.Mode.CONTINUOUS
3129
ads.data_rate = RATE
3230

3331
# First ADC channel read in continuous mode configures device
3432
# and waits 2 conversion cycles
35-
_ = chan0.value
33+
_ = chan.value
3634

3735
sample_interval = 1.0 / ads.data_rate
3836

@@ -51,7 +49,7 @@
5149
pass
5250

5351
# Read conversion value for ADC channel
54-
data[i] = chan0.value
52+
data[i] = chan.value
5553

5654
# Loop timing
5755
time_last_sample = time.monotonic()

examples/ads1x15_gain_example.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@
44
import time
55

66
import board
7-
import busio
87

9-
# import adafruit_ads1x15.ads1015 as ADS
10-
import adafruit_ads1x15.ads1115 as ADS
11-
from adafruit_ads1x15.analog_in import AnalogIn
8+
from adafruit_ads1x15 import ADS1115, AnalogIn, ads1x15
129

1310
# Create the I2C bus
14-
i2c = busio.I2C(board.SCL, board.SDA)
11+
i2c = board.I2C()
1512

1613
# Create the ADS object
1714
# ads = ADS.ADS1015(i2c)
18-
ads = ADS.ADS1115(i2c)
15+
ads = ADS1115(i2c)
1916

20-
# Create a single-ended channel on Pin 0
17+
# Create a single-ended channel on Pin A0
2118
# Max counts for ADS1015 = 2047
2219
# ADS1115 = 32767
23-
chan = AnalogIn(ads, ADS.P0)
20+
chan = AnalogIn(ads, ads1x15.Pin.A0)
2421

2522
# The ADS1015 and ADS1115 both have the same gain options.
2623
#

examples/ads1x15_simpletest.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44
import time
55

66
import board
7-
import busio
87

9-
import adafruit_ads1x15.ads1015 as ADS
10-
from adafruit_ads1x15.analog_in import AnalogIn
8+
from adafruit_ads1x15 import ADS1015, AnalogIn, ads1x15
119

1210
# Create the I2C bus
13-
i2c = busio.I2C(board.SCL, board.SDA)
11+
i2c = board.I2C()
1412

1513
# Create the ADC object using the I2C bus
16-
ads = ADS.ADS1015(i2c)
14+
ads = ADS1015(i2c)
1715

1816
# Create single-ended input on channel 0
19-
chan = AnalogIn(ads, ADS.P0)
17+
chan = AnalogIn(ads, ads1x15.Pin.A0)
2018

2119
# Create differential input between channel 0 and 1
22-
# chan = AnalogIn(ads, ADS.P0, ADS.P1)
20+
# chan = AnalogIn(ads, ads1x15.Pin.A0, ads1x15.Pin.A1)
2321

2422
print("{:>5}\t{:>5}".format("raw", "v"))
2523

0 commit comments

Comments
 (0)