/
lifx-touch-switch.ino
251 lines (213 loc) · 6.05 KB
/
lifx-touch-switch.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
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
// This #include statement was automatically added by the Particle IDE.
#include "stateEngine.h"
#include "common.h"
#include "gsl1680.h"
//#include "GestureRecognition.h"
#include "stateEngine.h"
#include "lifx.h"
int WAKE = D4;
int INTRPT = D2;
int LED = D7;
String myID;
uint16_t _waitingForMsg = 0;
uint32_t _myID = 69;
bool _msgSent = false;
uint32_t _msgSentTime = 0;
bool led = false;
bool Interrupt = false;
bool colorToggled = false;
uint32_t now;
uint32_t lastMsgTime = 0;
uint32_t lastInterrupt = 0;
uint32_t lastGetService = 0;
uint32_t lastStatus = 0;
uint32_t lastStep = 0;
/* UDP */
uint16_t localPort = 56701;
uint16_t remotePort = 56700;
uint16_t udpPacketSize = 0;
IPAddress broadcastIP;
// Objects
//GestureRecognition GR = GestureRecognition();
stateEngine SE = stateEngine();
gsl1680 GSL = gsl1680();
lifx LIFX;
UDP _udp;
void setup() {
// Time setup for serial print
Time.zone(10.00);
Time.setFormat(TIME_FORMAT_ISO8601_FULL);
Serial.begin(115200);
delay(1000);
IPAddress myIP = WiFi.localIP();
Serial.printlnf(Time.timeStr() + "My IP:%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
myID = System.deviceID();
// Serial.print("My ID: ");
// Serial.println(myID);
_myID = (myIP[3] & 0x000000FF);
// Serial.printlnf("My ID: %lu", (unsigned long)_myID);
broadcastIP = IPAddress(myIP[0], myIP[1], myIP[2], 255);
Serial.printlnf(Time.timeStr() + " - Get Set...");
// Setup IO
pinMode(LED, OUTPUT);
pinMode(WAKE, OUTPUT);
pinMode(INTRPT, INPUT_PULLUP);
digitalWrite(LED, HIGH);
// Initialise Objects
GSL.initialise(WAKE);
//GR.initialise();
SE.initialise();
// Serial.printlnf("Broadcast IP:%d.%d.%d.%d", broadcastIP[0], broadcastIP[1], broadcastIP[2], broadcastIP[3]);
// Start UDP
_udp.begin(remotePort);
_udp.joinMulticast(broadcastIP);
// Setup the LIFX object
LIFX = lifx();
LIFX.setUDP(_udp);
LIFX.setBroadcastIP(broadcastIP);
LIFX.setRemotePort(remotePort);
//Setup Test Lamp
/*uint8_t mac[6];
mac[0] = 0xD0;
mac[1] = 0x73;
mac[2] = 0xD5;
mac[3] = 0x00;
mac[4] = 0x07;
mac[5] = 0x43;*/
//HSBK hsbk = {42416, 49807, 25559, 9000};
// LIFX.addLight(mac, 56700);
Serial.printlnf(Time.timeStr() + " - Go...");
digitalWrite(LED, LOW);
//get status on start up.
LIFX.getStatus();
}
void loop() {
now = millis();
// check wifi is up
if (WiFi.ready() == false)
{
_udp.begin(remotePort);
_udp.joinMulticast(broadcastIP);
}
//update state engine
if(now - SE.lastUpdate > 100)
{
SE.update(now);
}
// Status Updates
if((now - lastStatus >= 30000) || ((_msgSent == true) && (now - _msgSentTime >= 1000)))
{
// Serial.printlnf("%lu - Status update...", now);
LIFX.getStatus();
lastStatus = now;
_msgSent = false;
}
// ---- Actions ---- //
// Check for claiming
if (SE.getClaim() == true)
{
LIFX.discover();
if (now - lastMsgTime > 1000)
{
SE.setClaim(false);
}
}
// Check for tap
if (SE.getTap())
{
// Tap action waiting
LIFX.togglePower();
SE.setTap(false);
}
// check for double tap
if (SE.getDoubleTap())
{
SE.setDoubleTap(false);
}
// check for Moving in the Y axis
if(SE.getMovingYAxis())
{
if(now - lastStep >= 50)
{
LIFX.dimLights(SE.getDeltaYPercent());
lastStep = now;
}
}
// check for Moving in the X axis
if((SE.getMovingXAxis() == true) && (colorToggled == false))
{
LIFX.toggleColor();
colorToggled = true;
} else if ((SE.getMovingXAxis() == false) && (colorToggled == true)) {
colorToggled = false;
}
// check for 2 fingers Moving in the Y axis
if(SE.getMovingYAxis2())
{
if(now - lastStep >= 50)
{
LIFX.cycleColor(SE.getDeltaYPercent());
lastStep = now;
}
}
// ---- End Actions ---- //
// Check if data has been received
udpPacketSize = _udp.parsePacket();
if (udpPacketSize > 0)
{
byte packetBuffer[128]; //buffer to hold incoming packet
// Read first 128 of data received
_udp.read(packetBuffer, 128);
// Ignore other chars
_udp.flush();
// Store sender ip and port
IPAddress senderIP = _udp.remoteIP();
int port = _udp.remotePort();
// Serial.printlnf("%lu - IP:%d.%d.%d.%d:%d", now, senderIP[0], senderIP[1], senderIP[2], senderIP[3], port);
// translate data
LIFX.msgIn(packetBuffer);
// Set last time we saw a msg
lastMsgTime = now;
}
// Check for touchscreen input data
if(digitalRead(INTRPT) == HIGH)
{
led = !led; //flick led
digitalWrite(LED, led);
bool success = GSL.readData();
if (success == 1)
{
int fCount = GSL.event.numberOfFingers;
if(fCount > 0) {
// Serial.printlnf("%lu - Touch%d: ", now, fCount);
} else {
// Serial.printlnf("%lu - High Release: ", now);
}
Interrupt = true;
lastInterrupt = now;
SE.addEvent(GSL.event);
} else {
// Serial.println("Interrupt High: Failed Read...");
}
} else {
if(Interrupt == true && (now - 100 > lastInterrupt)) {
bool success = GSL.readData();
if (success == 1)
{
int fCount = GSL.event.numberOfFingers;
if(fCount > 0) {
Interrupt = true;
lastInterrupt = now;
} else {
Interrupt = false;
}
// Serial.printlnf("%lu - Low Release: ", now);
SE.addEvent(GSL.event);
} else {
// Serial.println("Interrupt Low: Failed Read...");
Interrupt = true;
lastInterrupt = now;
}
}
}
}