Skip to content

Commit c896971

Browse files
committed
The code is better
1 parent 54f9086 commit c896971

File tree

4 files changed

+114
-66
lines changed

4 files changed

+114
-66
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"terminal.integrated.env.windows": {
3-
"PATH": "C:\\Users\\cubox\\.platformio\\penv\\Scripts;C:\\Users\\cubox\\.platformio\\penv;C:\\Python27\\;C:\\Python27\\Scripts;C:\\Program Files (x86)\\Embarcadero\\Studio\\19.0\\bin;C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\19.0\\Bpl;C:\\Program Files (x86)\\Embarcadero\\Studio\\19.0\\bin64;C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\19.0\\Bpl\\Win64;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.1\\bin;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.1\\libnvvp;c:\\programdata\\oracle\\java\\javapath;c:\\windows\\system32;c:\\windows;c:\\windows\\system32\\wbem;c:\\windows\\system32\\windowspowershell\\v1.0\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Git\\cmd;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files (x86)\\IVI Foundation\\VISA\\WinNT\\Bin\\;C:\\Program Files\\Zero Install;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Users\\cubox\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\cubox\\AppData\\Roaming\\Dashlane\\5.14.0.21388\\bin\\Firefox_Extension\\{442718d9-475e-452a-b3e1-fb1ee16b8e9f}\\components;C:\\Users\\cubox\\AppData\\Roaming\\Dashlane\\5.14.0.21388\\ucrt;C:\\Users\\cubox\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\cubox\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Python27\\;C:\\Python27\\Scripts;C:\\Program Files (x86)\\Embarcadero\\Studio\\19.0\\bin;C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\19.0\\Bpl;C:\\Program Files (x86)\\Embarcadero\\Studio\\19.0\\bin64;C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\19.0\\Bpl\\Win64;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.1\\bin;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.1\\libnvvp;c:\\programdata\\oracle\\java\\javapath;c:\\windows\\system32;c:\\windows;c:\\windows\\system32\\wbem;c:\\windows\\system32\\windowspowershell\\v1.0\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Git\\cmd;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files (x86)\\IVI Foundation\\VISA\\WinNT\\Bin\\;C:\\Program Files\\Zero Install;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Users\\cubox\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\cubox\\AppData\\Roaming\\Dashlane\\5.14.0.21388\\bin\\Firefox_Extension\\{442718d9-475e-452a-b3e1-fb1ee16b8e9f}\\components;C:\\Users\\cubox\\AppData\\Roaming\\Dashlane\\5.14.0.21388\\ucrt;C:\\Users\\cubox\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\cubox\\AppData\\Local\\Programs\\Microsoft VS Code\\bin",
3+
"PATH": "C:\\Users\\cubox\\.platformio\\penv\\Scripts;C:\\Users\\cubox\\.platformio\\penv;C:\\Python27\\;C:\\Python27\\Scripts;C:\\Program Files (x86)\\Embarcadero\\Studio\\19.0\\bin;C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\19.0\\Bpl;C:\\Program Files (x86)\\Embarcadero\\Studio\\19.0\\bin64;C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\19.0\\Bpl\\Win64;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.1\\bin;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.1\\libnvvp;c:\\programdata\\oracle\\java\\javapath;c:\\windows\\system32;c:\\windows;c:\\windows\\system32\\wbem;c:\\windows\\system32\\windowspowershell\\v1.0\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Git\\cmd;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files (x86)\\IVI Foundation\\VISA\\WinNT\\Bin\\;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\ProgramData\\chocolatey\\bin;;C:\\Users\\cubox\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\SysGCC\\esp8266\\bin;C:\\Python27\\;C:\\Python27\\Scripts;C:\\Program Files (x86)\\Embarcadero\\Studio\\19.0\\bin;C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\19.0\\Bpl;C:\\Program Files (x86)\\Embarcadero\\Studio\\19.0\\bin64;C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\19.0\\Bpl\\Win64;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.1\\bin;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.1\\libnvvp;c:\\programdata\\oracle\\java\\javapath;c:\\windows\\system32;c:\\windows;c:\\windows\\system32\\wbem;c:\\windows\\system32\\windowspowershell\\v1.0\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Git\\cmd;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files (x86)\\IVI Foundation\\VISA\\WinNT\\Bin\\;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\ProgramData\\chocolatey\\bin;;C:\\Users\\cubox\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\SysGCC\\esp8266\\bin",
44
"PLATFORMIO_CALLER": "vscode"
5+
},
6+
"files.associations": {
7+
"*.html": "cpp"
58
}
69
}

livingroomleds/platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ board = nodemcuv2
1414
framework = arduino
1515
upload_port = 192.168.0.198
1616

17-
lib_deps = FastLED ESP8266Ping RemoteDebug
17+
lib_deps = FastLED ESP8266Ping RemoteDebug Time NtpClient

livingroomleds/src/html.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ <h1>LEDS</h1>
106106
<button type="submit" name="rainbow" value="true" class="send rainbow">Rainbow</button>
107107
<button type="submit" name="epilepsy" value="true" class="send blinking">Epilepsy</button>
108108
<button type="submit" name="breath" value="true" class="send">Breath</button>
109+
<button type="submit" name="refresh" value="true" class="send">Refresh</button>
109110
</form>
110111
<button id="white" class="send">Blanc Normal</button>
111112
<button id="lowbrightness" class="send">Veilleuse</button>
113+
<button id="lightblue" class="send">Blanc froid</button>
112114
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
113115
<script>
114116
$(document).ready(function() {
@@ -118,6 +120,12 @@ <h1>LEDS</h1>
118120
$("#blue").val("20");
119121
$("#brightness").val(200);
120122
});
123+
$("#lightblue").click(function () {
124+
$("#red").val("100");
125+
$("#green").val("120");
126+
$("#blue").val("200");
127+
$("#brightness").val(150);
128+
});
121129
$("#lowbrightness").click(function () {
122130
$("#brightness").val(50);
123131
});

livingroomleds/src/livingroomleds.cpp

Lines changed: 101 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
#include <ESP8266WebServer.h>
55
#include <ESP8266WiFi.h>
66
#include <ESP8266mDNS.h>
7-
//#include <RemoteDebug.h>
7+
#define MAX_TIME_INACTIVE -1 // Disable timeout feature
8+
#include <RemoteDebug.h>
89
#include <WiFiUdp.h>
910
//#define FASTLED_INTERRUPT_RETRY_COUNT 1
1011
//#define FASTLED_ALLOW_INTERRUPTS 0
1112
#include <FastLED.h>
13+
14+
#include <TimeLib.h>
15+
#include <NTPClientLib.h>
16+
1217
#include "secrets.h"
1318

1419
#define LED_PIN 2
@@ -24,19 +29,21 @@ const char *password = PASSWORD;
2429
CRGBPalette16 currentPalette;
2530
TBlendType currentBlending;
2631

27-
byte red;
28-
byte green;
29-
byte blue;
30-
byte brightness;
31-
bool rainbow = false;
32-
bool epilepsy = false;
33-
bool breath = false;
34-
bool toUpdate = true;
35-
3632
#include "html.html" // htmlTemplate
3733

3834
ESP8266WebServer server(80);
39-
//RemoteDebug Debug;
35+
RemoteDebug Debug;
36+
37+
struct configuration {
38+
uint8_t brightness;
39+
uint8_t red, green, blue;
40+
bool rainbow, epilepsy, breath;
41+
};
42+
43+
bool toUpdate = true;
44+
45+
struct configuration savedConf;
46+
struct configuration loopConf;
4047

4148
void redirect303(const String &url) {
4249
server.sendHeader("Location", url);
@@ -54,49 +61,45 @@ void handleIndexGet() {
5461
memset(bufferHtml, 0, sizeof(htmlTemplate) + 42);
5562
memset(bufferCss, 0, sizeof(htmlTemplateCSS));
5663
strcpy_P(bufferCss, htmlTemplateCSS);
57-
sprintf_P(bufferHtml, htmlTemplate, bufferCss, red, green, blue, brightness);
64+
sprintf_P(bufferHtml, htmlTemplate, bufferCss, savedConf.red, savedConf.green, savedConf.blue, savedConf.brightness);
5865
server.send(200, "text/html", bufferHtml);
5966
free(bufferHtml);
6067
free(bufferCss);
6168
}
6269

6370
void handleIndexPost() {
6471
if (server.arg("red") != "same") {
65-
red = server.arg("red").toInt();
66-
EEPROM.write(0, red);
72+
savedConf.red = server.arg("red").toInt();
6773
}
6874
if (server.arg("green") != "same") {
69-
green = server.arg("green").toInt();
70-
EEPROM.write(1, green);
75+
savedConf.green = server.arg("green").toInt();
7176
}
7277
if (server.arg("blue") != "same") {
73-
blue = server.arg("blue").toInt();
74-
EEPROM.write(2, blue);
78+
savedConf.blue = server.arg("blue").toInt();
7579
}
7680
if (server.arg("brightness") != "same") {
77-
brightness = server.arg("brightness").toInt();
78-
EEPROM.write(3, brightness);
81+
savedConf.brightness = server.arg("brightness").toInt();
7982
}
8083

8184
if (server.arg("rainbow") != "") {
82-
rainbow = true;
83-
epilepsy = false;
84-
breath = false;
85+
savedConf.rainbow = true;
86+
savedConf.epilepsy = false;
87+
savedConf.breath = false;
8588
} else if (server.arg("epilepsy") != "") {
86-
epilepsy = true;
87-
rainbow = false;
88-
breath = false;
89+
savedConf.epilepsy = true;
90+
savedConf.rainbow = false;
91+
savedConf.breath = false;
8992
} else if (server.arg("breath") != "") {
90-
breath = true;
91-
rainbow = false;
92-
epilepsy = false;
93+
savedConf.breath = true;
94+
savedConf.rainbow = false;
95+
savedConf.epilepsy = false;
9396
} else {
94-
epilepsy = false;
95-
rainbow = false;
96-
breath = false;
97+
savedConf.epilepsy = false;
98+
savedConf.rainbow = false;
99+
savedConf.breath = false;
97100
}
98-
EEPROM.write(4, rainbow);
99-
EEPROM.write(5, breath);
101+
102+
EEPROM.put(0, savedConf);
100103

101104
EEPROM.commit();
102105
toUpdate = true;
@@ -107,7 +110,7 @@ void handleIndexPost() {
107110
void setup() {
108111
Serial.begin(115200);
109112
Serial.println("Booting");
110-
EEPROM.begin(6);
113+
EEPROM.begin(sizeof(struct configuration));
111114
WiFi.mode(WIFI_STA);
112115
WiFi.begin(ssid, password);
113116
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
@@ -116,12 +119,7 @@ void setup() {
116119
ESP.restart();
117120
}
118121

119-
red = EEPROM.read(0);
120-
green = EEPROM.read(1);
121-
blue = EEPROM.read(2);
122-
brightness = EEPROM.read(3); // Load from EEPROM here
123-
rainbow = EEPROM.read(4);
124-
breath = EEPROM.read(5);
122+
EEPROM.get(0, savedConf);
125123

126124
ArduinoOTA.setHostname("Leds");
127125

@@ -157,64 +155,103 @@ void setup() {
157155
Serial.print("IP address: ");
158156
Serial.println(WiFi.localIP());
159157

160-
//Debug.begin("Telnet_HostName");
158+
Debug.begin("Telnet_HostName");
159+
NTP.begin("europe.pool.ntp.org", 1, true, 0);
161160

162161
server.onNotFound(onMissing);
163162
server.on("/", HTTP_GET, handleIndexGet);
164163
server.on("/", HTTP_POST, handleIndexPost);
165164
server.begin();
166165

167166
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
168-
FastLED.setBrightness(brightness);
167+
FastLED.setBrightness(savedConf.brightness);
169168

170169
currentPalette = RainbowColors_p;
171170
currentBlending = LINEARBLEND;
172171
}
173172

174173
void FillLEDsFromPaletteColors(uint8_t colorIndex) {
175174
for (int i = 0; i < NUM_LEDS; i++) {
176-
leds[i] = ColorFromPalette(currentPalette, colorIndex, brightness, currentBlending);
175+
leds[i] = ColorFromPalette(currentPalette, colorIndex, loopConf.brightness, currentBlending);
177176
colorIndex += 3;
178177
}
179178
}
180179

181-
unsigned long previousMillis = 0;
180+
void rgb(struct configuration *conf, int red, int green, int blue) {
181+
conf->red = red;
182+
conf->green = green;
183+
conf->blue = blue;
184+
}
185+
186+
bool toUpdateNextLoop = false;
187+
188+
// long lastMillis = 0;
189+
// long loops = 0;
182190

183191
void loop() {
192+
// long currentMillis = millis();
193+
// loops++;
194+
// if (currentMillis - lastMillis > 1000){
195+
// Debug.print("Loops last second: ");
196+
// Debug.println(loops);
197+
198+
// lastMillis = currentMillis;
199+
// loops = 0;
200+
// }
184201
static uint8_t startIndex = 0;
185202
startIndex += 1; /* motion speed */
186203

187-
/* // Hardcoded mode
188-
toUpdate = true;
189-
breath = false;
190-
red = 255;
191-
green = 0;
192-
blue = 255;
193-
rainbow = false;
194-
epilepsy = false;
195-
brightness = 100; */
196-
197-
if (rainbow) {
204+
loopConf = savedConf;
205+
206+
if (toUpdateNextLoop) {
207+
toUpdate = true;
208+
toUpdateNextLoop = false;
209+
}
210+
211+
time_t t = now();
212+
if (hour(t) == 0 && minute(t) == 0 && second(t) == 0) { // midnight, set as full red, warning
213+
rgb(&loopConf, 255, 0, 0);
214+
loopConf.brightness = 255;
215+
toUpdate = true;
216+
} else if (hour(t) == 0 && minute(t) == 0 && second(t) == 2) { // two seconds later, dim the brightness. Set sensible colours
217+
savedConf.brightness = 50;
218+
toUpdate = true;
219+
} else if (hour(t) == 7 && minute(t) == 0 && second(t) == 0) { // 7 am, lights off
220+
savedConf.brightness = 0;
221+
} else if (minute(t) == 0 && second(t) <= 5) {
222+
loopConf.rainbow = true;
223+
loopConf.breath = false;
224+
loopConf.epilepsy = false;
225+
loopConf.brightness = 255;
226+
toUpdateNextLoop = true;
227+
}
228+
229+
if (loopConf.rainbow) {
198230
FillLEDsFromPaletteColors(startIndex);
199-
} if (epilepsy) {
231+
} else if (loopConf.epilepsy) {
200232
FastLED.delay(20);
201233
FastLED.setBrightness(0);
202234
FastLED.show();
203235
FastLED.delay(20);
204236
startIndex += 1;
205-
} if (breath) {
237+
} else if (loopConf.breath) {
206238
double breathValue, modifiedBrightness;
207239
breathValue = (exp(sin(millis()/2000.0*PI))-0.36787944)*108.0;
208-
modifiedBrightness = map(breathValue, 0, 255, 0, brightness);
240+
modifiedBrightness = map(breathValue, 0, 255, 0, loopConf.brightness);
209241
FastLED.setBrightness(modifiedBrightness);
210-
} if (toUpdate) {
211-
fill_solid(leds, NUM_LEDS, CRGB(red, green, blue));
212-
toUpdate = false;
213242
}
214-
if (!breath) {
215-
FastLED.setBrightness(brightness);
243+
244+
if (toUpdate && !loopConf.rainbow) {
245+
fill_solid(leds, NUM_LEDS, CRGB(loopConf.red, loopConf.green, loopConf.blue));
216246
}
247+
248+
if (!loopConf.breath) {
249+
FastLED.setBrightness(loopConf.brightness);
250+
}
251+
252+
toUpdate = false; // DO NOT PUT AFTER HTTP HANDLING
217253
FastLED.show();
218254
ArduinoOTA.handle();
219255
server.handleClient();
256+
Debug.handle();
220257
}

0 commit comments

Comments
 (0)