/ adafruit_ads1x15 / ads1015.py
ads1015.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 `ads1015` 24 ==================================================== 25 26 CircuitPython driver for ADS1015 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 _ADS1015_CONFIG_DR = { 37 128: 0x0000, 38 250: 0x0020, 39 490: 0x0040, 40 920: 0x0060, 41 1600: 0x0080, 42 2400: 0x00A0, 43 3300: 0x00C0, 44 } 45 46 # Pins 47 P0 = 0 48 P1 = 1 49 P2 = 2 50 P3 = 3 51 52 53 class ADS1015(ADS1x15): 54 """Class for the ADS1015 12 bit ADC.""" 55 56 @property 57 def bits(self): 58 """The ADC bit resolution.""" 59 return 12 60 61 @property 62 def rates(self): 63 """Possible data rate settings.""" 64 r = list(_ADS1015_CONFIG_DR.keys()) 65 r.sort() 66 return r 67 68 @property 69 def rate_config(self): 70 """Rate configuration masks.""" 71 return _ADS1015_CONFIG_DR 72 73 def _data_rate_default(self): 74 return 1600 75 76 def _conversion_value(self, raw_adc): 77 raw_adc = raw_adc.to_bytes(2, "big") 78 value = struct.unpack(">h", raw_adc)[0] 79 return value >> 4