Skip to content

Ran black, updated to pylint 2.x #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
94 changes: 56 additions & 38 deletions adafruit_bmp3xx.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"""

import time

try:
import struct
except ImportError:
Expand All @@ -55,29 +56,30 @@

_CHIP_ID = const(0x50)

# pylint: disable=bad-whitespace
_REGISTER_CHIPID = const(0x00)
_REGISTER_STATUS = const(0x03)
_REGISTER_PRESSUREDATA = const(0x04)
_REGISTER_TEMPDATA = const(0x07)
_REGISTER_CONTROL = const(0x1B)
_REGISTER_OSR = const(0x1C)
_REGISTER_ODR = const(0x1D)
_REGISTER_CONFIG = const(0x1F)
_REGISTER_CAL_DATA = const(0x31)
_REGISTER_CMD = const(0x7E)
# pylint: disable=import-outside-toplevel
_REGISTER_CHIPID = const(0x00)
_REGISTER_STATUS = const(0x03)
_REGISTER_PRESSUREDATA = const(0x04)
_REGISTER_TEMPDATA = const(0x07)
_REGISTER_CONTROL = const(0x1B)
_REGISTER_OSR = const(0x1C)
_REGISTER_ODR = const(0x1D)
_REGISTER_CONFIG = const(0x1F)
_REGISTER_CAL_DATA = const(0x31)
_REGISTER_CMD = const(0x7E)
# pylint: enable=bad-whitespace

_OSR_SETTINGS = (1, 2, 4, 8, 16, 32) # pressure and temperature oversampling settings
_IIR_SETTINGS = (0, 2, 4, 8, 16, 32, 64, 128) # IIR filter coefficients
_OSR_SETTINGS = (1, 2, 4, 8, 16, 32) # pressure and temperature oversampling settings
_IIR_SETTINGS = (0, 2, 4, 8, 16, 32, 64, 128) # IIR filter coefficients


class BMP3XX:
"""Base class for BMP3XX sensor."""

def __init__(self):
chip_id = self._read_byte(_REGISTER_CHIPID)
if _CHIP_ID != chip_id:
raise RuntimeError('Failed to find BMP3XX! Chip ID 0x%x' % chip_id)
raise RuntimeError("Failed to find BMP3XX! Chip ID 0x%x" % chip_id)
self._read_coefficients()
self.reset()
self.sea_level_pressure = 1013.25
Expand All @@ -97,7 +99,7 @@ def temperature(self):
def altitude(self):
"""The altitude in meters based on the currently set sea level pressure."""
# see https://www.weather.gov/media/epz/wxcalc/pressureAltitude.pdf
return 44307.7 * (1 - (self.pressure / self.sea_level_pressure)**0.190284)
return 44307.7 * (1 - (self.pressure / self.sea_level_pressure) ** 0.190284)

@property
def pressure_oversampling(self):
Expand All @@ -108,7 +110,9 @@ def pressure_oversampling(self):
def pressure_oversampling(self, oversample):
if oversample not in _OSR_SETTINGS:
raise ValueError("Oversampling must be one of: {}".format(_OSR_SETTINGS))
new_setting = self._read_byte(_REGISTER_OSR) & 0xF8 | _OSR_SETTINGS.index(oversample)
new_setting = self._read_byte(_REGISTER_OSR) & 0xF8 | _OSR_SETTINGS.index(
oversample
)
self._write_register_byte(_REGISTER_OSR, new_setting)

@property
Expand All @@ -120,7 +124,9 @@ def temperature_oversampling(self):
def temperature_oversampling(self, oversample):
if oversample not in _OSR_SETTINGS:
raise ValueError("Oversampling must be one of: {}".format(_OSR_SETTINGS))
new_setting = self._read_byte(_REGISTER_OSR) & 0xC7 | _OSR_SETTINGS.index(oversample) << 3
new_setting = (
self._read_byte(_REGISTER_OSR) & 0xC7 | _OSR_SETTINGS.index(oversample) << 3
)
self._write_register_byte(_REGISTER_OSR, new_setting)

@property
Expand All @@ -131,7 +137,9 @@ def filter_coefficient(self):
@filter_coefficient.setter
def filter_coefficient(self, coef):
if coef not in _IIR_SETTINGS:
raise ValueError("Filter coefficient must be one of: {}".format(_IIR_SETTINGS))
raise ValueError(
"Filter coefficient must be one of: {}".format(_IIR_SETTINGS)
)
self._write_register_byte(_REGISTER_CONFIG, _IIR_SETTINGS.index(coef) << 1)

def reset(self):
Expand Down Expand Up @@ -169,19 +177,19 @@ def _read(self):
P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11 = self._pressure_calib

pd1 = P6 * temperature
pd2 = P7 * temperature**2.
pd3 = P8 * temperature**3.
pd2 = P7 * temperature ** 2.0
pd3 = P8 * temperature ** 3.0
po1 = P5 + pd1 + pd2 + pd3

pd1 = P2 * temperature
pd2 = P3 * temperature**2.
pd3 = P4 * temperature**3.
pd2 = P3 * temperature ** 2.0
pd3 = P4 * temperature ** 3.0
po2 = adc_p * (P1 + pd1 + pd2 + pd3)

pd1 = adc_p**2.
pd1 = adc_p ** 2.0
pd2 = P9 + P10 * temperature
pd3 = pd1 * pd2
pd4 = pd3 + P11 * adc_p**3.
pd4 = pd3 + P11 * adc_p ** 3.0

pressure = po1 + po2 + pd4

Expand All @@ -197,20 +205,24 @@ def _read_coefficients(self):
# See datasheet, sec 9.1
# Note: forcing float math to prevent issues with boards that
# do not support long ints for 2**<large int>
self._temp_calib = ( coeff[0] / 2**-8. , # T1
coeff[1] / 2**30. , # T2
coeff[2] / 2**48. ) # T3
self._pressure_calib = ( (coeff[3]-2**14.) / 2**20. , # P1
(coeff[4]-2**14.) / 2**29. , # P2
coeff[5] / 2**32. , # P3
coeff[6] / 2**37. , # P4
coeff[7] / 2**-3. , # P5
coeff[8] / 2**6. , # P6
coeff[9] / 2**8. , # P7
coeff[10] / 2**15. , # P8
coeff[11] / 2**48. , # P9
coeff[12] / 2**48. , # P10
coeff[13] / 2**65. ) # P11
self._temp_calib = (
coeff[0] / 2 ** -8.0, # T1
coeff[1] / 2 ** 30.0, # T2
coeff[2] / 2 ** 48.0,
) # T3
self._pressure_calib = (
(coeff[3] - 2 ** 14.0) / 2 ** 20.0, # P1
(coeff[4] - 2 ** 14.0) / 2 ** 29.0, # P2
coeff[5] / 2 ** 32.0, # P3
coeff[6] / 2 ** 37.0, # P4
coeff[7] / 2 ** -3.0, # P5
coeff[8] / 2 ** 6.0, # P6
coeff[9] / 2 ** 8.0, # P7
coeff[10] / 2 ** 15.0, # P8
coeff[11] / 2 ** 48.0, # P9
coeff[12] / 2 ** 48.0, # P10
coeff[13] / 2 ** 65.0,
) # P11

def _read_byte(self, register):
"""Read a byte register value and return it"""
Expand All @@ -224,11 +236,14 @@ def _write_register_byte(self, register, value):
"""Low level register writing, not implemented in base class"""
raise NotImplementedError()


class BMP3XX_I2C(BMP3XX):
"""Driver for I2C connected BMP3XX. Default address is 0x77 but another address can be passed
in as an argument"""

def __init__(self, i2c, address=0x77):
import adafruit_bus_device.i2c_device as i2c_device

self._i2c = i2c_device.I2CDevice(i2c, address)
super().__init__()

Expand All @@ -245,10 +260,13 @@ def _write_register_byte(self, register, value):
with self._i2c as i2c:
i2c.write(bytes((register & 0xFF, value & 0xFF)))


class BMP3XX_SPI(BMP3XX):
"""Driver for SPI connected BMP3XX."""

def __init__(self, spi, cs):
import adafruit_bus_device.spi_device as spi_device

self._spi = spi_device.SPIDevice(spi, cs)
# toggle CS low/high to put BMP3XX in SPI mode
with self._spi:
Expand Down
Loading