7373_SHT31_PERIODIC_FETCH = const (0xE000 )
7474_SHT31_PERIODIC_BREAK = const (0x3093 )
7575
76- MODE_SINGLE = ' Single'
77- MODE_PERIODIC = ' Periodic'
76+ MODE_SINGLE = " Single"
77+ MODE_PERIODIC = " Periodic"
7878
7979_SHT31_MODES = (MODE_SINGLE , MODE_PERIODIC )
8080
81- REP_HIGH = ' High'
82- REP_MED = ' Medium'
83- REP_LOW = ' Low'
81+ REP_HIGH = " High"
82+ REP_MED = " Medium"
83+ REP_LOW = " Low"
8484
8585_SHT31_REP = (REP_HIGH , REP_MED , REP_LOW )
8686
9090FREQUENCY_4 = 4
9191FREQUENCY_10 = 10
9292
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 ))
122130
123131
124132def _crc (data ):
125- crc = 0xff
133+ crc = 0xFF
126134 for byte in data :
127135 crc ^= byte
128136 for _ in range (8 ):
@@ -133,18 +141,21 @@ def _crc(data):
133141 crc <<= 1
134142 return crc
135143
144+
136145def _unpack (data ):
137146 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 ]):
146157 raise RuntimeError ("CRC mismatch" )
147- return word [:length // 3 ]
158+ return word [: length // 3 ]
148159
149160
150161class SHT31D :
@@ -154,9 +165,10 @@ class SHT31D:
154165 :param i2c_bus: The `busio.I2C` object to use. This is the only required parameter.
155166 :param int address: (optional) The I2C address of the device.
156167 """
168+
157169 def __init__ (self , i2c_bus , address = _SHT31_DEFAULT_ADDRESS ):
158170 if address not in _SHT31_ADDRESSES :
159- raise ValueError (' Invalid address: 0x%x' % (address ))
171+ raise ValueError (" Invalid address: 0x%x" % (address ))
160172 self .i2c_device = I2CDevice (i2c_bus , address )
161173 self ._mode = MODE_SINGLE
162174 self ._repeatability = REP_HIGH
@@ -170,7 +182,7 @@ def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
170182
171183 def _command (self , command ):
172184 with self .i2c_device as i2c :
173- i2c .write (struct .pack ('>H' , command ))
185+ i2c .write (struct .pack (">H" , command ))
174186
175187 def _reset (self ):
176188 """
@@ -179,51 +191,58 @@ def _reset(self):
179191 device will not respond to a soft reset when in 'Periodic' mode.
180192 """
181193 self ._command (_SHT31_PERIODIC_BREAK )
182- time .sleep (.001 )
194+ time .sleep (0 .001 )
183195 self ._command (_SHT31_SOFTRESET )
184- time .sleep (.0015 )
196+ time .sleep (0 .0015 )
185197
186198 def _periodic (self ):
187199 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+ ):
190203 self ._command (command [2 ])
191- time .sleep (.001 )
204+ time .sleep (0 .001 )
192205 self ._last_read = 0
193206
194207 def _data (self ):
195208 if self .mode == MODE_PERIODIC :
196209 data = bytearray (48 )
197- data [0 ] = 0xff
210+ data [0 ] = 0xFF
198211 self ._command (_SHT31_PERIODIC_FETCH )
199- time .sleep (.001 )
212+ time .sleep (0 .001 )
200213 elif self .mode == MODE_SINGLE :
201214 data = bytearray (6 )
202- data [0 ] = 0xff
215+ data [0 ] = 0xFF
203216 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+ ):
205221 self ._command (command [2 ])
206222 if not self .clock_stretching :
207223 for delay in _DELAY :
208224 if self .repeatability == delay [0 ]:
209225 time .sleep (delay [1 ])
210226 else :
211- time .sleep (.001 )
227+ time .sleep (0 .001 )
212228 with self .i2c_device as i2c :
213229 i2c .readinto (data )
214230 word = _unpack (data )
215231 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 )
221237 if (len (temperature ) == 1 ) and (len (humidity ) == 1 ):
222238 return temperature [0 ], humidity [0 ]
223239 return temperature , humidity
224240
225241 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+ ):
227246 self ._cached_temperature , self ._cached_humidity = self ._data ()
228247 self ._last_read = time .time ()
229248 elif self .mode == MODE_SINGLE :
@@ -245,7 +264,7 @@ def mode(self, value):
245264 raise ValueError ("Mode '%s' not supported" % (value ))
246265 if self ._mode == MODE_PERIODIC and value != MODE_PERIODIC :
247266 self ._command (_SHT31_PERIODIC_BREAK )
248- time .sleep (.001 )
267+ time .sleep (0 .001 )
249268 if value == MODE_PERIODIC and self ._mode != MODE_PERIODIC :
250269 self ._periodic ()
251270 self ._mode = value
@@ -312,7 +331,9 @@ def frequency(self, value):
312331 if self .art :
313332 raise RuntimeError ("Frequency locked to '4 Hz' when ART enabled" )
314333 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+ )
316337 if self .mode == MODE_PERIODIC and not self ._frequency == value :
317338 self ._frequency = value
318339 self ._periodic ()
@@ -354,17 +375,17 @@ def heater(self):
354375 def heater (self , value = False ):
355376 if value :
356377 self ._command (_SHT31_HEATER_ENABLE )
357- time .sleep (.001 )
378+ time .sleep (0 .001 )
358379 else :
359380 self ._command (_SHT31_HEATER_DISABLE )
360- time .sleep (.001 )
381+ time .sleep (0 .001 )
361382
362383 @property
363384 def status (self ):
364385 """Device status."""
365386 data = bytearray (2 )
366387 self ._command (_SHT31_READSTATUS )
367- time .sleep (.001 )
388+ time .sleep (0 .001 )
368389 with self .i2c_device as i2c :
369390 i2c .readinto (data )
370391 status = data [0 ] << 8 | data [1 ]
@@ -374,9 +395,9 @@ def status(self):
374395 def serial_number (self ):
375396 """Device serial number."""
376397 data = bytearray (6 )
377- data [0 ] = 0xff
398+ data [0 ] = 0xFF
378399 self ._command (_SHT31_READSERIALNBR )
379- time .sleep (.001 )
400+ time .sleep (0 .001 )
380401 with self .i2c_device as i2c :
381402 i2c .readinto (data )
382403 word = _unpack (data )
0 commit comments