From ac27d8cf6d4a5127b50d66ff5c54716619cce885 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Sun, 4 Feb 2024 22:10:42 +1100 Subject: [PATCH] Run pylint and black --- examples/hdc1080_basic.py | 15 +-- examples/package.json | 2 +- package.json | 2 +- readme.md | 4 +- src/hdc1080.py | 205 ++++++++++++++++++++------------------ 5 files changed, 119 insertions(+), 109 deletions(-) diff --git a/examples/hdc1080_basic.py b/examples/hdc1080_basic.py index 3b62131..fc640c1 100644 --- a/examples/hdc1080_basic.py +++ b/examples/hdc1080_basic.py @@ -1,13 +1,16 @@ +# SPDX-FileCopyrightText: 2024 Mike Causer +# SPDX-License-Identifier: MIT + """ MicroPython HDC1080 Basic example Prints the temperature and humidity every 500ms """ -from machine import I2C, Pin -i2c = I2C(0) - +from machine import I2C from hdc1080 import HDC1080 + +i2c = I2C(0) hdc = HDC1080(i2c) from time import sleep_ms @@ -15,8 +18,8 @@ hdc.config(humid_res=14, temp_res=14, mode=0, heater=0) if hdc.check(): - print(f'Found HDC1080 with serial number {hdc.serial_number()}') + print(f"Found HDC1080 with serial number {hdc.serial_number()}") while True: - print(f'{hdc.temperature()} C, {hdc.humidity()} RH') - sleep_ms(500) + print(f"{hdc.temperature()} C, {hdc.humidity()} RH") + sleep_ms(500) diff --git a/examples/package.json b/examples/package.json index ff16815..7b902f7 100644 --- a/examples/package.json +++ b/examples/package.json @@ -3,5 +3,5 @@ ["hdc1080/examples/basic.py", "github:mcauser/micropython-hdc1080/examples/hdc1080_basic.py"] ], "deps": [], - "version": "1.0.0" + "version": "1.0.1" } diff --git a/package.json b/package.json index 35c113e..ff65614 100644 --- a/package.json +++ b/package.json @@ -3,5 +3,5 @@ ["hdc1080/__init__.py", "github:mcauser/micropython-hdc1080/src/hdc1080.py"] ], "deps": [], - "version": "1.0.0" + "version": "1.0.1" } diff --git a/readme.md b/readme.md index e20386d..c474bab 100644 --- a/readme.md +++ b/readme.md @@ -31,12 +31,12 @@ Copy `src/hdc1080.py` to the root directory of your device. ```python from machine import I2C, Pin -i2c = I2C(0) +from hdc1080 import HDC1080 +i2c = I2C(0) i2c.scan() # [64] -from hdc1080 import HDC1080 hdc = HDC1080(i2c) # set humidity resolution (in bits) diff --git a/src/hdc1080.py b/src/hdc1080.py index d6eda14..29baa7e 100644 --- a/src/hdc1080.py +++ b/src/hdc1080.py @@ -1,12 +1,12 @@ +# SPDX-FileCopyrightText: 2024 Mike Causer +# SPDX-License-Identifier: MIT + """ MicroPython HDC1080 Temperature & Humidity Sensor https://github.com/mcauser/micropython-hdc1080 - -MIT License -Copyright (c) 2024 Mike Causer """ -__version__ = '1.0.0' +__version__ = "1.0.1" from micropython import const from time import sleep_ms @@ -21,99 +21,106 @@ _MANUFACTURER_ID = const(0xFE) _DEVICE_ID = const(0xFF) + class HDC1080: - def __init__(self, i2c): - self._i2c = i2c - self._address = 0x40 # fixed I2C address - self._buf1 = bytearray(1) - self._buf2 = bytearray(2) - self._config = 0x10 - - def _read16(self, reg): - self._buf1[0] = reg - self._i2c.writeto(self._address, self._buf1) - sleep_ms(20) - self._i2c.readfrom_into(self._address, self._buf2) - return (self._buf2[0] << 8) | self._buf2[1] - - def _write_config(self): - self._buf2[0] = _CONFIG - self._buf2[1] = self._config - self._i2c.writeto(self._address, self._buf2) - - def _read_config(self): - # shift out the first 8 reserved bits - self._config = self._read16(_CONFIG) >> 8 - - def check(self): - if self._i2c.scan().count(self._address) == 0: - raise OSError('HDC1080 not found at I2C address {:#x}'.format(self._address)) - return True - - def config(self, config=None, humid_res=None, temp_res=None, mode=None, heater=None): - if config is not None: - self._config = config - self._write_config() - else: - self._read_config() - if humid_res is not None: - # 00 = 14-bit, 01 = 11-bit, 10 = 8-bit - if humid_res == 8: - self._config |= 2 - self._config &= ~1 - elif humid_res == 11: - self._config &= ~2 - self._config |= 1 - elif humid_res == 14: - self._config &= ~3 - else: - raise ValueError('humid_res must be 8, 11 or 14') - if temp_res is not None: - # 0 = 14-bit, 1 = 11-bit - if temp_res == 11: - self._config |= 4 - elif temp_res == 14: - self._config &= ~4 - else: - raise ValueError('temp_res must be 11 or 14') - if mode is not None: - # mode 0 = temp or humid acquired - # mode 1 = temp and humid acquired in sequence, temp first - self._config &= ~16 - self._config |= (mode & 1) << 4 - if heater is not None: - self._config &= ~32 - self._config |= (heater & 1) << 5 - self._write_config() - - def reset(self): - self._config = 128 - self._write_config() - # sw reset bit self clears - self._read_config() - - def battery_status(self): - # returns 0 if Vcc > 2.8V - # returns 1 if Vcc < 2.8V - self._read_config() - return (self._config >> 3) & 1 - - def temperature(self): - # temperature in celsius - return (self._read16(_TEMPERATURE) / 65536) * 165 - 40 - - def humidity(self): - # relative humidity percentage - return (self._read16(_HUMIDITY) / 65536) * 100 - - def serial_number(self): - # unique per device - return (self._read16(_SERIAL_ID0) << 24) | (self._read16(_SERIAL_ID1) << 8) | (self._read16(_SERIAL_ID2) >> 8) - - def manufacturer_id(self): - # fixed 21577 == 0x5449 == b'\x54\x49' == b'TI' - return self._read16(_MANUFACTURER_ID) - - def device_id(self): - # fixed 4176 == 0x1050 == b'\x10\x50' == b'\x10P' - return self._read16(_DEVICE_ID) + def __init__(self, i2c): + self._i2c = i2c + self._address = 0x40 # fixed I2C address + self._buf1 = bytearray(1) + self._buf2 = bytearray(2) + self._config = 0x10 + + def _read16(self, reg): + self._buf1[0] = reg + self._i2c.writeto(self._address, self._buf1) + sleep_ms(20) + self._i2c.readfrom_into(self._address, self._buf2) + return (self._buf2[0] << 8) | self._buf2[1] + + def _write_config(self): + self._buf2[0] = _CONFIG + self._buf2[1] = self._config + self._i2c.writeto(self._address, self._buf2) + + def _read_config(self): + # shift out the first 8 reserved bits + self._config = self._read16(_CONFIG) >> 8 + + def check(self): + if self._i2c.scan().count(self._address) == 0: + raise OSError(f"HDC1080 not found at I2C address {self._address:#x}") + return True + + def config( + self, config=None, humid_res=None, temp_res=None, mode=None, heater=None + ): + if config is not None: + self._config = config + self._write_config() + else: + self._read_config() + if humid_res is not None: + # 00 = 14-bit, 01 = 11-bit, 10 = 8-bit + if humid_res == 8: + self._config |= 2 + self._config &= ~1 + elif humid_res == 11: + self._config &= ~2 + self._config |= 1 + elif humid_res == 14: + self._config &= ~3 + else: + raise ValueError("humid_res must be 8, 11 or 14") + if temp_res is not None: + # 0 = 14-bit, 1 = 11-bit + if temp_res == 11: + self._config |= 4 + elif temp_res == 14: + self._config &= ~4 + else: + raise ValueError("temp_res must be 11 or 14") + if mode is not None: + # mode 0 = temp or humid acquired + # mode 1 = temp and humid acquired in sequence, temp first + self._config &= ~16 + self._config |= (mode & 1) << 4 + if heater is not None: + self._config &= ~32 + self._config |= (heater & 1) << 5 + self._write_config() + + def reset(self): + self._config = 128 + self._write_config() + # sw reset bit self clears + self._read_config() + + def battery_status(self): + # returns 0 if Vcc > 2.8V + # returns 1 if Vcc < 2.8V + self._read_config() + return (self._config >> 3) & 1 + + def temperature(self): + # temperature in celsius + return (self._read16(_TEMPERATURE) / 65536) * 165 - 40 + + def humidity(self): + # relative humidity percentage + return (self._read16(_HUMIDITY) / 65536) * 100 + + def serial_number(self): + # unique per device + return ( + (self._read16(_SERIAL_ID0) << 24) + | (self._read16(_SERIAL_ID1) << 8) + | (self._read16(_SERIAL_ID2) >> 8) + ) + + def manufacturer_id(self): + # fixed 21577 == 0x5449 == b'\x54\x49' == b'TI' + return self._read16(_MANUFACTURER_ID) + + def device_id(self): + # fixed 4176 == 0x1050 == b'\x10\x50' == b'\x10P' + return self._read16(_DEVICE_ID)