Skip to content

Commit

Permalink
Ran black, updated to pylint 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
evaherrada committed Mar 16, 2020
1 parent 566dbbd commit f334eda
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ spelling-store-unknown-words=no
[MISCELLANEOUS]

# List of note tags to take in consideration, separated by a comma.
notes=FIXME,XXX,TODO
# notes=FIXME,XXX,TODO
notes=FIXME,XXX


[TYPECHECK]
Expand Down
18 changes: 10 additions & 8 deletions adafruit_ads1x15/ads1015.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@
* Author(s): Carter Nelson
"""
import struct
#pylint: disable=unused-import

# pylint: disable=unused-import
from .ads1x15 import ADS1x15, Mode

# Data sample rates
_ADS1015_CONFIG_DR = {
128: 0x0000,
250: 0x0020,
490: 0x0040,
920: 0x0060,
1600: 0x0080,
2400: 0x00A0,
3300: 0x00C0
128: 0x0000,
250: 0x0020,
490: 0x0040,
920: 0x0060,
1600: 0x0080,
2400: 0x00A0,
3300: 0x00C0,
}

# Pins
Expand All @@ -48,6 +49,7 @@
P2 = 2
P3 = 3


class ADS1015(ADS1x15):
"""Class for the ADS1015 12 bit ADC."""

Expand Down
20 changes: 11 additions & 9 deletions adafruit_ads1x15/ads1115.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@
* Author(s): Carter Nelson
"""
import struct
#pylint: disable=unused-import

# 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
8: 0x0000,
16: 0x0020,
32: 0x0040,
64: 0x0060,
128: 0x0080,
250: 0x00A0,
475: 0x00C0,
860: 0x00E0,
}

# Pins
Expand All @@ -49,6 +50,7 @@
P2 = 2
P3 = 3


class ADS1115(ADS1x15):
"""Class for the ADS1115 16 bit ADC."""

Expand Down
81 changes: 44 additions & 37 deletions adafruit_ads1x15/ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,45 @@
from adafruit_bus_device.i2c_device import I2CDevice

# pylint: disable=bad-whitespace
_ADS1X15_DEFAULT_ADDRESS = const(0x48)
_ADS1X15_POINTER_CONVERSION = const(0x00)
_ADS1X15_POINTER_CONFIG = const(0x01)
_ADS1X15_CONFIG_OS_SINGLE = const(0x8000)
_ADS1X15_CONFIG_MUX_OFFSET = const(12)
_ADS1X15_CONFIG_COMP_QUE_DISABLE = const(0x0003)
_ADS1X15_DEFAULT_ADDRESS = const(0x48)
_ADS1X15_POINTER_CONVERSION = const(0x00)
_ADS1X15_POINTER_CONFIG = const(0x01)
_ADS1X15_CONFIG_OS_SINGLE = const(0x8000)
_ADS1X15_CONFIG_MUX_OFFSET = const(12)
_ADS1X15_CONFIG_COMP_QUE_DISABLE = const(0x0003)
_ADS1X15_CONFIG_GAIN = {
2/3: 0x0000,
1: 0x0200,
2: 0x0400,
4: 0x0600,
8: 0x0800,
16: 0x0A00
2 / 3: 0x0000,
1: 0x0200,
2: 0x0400,
4: 0x0600,
8: 0x0800,
16: 0x0A00,
}
# pylint: enable=bad-whitespace


class Mode:
"""An enum-like class representing possible ADC operating modes."""

# See datasheet "Operating Modes" section
# values here are masks for setting MODE bit in Config Register
#pylint: disable=too-few-public-methods
# pylint: disable=too-few-public-methods
CONTINUOUS = 0x0000
SINGLE = 0x0100


class ADS1x15(object):
class ADS1x15:
"""Base functionality for ADS1x15 analog to digital converters."""

def __init__(self, i2c, gain=1, data_rate=None, mode=Mode.SINGLE,
address=_ADS1X15_DEFAULT_ADDRESS):
#pylint: disable=too-many-arguments
def __init__(
self,
i2c,
gain=1,
data_rate=None,
mode=Mode.SINGLE,
address=_ADS1X15_DEFAULT_ADDRESS,
):
# pylint: disable=too-many-arguments
self._last_pin_read = None
self.buf = bytearray(3)
self._data_rate = self._gain = self._mode = None
Expand All @@ -89,12 +97,12 @@ def data_rate(self, rate):
@property
def rates(self):
"""Possible data rate settings."""
raise NotImplementedError('Subclass must implement rates property.')
raise NotImplementedError("Subclass must implement rates property.")

@property
def rate_config(self):
"""Rate configuration masks."""
raise NotImplementedError('Subclass must implement rate_config property.')
raise NotImplementedError("Subclass must implement rate_config property.")

@property
def gain(self):
Expand Down Expand Up @@ -122,7 +130,7 @@ def mode(self):

@mode.setter
def mode(self, mode):
if mode != Mode.CONTINUOUS and mode != Mode.SINGLE:
if mode not in (Mode.CONTINUOUS, Mode.SINGLE):
raise ValueError("Unsupported mode.")
self._mode = mode

Expand All @@ -140,33 +148,32 @@ def _data_rate_default(self):
"""Retrieve the default data rate for this ADC (in samples per second).
Should be implemented by subclasses.
"""
raise NotImplementedError('Subclasses must implement _data_rate_default!')
raise NotImplementedError("Subclasses must implement _data_rate_default!")

def _conversion_value(self, raw_adc):
"""Subclasses should override this function that takes the 16 raw ADC
values of a conversion result and returns a signed integer value.
"""
raise NotImplementedError('Subclass must implement _conversion_value function!')
raise NotImplementedError("Subclass must implement _conversion_value function!")

def _read(self, pin):
"""Perform an ADC read. Returns the signed integer result of the read."""
if self.mode == Mode.CONTINUOUS and self._last_pin_read == pin:
return self._conversion_value(self.get_last_result(True))
else:
self._last_pin_read = pin
config = _ADS1X15_CONFIG_OS_SINGLE
config |= (pin & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
config |= _ADS1X15_CONFIG_GAIN[self.gain]
config |= self.mode
config |= self.rate_config[self.data_rate]
config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
self._write_register(_ADS1X15_POINTER_CONFIG, config)

if self.mode == Mode.SINGLE:
while not self._conversion_complete():
pass

return self._conversion_value(self.get_last_result(False))
self._last_pin_read = pin
config = _ADS1X15_CONFIG_OS_SINGLE
config |= (pin & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
config |= _ADS1X15_CONFIG_GAIN[self.gain]
config |= self.mode
config |= self.rate_config[self.data_rate]
config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
self._write_register(_ADS1X15_POINTER_CONFIG, config)

if self.mode == Mode.SINGLE:
while not self._conversion_complete():
pass

return self._conversion_value(self.get_last_result(False))

def _conversion_complete(self):
"""Return status of ADC conversion."""
Expand Down
31 changes: 12 additions & 19 deletions adafruit_ads1x15/analog_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,12 @@
"""

# pylint: disable=bad-whitespace
_ADS1X15_DIFF_CHANNELS = {
(0, 1): 0,
(0, 3): 1,
(1, 3): 2,
(2, 3): 3
}
_ADS1X15_PGA_RANGE = {
2/3: 6.144,
1: 4.096,
2: 2.048,
4: 1.024,
8: 0.512,
16: 0.256
}
_ADS1X15_DIFF_CHANNELS = {(0, 1): 0, (0, 3): 1, (1, 3): 2, (2, 3): 3}
_ADS1X15_PGA_RANGE = {2 / 3: 6.144, 1: 4.096, 2: 2.048, 4: 1.024, 8: 0.512, 16: 0.256}
# pylint: enable=bad-whitespace

class AnalogIn():

class AnalogIn:
"""AnalogIn Mock Implementation for ADC Reads."""

def __init__(self, ads, positive_pin, negative_pin=None):
Expand All @@ -62,16 +51,20 @@ def __init__(self, ads, positive_pin, negative_pin=None):
if negative_pin is not None:
pins = (self._pin_setting, self._negative_pin)
if pins not in _ADS1X15_DIFF_CHANNELS:
raise ValueError("Differential channels must be one of: {}"
.format(list(_ADS1X15_DIFF_CHANNELS.keys())))
raise ValueError(
"Differential channels must be one of: {}".format(
list(_ADS1X15_DIFF_CHANNELS.keys())
)
)
self._pin_setting = _ADS1X15_DIFF_CHANNELS[pins]
self.is_differential = True

@property
def value(self):
"""Returns the value of an ADC pin as an integer."""
return self._ads.read(self._pin_setting,
is_differential=self.is_differential) << (16 - self._ads.bits)
return self._ads.read(
self._pin_setting, is_differential=self.is_differential
) << (16 - self._ads.bits)

@property
def voltage(self):
Expand Down
Loading

0 comments on commit f334eda

Please sign in to comment.