Skip to content

Commit 425f070

Browse files
authored
Merge pull request #10 from tekktrik/feature/add-typing
Add typing, small refactoring
2 parents 000d2d3 + c780253 commit 425f070

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

adafruit_hts221.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
from adafruit_register.i2c_bits import RWBits, ROBits
3535
from adafruit_register.i2c_bit import RWBit, ROBit
3636

37+
try:
38+
from typing import Union, Sequence, Tuple
39+
from busio import I2C
40+
except ImportError:
41+
pass
42+
3743
_WHO_AM_I = const(0x0F)
3844

3945
_CTRL_REG1 = const(0x20)
@@ -65,21 +71,21 @@ class CV:
6571
"""struct helper"""
6672

6773
@classmethod
68-
def add_values(cls, value_tuples):
74+
def add_values(
75+
cls, value_tuples: Sequence[Tuple[str, int, Union[int, float]]]
76+
) -> None:
6977
"""creates CV entries"""
70-
cls.string = {}
71-
cls.lsb = {}
78+
cls.label = {}
7279

7380
for value_tuple in value_tuples:
74-
name, value, string, lsb = value_tuple
81+
name, value, label = value_tuple
7582
setattr(cls, name, value)
76-
cls.string[value] = string
77-
cls.lsb[value] = lsb
83+
cls.label[value] = label
7884

7985
@classmethod
80-
def is_valid(cls, value):
86+
def is_valid(cls, value: int) -> bool:
8187
"""Returns true if the given value is a member of the CV"""
82-
return value in cls.string
88+
return value in cls.label
8389

8490

8591
class Rate(CV):
@@ -105,10 +111,10 @@ class Rate(CV):
105111

106112
Rate.add_values(
107113
(
108-
("ONE_SHOT", 0, 0, None),
109-
("RATE_1_HZ", 1, 1, None),
110-
("RATE_7_HZ", 2, 7, None),
111-
("RATE_12_5_HZ", 3, 12.5, None),
114+
("ONE_SHOT", 0, 0),
115+
("RATE_1_HZ", 1, 1),
116+
("RATE_7_HZ", 2, 7),
117+
("RATE_12_5_HZ", 3, 12.5),
112118
)
113119
)
114120

@@ -170,7 +176,7 @@ class HTS221: # pylint: disable=too-many-instance-attributes
170176
_h0_t0_out = ROUnaryStruct(_H0_T0_OUT, "<h")
171177
_h1_t0_out = ROUnaryStruct(_H1_T1_OUT, "<h")
172178

173-
def __init__(self, i2c_bus):
179+
def __init__(self, i2c_bus: I2C) -> None:
174180
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, _HTS221_DEFAULT_ADDRESS)
175181
if not self._chip_id in [_HTS221_CHIP_ID]:
176182
raise RuntimeError(
@@ -203,14 +209,14 @@ def __init__(self, i2c_bus):
203209
self.calib_hum_meas_1 = self._h1_t0_out
204210

205211
# This is the closest thing to a software reset. It re-loads the calibration values from flash
206-
def _boot(self):
212+
def _boot(self) -> None:
207213
self._boot_bit = True
208214
# wait for the reset to finish
209215
while self._boot_bit:
210216
pass
211217

212218
@property
213-
def relative_humidity(self):
219+
def relative_humidity(self) -> float:
214220
"""The current relative humidity measurement in %rH"""
215221
calibrated_value_delta = self.calib_hum_value_1 - self.calib_hum_value_0
216222
calibrated_measurement_delta = self.calib_hum_meas_1 - self.calib_hum_meas_0
@@ -228,7 +234,7 @@ def relative_humidity(self):
228234
return adjusted_humidity
229235

230236
@property
231-
def temperature(self):
237+
def temperature(self) -> float:
232238
"""The current temperature measurement in degrees Celsius"""
233239

234240
calibrated_value_delta = self.calibrated_value_1 - self.calib_temp_value_0
@@ -247,7 +253,7 @@ def temperature(self):
247253
return adjusted_temp
248254

249255
@property
250-
def data_rate(self):
256+
def data_rate(self) -> int:
251257
"""The rate at which the sensor measures :attr:`relative_humidity` and :attr:`temperature`.
252258
:attr:`data_rate` should be set to one of the values of :class:`adafruit_hts221.Rate`.
253259
Note that setting :attr:`data_rate` to ``Rate.ONE_SHOT`` will cause
@@ -256,23 +262,23 @@ def data_rate(self):
256262
return self._data_rate
257263

258264
@data_rate.setter
259-
def data_rate(self, value):
265+
def data_rate(self, value: int) -> None:
260266
if not Rate.is_valid(value):
261267
raise AttributeError("data_rate must be a `Rate`")
262268

263269
self._data_rate = value
264270

265271
@property
266-
def humidity_data_ready(self):
272+
def humidity_data_ready(self) -> bool:
267273
"""Returns true if a new relative humidity measurement is available to be read"""
268274
return self._humidity_status_bit
269275

270276
@property
271-
def temperature_data_ready(self):
277+
def temperature_data_ready(self) -> bool:
272278
"""Returns true if a new temperature measurement is available to be read"""
273279
return self._temperature_status_bit
274280

275-
def take_measurements(self):
281+
def take_measurements(self) -> None:
276282
"""Update the value of :attr:`relative_humidity` and :attr:`temperature` by taking a single
277283
measurement. Only meaningful if :attr:`data_rate` is set to ``ONE_SHOT``"""
278284
self._one_shot_bit = True

examples/hts221_simpletest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
i2c = board.I2C()
99
hts = adafruit_hts221.HTS221(i2c)
1010

11+
data_rate = adafruit_hts221.Rate.label[hts.data_rate]
12+
print("Using data rate of: {:.1f} Hz".format(data_rate))
13+
print("")
14+
1115
while True:
12-
print("Relative Humidity: %.2f %% rH" % hts.relative_humidity)
13-
print("Temperature: %.2f C" % hts.temperature)
16+
print("Relative Humidity: {:.2f} % rH".format(hts.relative_humidity))
17+
print("Temperature: {:.2f} C".format(hts.temperature))
1418
print("")
1519
time.sleep(1)

0 commit comments

Comments
 (0)