1111import asyncio
1212from micropython import const
1313
14- _ADDRESS = const (0x40 ) # HTU21D Address
1514_PAUSE_MS = const (60 ) # HTU21D acquisition delay
1615_READ_USER_REG = const (0xE7 )
1716
@@ -25,10 +24,11 @@ class HTU21D:
2524 START_TEMP_MEASURE = b"\xF3 " # Commands
2625 START_HUMD_MEASURE = b"\xF5 "
2726
28- def __init__ (self , i2c , read_delay = 10 ):
27+ def __init__ (self , i2c , read_delay = 10 , address = 0x40 ):
2928 self .i2c = i2c
30- if _ADDRESS not in self .i2c .scan ():
29+ if address not in self .i2c .scan ():
3130 raise OSError ("No HTU21D device found." )
31+ self .address = address
3232 self .temperature = None
3333 self .humidity = None
3434 asyncio .create_task (self ._run (read_delay ))
@@ -46,9 +46,9 @@ def __iter__(self): # Await 1st reading
4646 yield from asyncio .sleep (0 )
4747
4848 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
5050 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
5252 data , crc = ustruct .unpack (">HB" , value )
5353 remainder = (data << 8 ) | crc
5454 while bit > 128 :
@@ -61,4 +61,4 @@ async def _get_data(self, cmd, divisor=0x131 << 15, bit=1 << 23):
6161 return data & 0xFFFC # Clear the status bits
6262
6363 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