-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsd305lib.cpp
More file actions
210 lines (187 loc) · 5.19 KB
/
tsd305lib.cpp
File metadata and controls
210 lines (187 loc) · 5.19 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
/*
* Description : Source file of TSD305 Sensor (by TEConnectivity) for Arduino platform.
* Author : Pranjal Joshi
* Date : 25/05/2020
* License : MIT
* This code is published as open source software. Feel free to share/modify.
*/
#include "tsd305lib.h"
#include <Wire.h>
#if (defined(AVR))
#include <avr/pgmspace.h>
#else
#include <pgmspace.h>
#endif
#define TSD_ADDR (uint8_t)0x1E
#define TSD_ADC_CMD (uint8_t)0xAF
#define CONV_DLY 60
bool DEBUG = false;
tsd305::tsd305(void) {
// Constructor
}
tsd_eeprom_struct tsd305::begin(void) {
Wire.begin();
DEBUG = false;
return tsdReadEeprom();
}
tsd_eeprom_struct tsd305::begin(bool debug) {
Wire.begin();
if(debug) {
DEBUG = true;
Serial.println();
}
return tsdReadEeprom();
}
bool tsd305::isConnected(void) {
Wire.beginTransmission(TSD_ADDR);
if(Wire.endTransmission() == 0) {
if(DEBUG)
Serial.println("TSD305 Connected on I2C bus.");
return true;
}
if(DEBUG)
Serial.println("TSD305 Not Connected.");
return false;
}
// Read EEPROM addr - TYPECASTING req for int16_t
uint16_t tsd305::tsdReadCoef(uint8_t addr) {
uint8_t buf[3];
Wire.beginTransmission(TSD_ADDR);
Wire.write(addr);
Wire.endTransmission();
delay(1);
Wire.requestFrom(TSD_ADDR, 3U);
for(byte i=0;i<3;i++) {
buf[i] = Wire.read();
}
return ((buf[1] << 8) | buf[2]);
}
// Get float value from TSD EEPROM
float tsd305::tsdReadFloat(uint8_t addr) {
uint16_t hw,lw;
uint32_t w;
hw = tsdReadCoef(addr);
lw = tsdReadCoef(addr+1);
w = (uint32_t)(hw << 16 | lw);
float y = *(float*)&w;
return y;
}
tsd_eeprom_struct tsd305::tsdReadEeprom() {
tsd_eeprom_struct s;
s.amb_min = (int16_t)tsdReadCoef(0x1A);
s.amb_max = (int16_t)tsdReadCoef(0x1B);
s.obj_min = (int16_t)tsdReadCoef(0x1C);
s.obj_max = (int16_t)tsdReadCoef(0x1D);
s.adc_cal = (int16_t)tsdReadCoef(0x2C);
if(DEBUG) {
Serial.println("Reading TSD305 EEPROM Parameters:");
Serial.print("[+] amb_min: ");
Serial.println(s.amb_min,DEC);
Serial.print("[+] amb_max: ");
Serial.println(s.amb_max,DEC);
Serial.print("[+] obj_min: ");
Serial.println(s.obj_min,DEC);
Serial.print("[+] obj_max: ");
Serial.println(s.obj_max,DEC);
Serial.print("[+] adc_cal: ");
Serial.println(s.adc_cal,DEC);
}
return s;
}
void tsd305::tsdReadADCs(uint32_t *amb, uint32_t *obj) {
uint8_t buf[7], i;
Wire.beginTransmission(TSD_ADDR);
Wire.write(TSD_ADC_CMD);
if(Wire.endTransmission() != 0) {
if(DEBUG)
Serial.println("Failed to start TSD305 ADC conversion.");
}
delay(CONV_DLY);
Wire.requestFrom(TSD_ADDR, 7U);
for(i=0;i<7;i++)
buf[i] = Wire.read();
*obj = ((uint32_t)buf[1] << 16) | ((uint32_t)buf[2] << 8) | (uint32_t)buf[3];
*amb = ((uint32_t)buf[4] << 16) | ((uint32_t)buf[5] << 8) | (uint32_t)buf[6];
if(DEBUG) {
Serial.print("Ambient ADC Value (24-Bit): ");
Serial.println(*amb);
Serial.print("Object ADC Value (24-Bit): ");
Serial.println(*obj);
}
}
float tsd305::getSensorTemp(void) {
uint32_t adc_amb, adc_obj;
tsd_eeprom_struct t;
tsdReadADCs(&adc_amb, &adc_obj);
t = tsdReadEeprom();
float ts = ((adc_amb/(pow(2,24)))*(t.amb_max - t.amb_min) + t.amb_min);
if(DEBUG) {
Serial.print("Sensor Temp (°C): ");
Serial.println(ts);
}
return (float)ts;
}
float tsd305::getTCF(void) {
float tsens = getSensorTemp();
float tc, tref, tcf;
tc = tsdReadFloat(0x1E);
tref = tsdReadFloat(0x20);
tcf = 1 + ((tsens-tref)*tc);
#if DEBUG
Serial.print("TC Correction Factor: ");
Serial.println(tcf);
#endif
return tcf;
}
void tsd305::tsdGetTempCompensation(float *offset, float *offsettc) {
float tsens = getSensorTemp();
float k4,k3,k2,k1,k0;
k4 = tsdReadFloat(0x22);
k3 = tsdReadFloat(0x24);
k2 = tsdReadFloat(0x26);
k1 = tsdReadFloat(0x28);
k0 = tsdReadFloat(0x2A);
*offset = (k4*pow(tsens,4))
+ (k3*pow(tsens,3))
+ (k2*pow(tsens,2))
+ (k1*tsens)
+ k0;
*offsettc = *offset * getTCF();
if(DEBUG) {
Serial.println("Temperature Compensation:");
Serial.print("[+] Offset: ");
Serial.println(*offset);
Serial.print("[+] Offset-TC: ");
Serial.println(*offsettc);
}
}
float tsd305::getObjectTemp(void) {
float tk4,tk3,tk2,tk1,tk0;
float adc_comp, adc_comptc;
float tempOut;
float tsens = getSensorTemp();
float offset, offsettc;
tsdGetTempCompensation(&offset, &offsettc);
uint32_t adc_obj, adc_amb;
tsdReadADCs(&adc_amb, &adc_obj);
tk4 = tsdReadFloat(0x2E);
tk3 = tsdReadFloat(0x30);
tk2 = tsdReadFloat(0x32);
tk1 = tsdReadFloat(0x34);
tk0 = tsdReadFloat(0x36);
adc_comp = offsettc + adc_obj - pow(2,23);
adc_comptc = adc_comp / getTCF();
tempOut = tk4*pow(adc_comptc,4)
+ tk3*pow(adc_comptc,3)
+ tk2*pow(adc_comptc,2)
+ tk1*adc_comptc
+ tk0;
if(DEBUG) {
Serial.print("Object Temperature (°C): ");
Serial.println(tempOut);
}
return tempOut;
}
float tsd305::DtoF(float temp) {
return (temp*9/5)+32.0;
}