34
34
from adafruit_register .i2c_bit import RWBit , ROBit
35
35
from adafruit_register .i2c_bits import ROBits
36
36
37
+ try :
38
+ from typing import Dict , Optional , Union , List
39
+ from typing_extensions import Literal
40
+ from busio import I2C
41
+ except ImportError :
42
+ pass
43
+
37
44
__version__ = "0.0.0+auto.0"
38
45
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ENS160.git"
39
46
70
77
71
78
class ENS160 :
72
79
"""Driver for the ENS160 air quality sensor
80
+
73
81
:param ~busio.I2C i2c_bus: The I2C bus the ENS160 is connected to.
74
- :param address: The I2C device address. Defaults to :const:`0x53`
82
+ :param int address: The I2C device address. Defaults to :const:`0x53`
75
83
"""
76
84
77
85
part_id = ROUnaryStruct (_ENS160_REG_PARTID , "<H" )
@@ -96,7 +104,7 @@ class ENS160:
96
104
interrupt_on_data = RWBit (_ENS160_REG_CONFIG , 1 )
97
105
interrupt_enable = RWBit (_ENS160_REG_CONFIG , 0 )
98
106
99
- def __init__ (self , i2c_bus , address = ENS160_I2CADDR_DEFAULT ):
107
+ def __init__ (self , i2c_bus : I2C , address : int = ENS160_I2CADDR_DEFAULT ) -> None :
100
108
# pylint: disable=no-member
101
109
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
102
110
@@ -119,25 +127,25 @@ def __init__(self, i2c_bus, address=ENS160_I2CADDR_DEFAULT):
119
127
self .temperature_compensation = 25
120
128
self .humidity_compensation = 50
121
129
122
- def reset (self ):
130
+ def reset (self ) -> None :
123
131
"""Perform a soft reset command"""
124
132
self .mode = MODE_RESET
125
133
time .sleep (0.01 )
126
134
127
- def clear_command (self ):
135
+ def clear_command (self ) -> None :
128
136
"""Clears out custom data"""
129
137
self .command = COMMAND_NOP
130
138
self .command = COMMAND_CLRGPR
131
139
time .sleep (0.01 )
132
140
133
- def _read_gpr (self ):
141
+ def _read_gpr (self ) -> None :
134
142
"""Read 8 bytes of general purpose registers into self._buf"""
135
143
self ._buf [0 ] = _ENS160_REG_GPRREAD
136
144
with self .i2c_device as i2c :
137
145
i2c .write_then_readinto (self ._buf , self ._buf , out_end = 1 )
138
146
139
147
@property
140
- def new_data_available (self ):
148
+ def new_data_available (self ) -> bool :
141
149
"""This function is wierd, it checks if there's new data or
142
150
GPR (resistances) and if so immediately reads it into the
143
151
internal buffer... otherwise the status is lost!"""
@@ -166,13 +174,13 @@ def new_data_available(self):
166
174
167
175
return newdat
168
176
169
- def read_all_sensors (self ):
177
+ def read_all_sensors (self ) -> Dict [ str , Optional [ Union [ int , List [ int ]]]] :
170
178
"""All of the currently buffered sensor information"""
171
179
# return the currently buffered deets
172
180
return self ._bufferdict
173
181
174
182
@property
175
- def firmware_version (self ):
183
+ def firmware_version (self ) -> str :
176
184
"""Read the semver firmware version from the general registers"""
177
185
curr_mode = self .mode
178
186
self .mode = MODE_IDLE
@@ -184,34 +192,34 @@ def firmware_version(self):
184
192
return "%d.%d.%d" % (self ._buf [4 ], self ._buf [5 ], self ._buf [6 ])
185
193
186
194
@property
187
- def mode (self ):
188
- """Operational Mode, can be MODE_SLEEP, MODE_IDLE, or MODE_STANDARD """
195
+ def mode (self ) -> Literal [ 0 , 1 , 2 , 240 ] :
196
+ """Operational Mode, can be MODE_SLEEP, MODE_IDLE, MODE_STANDARD, or MODE_RESET """
189
197
return self ._mode
190
198
191
199
@mode .setter
192
- def mode (self , newmode ) :
200
+ def mode (self , newmode : Literal [ 0 , 1 , 2 , 240 ]) -> None :
193
201
if not newmode in _valid_modes :
194
202
raise RuntimeError (
195
- "Invalid mode: must be MODE_SLEEP, MODE_IDLE, or MODE_STANDARD "
203
+ "Invalid mode: must be MODE_SLEEP, MODE_IDLE, MODE_STANDARD, or MODE_RESET "
196
204
)
197
205
self ._mode = newmode
198
206
199
207
@property
200
- def temperature_compensation (self ):
208
+ def temperature_compensation (self ) -> float :
201
209
"""Temperature compensation setting, set this to ambient temperature
202
210
to get best gas sensor readings, floating point degrees C"""
203
211
return (self ._temp_in / 64.0 ) - 273.15
204
212
205
213
@temperature_compensation .setter
206
- def temperature_compensation (self , temp_c ) :
214
+ def temperature_compensation (self , temp_c : float ) -> None :
207
215
self ._temp_in = int ((temp_c + 273.15 ) * 64.0 + 0.5 )
208
216
209
217
@property
210
- def humidity_compensation (self ):
218
+ def humidity_compensation (self ) -> float :
211
219
"""Humidity compensation setting, set this to ambient relative
212
220
humidity (percentage 0-100) to get best gas sensor readings"""
213
221
return self ._rh_in / 512.0
214
222
215
223
@humidity_compensation .setter
216
- def humidity_compensation (self , hum_perc ) :
224
+ def humidity_compensation (self , hum_perc : float ) -> None :
217
225
self ._rh_in = int (hum_perc * 512 + 0.5 )
0 commit comments