10
10
New to qwiic? Take a look at the entire [SparkFun qwiic ecosystem](https://www.sparkfun.com/qwiic).
11
11
12
12
"""
13
- from __future__ import print_function , division
14
-
15
13
import qwiic_i2c
16
14
17
15
#======================================================================
@@ -119,10 +117,10 @@ def read_temp_c(self):
119
117
Reads the results from the sensor
120
118
:rtype: integer
121
119
"""
122
- data = self ._i2c . readBlock ( self . address , TEMPERATURE_REGISTER , 2 )
120
+ data = self .read_block_pointer_reg ( TEMPERATURE_REGISTER )
123
121
124
122
if (data [0 ] == 0xFF and data [1 ] == 0xFF ):
125
- return NaN
123
+ return None
126
124
127
125
if (data [1 ]& 0x01 ): # 13 bit mode
128
126
baseRead = ((data [0 ]) << 5 ) | (data [1 ] >> 3 )
@@ -151,14 +149,14 @@ def read_temp_f(self):
151
149
152
150
def set_conversion_rate (self , rate ):
153
151
"""
154
- // Set the conversion rate (0-3)
155
- // 0 - 0.25 Hz
156
- // 1 - 1 Hz
157
- // 2 - 4 Hz (default)
158
- // 3 - 8 Hz
152
+ Set the conversion rate (0-3)
153
+ 0 - 0.25 Hz
154
+ 1 - 1 Hz
155
+ 2 - 4 Hz (default)
156
+ 3 - 8 Hz
159
157
"""
160
158
161
- configByte = self ._i2c . readBlock ( self . address , CONFIG_REGISTER , 2 )
159
+ configByte = self .read_block_pointer_reg ( CONFIG_REGISTER )
162
160
rate = rate & 0x03
163
161
164
162
# Load new conversion rate
@@ -170,10 +168,10 @@ def set_conversion_rate(self, rate):
170
168
def set_extended_mode (self , mode ):
171
169
"""
172
170
Enable or disable extended mode
173
- 0 - disabled (-55C to +128C)
174
- 1 - enabled (-55C to +150C)
171
+ 0 - disabled (-55C to +128C)
172
+ 1 - enabled (-55C to +150C)
175
173
"""
176
- configByte = self ._i2c . readBlock ( self . address , CONFIG_REGISTER , 2 )
174
+ configByte = self .read_block_pointer_reg ( CONFIG_REGISTER )
177
175
178
176
# Load new value for extention mode
179
177
configByte [1 ] &= 0xEF # Clear EM (bit 4 of second byte)
@@ -185,15 +183,15 @@ def sleep(self):
185
183
"""
186
184
Switch sensor to low power mode
187
185
"""
188
- sleepValue = self ._i2c . readByte ( self . address , CONFIG_REGISTER )
186
+ sleepValue = self .read_block_pointer_reg ( CONFIG_REGISTER )[ 0 ]
189
187
sleepValue |= 0x01 # Set SD (bit 0 of first byte)
190
188
self ._i2c .writeByte (self .address , CONFIG_REGISTER , sleepValue )
191
189
192
190
def wakeup (self ):
193
191
"""
194
192
Wakeup and start running in normal power mode
195
193
"""
196
- wakeValue = self ._i2c . readByte ( self . address , CONFIG_REGISTER )
194
+ wakeValue = self .read_block_pointer_reg ( CONFIG_REGISTER )[ 0 ]
197
195
wakeValue &= 0xFE # Clear SD (bit 0 of first byte)
198
196
self ._i2c .writeByte (self .address , CONFIG_REGISTER , wakeValue )
199
197
@@ -203,7 +201,7 @@ def set_alert_polarity(self, polarity):
203
201
0 - Active LOW
204
202
1 - Active HIGH
205
203
"""
206
- configByte = self ._i2c . readByte ( self . address , CONFIG_REGISTER )
204
+ configByte = self .read_block_pointer_reg ( CONFIG_REGISTER )[ 0 ]
207
205
208
206
# Load new value for polarity
209
207
configByte &= 0xFB # Clear POL (bit 2 of registerByte)
@@ -215,15 +213,15 @@ def alert(self):
215
213
"""
216
214
Returns state of Alert register
217
215
"""
218
- alert = self ._i2c . readByte ( self . address , CONFIG_REGISTER )
216
+ alert = self .read_block_pointer_reg ( CONFIG_REGISTER )[ 1 ]
219
217
alert &= 0x20 #Clear everything but the alert bit (bit 5)
220
218
return alert >> 5
221
219
222
220
def one_shot (self , setOneShot = 0 ):
223
221
"""
224
222
Sets the SingleShot Register. Returns 1 after the conversion is complete
225
223
"""
226
- registerByte = self ._i2c . readByte ( self . address , CONFIG_REGISTER )
224
+ registerByte = self .read_block_pointer_reg ( CONFIG_REGISTER )[ 0 ]
227
225
if (setOneShot == 1 ):
228
226
registerByte |= (1 << 7 )
229
227
self ._i2c .writeByte (self .address , CONFIG_REGISTER , registerByte )
@@ -242,7 +240,7 @@ def set_low_temp_c(self, temperature):
242
240
if (temperature < - 55.0 ):
243
241
temperature = - 55.0
244
242
245
- registerByte = self ._i2c . readBlock ( self . address , CONFIG_REGISTER , 2 )
243
+ registerByte = self .read_block_pointer_reg ( CONFIG_REGISTER )
246
244
247
245
#Check if temperature should be 12 or 13 bits
248
246
# 0 - temp data will be 12 bits
@@ -252,12 +250,13 @@ def set_low_temp_c(self, temperature):
252
250
#Convert analog temperature to digital value
253
251
temperature = temperature / 0.0625
254
252
253
+ # Align data for the temperature registers (see pg. 19 of datasheet)
255
254
if (extendedMode ): #13-bit mode
256
255
registerByte [0 ] = int (temperature )>> 5
257
- registerByte [1 ] = (int (temperature )<< 3 )
256
+ registerByte [1 ] = (int (temperature ) & 0x1F ) << 3 # lower 5 bits
258
257
else :
259
258
registerByte [0 ] = int (temperature )>> 4
260
- registerByte [1 ] = int (temperature )<< 4
259
+ registerByte [1 ] = ( int (temperature ) & 0xF ) << 4 # lower 4 bits
261
260
262
261
self ._i2c .writeBlock (self .address , T_LOW_REGISTER , registerByte )
263
262
@@ -270,7 +269,7 @@ def set_high_temp_c(self, temperature):
270
269
temperature = 150.0
271
270
if (temperature < - 55.0 ):
272
271
temperature = - 55.0
273
- registerByte = self ._i2c . readBlock ( self . address , CONFIG_REGISTER , 2 )
272
+ registerByte = self .read_block_pointer_reg ( CONFIG_REGISTER )
274
273
275
274
#Check if temperature should be 12 or 13 bits
276
275
# 0 - temp data will be 12 bits
@@ -279,13 +278,14 @@ def set_high_temp_c(self, temperature):
279
278
280
279
#Convert analog temperature to digital value
281
280
temperature = temperature / 0.0625
282
-
281
+
282
+ # Align data for the temperature registers (see pg. 19 of datasheet)
283
283
if (extendedMode ): #13-bit mode
284
- registerByte [0 ] = int (temperature )>> 5
285
- registerByte [1 ] = (int (temperature )<< 3 )
284
+ registerByte [0 ] = int (temperature )>> 5
285
+ registerByte [1 ] = (int (temperature ) & 0x1F ) << 3 # lower 5 bits
286
286
else :
287
287
registerByte [0 ] = int (temperature )>> 4
288
- registerByte [1 ] = int (temperature )<< 4
288
+ registerByte [1 ] = ( int (temperature ) & 0xF ) << 4 # lower 4 bits
289
289
290
290
self ._i2c .writeBlock (self .address , T_HIGH_REGISTER , registerByte )
291
291
@@ -309,16 +309,16 @@ def read_low_temp_c(self):
309
309
"""
310
310
Gets T_LOW (degrees C) alert threshold
311
311
"""
312
- configByte = self ._i2c . readBlock ( self . address , CONFIG_REGISTER , 2 )
312
+ configByte = self .read_block_pointer_reg ( CONFIG_REGISTER )
313
313
314
314
# 0 - temp data will be 12 bits
315
315
# 1 - temp data will be 13 bits
316
316
extendedMode = (configByte [1 ]& 0x10 )>> 4
317
317
318
- lowTempByte = self ._i2c . readBlock ( self . address , T_LOW_REGISTER , 2 )
318
+ lowTempByte = self .read_block_pointer_reg ( T_LOW_REGISTER )
319
319
320
320
if (lowTempByte [0 ] == 0xFF and lowTempByte [1 ] == 0xFF ):
321
- return NAN
321
+ return None
322
322
323
323
if (extendedMode ):
324
324
digitalTemp = ((lowTempByte [0 ]) << 5 ) | (lowTempByte [1 ] >> 3 )
@@ -336,16 +336,16 @@ def read_high_temp_c(self):
336
336
"""
337
337
Gets T_HIGH (degrees C) alert threshold
338
338
"""
339
- configByte = self ._i2c . readBlock ( self . address , CONFIG_REGISTER , 2 )
339
+ configByte = self .read_block_pointer_reg ( CONFIG_REGISTER )
340
340
341
341
# 0 - temp data will be 12 bits
342
342
# 1 - temp data will be 13 bits
343
343
extendedMode = (configByte [1 ]& 0x10 )>> 4
344
344
345
- highTempByte = self ._i2c . readBlock ( self . address , T_HIGH_REGISTER , 2 )
345
+ highTempByte = self .read_block_pointer_reg ( T_HIGH_REGISTER )
346
346
347
347
if (highTempByte [0 ] == 0xFF and highTempByte [1 ] == 0xFF ):
348
- return NAN
348
+ return None
349
349
350
350
if (extendedMode ):
351
351
digitalTemp = ((highTempByte [0 ]) << 5 ) | (highTempByte [1 ] >> 3 )
@@ -375,15 +375,15 @@ def read_high_temp_f(self):
375
375
376
376
def set_fault (self , faultSetting ):
377
377
"""
378
- Set the number of consecutive faults
379
- 0 - 1 fault
380
- 1 - 2 faults
381
- 2 - 4 faults
382
- 3 - 6 faults
378
+ Set the number of consecutive faults
379
+ 0 - 1 fault
380
+ 1 - 2 faults
381
+ 2 - 4 faults
382
+ 3 - 6 faults
383
383
"""
384
384
faultSetting = faultSetting & 3 #Make sure rate is not set higher than 3.
385
385
386
- configByte = self ._i2c . readByte ( self . address , CONFIG_REGISTER )
386
+ configByte = self .read_block_pointer_reg ( CONFIG_REGISTER )[ 0 ]
387
387
388
388
#Load new conversion rate
389
389
configByte &= 0xE7 # Clear F0/1 (bit 3 and 4 of first byte)
@@ -393,17 +393,34 @@ def set_fault(self, faultSetting):
393
393
394
394
def set_alert_mode (self , mode ):
395
395
"""
396
- // Set Alert type
397
- // 0 - Comparator Mode: Active from temp > T_HIGH until temp < T_LOW
398
- // 1 - Thermostat Mode: Active when temp > T_HIGH until any read operation occurs
399
-
396
+ Set Alert type
397
+ 0 - Comparator Mode: Active from temp > T_HIGH until temp < T_LOW
398
+ 1 - Thermostat Mode: Active when temp > T_HIGH until any read operation occurs
400
399
"""
401
- configByte = self ._i2c . readByte ( self . address , CONFIG_REGISTER )
400
+ configByte = self .read_block_pointer_reg ( CONFIG_REGISTER )[ 0 ]
402
401
403
402
#Load new conversion rate
404
403
configByte &= 0xFD # Clear old TM bit (bit 1 of first byte)
405
404
configByte |= mode << 1 # Shift in new TM bit
406
405
407
406
self ._i2c .writeByte (self .address , CONFIG_REGISTER , configByte )
408
407
409
-
408
+ def read_block_pointer_reg (self , reg , numBytes = 2 ):
409
+ """
410
+ To read from the device, we first write the register we want to read then explicitly send a stop bit.
411
+ Then, restart the connection to read the data from the device.
412
+
413
+ See datasheet page. 13
414
+
415
+ :param reg: The register to read from.
416
+ :type reg: int
417
+
418
+ :param numBytes: The number of bytes to read.
419
+ :type numBytes: int
420
+
421
+ :return: A list of bytes from the device.
422
+ :rtype: list
423
+ """
424
+ self ._i2c .writeCommand (self .address , reg )
425
+
426
+ return list (self ._i2c .readBlock (self .address , reg , numBytes ))
0 commit comments