-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Max44009.cpp
246 lines (190 loc) · 4.79 KB
/
Max44009.cpp
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
//
// FILE: Max44009.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.6.0
// DATE: 2010-??-??
// PURPOSE: library for MAX44009 lux sensor Arduino
// URL: https://github.com/RobTillaart/MAX44009
#include "Max44009.h"
// MAX44009 KEY VALUES
#define MAX44009_MIN_LUX (0.045)
#define MAX44009_MAX_LUX (188006.0)
Max44009::Max44009(const uint8_t address, TwoWire *wire)
{
_address = address;
_data = 0;
_error = MAX44009_OK;
_wire = wire;
}
bool Max44009::isConnected()
{
_wire->beginTransmission(_address);
_error = _wire->endTransmission();
return (_error == 0);
}
uint8_t Max44009::getAddress()
{
return _address;
}
float Max44009::getLux(void)
{
uint8_t datahigh = read(MAX44009_LUX_READING_HIGH);
if (_error != MAX44009_OK)
{
_error = MAX44009_ERROR_HIGH_BYTE;
return _error;
}
uint8_t datalow = read(MAX44009_LUX_READING_LOW);
if (_error != MAX44009_OK)
{
_error = MAX44009_ERROR_LOW_BYTE;
return _error;
}
uint8_t exponent = datahigh >> 4;
if (exponent == 0x0F)
{
_error = MAX44009_ERROR_OVERFLOW;
return _error;
}
float lux = convertToLux(datahigh, datalow);
return lux;
}
int Max44009::getError()
{
int err = _error;
_error = MAX44009_OK;
return err;
}
bool Max44009::setHighThreshold(const float value)
{
return setThreshold(MAX44009_THRESHOLD_HIGH, value);
}
float Max44009::getHighThreshold(void)
{
return getThreshold(MAX44009_THRESHOLD_HIGH);
}
bool Max44009::setLowThreshold(const float value)
{
return setThreshold(MAX44009_THRESHOLD_LOW, value);
}
float Max44009::getLowThreshold(void)
{
return getThreshold(MAX44009_THRESHOLD_LOW);
}
void Max44009::setThresholdTimer(const uint8_t value)
{
write(MAX44009_THRESHOLD_TIMER, value);
}
uint8_t Max44009::getThresholdTimer()
{
return read(MAX44009_THRESHOLD_TIMER);
}
void Max44009::setConfiguration(const uint8_t value)
{
write(MAX44009_CONFIGURATION, value);
}
uint8_t Max44009::getConfiguration()
{
return read(MAX44009_CONFIGURATION);
}
void Max44009::setAutomaticMode()
{
// CDR and TIM cannot be written in automatic mode
uint8_t config = read(MAX44009_CONFIGURATION);
config &= ~MAX44009_CFG_MANUAL;
write(MAX44009_CONFIGURATION, config);
}
void Max44009::setContinuousMode()
{
uint8_t config = read(MAX44009_CONFIGURATION);
config |= MAX44009_CFG_CONTINUOUS;
write(MAX44009_CONFIGURATION, config);
}
void Max44009::clrContinuousMode()
{
uint8_t config = read(MAX44009_CONFIGURATION);
config &= ~MAX44009_CFG_CONTINUOUS;
write(MAX44009_CONFIGURATION, config);
}
void Max44009::setManualMode(uint8_t CDR, uint8_t TIM)
{
if (CDR !=0) CDR = 1; // only 0 or 1
if (TIM > 7) TIM = 7;
uint8_t config = read(MAX44009_CONFIGURATION);
config |= MAX44009_CFG_MANUAL;
config &= 0xF0; // clear old CDR & TIM bits
config |= CDR << 3 | TIM; // set new CDR & TIM bits
write(MAX44009_CONFIGURATION, config);
}
int Max44009::getCurrentDivisorRatio()
{
uint8_t CDR = read(MAX44009_CONFIGURATION) & 0x08;
return CDR >> 3;
}
int Max44009::getIntegrationTime()
{
uint8_t TIM = read(MAX44009_CONFIGURATION) & 0x07;
return 800 >> TIM;
}
// datahigh = [eeee mmmm]
// datalow = [ mmmm]
float Max44009::convertToLux(uint8_t datahigh, uint8_t datalow)
{
uint8_t exponent = datahigh >> 4;
uint32_t mantissa = ((datahigh & 0x0F) << 4) + (datalow & 0x0F);
float lux = ((0x0001 << exponent) * MAX44009_MIN_LUX) * mantissa;
return lux;
}
///////////////////////////////////////////////////////////
//
// PRIVATE
//
bool Max44009::setThreshold(const uint8_t reg, const float value)
{
// CHECK RANGE OF VALUE
if ((value < 0.0) || (value > MAX44009_MAX_LUX)) return false;
// compile time optimized.
uint32_t mantissa = round(value * (1.0 / MAX44009_MIN_LUX));
uint8_t exponent = 0;
while (mantissa > 255)
{
mantissa >>= 1; // bits get lost
exponent++;
};
mantissa = (mantissa >> 4) & 0x0F;
exponent <<= 4;
write(reg, exponent | mantissa);
return true;
}
float Max44009::getThreshold(uint8_t reg)
{
uint8_t datahigh = read(reg);
// 0x08 = correction for lost bits
float lux = convertToLux(datahigh, 0x08);
return lux;
}
uint8_t Max44009::read(uint8_t reg)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_error = _wire->endTransmission();
if (_error != MAX44009_OK)
{
return _data; // last value
}
if (_wire->requestFrom(_address, (uint8_t) 1) != 1)
{
_error = MAX44009_ERROR_WIRE_REQUEST;
return _data; // last value
}
_data = _wire->read();
return _data;
}
void Max44009::write(uint8_t reg, uint8_t value)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value);
_error = _wire->endTransmission();
}
// -- END OF FILE --