/ adafruit_ads1x15 / ads1115.py
ads1115.py
 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  `ads1115`
24  ====================================================
25  
26  CircuitPython driver for 1115 ADCs.
27  
28  * Author(s): Carter Nelson
29  """
30  import struct
31  
32  # pylint: disable=unused-import
33  from .ads1x15 import ADS1x15, Mode
34  
35  # Data sample rates
36  _ADS1115_CONFIG_DR = {
37      8: 0x0000,
38      16: 0x0020,
39      32: 0x0040,
40      64: 0x0060,
41      128: 0x0080,
42      250: 0x00A0,
43      475: 0x00C0,
44      860: 0x00E0,
45  }
46  
47  # Pins
48  P0 = 0
49  P1 = 1
50  P2 = 2
51  P3 = 3
52  
53  
54  class ADS1115(ADS1x15):
55      """Class for the ADS1115 16 bit ADC."""
56  
57      @property
58      def bits(self):
59          """The ADC bit resolution."""
60          return 16
61  
62      @property
63      def rates(self):
64          """Possible data rate settings."""
65          r = list(_ADS1115_CONFIG_DR.keys())
66          r.sort()
67          return r
68  
69      @property
70      def rate_config(self):
71          """Rate configuration masks."""
72          return _ADS1115_CONFIG_DR
73  
74      def _data_rate_default(self):
75          return 128
76  
77      def _conversion_value(self, raw_adc):
78          raw_adc = raw_adc.to_bytes(2, "big")
79          value = struct.unpack(">h", raw_adc)[0]
80          return value