forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_P064_APDS9960.ino
217 lines (172 loc) · 6.68 KB
/
_P064_APDS9960.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
210
211
212
213
214
215
216
217
//#######################################################################################################
//#################################### Plugin 064: APDS9960 Gesture ##############################
//#######################################################################################################
// ESPEasy Plugin to scan a gesture, proximity and light chip APDS9960
// written by Jochen Krapf ([email protected])
// A new gesture is send immediately to controllers.
// Proximity and Light are scanned frequently by given 'Delay' setting.
// RGB is not scanned because there are only 4 vars per task.
// Known BUG: While performing a gesture the reader function blocks rest of ESPEasy processing!!! (Feel free to fix...)
// Note: The chip has a wide view-of-angle. If housing is in this angle the chip blocks!
#ifdef PLUGIN_BUILD_DEV
#define PLUGIN_064
#define PLUGIN_ID_064 64
#define PLUGIN_NAME_064 "Gesture - APDS9960 [DEVELOPMENT]"
#define PLUGIN_VALUENAME1_064 "Gesture"
#define PLUGIN_VALUENAME2_064 "Proximity"
#define PLUGIN_VALUENAME3_064 "Light"
/*
#define PLUGIN_VALUENAME4_064 "R"
#define PLUGIN_VALUENAME5_064 "G"
#define PLUGIN_VALUENAME6_064 "B"
*/
#include <SparkFun_APDS9960.h> //Lib is modified to work with ESP
#ifndef CONFIG
#define CONFIG(n) (Settings.TaskDevicePluginConfig[event->TaskIndex][n])
#endif
SparkFun_APDS9960* PLUGIN_064_pds = NULL;
boolean Plugin_064(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_064;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].Ports = 0;
Device[deviceCount].VType = SENSOR_TYPE_SWITCH;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 3;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].TimerOptional = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_064);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_064));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_064));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_064));
/*
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], PSTR(PLUGIN_VALUENAME4_064));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[4], PSTR(PLUGIN_VALUENAME5_064));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[5], PSTR(PLUGIN_VALUENAME6_064));
*/
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte addr = 0x39; // CONFIG(0); chip has only 1 address
int optionValues[1] = { 0x39 };
addFormSelectorI2C(string, F("i2c_addr"), 1, optionValues, addr); //Only for display I2C address
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
//CONFIG(0) = getFormItemInt(F("i2c_addr"));
success = true;
break;
}
case PLUGIN_INIT:
{
if (PLUGIN_064_pds)
delete PLUGIN_064_pds;
PLUGIN_064_pds = new SparkFun_APDS9960();
String log = F("APDS : ");
if ( PLUGIN_064_pds->init() )
{
log += F("Init");
PLUGIN_064_pds->enablePower();
if (! PLUGIN_064_pds->enableLightSensor(false))
log += F(" - Error during light sensor init!");
if (! PLUGIN_064_pds->enableProximitySensor(false))
log += F(" - Error during proximity sensor init!");
if (! PLUGIN_064_pds->enableGestureSensor(false))
log += F(" - Error during gesture sensor init!");
}
else
{
log += F("Error during APDS-9960 init!");
}
addLog(LOG_LEVEL_INFO, log);
success = true;
break;
}
case PLUGIN_FIFTY_PER_SECOND:
{
if (!PLUGIN_064_pds)
break;
if ( !PLUGIN_064_pds->isGestureAvailable() )
break;
int gesture = PLUGIN_064_pds->readGesture();
//int gesture = PLUGIN_064_pds->readGestureNonBlocking();
//if (gesture == -1) Serial.print(".");
//if (gesture == -2) Serial.print(":");
//if (gesture == -3) Serial.print("|");
//if ( 0 && PLUGIN_064_pds->isGestureAvailable() )
if (gesture >= 0)
{
String log = F("APDS : Gesture=");
switch ( gesture )
{
case DIR_UP: log += F("UP"); break;
case DIR_DOWN: log += F("DOWN"); break;
case DIR_LEFT: log += F("LEFT"); break;
case DIR_RIGHT: log += F("RIGHT"); break;
case DIR_NEAR: log += F("NEAR"); break;
case DIR_FAR: log += F("FAR"); break;
default: log += F("NONE"); break;
}
log += F(" (");
log += gesture;
log += F(")");
UserVar[event->BaseVarIndex] = (float)gesture;
event->sensorType = SENSOR_TYPE_SWITCH;
sendData(event);
addLog(LOG_LEVEL_INFO, log);
}
success = true;
break;
}
case PLUGIN_READ:
{
if (!PLUGIN_064_pds)
break;
// Gesture - work is done in PLUGIN_FIFTY_PER_SECOND
if (1)
{
uint8_t proximity_data = 0;
PLUGIN_064_pds->readProximity(proximity_data);
UserVar[event->BaseVarIndex + 1] = (float)proximity_data;
uint16_t ambient_light = 0;
PLUGIN_064_pds->readAmbientLight(ambient_light);
UserVar[event->BaseVarIndex + 2] = (float)ambient_light;
/*
uint16_t red_light = 0;
uint16_t green_light = 0;
uint16_t blue_light = 0;
PLUGIN_064_pds->readRedLight(red_light);
PLUGIN_064_pds->readGreenLight(green_light);
PLUGIN_064_pds->readBlueLight(blue_light);
UserVar[event->BaseVarIndex + 3] = (float)red_light;
UserVar[event->BaseVarIndex + 4] = (float)green_light;
UserVar[event->BaseVarIndex + 5] = (float)blue_light;
*/
}
success = true;
break;
}
}
return success;
}
#endif