Skip to content

Ran black, updated to pylint 2.x #13

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 17, 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
143 changes: 78 additions & 65 deletions adafruit_mpl3115a2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import time

from micropython import const

try:
import ustruct as struct
except ImportError:
Expand All @@ -43,58 +44,58 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2.git"


#pylint: disable=bad-whitespace
# pylint: disable=bad-whitespace
# Internal constants:
_MPL3115A2_ADDRESS = const(0x60)
_MPL3115A2_REGISTER_STATUS = const(0x00)
_MPL3115A2_REGISTER_PRESSURE_MSB = const(0x01)
_MPL3115A2_REGISTER_PRESSURE_CSB = const(0x02)
_MPL3115A2_REGISTER_PRESSURE_LSB = const(0x03)
_MPL3115A2_REGISTER_TEMP_MSB = const(0x04)
_MPL3115A2_REGISTER_TEMP_LSB = const(0x05)
_MPL3115A2_REGISTER_DR_STATUS = const(0x06)
_MPL3115A2_OUT_P_DELTA_MSB = const(0x07)
_MPL3115A2_OUT_P_DELTA_CSB = const(0x08)
_MPL3115A2_OUT_P_DELTA_LSB = const(0x09)
_MPL3115A2_OUT_T_DELTA_MSB = const(0x0A)
_MPL3115A2_OUT_T_DELTA_LSB = const(0x0B)
_MPL3115A2_WHOAMI = const(0x0C)
_MPL3115A2_BAR_IN_MSB = const(0x14)
_MPL3115A2_BAR_IN_LSB = const(0x15)

_MPL3115A2_REGISTER_STATUS_TDR = const(0x02)
_MPL3115A2_REGISTER_STATUS_PDR = const(0x04)
_MPL3115A2_REGISTER_STATUS_PTDR = const(0x08)

_MPL3115A2_PT_DATA_CFG = const(0x13)
_MPL3115A2_PT_DATA_CFG_TDEFE = const(0x01)
_MPL3115A2_PT_DATA_CFG_PDEFE = const(0x02)
_MPL3115A2_PT_DATA_CFG_DREM = const(0x04)

_MPL3115A2_CTRL_REG1 = const(0x26)
_MPL3115A2_CTRL_REG2 = const(0x27)
_MPL3115A2_CTRL_REG3 = const(0x28)
_MPL3115A2_CTRL_REG4 = const(0x29)
_MPL3115A2_CTRL_REG5 = const(0x2A)

_MPL3115A2_CTRL_REG1_SBYB = const(0x01)
_MPL3115A2_CTRL_REG1_OST = const(0x02)
_MPL3115A2_CTRL_REG1_RST = const(0x04)
_MPL3115A2_CTRL_REG1_RAW = const(0x40)
_MPL3115A2_CTRL_REG1_ALT = const(0x80)
_MPL3115A2_CTRL_REG1_BAR = const(0x00)

_MPL3115A2_CTRL_REG1_OS1 = const(0x00)
_MPL3115A2_CTRL_REG1_OS2 = const(0x08)
_MPL3115A2_CTRL_REG1_OS4 = const(0x10)
_MPL3115A2_CTRL_REG1_OS8 = const(0x18)
_MPL3115A2_CTRL_REG1_OS16 = const(0x20)
_MPL3115A2_CTRL_REG1_OS32 = const(0x28)
_MPL3115A2_CTRL_REG1_OS64 = const(0x30)
_MPL3115A2_CTRL_REG1_OS128 = const(0x38)

_MPL3115A2_REGISTER_STARTCONVERSION = const(0x12)
#pylint: enable=bad-whitespace
_MPL3115A2_ADDRESS = const(0x60)
_MPL3115A2_REGISTER_STATUS = const(0x00)
_MPL3115A2_REGISTER_PRESSURE_MSB = const(0x01)
_MPL3115A2_REGISTER_PRESSURE_CSB = const(0x02)
_MPL3115A2_REGISTER_PRESSURE_LSB = const(0x03)
_MPL3115A2_REGISTER_TEMP_MSB = const(0x04)
_MPL3115A2_REGISTER_TEMP_LSB = const(0x05)
_MPL3115A2_REGISTER_DR_STATUS = const(0x06)
_MPL3115A2_OUT_P_DELTA_MSB = const(0x07)
_MPL3115A2_OUT_P_DELTA_CSB = const(0x08)
_MPL3115A2_OUT_P_DELTA_LSB = const(0x09)
_MPL3115A2_OUT_T_DELTA_MSB = const(0x0A)
_MPL3115A2_OUT_T_DELTA_LSB = const(0x0B)
_MPL3115A2_WHOAMI = const(0x0C)
_MPL3115A2_BAR_IN_MSB = const(0x14)
_MPL3115A2_BAR_IN_LSB = const(0x15)

_MPL3115A2_REGISTER_STATUS_TDR = const(0x02)
_MPL3115A2_REGISTER_STATUS_PDR = const(0x04)
_MPL3115A2_REGISTER_STATUS_PTDR = const(0x08)

_MPL3115A2_PT_DATA_CFG = const(0x13)
_MPL3115A2_PT_DATA_CFG_TDEFE = const(0x01)
_MPL3115A2_PT_DATA_CFG_PDEFE = const(0x02)
_MPL3115A2_PT_DATA_CFG_DREM = const(0x04)

_MPL3115A2_CTRL_REG1 = const(0x26)
_MPL3115A2_CTRL_REG2 = const(0x27)
_MPL3115A2_CTRL_REG3 = const(0x28)
_MPL3115A2_CTRL_REG4 = const(0x29)
_MPL3115A2_CTRL_REG5 = const(0x2A)

_MPL3115A2_CTRL_REG1_SBYB = const(0x01)
_MPL3115A2_CTRL_REG1_OST = const(0x02)
_MPL3115A2_CTRL_REG1_RST = const(0x04)
_MPL3115A2_CTRL_REG1_RAW = const(0x40)
_MPL3115A2_CTRL_REG1_ALT = const(0x80)
_MPL3115A2_CTRL_REG1_BAR = const(0x00)

_MPL3115A2_CTRL_REG1_OS1 = const(0x00)
_MPL3115A2_CTRL_REG1_OS2 = const(0x08)
_MPL3115A2_CTRL_REG1_OS4 = const(0x10)
_MPL3115A2_CTRL_REG1_OS8 = const(0x18)
_MPL3115A2_CTRL_REG1_OS16 = const(0x20)
_MPL3115A2_CTRL_REG1_OS32 = const(0x28)
_MPL3115A2_CTRL_REG1_OS64 = const(0x30)
_MPL3115A2_CTRL_REG1_OS128 = const(0x38)

_MPL3115A2_REGISTER_STARTCONVERSION = const(0x12)
# pylint: enable=bad-whitespace


class MPL3115A2:
Expand All @@ -114,7 +115,7 @@ def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
self._device = i2c_device.I2CDevice(i2c, address)
# Validate the chip ID.
if self._read_u8(_MPL3115A2_WHOAMI) != 0xC4:
raise RuntimeError('Failed to find MPL3115A2, check your wiring!')
raise RuntimeError("Failed to find MPL3115A2, check your wiring!")
# Reset. Note the chip immediately resets and won't send an I2C back
# so we need to catch the OSError and swallow it (otherwise this fails
# expecting an ACK that never comes).
Expand All @@ -128,9 +129,12 @@ def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
# Configure the chip registers with default values.
self._ctrl_reg1 = _MPL3115A2_CTRL_REG1_OS128 | _MPL3115A2_CTRL_REG1_ALT
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
self._write_u8(_MPL3115A2_PT_DATA_CFG, _MPL3115A2_PT_DATA_CFG_TDEFE | \
_MPL3115A2_PT_DATA_CFG_PDEFE | \
_MPL3115A2_PT_DATA_CFG_DREM)
self._write_u8(
_MPL3115A2_PT_DATA_CFG,
_MPL3115A2_PT_DATA_CFG_TDEFE
| _MPL3115A2_PT_DATA_CFG_PDEFE
| _MPL3115A2_PT_DATA_CFG_DREM,
)

def _read_into(self, address, buf, count=None):
# Read bytes from the specified 8-bit address into the provided buffer.
Expand Down Expand Up @@ -176,17 +180,20 @@ def pressure(self):
# Set control bits for pressure reading.
self._ctrl_reg1 &= ~0b10000000 # Turn off bit 7, ALT.
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
self._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
self._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
# Poll status for PDR to be set.
while self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_PDR == 0:
while (
self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_PDR
== 0
):
time.sleep(0.01)
# Read 3 bytes of pressure data into buffer.
self._read_into(_MPL3115A2_REGISTER_PRESSURE_MSB, self._BUFFER, count=3)
# Reconstruct 20-bit pressure value.
pressure = ((self._BUFFER[0] << 16) | \
(self._BUFFER[1] << 8) | \
self._BUFFER[2]) & 0xFFFFFF
pressure = (
(self._BUFFER[0] << 16) | (self._BUFFER[1] << 8) | self._BUFFER[2]
) & 0xFFFFFF
pressure >>= 4
# Scale down to pascals.
return pressure / 4.0
Expand All @@ -203,10 +210,13 @@ def altitude(self):
# Set control bits for pressure reading.
self._ctrl_reg1 |= 0b10000000 # Turn on bit 0, ALT.
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
self._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
self._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
# Poll status for PDR to be set.
while self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_PDR == 0:
while (
self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_PDR
== 0
):
time.sleep(0.01)
# Read 3 bytes of altitude data into buffer.
# Yes even though this is the address of the pressure register it
Expand All @@ -215,7 +225,7 @@ def altitude(self):
# Reconstruct signed 32-bit altitude value (actually 24 bits shifted up
# and then scaled down).
self._BUFFER[3] = 0 # Top 3 bytes of buffer were read from the chip.
altitude = struct.unpack('>i', self._BUFFER[0:4])[0]
altitude = struct.unpack(">i", self._BUFFER[0:4])[0]
# Scale down to meters.
return altitude / 65535.0

Expand All @@ -224,12 +234,15 @@ def temperature(self):
"""Read the temperature as measured by the sensor in degrees Celsius.
"""
# Poll status for TDR to be set.
while self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_TDR == 0:
while (
self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_TDR
== 0
):
time.sleep(0.01)
# Read 2 bytes of data from temp register.
self._read_into(_MPL3115A2_REGISTER_TEMP_MSB, self._BUFFER, count=2)
# Reconstruct signed 12-bit value.
temperature = struct.unpack('>h', self._BUFFER[0:2])[0]
temperature = struct.unpack(">h", self._BUFFER[0:2])[0]
temperature >>= 4
# Scale down to degrees Celsius.
return temperature / 16.0
Expand Down
Loading