Fixed vertical line position in tc data graph. Added current pwm info to state light=x output

This commit is contained in:
Kai Lauterbach 2023-04-28 12:01:50 +02:00
parent c7c90ff4b0
commit ad31eb0748
2 changed files with 63 additions and 23 deletions

View file

@ -51,6 +51,7 @@ bool in_transition;
int transitiontime[LIGHTS_COUNT]; int transitiontime[LIGHTS_COUNT];
int bri[LIGHTS_COUNT]; int bri[LIGHTS_COUNT];
uint16_t current_pwm[LIGHTS_COUNT];
float step_level[LIGHTS_COUNT]; float step_level[LIGHTS_COUNT];
float current_bri[LIGHTS_COUNT]; float current_bri[LIGHTS_COUNT];
@ -98,10 +99,13 @@ void lightEngine() {
last_lightengine_activity = millis(); last_lightengine_activity = millis();
for (int i = 0; i < LIGHTS_COUNT; i++) { for (int i = 0; i < LIGHTS_COUNT; i++)
{
if (light_state[i]) { if (light_state[i])
if (bri[i] != current_bri[i]) { {
if (bri[i] != current_bri[i])
{
in_transition = true; in_transition = true;
current_bri[i] += step_level[i] / BRI_MOD_STEPS_PER_SEC; current_bri[i] += step_level[i] / BRI_MOD_STEPS_PER_SEC;
if ((step_level[i] > 0.0 && current_bri[i] > bri[i]) || (step_level[i] < 0.0 && current_bri[i] < bri[i])) { if ((step_level[i] > 0.0 && current_bri[i] > bri[i]) || (step_level[i] < 0.0 && current_bri[i] < bri[i])) {
@ -110,20 +114,24 @@ void lightEngine() {
} }
uint16_t tmp_pwm = calcPWM(current_bri[i]); uint16_t tmp_pwm = calcPWM(current_bri[i]);
current_pwm[i] = tmp_pwm;
//Serial.println("lon: pin" + (String)i + " = PWM(" + (String)tmp_pwm + ")"); //Serial.println("lon: pin" + (String)i + " = PWM(" + (String)tmp_pwm + ")");
analogWrite(pins[i], tmp_pwm); analogWrite(pins[i], tmp_pwm);
} }
} else { } else {
if (current_bri[i] != 0) { if (current_bri[i] != 0)
{
in_transition = true; in_transition = true;
current_bri[i] -= step_level[i] / BRI_MOD_STEPS_PER_SEC; current_bri[i] -= step_level[i] / BRI_MOD_STEPS_PER_SEC;
if (current_bri[i] < 0) { if (current_bri[i] < 0)
{
current_bri[i] = 0; current_bri[i] = 0;
//Serial.println("Reached target bri[" + (String)i + "] = " + (String)bri[i]); //Serial.println("Reached target bri[" + (String)i + "] = " + (String)bri[i]);
} }
uint16_t tmp_pwm = calcPWM(current_bri[i]); uint16_t tmp_pwm = calcPWM(current_bri[i]);
current_pwm[i] = tmp_pwm;
//Serial.println("loff: pin" + (String)i + " = PWM(" + (String)tmp_pwm + ")"); //Serial.println("loff: pin" + (String)i + " = PWM(" + (String)tmp_pwm + ")");
analogWrite(pins[i], tmp_pwm); analogWrite(pins[i], tmp_pwm);
} }
@ -131,7 +139,8 @@ void lightEngine() {
} // for loop end } // for loop end
if (in_transition) { if (in_transition)
{
delay(6); delay(6);
in_transition = false; in_transition = false;
} }
@ -139,35 +148,44 @@ void lightEngine() {
//********************************// //********************************//
uint16_t calcPWM(float tbri) { uint16_t calcPWM(float tbri)
{
uint16_t tmp_pwm = PWM_OFF; uint16_t tmp_pwm = PWM_OFF;
if (tbri > 0.0) { if (tbri > 0.0)
{
tmp_pwm = PWM_MIN + (int)(tbri * BRI_TO_PWM_FACTOR); tmp_pwm = PWM_MIN + (int)(tbri * BRI_TO_PWM_FACTOR);
} }
if (tmp_pwm > PWM_MAX) { if (tmp_pwm > PWM_MAX) {
tmp_pwm = PWM_MAX; tmp_pwm = PWM_MAX;
} }
//Serial.println((String)tbri + " => " + (String)tmp_pwm);
return tmp_pwm; return tmp_pwm;
} }
//********************************// //********************************//
void read_eeprom_config() { void read_eeprom_config()
for (uint8_t light = 0; light < LIGHTS_COUNT; light++) { {
for (uint8_t light = 0; light < LIGHTS_COUNT; light++)
{
apply_scene(EEPROM.read(EEPROM_SCENE_ADDRESS), light); apply_scene(EEPROM.read(EEPROM_SCENE_ADDRESS), light);
step_level[light] = bri[light] / 150.0; step_level[light] = bri[light] / 150.0;
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == LAST_STATE_STARTUP_LIGHT_LAST_STATE || (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == LAST_STATE_STARTUP_LIGHT_ON_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_ON)) { if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == LAST_STATE_STARTUP_LIGHT_LAST_STATE ||
(EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == LAST_STATE_STARTUP_LIGHT_ON_STATE &&
EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_ON))
{
light_state[light] = true; // set the light state to on light_state[light] = true; // set the light state to on
} }
} }
uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS); uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS);
if (tmp == TIMING_CONTROL_DISABLED) { if (tmp == TIMING_CONTROL_DISABLED)
{
tc_enabled = TIMING_CONTROL_DISABLED; tc_enabled = TIMING_CONTROL_DISABLED;
} else if (tmp == TIMING_CONTROL_ENABLED) { } else if (tmp == TIMING_CONTROL_ENABLED)
{
tc_enabled = TIMING_CONTROL_ENABLED; tc_enabled = TIMING_CONTROL_ENABLED;
} else { } else {
@ -258,7 +276,8 @@ void loop()
//********************************// //********************************//
void handleNotFound() { void handleNotFound()
{
String message = "File Not Found\n\n"; String message = "File Not Found\n\n";
message += "URI: "; message += "URI: ";
message += server.uri(); message += server.uri();
@ -276,7 +295,8 @@ void handleNotFound() {
//********************************// //********************************//
void init_webserver() { void init_webserver()
{
#ifndef DISABLE_WEB_CONTROL #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
@ -335,6 +355,7 @@ void init_webserver() {
root["on"] = light_state[light]; root["on"] = light_state[light];
root["bri"] = bri[light]; root["bri"] = bri[light];
root["curbri"] = (int)current_bri[light]; root["curbri"] = (int)current_bri[light];
root["curpwm"] = current_pwm[light];
String output; String output;
serializeJson(root, output); serializeJson(root, output);
server.send(200, "text/plain", output); server.send(200, "text/plain", output);
@ -520,6 +541,7 @@ void init_webserver() {
http_content += "<title>Light Setup</title>"; http_content += "<title>Light Setup</title>";
http_content += "<script src=\"https://code.jquery.com/jquery-3.6.0.min.js\"></script>"; http_content += "<script src=\"https://code.jquery.com/jquery-3.6.0.min.js\"></script>";
http_content += "<script src=\"https://cdn.plot.ly/plotly-latest.min.js\"></script>"; http_content += "<script src=\"https://cdn.plot.ly/plotly-latest.min.js\"></script>";
http_content += "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js\"></script>";
http_content += "<link rel=\"stylesheet\" href=\"https://unpkg.com/purecss@0.6.2/build/pure-min.css\">"; http_content += "<link rel=\"stylesheet\" href=\"https://unpkg.com/purecss@0.6.2/build/pure-min.css\">";
http_content += "</head>"; http_content += "</head>";
http_content += "<body>"; http_content += "<body>";
@ -705,7 +727,7 @@ void init_webserver() {
"}" "}"
"currenttime.push(data['currenttime']['hour']);" "currenttime.push(data['currenttime']['hour']);"
"currenttime.push(data['currenttime']['min']);" "currenttime.push(data['currenttime']['min']);"
//"console.log(currenttime);" "console.log(currenttime);"
// index suche für die vertikale linie // index suche für die vertikale linie
"var currentTimeStr = currenttime[0] + ':' + (currenttime[1] < 10 ? '0' : '') + currenttime[1];" "var currentTimeStr = currenttime[0] + ':' + (currenttime[1] < 10 ? '0' : '') + currenttime[1];"
@ -714,12 +736,30 @@ void init_webserver() {
"var lowerIndex = -1;" "var lowerIndex = -1;"
"var upperIndex = -1;" "var upperIndex = -1;"
"for (var i = 0; i < time.length - 1; i++) {" "for (var i = 0; i < time.length - 1; i++) {"
"if (time[i] <= currentTimeStr && currentTimeStr <= time[i+1]) {" "console.log(time[i] + ' <= ' + currentTimeStr + ' >= ' + time[i+1]);"
"const currentDate = new Date();"
"const year = currentDate.getFullYear();"
"const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');"
"const day = currentDate.getDate().toString().padStart(2, '0');"
"const dateString = `${year}-${month}-${day}`;"
//"console.log(dateString);" // 2023-04-28
"const start = moment(dateString+' '+time[i], 'YYYY-MM-DD HH:mm');"
"const curr = moment(dateString+' '+currentTimeStr, 'YYYY-MM-DD HH:mm');"
"const end = moment(dateString+' '+time[i+1], 'YYYY-MM-DD HH:mm');"
"console.log(start.format('YYYY-MM-DD HH:mm') + ' <= ' + curr.format('YYYY-MM-DD HH:mm') + ' >= ' + end.format('YYYY-MM-DD HH:mm'));"
"console.log(curr.isBetween(start, end));"
"if (curr.isBetween(start, end)) {"
"lowerIndex = i;" "lowerIndex = i;"
"upperIndex = i + 1;" "upperIndex = i + 1;"
"break;" "break;"
"}" "}"
"}" "}"
"console.log('lowerIndex='+lowerIndex);"
"console.log('upperIndex='+upperIndex);"
"if (lowerIndex === -1 || upperIndex === -1) {" "if (lowerIndex === -1 || upperIndex === -1) {"
"console.log(\"Error: Current time not found in time array and not between two elements in time array.\");" "console.log(\"Error: Current time not found in time array and not between two elements in time array.\");"
"return;" "return;"
@ -728,15 +768,15 @@ void init_webserver() {
"var upperTime = time[upperIndex].split(\":\");" "var upperTime = time[upperIndex].split(\":\");"
"var timeDiff = (currenttime[0] - lowerTime[0]) + ((currenttime[1] - lowerTime[1]) / 60);" "var timeDiff = (currenttime[0] - lowerTime[0]) + ((currenttime[1] - lowerTime[1]) / 60);"
"var indexFloat = lowerIndex + timeDiff / ((upperTime[0] - lowerTime[0]) + ((upperTime[1] - lowerTime[1]) / 60));" "var indexFloat = lowerIndex + timeDiff / ((upperTime[0] - lowerTime[0]) + ((upperTime[1] - lowerTime[1]) / 60));"
//"console.log(\"Index (float): \" + indexFloat);" "console.log(\"Index (float): \" + indexFloat);"
"} else {" "} else {"
//"console.log(\"Index (integer): \" + index);" "console.log(\"Index (integer): \" + index);"
//"console.log(\"Index (float): \" + index);" "console.log(\"Index (float): \" + index);"
"}" "}"
"if (indexFloat > index) {" "if (indexFloat > index) {"
"index = indexFloat;" "index = indexFloat;"
"}" "}"
//"console.log(\">>>\" + index);" "console.log(\">>>\" + index);"
"var trace1 = {" "var trace1 = {"
"x: time," "x: time,"

View file

@ -395,7 +395,7 @@ String tc_getJsonData()
} }
if (tc_testOngoing == false) if (tc_testOngoing == false)
{ {
output += "], \"currenttime\":{\"hour\":" + (String)timeClient.getHours() + ",\"min\":" + (String)timeClient.getMinutes() + "}}"; output += "], \"currenttime\":{\"hour\":" + (String)hour() + ",\"min\":" + (String)minute() + "}}";
} else { } else {
output += "], \"currenttime\":{\"hour\":16,\"min\":0}}"; output += "], \"currenttime\":{\"hour\":16,\"min\":0}}";
} }