11
11
import asyncio
12
12
from micropython import const
13
13
14
- _ADDRESS = const (0x40 ) # HTU21D Address
15
14
_PAUSE_MS = const (60 ) # HTU21D acquisition delay
16
15
_READ_USER_REG = const (0xE7 )
17
16
@@ -25,10 +24,11 @@ class HTU21D:
25
24
START_TEMP_MEASURE = b"\xF3 " # Commands
26
25
START_HUMD_MEASURE = b"\xF5 "
27
26
28
- def __init__ (self , i2c , read_delay = 10 ):
27
+ def __init__ (self , i2c , read_delay = 10 , address = 0x40 ):
29
28
self .i2c = i2c
30
- if _ADDRESS not in self .i2c .scan ():
29
+ if address not in self .i2c .scan ():
31
30
raise OSError ("No HTU21D device found." )
31
+ self .address = address
32
32
self .temperature = None
33
33
self .humidity = None
34
34
asyncio .create_task (self ._run (read_delay ))
@@ -46,9 +46,9 @@ def __iter__(self): # Await 1st reading
46
46
yield from asyncio .sleep (0 )
47
47
48
48
async def _get_data (self , cmd , divisor = 0x131 << 15 , bit = 1 << 23 ):
49
- self .i2c .writeto (_ADDRESS , cmd ) # Start reading
49
+ self .i2c .writeto (self . address , cmd ) # Start reading
50
50
await asyncio .sleep_ms (_PAUSE_MS ) # Wait for device
51
- value = self .i2c .readfrom (_ADDRESS , 3 ) # Read result, check CRC8
51
+ value = self .i2c .readfrom (self . address , 3 ) # Read result, check CRC8
52
52
data , crc = ustruct .unpack (">HB" , value )
53
53
remainder = (data << 8 ) | crc
54
54
while bit > 128 :
@@ -61,4 +61,4 @@ async def _get_data(self, cmd, divisor=0x131 << 15, bit=1 << 23):
61
61
return data & 0xFFFC # Clear the status bits
62
62
63
63
def user_register (self ): # Read the user register byte (should be 2)
64
- return self .i2c .readfrom_mem (_ADDRESS , _READ_USER_REG , 1 )[0 ]
64
+ return self .i2c .readfrom_mem (self . address , _READ_USER_REG , 1 )[0 ]
0 commit comments