Skip to content

Commit b4a1f7b

Browse files
add fixes for micropython. Add packages.json for mip packaging
1 parent 3286344 commit b4a1f7b

File tree

4 files changed

+70
-46
lines changed

4 files changed

+70
-46
lines changed

examples/Example1-GetTemperature.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
# Example 1
4040
#
4141

42-
from __future__ import print_function
4342
import qwiic_tmp102
4443
import time
4544
import sys

examples/Example2_One-Shot_Temperature_Reading.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
# Distributed as-is; no warranty is given.
2020
#-----------------------------------------------------------------------------
2121

22-
from __future__ import print_function
2322
import qwiic_tmp102
2423
import time
2524
import sys

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"urls": [
3+
["qwiic_tmp102.py", "github:sparkfun/Qwiic_TMP102_Py/qwiic_tmp102.py"]
4+
],
5+
"deps": [
6+
["github:sparkfun/Qwiic_I2C_Py", "master"]
7+
],
8+
"version": "0.0.4"
9+
}

qwiic_tmp102.py

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
New to qwiic? Take a look at the entire [SparkFun qwiic ecosystem](https://www.sparkfun.com/qwiic).
1111
1212
"""
13-
from __future__ import print_function, division
14-
1513
import qwiic_i2c
1614

1715
#======================================================================
@@ -119,10 +117,10 @@ def read_temp_c(self):
119117
Reads the results from the sensor
120118
:rtype: integer
121119
"""
122-
data = self._i2c.readBlock(self.address, TEMPERATURE_REGISTER, 2)
120+
data = self.read_block_pointer_reg(TEMPERATURE_REGISTER)
123121

124122
if (data[0] == 0xFF and data[1] == 0xFF):
125-
return NaN
123+
return None
126124

127125
if(data[1]&0x01): # 13 bit mode
128126
baseRead = ((data[0]) << 5) | (data[1] >> 3)
@@ -151,14 +149,14 @@ def read_temp_f(self):
151149

152150
def set_conversion_rate(self, rate):
153151
"""
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
159157
"""
160158

161-
configByte = self._i2c.readBlock(self.address, CONFIG_REGISTER, 2)
159+
configByte = self.read_block_pointer_reg(CONFIG_REGISTER)
162160
rate = rate&0x03
163161

164162
# Load new conversion rate
@@ -170,10 +168,10 @@ def set_conversion_rate(self, rate):
170168
def set_extended_mode(self, mode):
171169
"""
172170
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)
175173
"""
176-
configByte = self._i2c.readBlock(self.address, CONFIG_REGISTER, 2)
174+
configByte = self.read_block_pointer_reg(CONFIG_REGISTER)
177175

178176
# Load new value for extention mode
179177
configByte[1] &= 0xEF # Clear EM (bit 4 of second byte)
@@ -185,15 +183,15 @@ def sleep(self):
185183
"""
186184
Switch sensor to low power mode
187185
"""
188-
sleepValue = self._i2c.readByte(self.address, CONFIG_REGISTER)
186+
sleepValue = self.read_block_pointer_reg(CONFIG_REGISTER)[0]
189187
sleepValue |= 0x01 # Set SD (bit 0 of first byte)
190188
self._i2c.writeByte(self.address, CONFIG_REGISTER, sleepValue)
191189

192190
def wakeup(self):
193191
"""
194192
Wakeup and start running in normal power mode
195193
"""
196-
wakeValue = self._i2c.readByte(self.address, CONFIG_REGISTER)
194+
wakeValue = self.read_block_pointer_reg(CONFIG_REGISTER)[0]
197195
wakeValue &= 0xFE # Clear SD (bit 0 of first byte)
198196
self._i2c.writeByte(self.address, CONFIG_REGISTER, wakeValue)
199197

@@ -203,7 +201,7 @@ def set_alert_polarity(self, polarity):
203201
0 - Active LOW
204202
1 - Active HIGH
205203
"""
206-
configByte = self._i2c.readByte(self.address, CONFIG_REGISTER)
204+
configByte = self.read_block_pointer_reg(CONFIG_REGISTER)[0]
207205

208206
# Load new value for polarity
209207
configByte &= 0xFB # Clear POL (bit 2 of registerByte)
@@ -215,15 +213,15 @@ def alert(self):
215213
"""
216214
Returns state of Alert register
217215
"""
218-
alert = self._i2c.readByte(self.address, CONFIG_REGISTER)
216+
alert = self.read_block_pointer_reg(CONFIG_REGISTER)[1]
219217
alert &= 0x20 #Clear everything but the alert bit (bit 5)
220218
return alert>>5
221219

222220
def one_shot(self, setOneShot = 0):
223221
"""
224222
Sets the SingleShot Register. Returns 1 after the conversion is complete
225223
"""
226-
registerByte = self._i2c.readByte(self.address, CONFIG_REGISTER)
224+
registerByte = self.read_block_pointer_reg(CONFIG_REGISTER)[0]
227225
if(setOneShot == 1):
228226
registerByte |= (1<<7)
229227
self._i2c.writeByte(self.address, CONFIG_REGISTER, registerByte)
@@ -242,7 +240,7 @@ def set_low_temp_c(self, temperature):
242240
if(temperature < -55.0):
243241
temperature = -55.0
244242

245-
registerByte = self._i2c.readBlock(self.address, CONFIG_REGISTER, 2)
243+
registerByte = self.read_block_pointer_reg(CONFIG_REGISTER)
246244

247245
#Check if temperature should be 12 or 13 bits
248246
# 0 - temp data will be 12 bits
@@ -252,12 +250,13 @@ def set_low_temp_c(self, temperature):
252250
#Convert analog temperature to digital value
253251
temperature = temperature/0.0625
254252

253+
# Align data for the temperature registers (see pg. 19 of datasheet)
255254
if(extendedMode): #13-bit mode
256255
registerByte[0] = int(temperature)>>5
257-
registerByte[1] = (int(temperature)<<3)
256+
registerByte[1] = (int(temperature) & 0x1F)<<3 # lower 5 bits
258257
else:
259258
registerByte[0] = int(temperature)>>4
260-
registerByte[1] = int(temperature)<<4
259+
registerByte[1] = (int(temperature) & 0xF)<<4 # lower 4 bits
261260

262261
self._i2c.writeBlock(self.address, T_LOW_REGISTER, registerByte)
263262

@@ -270,7 +269,7 @@ def set_high_temp_c(self, temperature):
270269
temperature = 150.0
271270
if(temperature < -55.0):
272271
temperature = -55.0
273-
registerByte = self._i2c.readBlock(self.address, CONFIG_REGISTER, 2)
272+
registerByte = self.read_block_pointer_reg(CONFIG_REGISTER)
274273

275274
#Check if temperature should be 12 or 13 bits
276275
# 0 - temp data will be 12 bits
@@ -279,13 +278,14 @@ def set_high_temp_c(self, temperature):
279278

280279
#Convert analog temperature to digital value
281280
temperature = temperature/0.0625
282-
281+
282+
# Align data for the temperature registers (see pg. 19 of datasheet)
283283
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
286286
else:
287287
registerByte[0] = int(temperature)>>4
288-
registerByte[1] = int(temperature)<<4
288+
registerByte[1] = (int(temperature) & 0xF)<<4 # lower 4 bits
289289

290290
self._i2c.writeBlock(self.address, T_HIGH_REGISTER, registerByte)
291291

@@ -309,16 +309,16 @@ def read_low_temp_c(self):
309309
"""
310310
Gets T_LOW (degrees C) alert threshold
311311
"""
312-
configByte = self._i2c.readBlock(self.address, CONFIG_REGISTER, 2)
312+
configByte = self.read_block_pointer_reg(CONFIG_REGISTER)
313313

314314
# 0 - temp data will be 12 bits
315315
# 1 - temp data will be 13 bits
316316
extendedMode = (configByte[1]&0x10)>>4
317317

318-
lowTempByte = self._i2c.readBlock(self.address, T_LOW_REGISTER, 2)
318+
lowTempByte = self.read_block_pointer_reg(T_LOW_REGISTER)
319319

320320
if(lowTempByte[0] == 0xFF and lowTempByte[1] == 0xFF):
321-
return NAN
321+
return None
322322

323323
if (extendedMode):
324324
digitalTemp = ((lowTempByte[0]) << 5) | (lowTempByte[1] >> 3)
@@ -336,16 +336,16 @@ def read_high_temp_c(self):
336336
"""
337337
Gets T_HIGH (degrees C) alert threshold
338338
"""
339-
configByte = self._i2c.readBlock(self.address, CONFIG_REGISTER, 2)
339+
configByte = self.read_block_pointer_reg(CONFIG_REGISTER)
340340

341341
# 0 - temp data will be 12 bits
342342
# 1 - temp data will be 13 bits
343343
extendedMode = (configByte[1]&0x10)>>4
344344

345-
highTempByte = self._i2c.readBlock(self.address, T_HIGH_REGISTER, 2)
345+
highTempByte = self.read_block_pointer_reg(T_HIGH_REGISTER)
346346

347347
if(highTempByte[0] == 0xFF and highTempByte[1] == 0xFF):
348-
return NAN
348+
return None
349349

350350
if (extendedMode):
351351
digitalTemp = ((highTempByte[0]) << 5) | (highTempByte[1] >> 3)
@@ -375,15 +375,15 @@ def read_high_temp_f(self):
375375

376376
def set_fault(self, faultSetting):
377377
"""
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
383383
"""
384384
faultSetting = faultSetting&3 #Make sure rate is not set higher than 3.
385385

386-
configByte = self._i2c.readByte(self.address, CONFIG_REGISTER)
386+
configByte = self.read_block_pointer_reg(CONFIG_REGISTER)[0]
387387

388388
#Load new conversion rate
389389
configByte &= 0xE7 # Clear F0/1 (bit 3 and 4 of first byte)
@@ -393,17 +393,34 @@ def set_fault(self, faultSetting):
393393

394394
def set_alert_mode(self, mode):
395395
"""
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
400399
"""
401-
configByte = self._i2c.readByte(self.address, CONFIG_REGISTER)
400+
configByte = self.read_block_pointer_reg(CONFIG_REGISTER)[0]
402401

403402
#Load new conversion rate
404403
configByte &= 0xFD # Clear old TM bit (bit 1 of first byte)
405404
configByte |= mode<<1 # Shift in new TM bit
406405

407406
self._i2c.writeByte(self.address, CONFIG_REGISTER, configByte)
408407

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

Comments
 (0)