Skip to content

Commit 2e61b96

Browse files
authored
Merge pull request #19 from caternuson/iss17_update
Interface change for #17
2 parents 1c1f3da + b7509ea commit 2e61b96

14 files changed

+492
-610
lines changed

README.rst

+17-19
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,29 @@ Single Ended
6262

6363
.. code-block:: python
6464
65-
import board
66-
import busio
67-
from adafruit_ads1x15.single_ended import ADS1015
65+
import time
66+
import board
67+
import busio
68+
import adafruit_ads1x15.ads1015 as ADS
69+
from adafruit_ads1x15.analog_in import AnalogIn
6870
69-
i2c = busio.I2C(board.SCL, board.SDA)
70-
adc = ADS1015(i2c)
71-
while True:
72-
# channel 0
73-
print(adc[0].value, adc[0].volts)
71+
# Create the I2C bus
72+
i2c = busio.I2C(board.SCL, board.SDA)
7473
75-
Differential
76-
------------
74+
# Create the ADC object using the I2C bus
75+
ads = ADS.ADS1015(i2c)
7776
78-
.. code-block:: python
77+
# Create single-ended input on channel 0
78+
chan = AnalogIn(ads, ADS.P0)
7979
80-
import board
81-
import busio
82-
from adafruit_ads1x15.differential import ADS1015
80+
# Create differential input between channel 0 and 1
81+
#chan = AnalogIn(ads, ADS.P0, ADS.P1)
8382
84-
i2c = busio.I2C(board.SCL, board.SDA)
85-
adc = ADS1015(i2c)
86-
while True:
87-
# channel 0 - channel 1
88-
print(adc[(0,1)].value, adc[(0,1)].volts)
83+
print("{:>5}\t{:>5}".format('raw', 'v'))
8984
85+
while True:
86+
print("{:>5}\t{:>5.3f}".format(chan.value, chan.voltage))
87+
time.sleep(0.5)
9088
9189
Contributing
9290
============

adafruit_ads1x15/adafruit_ads1x15.py

-233
This file was deleted.

adafruit_ads1x15/ads1015.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2018 Carter Nelson for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`ads1015`
24+
====================================================
25+
26+
CircuitPython driver for ADS1015 ADCs.
27+
28+
* Author(s): Carter Nelson
29+
"""
30+
import struct
31+
from .ads1x15 import ADS1x15
32+
33+
# Data sample rates
34+
_ADS1015_CONFIG_DR = {
35+
128: 0x0000,
36+
250: 0x0020,
37+
490: 0x0040,
38+
920: 0x0060,
39+
1600: 0x0080,
40+
2400: 0x00A0,
41+
3300: 0x00C0
42+
}
43+
44+
# Pins
45+
P0 = 0
46+
P1 = 1
47+
P2 = 2
48+
P3 = 3
49+
50+
class ADS1015(ADS1x15):
51+
"""Class for the ADS1015 12 bit ADC."""
52+
53+
@property
54+
def bits(self):
55+
"""The ADC bit resolution."""
56+
return 12
57+
58+
@property
59+
def rates(self):
60+
"""Possible data rate settings."""
61+
r = list(_ADS1015_CONFIG_DR.keys())
62+
r.sort()
63+
return r
64+
65+
@property
66+
def rate_config(self):
67+
"""Rate configuration masks."""
68+
return _ADS1015_CONFIG_DR
69+
70+
def _data_rate_default(self):
71+
return 1600
72+
73+
def _conversion_value(self, raw_adc):
74+
raw_adc = raw_adc.to_bytes(2, "big")
75+
value = struct.unpack(">h", raw_adc)[0]
76+
return value >> 4

0 commit comments

Comments
 (0)