Compare commits

..

2 commits

2 changed files with 100 additions and 51 deletions

View file

@ -9,8 +9,8 @@
#include "config.h"
//********* Config block *********//
uint8_t pins[LIGHTS_COUNT] = { 12, 15, 13, 14 };
// ch1, ch2, ch3, ch4
uint8_t pins[LIGHTS_COUNT] = { 14, 12, 15, 13 };
IPAddress strip_ip(192, 168, 0, 26); // choose an unique IP Adress
IPAddress gateway_ip(192, 168, 0, 1); // Router IP
@ -37,9 +37,9 @@ IPAddress dns(192, 168, 0, 1);
// 10 bit PWM
#define PWM_OFF 0 // 0V
#define PWM_MIN 640 // 15V - minimum light amount (~1%)
#define PWM_MIN 0 // 0V - minimum light amount (~1%)
#define PWM_MAX 1023 // 24V - maximum light amount (100%)
#define BRI_TO_PWM_FACTOR 1.506 // 24V-15V = 9V range; 9V ≙ 1024-640 = 384 counts; 384/255 counts =~ 1,506 counts
#define BRI_TO_PWM_FACTOR 4.012 // 24V-15V = 24V range
//********************************//
@ -177,6 +177,7 @@ void read_eeprom_config()
{
light_state[light] = true; // set the light state to on
}
Serial.println("light[" + (String)light + "] = " + (String)light_state[light]);
}
uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS);
@ -195,23 +196,29 @@ void read_eeprom_config()
tc_enabled = TIMING_CONTROL_DISABLED;
}
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == 255) {
Serial.println("tc_enabled = " + (String)tc_enabled);
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == 255)
{
// set the default value on uninitialized EEPROM
EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, 0);
EEPROM.commit();
}
#ifdef USE_STATIC_IP
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255) {
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255)
{
EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 0);
EEPROM.commit();
}
#else
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255) {
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255)
{
EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 1);
EEPROM.commit();
}
#endif
}
//********************************//
@ -222,13 +229,15 @@ void setup()
Serial.begin(SERIAL_BAUD_RATE);
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 0) {
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 0)
{
WiFi.config(strip_ip, gateway_ip, subnet_mask, dns);
}
read_eeprom_config();
for (int j = 0; j < 200; j++) {
for (int j = 0; j < 200; j++)
{
lightEngine();
}
@ -241,7 +250,8 @@ void setup()
Serial.print("IP: ");
Serial.println(myIP);
if (!light_state[0]) {
if (!light_state[0])
{
// Show that we are connected
analogWrite(pins[0], 100);
delay(500);
@ -269,7 +279,9 @@ void loop()
server.handleClient();
lightEngine();
if (tc_enabled == TIMING_CONTROL_ENABLED) {
if (tc_enabled == TIMING_CONTROL_ENABLED)
{
//Serial.println("tc_enabled = " + (String)tc_enabled);
tc_update_loop();
}
}
@ -287,7 +299,8 @@ void handleNotFound()
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
@ -299,7 +312,8 @@ void init_webserver()
{
#ifndef DISABLE_WEB_CONTROL
server.on("/state", HTTP_PUT, []() { // HTTP PUT request used to set a new light state
server.on("/state", HTTP_PUT, []()
{ // HTTP PUT request used to set a new light state
DynamicJsonDocument root(1024);
DeserializationError error = deserializeJson(root, server.arg("plain"));
@ -307,38 +321,46 @@ void init_webserver()
server.send(404, "text/plain", "FAIL. " + server.arg("plain"));
} else {
for (JsonPair state : root.as<JsonObject>()) {
for (JsonPair state : root.as<JsonObject>())
{
const char* key = state.key().c_str();
int light = atoi(key) - 1;
JsonObject values = state.value();
int transitiontime = 4;
uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS);
if (values.containsKey("on")) {
if (values["on"]) {
if (values.containsKey("on"))
{
if (values["on"])
{
light_state[light] = true;
if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_OFF) {
if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_OFF)
{
EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_ON);
}
} else {
light_state[light] = false;
if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_ON) {
if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_ON)
{
EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_OFF);
}
}
}
if (values.containsKey("bri")) {
if (values.containsKey("bri"))
{
bri[light] = values["bri"];
}
if (values.containsKey("bri_inc")) {
if (values.containsKey("bri_inc"))
{
bri[light] += (int)values["bri_inc"];
if (bri[light] > 255) bri[light] = 255;
else if (bri[light] < 1) bri[light] = 1;
}
if (values.containsKey("transitiontime")) {
if (values.containsKey("transitiontime"))
{
transitiontime = values["transitiontime"];
}
process_lightdata(light, transitiontime);
@ -349,7 +371,8 @@ void init_webserver()
}
});
server.on("/state", HTTP_GET, []() { // HTTP GET request used to fetch current light state
server.on("/state", HTTP_GET, []()
{ // HTTP GET request used to fetch current light state
uint8_t light = server.arg("light").toInt() - 1;
DynamicJsonDocument root(1024);
root["on"] = light_state[light];
@ -361,7 +384,8 @@ void init_webserver()
server.send(200, "text/plain", output);
});
server.on("/detect", []() { // HTTP GET request used to discover the light type
server.on("/detect", []()
{ // HTTP GET request used to discover the light type
char macString[32] = { 0 };
sprintf(macString, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
DynamicJsonDocument root(1024);
@ -377,7 +401,8 @@ void init_webserver()
server.send(200, "text/plain", output);
});
server.on("/tc_data_blocks", []() {
server.on("/tc_data_blocks", []()
{
String output = tc_getJsonData();
server.send(200, "application/json", output);
@ -385,24 +410,29 @@ void init_webserver()
#endif // DISABLE_WEB_CONTROL
server.on("/", []() {
server.on("/", []()
{
#ifndef DISABLE_WEB_CONTROL
static float transitiontime = 4.0;
if (server.hasArg("transition")) {
if (server.hasArg("transition"))
{
transitiontime = server.arg("transition").toFloat();
}
// startup behavior switch handling
if (server.hasArg("startup")) {
if (server.hasArg("startup"))
{
int startup = server.arg("startup").toInt();
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) != startup) {
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) != startup)
{
EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, startup);
for (uint8_t i = 0; i < LIGHTS_COUNT; i++) {
uint8_t tmp = (light_state[i] == true ? LIGHT_STATE_ON : LIGHT_STATE_OFF);
if (EEPROM.read(EEPROM_LAST_STATE_ADDRESS + i) != tmp) {
if (EEPROM.read(EEPROM_LAST_STATE_ADDRESS + i) != tmp)
{
EEPROM.write(EEPROM_LAST_STATE_ADDRESS + i, tmp);
}
}
@ -415,10 +445,14 @@ void init_webserver()
#endif // DISABLE_WEB_CONTROL
// timing controller switch handling
if (server.hasArg("tc")) {
if (server.arg("tc") == "true") {
if (tc_enabled == TIMING_CONTROL_DISABLED) {
if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_ENABLED) {
if (server.hasArg("tc"))
{
if (server.arg("tc") == "true")
{
if (tc_enabled == TIMING_CONTROL_DISABLED)
{
if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_ENABLED)
{
tc_enabled = TIMING_CONTROL_ENABLED;
EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_ENABLED);
EEPROM.commit();
@ -429,9 +463,11 @@ void init_webserver()
} else { // tc is set to false or something else
if (tc_enabled == TIMING_CONTROL_ENABLED) {
if (tc_enabled == TIMING_CONTROL_ENABLED)
{
tc_enabled = TIMING_CONTROL_DISABLED;
if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_DISABLED) {
if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_DISABLED)
{
EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_DISABLED);
EEPROM.commit();
Serial.print("Timing control = ");
@ -468,7 +504,8 @@ void init_webserver()
// process the received data for every light
for (int light = 0; light < LIGHTS_COUNT; light++) {
if (server.hasArg("bri" + (String)light)) {
if (server.hasArg("bri" + (String)light))
{
bri[light] = (int)server.arg("bri" + (String)light).toInt();
Serial.print("Brightness ");
Serial.print(light);
@ -715,6 +752,7 @@ void init_webserver()
http_content += "<script>"
"function loadData() {" // load tc data and generate graph
"console.log('----> generate graph <----');"
"$.getJSON('/tc_data_blocks', function(data) {"
"var currenttime = [];"
"var time = [];"
@ -785,8 +823,9 @@ void init_webserver()
"if (indexFloat > index) {"
"index = indexFloat;"
"}"
"console.log(\">>>\" + index);"
"console.log(\"index in graph >>>\" + index);"
// TODO in array dynamisch erzeugt umschreiben
"var trace1 = {"
"x: time,"
"y: channel1,"
@ -815,6 +854,7 @@ void init_webserver()
"type: 'scatter',"
"mode: 'lines+markers',"
"};"
"var layout = {"
"title: 'Timing Control Data Blocks',"
"xaxis: {"
@ -838,12 +878,14 @@ void init_webserver()
"}"
"}]"
"};"
"Plotly.newPlot('plot_chart', [trace1, trace2, trace3, trace4], layout);"
"Plotly.newPlot('plot_chart', [trace1, trace2, trace3, trace4], layout);" // TODO array der traces dynamisch erzeugen
"});"
"}"
"setInterval(loadData, 10000);"
"loadData();"
"function updateLightState() {" // load the light data from server and set on state and brightness
"console.log('----> setting bri and power switch <----');"
"for (let i = 1; i <= 4; i++) {"
"const lightURL = `http://192.168.0.26/state?light=${i}`;"
"fetch(lightURL)"
@ -855,12 +897,15 @@ void init_webserver()
"const onLinkOff = document.getElementById(`on${i - 1}_off`);"
"briSlider.value = data.bri;"
"briSliderVal.innerHTML = (Math.round((data.bri * 100.0 / 255.0) * 100) / 100).toFixed(2);"
"if (data.on == 'true') {"
"onLinkOn.classList.remove('pure-button-primary');"
"onLinkOff.classList.add('pure-button-primary');"
"} else {"
"console.log('data.on ' + i + ' = ' + data.on);"
"if (data.on == true) {"
//"console.log('true');"
"onLinkOn.classList.add('pure-button-primary');"
"onLinkOff.classList.remove('pure-button-primary');"
"} else {"
//"console.log('false');"
"onLinkOn.classList.remove('pure-button-primary');"
"onLinkOff.classList.add('pure-button-primary');"
"}"
"})"
".catch(error => console.error(error));"
@ -871,6 +916,7 @@ void init_webserver()
// show pwm values in webinterface
"function updatePWMValues() {"
"console.log('----> setting pwm data <----');"
"for (let i = 0; i < " + (String)LIGHTS_COUNT + "; i++) {"
"const lightID = i + 1;"
"const pwmElement = document.getElementById(`light${i}_pwm`);"
@ -880,7 +926,7 @@ void init_webserver()
"fetch(url)"
".then(response => response.json())"
".then(data => {"
"const pwmValue = ((Math.round((data.curpwm - ((data.curpwm >= 640.0) ? 640.0 : 0)) / 383.0 * 100.0) * 100.0) / 100.0).toFixed(2);" // pwm as % 640 to 1023
"const pwmValue = ((Math.round((data.curpwm - ((data.curpwm >= " + (String)PWM_MIN+ ") ? " + (String)PWM_MIN + " : 0)) / " + (String)PWM_MAX + " * 100.0) * 100.0) / 100.0).toFixed(2);" // pwm as % PWM_MIN to PWM_MAX
"console.log('curpwm[' + i + '] = ' + data.curpwm + ' = ' + pwmValue);"
"pwmElement.innerText = pwmValue.toString();"
"pwmElement.value = pwmValue;"

View file

@ -117,8 +117,11 @@ void tc_init()
}
}
if (tc_enabled == TIMING_CONTROL_ENABLED)
{
Serial.println("tc_enabled = " + (String)tc_enabled);
tc_update_main();
}
}
//********************************//