-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNT3H2.py
More file actions
47 lines (40 loc) · 1.25 KB
/
NT3H2.py
File metadata and controls
47 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# SPDX-FileCopyrightText: 2023 Emil Renner Berthing
# SPDX-FileCopyrightText: 2023 Mikkel Kirkgaard Nielsen
# SPDX-FileCopyrightText: Copyright (c) 2021 Electronic Cats for Electronic Cats
#
# SPDX-License-Identifier: MIT
"""
Util for NT3H2
"""
from busio import I2C
class NT3H2:
"""Class NT3H2."""
def __init__(self, i2c: I2C, addr: int = 0x55):
"""Function __init__."""
self._i2c = i2c
self._addr = addr
self._buf = bytearray(17)
def _readpage(self, page: int):
"""Function _readpage."""
self._buf[0] = page
self._i2c.writeto_then_readfrom(
self._addr, self._buf, self._buf, out_end=1, in_start=1
)
def readpage(self, page: int):
"""Function read page."""
assert self._i2c.try_lock()
self._readpage(page)
self._i2c.unlock()
print(
"Page {:02}:{}".format(
page, "".join(" {:02x}".format(x) for x in self._buf[1:])
)
)
def set_addr(self, addr: int):
"""Function set address."""
assert self._i2c.try_lock()
self._readpage(0x00)
self._buf[1] = 2 * addr
self._i2c.writeto(self._addr, self._buf)
self._addr = addr
self._i2c.unlock()