-
Notifications
You must be signed in to change notification settings - Fork 6
/
utility.ino
216 lines (194 loc) · 5.69 KB
/
utility.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
void setupPins() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BUTTON_BUILTIN, INPUT);
pinMode(EXTERNAL_BUTTON, INPUT_PULLUP);
pinMode(FADE_3, INPUT_PULLUP);
pinMode(FADE_1, INPUT_PULLUP);
ButtonConfig* buttonConfigBuiltIn = buttonBuiltIn.getButtonConfig();
buttonConfigBuiltIn->setEventHandler(handleButtonEvent);
buttonConfigBuiltIn->setFeature(ButtonConfig::kFeatureClick);
buttonConfigBuiltIn->setFeature(ButtonConfig::kFeatureLongPress);
buttonConfigBuiltIn->setLongPressDelay(LONG_PRESS);
touchConfig.setFeature(ButtonConfig::kFeatureClick);
touchConfig.setFeature(ButtonConfig::kFeatureLongPress);
touchConfig.setEventHandler(handleTouchEvent);
touchConfig.setLongPressDelay(LONG_TOUCH);
}
//internal led functions
void blinkDevice() {
if (readyToBlink == false) {
readyToBlink = true;
}
}
void ledHandler() {
if (readyToBlink == true && isBlinking == false) {
isBlinking = true;
blinkTime = millis();
digitalWrite(LED_BUILTIN, 1);
}
if (millis() - blinkTime > blinkDuration && isBlinking == true) {
digitalWrite(LED_BUILTIN, 0);
isBlinking = false;
readyToBlink = false;
}
}
void blinkOnConnect() {
byte NUM_BLINKS = 3;
for (byte i = 0; i < NUM_BLINKS; i++) {
digitalWrite(LED_BUILTIN, 1);
delay(100);
digitalWrite(LED_BUILTIN, 0);
delay(400);
}
delay(600);
}
// button functions
void handleButtonEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.print("ID IS: ");
Serial.println(button->getPin());
switch (button->getPin()) {
case 0:
switch (eventType) {
case AceButton::kEventPressed:
break;
case AceButton::kEventReleased:
if (currentSetupStatus == setup_finished) socketIO_sendButtonPress();
break;
case AceButton::kEventLongPressed:
factoryReset();
break;
case AceButton::kEventRepeatPressed:
break;
}
break;
case EXTERNAL_BUTTON:
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("TOUCH: pressed");
break;
case AceButton::kEventLongPressed:
Serial.println("TOUCH: Long pressed");
isSelectingColour = true;
ledChanged[USERLED] = true;
// TODO also hold the LED at the colour for a little bit
break;
case AceButton::kEventReleased:
Serial.println("TOUCH: released");
if (isSelectingColour == true) {
ledChanged[USERLED] = true;
fadeRGB(USERLED);
} else {
ledChanged[USERLED] = true;
fadeRGB(USERLED);
socketIO_sendColour();
}
isSelectingColour = false;
break;
case AceButton::kEventClicked:
Serial.println("TOUCH: clicked");
break;
}
break;
}
}
// button functions
void handleTouchEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println(button->getId());
switch (eventType) {
case AceButton::kEventPressed:
Serial.println("TOUCH: pressed");
break;
case AceButton::kEventLongPressed:
Serial.println("TOUCH: Long pressed");
isSelectingColour = true;
ledChanged[USERLED] = true;
// TODO also hold the LED at the colour for a little bit
break;
case AceButton::kEventReleased:
Serial.println("TOUCH: released");
if (isSelectingColour == true) {
ledChanged[USERLED] = true;
fadeRGB(USERLED);
} else {
ledChanged[USERLED] = true;
fadeRGB(USERLED);
isFadingRGB[USERLED] = false;
startLongFade(USERLED);
socketIO_sendColour();
}
isSelectingColour = false;
break;
case AceButton::kEventClicked:
Serial.println("TOUCH: clicked");
break;
}
}
//reset functions
void factoryReset() {
Serial.println("factoryReset");
preferences.begin("scads", false);
preferences.clear();
preferences.end();
currentSetupStatus = setup_pending;
ESP.restart();
}
void softReset(int delayMs) {
if (isResetting == false) {
isResetting = true;
resetTime = millis() + delayMs;
}
}
void checkReset() {
if (isResetting) {
if (millis() > resetTime + resetDurationMs) {
ESP.restart();
}
}
}
String generateID() {
//https://github.com/espressif/arduino-esp32/issues/3859#issuecomment-689171490
uint64_t chipID = ESP.getEfuseMac();
uint32_t low = chipID % 0xFFFFFFFF;
uint32_t high = (chipID >> 32) % 0xFFFFFFFF;
String out = String(low);
return out;
}
void setupCapacitiveTouch() {
int touchAverage = 0;
for (byte i = 0; i < 10; i++) {
touchAverage = touchAverage + touchRead(CAPTOUCH);
delay(100);
}
touchAverage = touchAverage / 10;
TOUCH_THRESHOLD = touchAverage - TOUCH_HYSTERESIS;
Serial.print("Touch threshold is:");
Serial.println(TOUCH_THRESHOLD);
touchConfig.setThreshold(TOUCH_THRESHOLD);
}
long checkFadingLength() {
if (digitalRead(FADE_3) == 0 && digitalRead(FADE_1) == 1) {
Serial.println("Your fade time is 3 hours");
return 180;
} else if (digitalRead(FADE_1) == 0 && digitalRead(FADE_3) == 1) {
Serial.println("Your fade time is 1 hour");
return 60;
} else if (digitalRead(FADE_3) == 0 && digitalRead(FADE_1) == 0) {
Serial.println("Your fade time is 9 hours");
return 540;
} else {
Serial.println("Your fade time is 6 hours");
return 360;
}
}
void setLastConnected(String ssid) {
preferences.begin("scads", false);
preferences.putString("lastConnected",ssid);
preferences.end();
}
String getLastConnected() {
String lastConnected;
preferences.begin("scads", false);
lastConnected = preferences.getString("lastConnected", "");
preferences.end();
return lastConnected;
}