|
| 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