80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
# The MIT License (MIT)
|
|
#
|
|
# Copyright (c) 2018 Carter Nelson for Adafruit Industries
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in
|
|
# all copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
# THE SOFTWARE.
|
|
"""
|
|
`ads1115`
|
|
====================================================
|
|
|
|
CircuitPython driver for 1115 ADCs.
|
|
|
|
* Author(s): Carter Nelson
|
|
"""
|
|
import struct
|
|
|
|
# pylint: disable=unused-import
|
|
from .ads1x15 import ADS1x15, Mode
|
|
|
|
# Data sample rates
|
|
_ADS1115_CONFIG_DR = {
|
|
8: 0x0000,
|
|
16: 0x0020,
|
|
32: 0x0040,
|
|
64: 0x0060,
|
|
128: 0x0080,
|
|
250: 0x00A0,
|
|
475: 0x00C0,
|
|
860: 0x00E0,
|
|
}
|
|
|
|
# Pins
|
|
P0 = 0
|
|
P1 = 1
|
|
P2 = 2
|
|
P3 = 3
|
|
|
|
|
|
class ADS1115(ADS1x15):
|
|
"""Class for the ADS1115 16 bit ADC."""
|
|
|
|
@property
|
|
def bits(self):
|
|
"""The ADC bit resolution."""
|
|
return 16
|
|
|
|
@property
|
|
def rates(self):
|
|
"""Possible data rate settings."""
|
|
r = list(_ADS1115_CONFIG_DR.keys())
|
|
r.sort()
|
|
return r
|
|
|
|
@property
|
|
def rate_config(self):
|
|
"""Rate configuration masks."""
|
|
return _ADS1115_CONFIG_DR
|
|
|
|
def _data_rate_default(self):
|
|
return 128
|
|
|
|
def _conversion_value(self, raw_adc):
|
|
raw_adc = raw_adc.to_bytes(2, "big")
|
|
value = struct.unpack(">h", raw_adc)[0]
|
|
return value
|