-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathISL29023Module.cpp
More file actions
282 lines (249 loc) · 8.34 KB
/
ISL29023Module.cpp
File metadata and controls
282 lines (249 loc) · 8.34 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
/*
* ISL29023Module.cpp
*
* Created on: Feb 21, 2014
* Author: Saminda
*/
#include "ISL29023Module.h"
MAKE_MODULE(ISL29023Module)
#if defined(ENERGIA)
#include "Wire.h"
#endif
//*****************************************************************************
//
// Constants to hold the floating point version of the thresholds for each
// range setting. Numbers represent an 81% and 19 % threshold levels. This
// creates a +/- 1% hysteresis band between range adjustments.
//
//*****************************************************************************
const float g_fThresholdHigh[4] =
{ 810.0f, 3240.0f, 12960.0f, 64000.0f };
const float g_fThresholdLow[4] =
{ 0.0f, 760.0f, 3040.0f, 12160.0f };
//*****************************************************************************
//
// Range setting to floating point range value lookup table
//
//*****************************************************************************
const float g_fRangeLookup[4] =
{ 1000.0, 4000.0, 16000.0, 64000.0 };
//*****************************************************************************
//
// Resolution setting to floating point resolution value lookup table
//
//*****************************************************************************
const float g_fResolutionLookup[4] =
{ 65536.0, 4096.0, 256.0, 16.0 };
//*****************************************************************************
//
// Beta value lookup based on datasheet typical values for DATA_IR1, DATA_IR2,
// DATA_IR3, DATA_IR4. These should be reasonable for 16 bit conversions.
// However, Beta changes with resolution and background IR conditions.
//
//*****************************************************************************
const float g_fBetaLookup[4] =
{ 95.238, 23.810, 5.952, 1.486 };
void ISL29023Module::update(ISL29023Representation& theISL29023Representation)
{
#if defined(ENERGIA)
//
// Get the raw light data from the instance structure
//
I2CMRead();
uint16_t ui16Light = (parameters.pui8Data[1] << 8) | parameters.pui8Data[0];
//
// Get the floating point values for range and resolution from the lookup.
//
float fRange = g_fRangeLookup[parameters.ui8Range];
float fResolution = g_fResolutionLookup[parameters.ui8Resolution];
//
// Calculate light reading in lux.
//
theISL29023Representation.fAmbient = ((float) ui16Light) * (fRange / fResolution);
//
// Check if the intensity of light has crossed a threshold. If so
// then adjust range of sensor readings to track intensity.
//
ISL29023AppAdjustRange(theISL29023Representation);
//
// Print the temperature as integer and fraction parts.
//
//Serial.print("Visible Lux: ");
//Serial.println(theISL29023Representation.fAmbient, 3);
#endif
}
void ISL29023Module::init()
{
#if defined(ENERGIA)
parameters.ui8Range = ISL29023_CMD_II_RANGE_1K >> ISL29023_CMD_II_RANGE_S;
parameters.ui8Resolution = (ISL29023_CMD_II_ADC_RES_16 >> ISL29023_CMD_II_ADC_RES_S);
//
// Put the device into power down mode.
//
parameters.pui8Data[0] = ISL29023_O_CMD_I;
parameters.pui8Data[1] = ISL29023_CMD_I_OP_MODE_POWER_DOWN;
Wire.beginTransmission(parameters.ui8Addr);
for (int i = 0; i < 2; i++)
Wire.write(parameters.pui8Data[i]);
Wire.endTransmission();
//
// Configure the ISL29023 to measure ambient light continuously. Set a 8
// sample persistence before the INT pin is asserted. Clears the INT flag.
// Persistence setting of 8 is sufficient to ignore camera flashes.
//
ISL29023ReadModifyWrite(ISL29023_O_CMD_I,
~(ISL29023_CMD_I_OP_MODE_M | ISL29023_CMD_I_INT_PERSIST_M | ISL29023_CMD_I_INT_FLAG_M),
(ISL29023_CMD_I_OP_MODE_ALS_CONT | ISL29023_CMD_I_INT_PERSIST_8));
//
// Configure the upper threshold to 80% of maximum value
//
parameters.pui8Data[0] = 0xCC;
parameters.pui8Data[1] = 0xCC;
Wire.beginTransmission(parameters.ui8Addr);
Wire.write(ISL29023_O_INT_HT_LSB);
for (int i = 0; i < 2; i++)
Wire.write(parameters.pui8Data[i]);
Wire.endTransmission();
//
// Configure the lower threshold to 20% of maximum value
//
parameters.pui8Data[0] = 0x33;
parameters.pui8Data[1] = 0x33;
Wire.beginTransmission(parameters.ui8Addr);
Wire.write(ISL29023_O_INT_LT_LSB);
for (int i = 0; i < 2; i++)
Wire.write(parameters.pui8Data[i]);
Wire.endTransmission();
#endif
}
void ISL29023Module::I2CMRead()
{
#if defined(ENERGIA)
Wire.beginTransmission(parameters.ui8Addr);
Wire.write(ISL29023_O_DATA_OUT_LSB);
Wire.endTransmission(false);
Wire.requestFrom(parameters.ui8Addr, (uint8_t) 2);
while (Wire.available() < 1)
;
parameters.pui8Data[0] = Wire.read();
parameters.pui8Data[1] = Wire.read();
#endif
}
void ISL29023Module::ISL29023ReadModifyWrite(const uint_fast8_t& ui8Reg, const uint8_t& ui8Mask,
const uint8_t& ui8Value)
{
#if defined(ENERGIA)
//
// Configure the ISL29023 to measure ambient light continuously. Set a 8
// sample persistence before the INT pin is asserted. Clears the INT flag.
// Persistence setting of 8 is sufficient to ignore camera flashes.
//
parameters.ui8Reg = ui8Reg;
parameters.ui8Mask = ui8Mask;
parameters.ui8Value = ui8Value;
//
// Construct the I2C command to access the requested register.
//
parameters.pui8Data[0] = parameters.ui8Reg;
if (parameters.ui8Mask == 0)
{
//
// Set the new register value in the command buffer.
//
parameters.pui8Data[1] = parameters.ui8Value;
Wire.beginTransmission(parameters.ui8Addr);
for (int i = 0; i < 2; i++)
Wire.write(parameters.pui8Data[i]);
Wire.endTransmission();
}
else
{
Wire.beginTransmission(parameters.ui8Addr);
Wire.write(parameters.pui8Data[0]);
Wire.endTransmission(false);
Wire.requestFrom(parameters.ui8Addr, (uint8_t) 1);
while (Wire.available() == 0)
;
parameters.pui8Data[1] = Wire.read();
//
// The read portion of the read-modify-write has completed.
//
//
// Modify the register data that was just read.
//
parameters.pui8Data[1] = ((parameters.pui8Data[1] & parameters.ui8Mask) | parameters.ui8Value);
//
// Write the data back to the device.
//
Wire.beginTransmission(parameters.ui8Addr);
for (int i = 0; i < 2; i++)
Wire.write(parameters.pui8Data[i]);
Wire.endTransmission();
}
#endif
}
void ISL29023Module::ISL29023AppAdjustRange(ISL29023Representation& theISL29023Representation)
{
#if defined(ENERGIA)
bool isRangeChanged = false;
//
// Check if we crossed the upper threshold.
//
if (theISL29023Representation.fAmbient > g_fThresholdHigh[parameters.ui8Range])
{
//
// The current intensity is over our threshold so adjsut the range
// accordingly
//
if (parameters.ui8Range < ISL29023_CMD_II_RANGE_64K)
{
parameters.ui8Range = parameters.ui8Range + 1;
isRangeChanged = true;
}
}
//
// Check if we crossed the lower threshold
//
if (theISL29023Representation.fAmbient < g_fThresholdLow[parameters.ui8Range])
{
//
// If possible go to the next lower range setting and reconfig the
// thresholds.
//
if (parameters.ui8Range > ISL29023_CMD_II_RANGE_1K)
{
parameters.ui8Range = parameters.ui8Range - 1;
isRangeChanged = true;
}
}
//
// If the desired range value changed then send the new range to the sensor
//
if (isRangeChanged)
{
ISL29023ReadModifyWrite(ISL29023_O_CMD_II, ~ISL29023_CMD_II_RANGE_M, parameters.ui8Range);
if (parameters.pui8Data[0] == ISL29023_O_CMD_II)
{
//
// Store the latest range and resolution settings
//
parameters.ui8Range = ((parameters.pui8Data[1] & ISL29023_CMD_II_RANGE_M) >>
ISL29023_CMD_II_RANGE_S);
parameters.ui8Resolution = ((parameters.pui8Data[1] &
ISL29023_CMD_II_ADC_RES_M) >>
ISL29023_CMD_II_ADC_RES_S);
}
//
// Now we must manually clear the flag in the ISL29023
// register.
//
Wire.beginTransmission(parameters.ui8Addr);
Wire.write(ISL29023_O_CMD_I);
Wire.endTransmission(false);
Wire.requestFrom(parameters.ui8Addr, (uint8_t) 1);
while (Wire.available() == 0)
;
parameters.pui8Data[0] = Wire.read();
}
#endif
}