73
73
_SHT31_PERIODIC_FETCH = const (0xE000 )
74
74
_SHT31_PERIODIC_BREAK = const (0x3093 )
75
75
76
- MODE_SINGLE = ' Single'
77
- MODE_PERIODIC = ' Periodic'
76
+ MODE_SINGLE = " Single"
77
+ MODE_PERIODIC = " Periodic"
78
78
79
79
_SHT31_MODES = (MODE_SINGLE , MODE_PERIODIC )
80
80
81
- REP_HIGH = ' High'
82
- REP_MED = ' Medium'
83
- REP_LOW = ' Low'
81
+ REP_HIGH = " High"
82
+ REP_MED = " Medium"
83
+ REP_LOW = " Low"
84
84
85
85
_SHT31_REP = (REP_HIGH , REP_MED , REP_LOW )
86
86
90
90
FREQUENCY_4 = 4
91
91
FREQUENCY_10 = 10
92
92
93
- _SHT31_FREQUENCIES = (FREQUENCY_0_5 , FREQUENCY_1 , FREQUENCY_2 , FREQUENCY_4 , FREQUENCY_10 )
94
-
95
- _SINGLE_COMMANDS = ((REP_LOW , const (False ), const (0x2416 )),
96
- (REP_MED , const (False ), const (0x240B )),
97
- (REP_HIGH , const (False ), const (0x2400 )),
98
- (REP_LOW , const (True ), const (0x2C10 )),
99
- (REP_MED , const (True ), const (0x2C0D )),
100
- (REP_HIGH , const (True ), const (0x2C06 )))
101
-
102
- _PERIODIC_COMMANDS = ((True , None , const (0x2B32 )),
103
- (REP_LOW , FREQUENCY_0_5 , const (0x202F )),
104
- (REP_MED , FREQUENCY_0_5 , const (0x2024 )),
105
- (REP_HIGH , FREQUENCY_0_5 , const (0x2032 )),
106
- (REP_LOW , FREQUENCY_1 , const (0x212D )),
107
- (REP_MED , FREQUENCY_1 , const (0x2126 )),
108
- (REP_HIGH , FREQUENCY_1 , const (0x2130 )),
109
- (REP_LOW , FREQUENCY_2 , const (0x222B )),
110
- (REP_MED , FREQUENCY_2 , const (0x2220 )),
111
- (REP_HIGH , FREQUENCY_2 , const (0x2236 )),
112
- (REP_LOW , FREQUENCY_4 , const (0x2329 )),
113
- (REP_MED , FREQUENCY_4 , const (0x2322 )),
114
- (REP_HIGH , FREQUENCY_4 , const (0x2334 )),
115
- (REP_LOW , FREQUENCY_10 , const (0x272A )),
116
- (REP_MED , FREQUENCY_10 , const (0x2721 )),
117
- (REP_HIGH , FREQUENCY_10 , const (0x2737 )))
118
-
119
- _DELAY = ((REP_LOW , .0045 ),
120
- (REP_MED , .0065 ),
121
- (REP_HIGH , .0155 ))
93
+ _SHT31_FREQUENCIES = (
94
+ FREQUENCY_0_5 ,
95
+ FREQUENCY_1 ,
96
+ FREQUENCY_2 ,
97
+ FREQUENCY_4 ,
98
+ FREQUENCY_10 ,
99
+ )
100
+
101
+ _SINGLE_COMMANDS = (
102
+ (REP_LOW , const (False ), const (0x2416 )),
103
+ (REP_MED , const (False ), const (0x240B )),
104
+ (REP_HIGH , const (False ), const (0x2400 )),
105
+ (REP_LOW , const (True ), const (0x2C10 )),
106
+ (REP_MED , const (True ), const (0x2C0D )),
107
+ (REP_HIGH , const (True ), const (0x2C06 )),
108
+ )
109
+
110
+ _PERIODIC_COMMANDS = (
111
+ (True , None , const (0x2B32 )),
112
+ (REP_LOW , FREQUENCY_0_5 , const (0x202F )),
113
+ (REP_MED , FREQUENCY_0_5 , const (0x2024 )),
114
+ (REP_HIGH , FREQUENCY_0_5 , const (0x2032 )),
115
+ (REP_LOW , FREQUENCY_1 , const (0x212D )),
116
+ (REP_MED , FREQUENCY_1 , const (0x2126 )),
117
+ (REP_HIGH , FREQUENCY_1 , const (0x2130 )),
118
+ (REP_LOW , FREQUENCY_2 , const (0x222B )),
119
+ (REP_MED , FREQUENCY_2 , const (0x2220 )),
120
+ (REP_HIGH , FREQUENCY_2 , const (0x2236 )),
121
+ (REP_LOW , FREQUENCY_4 , const (0x2329 )),
122
+ (REP_MED , FREQUENCY_4 , const (0x2322 )),
123
+ (REP_HIGH , FREQUENCY_4 , const (0x2334 )),
124
+ (REP_LOW , FREQUENCY_10 , const (0x272A )),
125
+ (REP_MED , FREQUENCY_10 , const (0x2721 )),
126
+ (REP_HIGH , FREQUENCY_10 , const (0x2737 )),
127
+ )
128
+
129
+ _DELAY = ((REP_LOW , 0.0045 ), (REP_MED , 0.0065 ), (REP_HIGH , 0.0155 ))
122
130
123
131
124
132
def _crc (data ):
125
- crc = 0xff
133
+ crc = 0xFF
126
134
for byte in data :
127
135
crc ^= byte
128
136
for _ in range (8 ):
@@ -133,18 +141,21 @@ def _crc(data):
133
141
crc <<= 1
134
142
return crc
135
143
144
+
136
145
def _unpack (data ):
137
146
length = len (data )
138
- crc = [None ] * (length // 3 )
139
- word = [None ] * (length // 3 )
140
- for i in range (length // 6 ):
141
- word [i * 2 ], crc [i * 2 ], word [(i * 2 )+ 1 ], crc [(i * 2 )+ 1 ] = struct .unpack ('>HBHB' , data [i * 6 :(i * 6 )+ 6 ])
142
- if crc [i * 2 ] == _crc (data [i * 6 :(i * 6 )+ 2 ]):
143
- length = (i + 1 )* 6
144
- for i in range (length // 3 ):
145
- if crc [i ] != _crc (data [i * 3 :(i * 3 )+ 2 ]):
147
+ crc = [None ] * (length // 3 )
148
+ word = [None ] * (length // 3 )
149
+ for i in range (length // 6 ):
150
+ word [i * 2 ], crc [i * 2 ], word [(i * 2 ) + 1 ], crc [(i * 2 ) + 1 ] = struct .unpack (
151
+ ">HBHB" , data [i * 6 : (i * 6 ) + 6 ]
152
+ )
153
+ if crc [i * 2 ] == _crc (data [i * 6 : (i * 6 ) + 2 ]):
154
+ length = (i + 1 ) * 6
155
+ for i in range (length // 3 ):
156
+ if crc [i ] != _crc (data [i * 3 : (i * 3 ) + 2 ]):
146
157
raise RuntimeError ("CRC mismatch" )
147
- return word [:length // 3 ]
158
+ return word [: length // 3 ]
148
159
149
160
150
161
class SHT31D :
@@ -154,9 +165,10 @@ class SHT31D:
154
165
:param i2c_bus: The `busio.I2C` object to use. This is the only required parameter.
155
166
:param int address: (optional) The I2C address of the device.
156
167
"""
168
+
157
169
def __init__ (self , i2c_bus , address = _SHT31_DEFAULT_ADDRESS ):
158
170
if address not in _SHT31_ADDRESSES :
159
- raise ValueError (' Invalid address: 0x%x' % (address ))
171
+ raise ValueError (" Invalid address: 0x%x" % (address ))
160
172
self .i2c_device = I2CDevice (i2c_bus , address )
161
173
self ._mode = MODE_SINGLE
162
174
self ._repeatability = REP_HIGH
@@ -170,7 +182,7 @@ def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
170
182
171
183
def _command (self , command ):
172
184
with self .i2c_device as i2c :
173
- i2c .write (struct .pack ('>H' , command ))
185
+ i2c .write (struct .pack (">H" , command ))
174
186
175
187
def _reset (self ):
176
188
"""
@@ -179,51 +191,58 @@ def _reset(self):
179
191
device will not respond to a soft reset when in 'Periodic' mode.
180
192
"""
181
193
self ._command (_SHT31_PERIODIC_BREAK )
182
- time .sleep (.001 )
194
+ time .sleep (0 .001 )
183
195
self ._command (_SHT31_SOFTRESET )
184
- time .sleep (.0015 )
196
+ time .sleep (0 .0015 )
185
197
186
198
def _periodic (self ):
187
199
for command in _PERIODIC_COMMANDS :
188
- if self .art == command [0 ] or \
189
- (self .repeatability == command [0 ] and self .frequency == command [1 ]):
200
+ if self .art == command [0 ] or (
201
+ self .repeatability == command [0 ] and self .frequency == command [1 ]
202
+ ):
190
203
self ._command (command [2 ])
191
- time .sleep (.001 )
204
+ time .sleep (0 .001 )
192
205
self ._last_read = 0
193
206
194
207
def _data (self ):
195
208
if self .mode == MODE_PERIODIC :
196
209
data = bytearray (48 )
197
- data [0 ] = 0xff
210
+ data [0 ] = 0xFF
198
211
self ._command (_SHT31_PERIODIC_FETCH )
199
- time .sleep (.001 )
212
+ time .sleep (0 .001 )
200
213
elif self .mode == MODE_SINGLE :
201
214
data = bytearray (6 )
202
- data [0 ] = 0xff
215
+ data [0 ] = 0xFF
203
216
for command in _SINGLE_COMMANDS :
204
- if self .repeatability == command [0 ] and self .clock_stretching == command [1 ]:
217
+ if (
218
+ self .repeatability == command [0 ]
219
+ and self .clock_stretching == command [1 ]
220
+ ):
205
221
self ._command (command [2 ])
206
222
if not self .clock_stretching :
207
223
for delay in _DELAY :
208
224
if self .repeatability == delay [0 ]:
209
225
time .sleep (delay [1 ])
210
226
else :
211
- time .sleep (.001 )
227
+ time .sleep (0 .001 )
212
228
with self .i2c_device as i2c :
213
229
i2c .readinto (data )
214
230
word = _unpack (data )
215
231
length = len (word )
216
- temperature = [None ] * (length // 2 )
217
- humidity = [None ] * (length // 2 )
218
- for i in range (length // 2 ):
219
- temperature [i ] = - 45 + (175 * (word [i * 2 ] / 65535 ))
220
- humidity [i ] = 100 * (word [(i * 2 ) + 1 ] / 65535 )
232
+ temperature = [None ] * (length // 2 )
233
+ humidity = [None ] * (length // 2 )
234
+ for i in range (length // 2 ):
235
+ temperature [i ] = - 45 + (175 * (word [i * 2 ] / 65535 ))
236
+ humidity [i ] = 100 * (word [(i * 2 ) + 1 ] / 65535 )
221
237
if (len (temperature ) == 1 ) and (len (humidity ) == 1 ):
222
238
return temperature [0 ], humidity [0 ]
223
239
return temperature , humidity
224
240
225
241
def _read (self ):
226
- if self .mode == MODE_PERIODIC and time .time () > self ._last_read + 1 / self .frequency :
242
+ if (
243
+ self .mode == MODE_PERIODIC
244
+ and time .time () > self ._last_read + 1 / self .frequency
245
+ ):
227
246
self ._cached_temperature , self ._cached_humidity = self ._data ()
228
247
self ._last_read = time .time ()
229
248
elif self .mode == MODE_SINGLE :
@@ -245,7 +264,7 @@ def mode(self, value):
245
264
raise ValueError ("Mode '%s' not supported" % (value ))
246
265
if self ._mode == MODE_PERIODIC and value != MODE_PERIODIC :
247
266
self ._command (_SHT31_PERIODIC_BREAK )
248
- time .sleep (.001 )
267
+ time .sleep (0 .001 )
249
268
if value == MODE_PERIODIC and self ._mode != MODE_PERIODIC :
250
269
self ._periodic ()
251
270
self ._mode = value
@@ -312,7 +331,9 @@ def frequency(self, value):
312
331
if self .art :
313
332
raise RuntimeError ("Frequency locked to '4 Hz' when ART enabled" )
314
333
if not value in _SHT31_FREQUENCIES :
315
- raise ValueError ("Data acquisition frequency '%s Hz' not supported" % (value ))
334
+ raise ValueError (
335
+ "Data acquisition frequency '%s Hz' not supported" % (value )
336
+ )
316
337
if self .mode == MODE_PERIODIC and not self ._frequency == value :
317
338
self ._frequency = value
318
339
self ._periodic ()
@@ -354,17 +375,17 @@ def heater(self):
354
375
def heater (self , value = False ):
355
376
if value :
356
377
self ._command (_SHT31_HEATER_ENABLE )
357
- time .sleep (.001 )
378
+ time .sleep (0 .001 )
358
379
else :
359
380
self ._command (_SHT31_HEATER_DISABLE )
360
- time .sleep (.001 )
381
+ time .sleep (0 .001 )
361
382
362
383
@property
363
384
def status (self ):
364
385
"""Device status."""
365
386
data = bytearray (2 )
366
387
self ._command (_SHT31_READSTATUS )
367
- time .sleep (.001 )
388
+ time .sleep (0 .001 )
368
389
with self .i2c_device as i2c :
369
390
i2c .readinto (data )
370
391
status = data [0 ] << 8 | data [1 ]
@@ -374,9 +395,9 @@ def status(self):
374
395
def serial_number (self ):
375
396
"""Device serial number."""
376
397
data = bytearray (6 )
377
- data [0 ] = 0xff
398
+ data [0 ] = 0xFF
378
399
self ._command (_SHT31_READSERIALNBR )
379
- time .sleep (.001 )
400
+ time .sleep (0 .001 )
380
401
with self .i2c_device as i2c :
381
402
i2c .readinto (data )
382
403
word = _unpack (data )
0 commit comments