forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P153_MAX44009.ino
209 lines (189 loc) · 6.67 KB
/
_P153_MAX44009.ino
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
//#######################################################################################################
//############################### Plugin 153: MAX44009 I2C 0x4A #######################################
//#######################################################################################################
// based on :
// 1) https://github.com/RobTillaart/Arduino/tree/master/libraries/Max44009
// 2) https://github.com/dantudose/MAX44009/blob/master/MAX44009.cpp
//
// written by https://github.com/apszowski
#ifdef PLUGIN_BUILD_TESTING
#define PLUGIN_153
#define PLUGIN_ID_153 153
#define PLUGIN_NAME_153 "Light/Lux - MAX44009 (GY-49)"
#define PLUGIN_VALUENAME1_153 "Lux"
boolean Plugin_153_init = false;
enum {
//I2C address
MAX44009_I2C_ADDR = 0x4A,
// CONFIGURATION
MAX44009_REGISTER_CONFIGURATION = 0x02,
MAX44009_CFG_MANUAL = 0x40,
MAX44009_CFG_CONTINUOUS = 0x80,
//LUX READING
MAX44009_REGISTER_LUX_HIGH = 0x03,
MAX44009_REGISTER_LUX_LOW = 0x04,
};
uint16_t Plugin_153_readRegister(uint8_t reg) {
uint16_t ret;
Wire.beginTransmission(MAX44009_I2C_ADDR);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(MAX44009_I2C_ADDR,1);
ret = Wire.read();
return ret;
}
void Plugin_153_writeRegister(uint8_t reg, uint8_t value){
Wire.beginTransmission(MAX44009_I2C_ADDR);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
float Plugin_153_readLux(void)
{
uint8_t luxHigh = Plugin_153_readRegister(MAX44009_REGISTER_LUX_HIGH);
uint8_t luxLow = Plugin_153_readRegister(MAX44009_REGISTER_LUX_LOW);
uint8_t e = (luxHigh & 0xF0) >> 4;
uint8_t m = (luxHigh & 0x0F) << 4 | luxLow;
float lux = pow(2,e) * m * 0.045;
return lux;
}
void Plugin_153_setModeAutomatic(void)
{
uint8_t config = Plugin_153_readRegister(MAX44009_REGISTER_CONFIGURATION);
config &= ~MAX44009_CFG_CONTINUOUS; // off
config &= ~MAX44009_CFG_MANUAL; // off
Plugin_153_writeRegister(MAX44009_REGISTER_CONFIGURATION, config);
}
void Plugin_153_setModeContinuous(void)
{
uint8_t config = Plugin_153_readRegister(MAX44009_REGISTER_CONFIGURATION);
config |= MAX44009_CFG_CONTINUOUS; // on
config &= ~MAX44009_CFG_MANUAL; // off
Plugin_153_writeRegister(MAX44009_REGISTER_CONFIGURATION, config);
}
void Plugin_153_setModeManual(uint8_t CDR, uint8_t TIM)
{
uint8_t config = Plugin_153_readRegister(MAX44009_REGISTER_CONFIGURATION);
config &= ~MAX44009_CFG_CONTINUOUS; // off
config |= MAX44009_CFG_MANUAL; // on
config &= 0xF0; // clear CDR & TIM bits
config |= CDR << 3 | TIM;
Plugin_153_writeRegister(MAX44009_REGISTER_CONFIGURATION, config);
}
boolean Plugin_153(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_153;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_153);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_153));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice_mode = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
String options[8];
int optionValues[8];
options[0] = F("Automatic");
optionValues[0] = 0;
options[1] = F("Continuous");
optionValues[1] = 1;
options[2] = F("Manual");
optionValues[2] = 2;
addFormSelector(string, F("Mode"), F("plugin_153_mode"), 3, options, optionValues, choice_mode);
if( choice_mode != 0)
{
//########################
byte choice_division = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
options[0] = F("Not divided");
optionValues[0] = 0;
options[1] = F("Divided 1/8");
optionValues[1] = 1;
addFormSelector(string, F("Current Division Ratio"), F("plugin_153_divide"), 2, options, optionValues, choice_division);
//########################
byte choice_time = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
float integration_time = 800;
for( uint8_t i = 0 ; i <= 7 ; i++)
{
options[i] = integration_time;
options[i] += F(" ms");
optionValues[i] = i;
integration_time = integration_time/2.0;
}
addFormSelector(string, F("Integration Time"), F("plugin_153_time"), 8, options, optionValues, choice_time);
}
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("plugin_153_mode"));
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = getFormItemInt(F("plugin_153_divide"));
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = getFormItemInt(F("plugin_153_time"));
Plugin_153_init = false;
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_153_init = true;
uint8_t mode = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
uint8_t divide = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
uint8_t tim = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
String log = F("MAX44009 :\rINIT MODE = ");
if( mode == 0)
{
Plugin_153_setModeAutomatic();
log += F("Automatic");
}
else if ( mode == 1)
{
Plugin_153_setModeContinuous();
log += F("Continuous");
}
else
{
Plugin_153_setModeManual(divide,tim);
log += F("Manual , CDR = ");
log += divide;
log += F(", Integration Time = ");
log += tim;
}
addLog(LOG_LEVEL_INFO,log);
success = true;
break;
}
case PLUGIN_READ:
{
UserVar[event->BaseVarIndex] = (float) Plugin_153_readLux();
String log = F("MAX44009 : Ambient Light: ");
log += UserVar[event->BaseVarIndex];
addLog(LOG_LEVEL_INFO,log);
success = true;
break;
}
}
return success;
}
#endif