forked from DFRobot/DFRobot_MLX90614
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFRobot_MLX90614.cpp
More file actions
456 lines (383 loc) · 11.6 KB
/
DFRobot_MLX90614.cpp
File metadata and controls
456 lines (383 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*!
* @file DFRobot_MLX90614.cpp
* @brief DFRobot_MLX90614.cpp Initialize the I2C,
* @n get the celsius temperature and fahrenheit temperature values,get the register values.
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [qsjhyy](yihuan.huang@dfrobot.com)
* @version V1.0
* @date 2021-07-29
* @url https://github.com/DFRobot/DFRobot_MLX90614
*/
#include "DFRobot_MLX90614.h"
DFRobot_MLX90614::DFRobot_MLX90614()
{
}
int DFRobot_MLX90614::begin(void)
{
uint8_t idBuf[2];
if (0 == readReg(MLX90614_ID_NUMBER, idBuf)) { // Judge whether the data bus is successful
DBG("ERR_DATA_BUS");
return ERR_DATA_BUS;
}
DBG("real sensor id=");DBG((uint16_t)idBuf[0] | (uint16_t)(idBuf[1] << 8), HEX);
if (0 == ((uint16_t)idBuf[0] | (uint16_t)(idBuf[1] << 8))) { // Judge whether the chip version matches
DBG("ERR_IC_VERSION");
return ERR_IC_VERSION;
}
delay(200);
DBG("begin ok!");
return NO_ERR;
}
void DFRobot_MLX90614::setEmissivityCorrectionCoefficient(float calibrationValue, bool set0X0F)
{
if (calibrationValue > 1.0 || calibrationValue < 0.1) {
return;
}
uint16_t emissivity = round(65535 * calibrationValue);
DBG(emissivity, HEX);
uint16_t emissivity_reg = getEmissivityReg();
if (emissivity == emissivity_reg) {
DBG("Same emissivity to set");
return;
}
uint8_t buf[2] = { 0 }; // Avoid endianness
uint16_t curE = 0;
uint16_t data = 0;
if (set0X0F) {
readReg(MLX90614_EMISSIVITY, buf);
curE = TWO_BYTES_CONCAT(buf);
DBG(curE, HEX);
readReg(MLX90614_FOR_EMISSIVITY, buf);
data = TWO_BYTES_CONCAT(buf);
DBG(data, HEX);
// https://github.com/melexis/mlx90614-library/blob/6ba8c8919db9512b6f92d99378386ae4ae822954/functions/MLX90614_API.cpp#L183
data = round(((float)data / emissivity * curE));
DBG(data, HEX);
if (data > 0x7FFF) {
return;
}
sendCommand(0x60); // unlock key
// sendCommand(0x61); // lock key
}
memset(buf, 0, sizeof(buf));
writeReg(MLX90614_EMISSIVITY, buf); // 0x04
delay(10);
buf[0] = (emissivity & 0x00FF);
buf[1] = ((emissivity & 0xFF00) >> 8);
writeReg(MLX90614_EMISSIVITY, buf);
delay(10);
readReg(MLX90614_EMISSIVITY, buf);
DBG(TWO_BYTES_CONCAT(buf), HEX);
if (set0X0F) {
memset(buf, 0, sizeof(buf));
writeReg(MLX90614_FOR_EMISSIVITY, buf); // 0x0F
delay(10);
buf[0] = (data & 0x00FF);
buf[1] = ((data & 0xFF00) >> 8);
writeReg(MLX90614_FOR_EMISSIVITY, buf);
delay(10);
readReg(MLX90614_FOR_EMISSIVITY, buf);
DBG(TWO_BYTES_CONCAT(buf), HEX);
sendCommand(0x61); // lock key
}
}
float DFRobot_MLX90614::getEmissivityCorrectionCoefficient(void)
{
return ((float)getEmissivityReg())/65535.0;
}
uint16_t DFRobot_MLX90614::getEmissivityReg(void)
{
uint16_t emissivity = 0;
uint8_t buf[2] = { 0 };
readReg(MLX90614_EMISSIVITY, buf);
emissivity = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8));
DBG(emissivity, HEX);
delay(10);
return emissivity;
}
void DFRobot_MLX90614::setMeasuredParameters(eIIRMode_t IIRMode, eFIRMode_t FIRMode)
{
uint8_t buf[2] = { 0 };
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
if ( (buf[0]&0x07)==IIRMode && (buf[1]&0x07)==FIRMode ) {
DBG("Same filters to set; abort writing");
return;
}
//First write 0 in the EEPROM; see 8.3.3.1 in datasheet pg 17
uint8_t wbuf[2] = {0}; //write 16bits buffer with 0
writeReg(MLX90614_CONFIG_REG1, wbuf);
delay(10);
buf[0] = (buf[0] & 0xF8) | IIRMode;
buf[1] = (buf[1] & 0xF8) | FIRMode;
writeReg(MLX90614_CONFIG_REG1, buf);
delay(10);
}
uint16_t DFRobot_MLX90614::getConfigRegister1(void)
{
uint8_t buf[2] = {0};
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
return ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8));
}
uint8_t DFRobot_MLX90614::getFIRBits(void)
{
uint8_t buf[2] = {0};
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
return (buf[1]&0x07);
}
uint16_t DFRobot_MLX90614::getFIRLength(void)
{
uint16_t FIRLengths[]={8,16,32,64,128,256,512,1024};
return FIRLengths[getFIRBits()];
}
uint8_t DFRobot_MLX90614::getIIRBits(void) {
uint8_t buf[2] = {0};
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
return (buf[0]&0x07);
}
uint8_t DFRobot_MLX90614::getIIRSpikeLimit(void)
{
uint8_t SpikeLimits[]={50,25,17,13,100,80,67,57};
return SpikeLimits[getIIRBits()];
}
uint8_t DFRobot_MLX90614::getGainBits(void)
{
uint8_t buf[2] = {0};
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
return ((buf[1]>>3)&0x07);
}
uint8_t DFRobot_MLX90614::getGainValue(void)
{
uint8_t vGAIN[]={1,3,9,12,25,50,100,100};
return vGAIN[getGainBits()];
}
void DFRobot_MLX90614::setGainBits(eGAINMode_t GAINMode)
{
uint8_t gainbits = (uint8_t)GAINMode;
uint8_t buf[2] = {0};
DBG(gainbits);
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
if (gainbits != ((buf[1]>>3)&0x07)) {
uint8_t wbuf[2] = {0};
writeReg(MLX90614_CONFIG_REG1, wbuf);
delay(10);
buf[1] = (buf[1] & 0xC7) | (gainbits<<3);
DBG((uint16_t)buf[0] | (uint16_t)(buf[1] << 8),HEX)
writeReg(MLX90614_CONFIG_REG1, buf);
delay(10);
}
}
uint8_t DFRobot_MLX90614::setGainValue(uint8_t gainvalue) {
uint8_t vGAIN[]={1,3,9,12,25,50,100,100};
uint8_t gainbits=0;
while ((gainbits<8) && (gainvalue>=vGAIN[gainbits])) gainbits++;
if (gainbits) {
setGainBits((eGAINMode_t)(--gainbits));
return vGAIN[gainbits];
}
return 0;
}
float DFRobot_MLX90614::getAmbientTempCelsius(void)
{
uint8_t buf[2];
readReg(MLX90614_TA, buf);
float temp = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8)) * 0.02 - 273.15;
return temp; // Get celsius temperature of the ambient
}
uint16_t DFRobot_MLX90614::getAmbientTemp(void)
{
uint8_t buf[3];
readReg(MLX90614_TA, buf);
uint16_t temp = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8));
return temp; // Get raw temperature of the ambient
}
float DFRobot_MLX90614::getObjectTempCelsius(void)
{
uint8_t buf[2];
readReg(MLX90614_TOBJ1, buf);
// DBG((buf[0] | buf[1] << 8), HEX);
float temp = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8)) * 0.02 - 273.15;
return temp; // Get celsius temperature of the object
}
uint16_t DFRobot_MLX90614::getObjectTemp(void)
{
uint8_t buf[3];
readReg(MLX90614_TOBJ1, buf);
// DBG((buf[0] | buf[1] << 8), HEX);
uint16_t temp = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8));
return temp; // Get raw temperature of the object
}
float DFRobot_MLX90614::getObject2TempCelsius(void)
{
uint8_t buf[2];
readReg(MLX90614_TOBJ2, buf);
// DBG((buf[0] | buf[1] << 8), HEX);
float temp = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8)) * 0.02 - 273.15;
return temp; // Get celsius temperature of the object
}
uint8_t DFRobot_MLX90614::readModuleFlags(void)
{
uint8_t flagBuf[2], ret = 0;
readReg(MLX90614_FLAGS, flagBuf);
if (flagBuf[0] & (1 << 3)) {
ret |= 1;
DBG("Not implemented.");
}
if (!(flagBuf[0] & (1 << 4))) {
ret |= (1 << 1);
DBG("INIT - POR initialization routine is still ongoing. Low active.");
}
if (flagBuf[0] & (1 << 5)) {
ret |= (1 << 2);
DBG("EE_DEAD - EEPROM double error has occurred. High active.");
}
if (flagBuf[0] & (1 << 7)) {
ret |= (1 << 3);
DBG("EEBUSY - the previous write/erase EEPROM access is still in progress. High active.");
}
return ret;
}
/************ Initialization of I2C interfaces reading and writing ***********/
DFRobot_MLX90614_I2C::DFRobot_MLX90614_I2C(uint8_t i2cAddr, TwoWire* pWire)
{
_deviceAddr = i2cAddr;
_pWire = pWire;
}
int DFRobot_MLX90614_I2C::begin(void)
{
// _pWire->begin(); // Wire.h(I2C)library function initialize wire library
enterSleepMode(false);
delay(50);
return DFRobot_MLX90614::begin(); // Use the initialization function of the parent class
}
void DFRobot_MLX90614_I2C::enterSleepMode(bool mode)
{
if (mode) {
// sleep command, refer to the chip datasheet
_pWire->beginTransmission(_deviceAddr);
_pWire->write(MLX90614_SLEEP_MODE);
_pWire->write(MLX90614_SLEEP_MODE_PEC);
_pWire->endTransmission();
DBG("enter sleep mode");
} else {
#if defined(ESP32)
_pWire->~TwoWire();
#elif !defined(ESP8266)
_pWire->end();
#endif
// wake up command, refer to the chip datasheet
pinMode(SDA, OUTPUT);
pinMode(SCL, OUTPUT);
digitalWrite(SCL, LOW);
digitalWrite(SDA, HIGH);
delay(50);
digitalWrite(SCL, HIGH);
digitalWrite(SDA, LOW);
delay(50);
_pWire->begin(); // Wire.h(I2C)library function initialize wire library
#ifdef ESP8266
digitalWrite(SCL, LOW);
#endif
_pWire->beginTransmission(_deviceAddr);
_pWire->endTransmission();
DBG("exit sleep mode");
}
delay(200);
}
void DFRobot_MLX90614_I2C::setI2CAddress(uint8_t addr)
{
uint8_t buf[2] = { 0 };
writeReg(MLX90614_SMBUS_ADDR, buf);
delay(10);
buf[0] = addr;
writeReg(MLX90614_SMBUS_ADDR, buf);
delay(10);
}
unsigned char DFRobot_MLX90614_I2C::crc8Polyomial107(unsigned char* ptr, size_t len)
{
unsigned char i;
unsigned char crc = 0x00; // calculated initial crc value
while (len--) {
// DBG("*ptr, HEX");
// DBG(*ptr, HEX);
// first xor with the data to be calculated every time, point to the next data after completing the calculation
crc ^= *ptr++;
for (i = 8; i > 0; i--) {
// the following calculation is the same as calculating a byte crc
if (crc & 0x80) {
crc = (crc << 1) ^ 0x07;
} else {
crc = (crc << 1);
}
}
}
// DBG(crc, HEX);
return (crc);
}
void DFRobot_MLX90614_I2C::sendCommand(uint8_t cmd)
{
if (cmd != 0x60 && cmd != 0x61) {
return;
}
unsigned char crc_write[3] = { (uint8_t)(_deviceAddr << 1), cmd, '\0' }; // the array prepared for calculating the check code
_pWire->beginTransmission(_deviceAddr);
_pWire->write(cmd);
_pWire->write(crc8Polyomial107(crc_write, 2));
_pWire->endTransmission();
}
void DFRobot_MLX90614_I2C::writeReg(uint8_t reg, const void* pBuf)
{
if (pBuf == NULL) {
DBG("pBuf ERROR!! : null pointer");
}
uint8_t* _pBuf = (uint8_t*)pBuf;
unsigned char crc_write[5] = { (uint8_t)(_deviceAddr << 1), reg, _pBuf[0], _pBuf[1], '\0' }; // the array prepared for calculating the check code
_pWire->beginTransmission(_deviceAddr);
_pWire->write(reg);
for (size_t i = 0; i < 2; i++) {
_pWire->write(_pBuf[i]);
}
_pWire->write(crc8Polyomial107(crc_write, 4));
_pWire->endTransmission();
}
size_t DFRobot_MLX90614_I2C::readReg(uint8_t reg, void* pBuf)
{
size_t count = 0;
uint8_t pec = 0;
if (NULL == pBuf) {
DBG("pBuf ERROR!! : null pointer");
}
uint8_t* _pBuf = (uint8_t*)pBuf;
_pWire->beginTransmission(_deviceAddr);
_pWire->write(reg);
if (0 != _pWire->endTransmission(false)) {
// Used Wire.endTransmission() to end a slave transmission started by beginTransmission() and arranged by write().
DBG("endTransmission ERROR!!");
} else {
// Master device requests size bytes from slave device, which can be accepted by master device with read() or available()
_pWire->requestFrom(_deviceAddr, (uint8_t)3);
while (_pWire->available()) {
if (2 > count) { // The incoming buf is only two bytes, and crossing the boundary will cause an error in the previous buf data
_pBuf[count++] = _pWire->read(); // Use read() to receive and put into buf
} else {
pec = _pWire->read();
}
// DBG(_pBuf[count-1], HEX);
}
_pWire->endTransmission();
// the array prepared for calculating the check code
unsigned char crc_read[6] = { (uint8_t)(_deviceAddr << 1), reg, (uint8_t)((_deviceAddr << 1) | 1), _pBuf[0], _pBuf[1], '\0' };
if (pec != crc8Polyomial107(crc_read, 5)) {
count = 0;
DBG("crc8Polyomial107 ERROR!!");
DBG(pec, HEX);
}
}
return count;
}