37
37
from adafruit_bus_device .i2c_device import I2CDevice
38
38
from micropython import const
39
39
40
+ try :
41
+ # Used only for typing
42
+ import typing # pylint: disable=unused-import
43
+ from busio import I2C
44
+ except ImportError :
45
+ pass
46
+
40
47
__version__ = "0.0.0-auto.0"
41
48
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_am2320.git"
42
49
47
54
AM2320_REG_HUM_H = const (0x00 )
48
55
49
56
50
- def _crc16 (data ) :
57
+ def _crc16 (data : bytearray ) -> int :
51
58
crc = 0xFFFF
52
59
for byte in data :
53
60
crc ^= byte
@@ -94,7 +101,7 @@ class AM2320:
94
101
95
102
"""
96
103
97
- def __init__ (self , i2c_bus , address = AM2320_DEFAULT_ADDR ):
104
+ def __init__ (self , i2c_bus : I2C , address : int = AM2320_DEFAULT_ADDR ):
98
105
for _ in range (3 ):
99
106
# retry since we have to wake up the devices
100
107
try :
@@ -105,7 +112,7 @@ def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR):
105
112
time .sleep (0.25 )
106
113
raise ValueError ("AM2320 not found" )
107
114
108
- def _read_register (self , register , length ) :
115
+ def _read_register (self , register : int , length : int ) -> bytearray :
109
116
with self ._i2c as i2c :
110
117
# wake up sensor
111
118
try :
@@ -133,15 +140,15 @@ def _read_register(self, register, length):
133
140
return result [2 :- 2 ]
134
141
135
142
@property
136
- def temperature (self ):
143
+ def temperature (self ) -> float :
137
144
"""The measured temperature in Celsius."""
138
145
temperature = struct .unpack (">H" , self ._read_register (AM2320_REG_TEMP_H , 2 ))[0 ]
139
146
if temperature >= 32768 :
140
147
temperature = 32768 - temperature
141
148
return temperature / 10.0
142
149
143
150
@property
144
- def relative_humidity (self ):
151
+ def relative_humidity (self ) -> float :
145
152
"""The measured relative humidity in percent."""
146
153
humidity = struct .unpack (">H" , self ._read_register (AM2320_REG_HUM_H , 2 ))[0 ]
147
154
return humidity / 10.0
0 commit comments