Skip to content

Commit

Permalink
Run pylint and black
Browse files Browse the repository at this point in the history
  • Loading branch information
mcauser committed Feb 4, 2024
1 parent eb11153 commit ac27d8c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 109 deletions.
15 changes: 9 additions & 6 deletions examples/hdc1080_basic.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# SPDX-FileCopyrightText: 2024 Mike Causer <https://github.com/mcauser>
# 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

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)
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
["hdc1080/__init__.py", "github:mcauser/micropython-hdc1080/src/hdc1080.py"]
],
"deps": [],
"version": "1.0.0"
"version": "1.0.1"
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
205 changes: 106 additions & 99 deletions src/hdc1080.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# SPDX-FileCopyrightText: 2024 Mike Causer <https://github.com/mcauser>
# 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
Expand All @@ -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)

0 comments on commit ac27d8c

Please sign in to comment.