Skip to content

Commit 8784b04

Browse files
authored
Merge pull request #10 from rkaczorek/fixes
Fixes
2 parents ebee935 + 4bcf73b commit 8784b04

File tree

2 files changed

+73
-63
lines changed

2 files changed

+73
-63
lines changed

src/SparkFunMLX90614.cpp

+24-24
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ IRTherm::IRTherm()
3131
uint8_t IRTherm::begin(uint8_t address)
3232
{
3333
_deviceAddress = address; // Store the address in a private member
34-
34+
3535
Wire.begin(); // Initialize I2C
3636
//! TODO: read a register, return success only if the register
3737
//! produced a known-good value.
@@ -104,7 +104,7 @@ uint8_t IRTherm::readObject()
104104
_rawObject = rawObj;
105105
return 1;
106106
}
107-
return 0;
107+
return 0;
108108
}
109109

110110
uint8_t IRTherm::readObject2()
@@ -122,7 +122,7 @@ uint8_t IRTherm::readObject2()
122122
_rawObject2 = rawObj;
123123
return 1;
124124
}
125-
return 0;
125+
return 0;
126126
}
127127

128128
uint8_t IRTherm::readAmbient()
@@ -174,7 +174,7 @@ uint8_t IRTherm::setMin(float minTemp)
174174
{
175175
// Convert the unit-ed value to a raw ADC value:
176176
int16_t rawMin = calcRawTemp(minTemp);
177-
// Write that value to the TOMIN EEPROM address:
177+
// Write that value to the TOMIN EEPROM address:
178178
return writeEEPROM(MLX90614_REGISTER_TOMIN, rawMin);
179179
}
180180

@@ -226,15 +226,15 @@ uint8_t IRTherm::setAddress(uint8_t newAdd)
226226
{
227227
tempAdd &= 0xFF00; // Mask out the address (MSB is junk?)
228228
tempAdd |= newAdd; // Add the new address
229-
229+
230230
// Write the new addres back to EEPROM:
231231
return writeEEPROM(MLX90614_REGISTER_ADDRESS, tempAdd);
232-
}
232+
}
233233
return 0;
234234
}
235235

236236
uint8_t IRTherm::readID(void)
237-
{
237+
{
238238
for (int i=0; i<4; i++)
239239
{
240240
int16_t temp = 0;
@@ -265,13 +265,13 @@ uint8_t IRTherm::sleep(void)
265265
// Bits sent: _deviceAddress (shifted left 1) + 0xFF
266266
uint8_t crc = crc8(0, (_deviceAddress << 1));
267267
crc = crc8(crc, MLX90614_REGISTER_SLEEP);
268-
268+
269269
// Manually send the sleep command:
270270
Wire.beginTransmission(_deviceAddress);
271271
Wire.write(MLX90614_REGISTER_SLEEP);
272272
Wire.write(crc);
273273
Wire.endTransmission(true);
274-
274+
275275
// Set the SCL pin LOW, and SDA pin HIGH (should be pulled up)
276276
pinMode(SCL, OUTPUT);
277277
digitalWrite(SCL, LOW);
@@ -281,7 +281,7 @@ uint8_t IRTherm::sleep(void)
281281
uint8_t IRTherm::wake(void)
282282
{
283283
// Wake operation from datasheet
284-
Wire.end(); // stop i2c bus to send wake up request via digital pins
284+
Wire.endTransmission(true); // stop i2c bus transmission BEFORE sending wake up request
285285
pinMode(SCL, INPUT); // SCL high
286286
pinMode(SDA, OUTPUT);
287287
digitalWrite(SDA, LOW); // SDA low
@@ -293,13 +293,13 @@ uint8_t IRTherm::wake(void)
293293
digitalWrite(SCL, LOW); // SCL low
294294
delay(10); // Delay at least 1.44ms
295295
pinMode(SCL, INPUT); // SCL high
296-
Wire.begin();
296+
Wire.beginTransmission(_deviceAddress); // reactivate i2c bus transmission AFTER sending wake up request
297297
}
298298

299299
int16_t IRTherm::calcRawTemp(float calcTemp)
300300
{
301301
int16_t rawTemp; // Value to eventually be returned
302-
302+
303303
if (_defaultUnit == TEMP_RAW)
304304
{
305305
// If unit is set to raw, just return that:
@@ -332,7 +332,7 @@ int16_t IRTherm::calcRawTemp(float calcTemp)
332332
float IRTherm::calcTemperature(int16_t rawTemp)
333333
{
334334
float retTemp;
335-
335+
336336
if (_defaultUnit == TEMP_RAW)
337337
{
338338
retTemp = (float) rawTemp;
@@ -349,28 +349,28 @@ float IRTherm::calcTemperature(int16_t rawTemp)
349349
}
350350
}
351351
}
352-
352+
353353
return retTemp;
354354
}
355355

356356
uint8_t IRTherm::I2CReadWord(byte reg, int16_t * dest)
357357
{
358358
Wire.beginTransmission(_deviceAddress);
359359
Wire.write(reg);
360-
360+
361361
Wire.endTransmission(false); // Send restart
362362
Wire.requestFrom(_deviceAddress, (uint8_t) 3);
363-
363+
364364
uint8_t lsb = Wire.read();
365365
uint8_t msb = Wire.read();
366366
uint8_t pec = Wire.read();
367-
367+
368368
uint8_t crc = crc8(0, (_deviceAddress << 1));
369369
crc = crc8(crc, reg);
370370
crc = crc8(crc, (_deviceAddress << 1) + 1);
371371
crc = crc8(crc, lsb);
372372
crc = crc8(crc, msb);
373-
373+
374374
if (crc == pec)
375375
{
376376
*dest = (msb << 8) | lsb;
@@ -383,32 +383,32 @@ uint8_t IRTherm::I2CReadWord(byte reg, int16_t * dest)
383383
}
384384

385385
uint8_t IRTherm::writeEEPROM(byte reg, int16_t data)
386-
{
386+
{
387387
// Clear out EEPROM first:
388388
if (I2CWriteWord(reg, 0) != 0)
389389
return 0; // If the write failed, return 0
390390
delay(5); // Delay tErase
391-
391+
392392
uint8_t i2cRet = I2CWriteWord(reg, data);
393393
delay(5); // Delay tWrite
394-
394+
395395
if (i2cRet == 0)
396396
return 1;
397397
else
398-
return 0;
398+
return 0;
399399
}
400400

401401
uint8_t IRTherm::I2CWriteWord(byte reg, int16_t data)
402402
{
403403
uint8_t crc;
404404
uint8_t lsb = data & 0x00FF;
405405
uint8_t msb = (data >> 8);
406-
406+
407407
crc = crc8(0, (_deviceAddress << 1));
408408
crc = crc8(crc, reg);
409409
crc = crc8(crc, lsb);
410410
crc = crc8(crc, msb);
411-
411+
412412
Wire.beginTransmission(_deviceAddress);
413413
Wire.write(reg);
414414
Wire.write(lsb);

src/SparkFunMLX90614.h

+49-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/******************************************************************************
1+
/******************************************************************************
22
SparkFunMLX90614.h
33
Header file for the SparkFun IR Thermometer Library
44
@@ -20,6 +20,16 @@ SparkFun IR Thermometer Evaluation Board - MLX90614
2020
#include <Arduino.h>
2121
#include <Wire.h>
2222

23+
///////////////////////////////////////////
24+
// Default I2C PIN for non Atmega Boards //
25+
///////////////////////////////////////////
26+
#ifndef SDA
27+
#define SDA (digitalPinToPinName(PIN_WIRE_SDA))
28+
#endif
29+
#ifndef SCL
30+
#define SCL (digitalPinToPinName(PIN_WIRE_SCL))
31+
#endif
32+
2333
//////////////////////////////////
2434
// MLX90614 Default I2C Address //
2535
//////////////////////////////////
@@ -53,134 +63,134 @@ typedef enum {
5363
TEMP_F
5464
} temperature_units;
5565

56-
class IRTherm
66+
class IRTherm
5767
{
5868
public:
5969
// Default constructor, does very little besides setting class variable
6070
// initial values.
6171
IRTherm();
62-
63-
// begin(<address>) initializes the Wire library, and prepares
64-
// communication with an MLX90614 device at the specified 7-bit I2C
72+
73+
// begin(<address>) initializes the Wire library, and prepares
74+
// communication with an MLX90614 device at the specified 7-bit I2C
6575
// address.
6676
// If no parameter is supplied, the default MLX90614 address is used.
6777
uint8_t begin(uint8_t address = MLX90614_DEFAULT_ADDRESS);
68-
69-
// setUnit(<unit>) configures the units returned by the ambient(),
70-
// object(), minimum() and maximum() functions, and it determines what
78+
79+
// setUnit(<unit>) configures the units returned by the ambient(),
80+
// object(), minimum() and maximum() functions, and it determines what
7181
// units the setMin() and setMax() functions should expect.
7282
// <unit> can be either:
7383
// - TEMP_RAW: No conversion, just the raw 12-bit ADC reading
7484
// - TEMP_K: Kelvin
7585
// - TEMP_C: Celsius
7686
// - TEMP_F: Farenheit
7787
void setUnit(temperature_units unit);
78-
79-
// read() pulls the latest ambient and object temperatures from the
80-
// MLX90614. It will return either 1 on success or 0 on failure. (Failure
88+
89+
// read() pulls the latest ambient and object temperatures from the
90+
// MLX90614. It will return either 1 on success or 0 on failure. (Failure
8191
// can result from either a timed out I2C transmission, or an incorrect
8292
// checksum value).
8393
uint8_t read(void);
84-
85-
// object() returns the MLX90614's most recently read object temperature
94+
95+
// object() returns the MLX90614's most recently read object temperature
8696
// after the read() function has returned successfully. The float value
8797
// returned will be in the units specified by setUnit().
8898
float object(void);
89-
90-
// ambient() returns the MLX90614's most recently read ambient temperature
99+
100+
// ambient() returns the MLX90614's most recently read ambient temperature
91101
// after the read() function has returned successfully. The float value
92102
// returned will be in the units specified by setUnit().
93103
float ambient(void);
94-
95-
// readEmissivity() reads the MLX90614's emissivity setting. It will
104+
105+
// readEmissivity() reads the MLX90614's emissivity setting. It will
96106
// return a value between 0.1 and 1.0.
97107
float readEmissivity(void);
98-
99-
// setEmissivity(<emis>) can set the MLX90614's configured emissivity
108+
109+
// setEmissivity(<emis>) can set the MLX90614's configured emissivity
100110
// EEPROM value.
101111
// The <emis> parameter should be a value between 0.1 and 1.0.
102112
// The function will return either 1 on success or 0 on failure.
103113
uint8_t setEmissivity(float emis);
104-
114+
105115
// readAddress() returns the MLX90614's configured 7-bit I2C bus address.
106116
// A value between 0x01 and 0x7F should be returned.
107117
uint8_t readAddress(void);
108-
118+
109119
// setAddress(<newAdd>) can set the MLX90614's 7-bit I2C bus address.
110120
// The <newAdd> parameter should be a value between 0x01 and 0x7F.
111121
// The function returns 1 on success and 0 on failure.
112122
// The new address won't take effect until the device is reset.
113123
uint8_t setAddress(uint8_t newAdd);
114-
124+
115125
// readID() reads the 64-bit ID of the MLX90614.
116126
// Return value is either 1 on success or 0 on failure.
117127
uint8_t readID(void);
118-
128+
119129
// After calling readID() getIDH() and getIDL() can be called to read
120130
// the upper 4 bytes and lower 4-bytes, respectively, of the MLX90614's
121131
// identification registers.
122132
uint32_t getIDH(void);
123133
uint32_t getIDL(void);
124-
134+
125135
// readRange() pulls the object maximum and minimum values stored in the
126136
// MLX90614's EEPROM.
127137
// It will return either 1 on success or 0 on failure.
128138
uint8_t readRange(void);
129-
139+
130140
// minimum() and maximum() return the MLX90614's minimum and maximum object
131141
// sensor readings.
132142
// The float values returned will be in the units specified by setUnit().
133143
float minimum(void);
134144
float maximum(void);
135-
136-
// setMax(<maxTemp>) and setMin(<minTemp>) configure the MLX90614's
145+
146+
// setMax(<maxTemp>) and setMin(<minTemp>) configure the MLX90614's
137147
// maximum and minimum object sensor temperatures.
138148
uint8_t setMax(float maxTemp);
139149
uint8_t setMin(float minTemp);
140-
150+
141151
// sleep() sets the MLX90614 into a low-power sleep mode.
142152
uint8_t sleep(void);
143-
153+
144154
// wake() should revive the MLX90614 from low-power sleep mode.
145155
uint8_t wake(void);
146-
156+
147157
private:
148158
uint8_t _deviceAddress; // MLX90614's 7-bit I2C address
149159
temperature_units _defaultUnit; // Keeps track of configured temperature unit
150-
160+
151161
// These keep track of the raw temperature values read from the sensor:
152162
int16_t _rawAmbient, _rawObject, _rawObject2, _rawMax, _rawMin;
153-
163+
154164
uint16_t id[4]; // Keeps track of the 64-bit ID value
155-
165+
156166
// These functions individually read the object, object2, and ambient
157167
// temperature values from the MLX90614's RAM:
158168
uint8_t readObject(void);
159169
uint8_t readObject2(void);
160170
uint8_t readAmbient(void);
161-
171+
162172
// These functions individually read the min and mx temperatures in
163173
// the MLX90614's EEPROM:
164174
uint8_t readMax(void);
165175
uint8_t readMin(void);
166-
176+
167177
// calcTemperature converts a raw ADC temperature reading to the
168178
// set unit.
169179
float calcTemperature(int16_t rawTemp);
170-
180+
171181
// calcRawTemperature converts a set unit temperature to a
172182
// raw ADC value:
173183
int16_t calcRawTemp(float calcTemp);
174-
184+
175185
// Abstract function to write 16-bits to an address in the MLX90614's
176186
// EEPROM
177187
uint8_t writeEEPROM(byte reg, int16_t data);
178-
188+
179189
// Abstract functions to read and write 16-bit values from a RAM
180190
// or EEPROM address in the MLX90614
181191
uint8_t I2CReadWord(byte reg, int16_t * dest);
182192
uint8_t I2CWriteWord(byte reg, int16_t data);
183-
193+
184194
// crc8 returns a calculated crc value given an initial value and
185195
// input data.
186196
// It's configured to calculate the CRC using a x^8+x^2+x^1+1 poly

0 commit comments

Comments
 (0)