From ad31eb074800a9dc80ec8d24ab67aa3b479087a4 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Fri, 28 Apr 2023 12:01:50 +0200 Subject: [PATCH] Fixed vertical line position in tc data graph. Added current pwm info to state light=x output --- firmware/firmware.ino | 84 +++++++++++++++++++++++++++---------- firmware/timing_control.ino | 2 +- 2 files changed, 63 insertions(+), 23 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 45ea629..be1d4ce 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -51,6 +51,7 @@ bool in_transition; int transitiontime[LIGHTS_COUNT]; int bri[LIGHTS_COUNT]; +uint16_t current_pwm[LIGHTS_COUNT]; float step_level[LIGHTS_COUNT]; float current_bri[LIGHTS_COUNT]; @@ -98,10 +99,13 @@ void lightEngine() { 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 (bri[i] != current_bri[i]) { + if (light_state[i]) + { + if (bri[i] != current_bri[i]) + { in_transition = true; 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])) { @@ -110,20 +114,24 @@ void lightEngine() { } uint16_t tmp_pwm = calcPWM(current_bri[i]); + current_pwm[i] = tmp_pwm; //Serial.println("lon: pin" + (String)i + " = PWM(" + (String)tmp_pwm + ")"); analogWrite(pins[i], tmp_pwm); } } else { - if (current_bri[i] != 0) { + if (current_bri[i] != 0) + { in_transition = true; 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; //Serial.println("Reached target bri[" + (String)i + "] = " + (String)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 + ")"); analogWrite(pins[i], tmp_pwm); } @@ -131,7 +139,8 @@ void lightEngine() { } // for loop end - if (in_transition) { + if (in_transition) + { delay(6); 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; - if (tbri > 0.0) { + if (tbri > 0.0) + { tmp_pwm = PWM_MIN + (int)(tbri * BRI_TO_PWM_FACTOR); } if (tmp_pwm > PWM_MAX) { tmp_pwm = PWM_MAX; } - //Serial.println((String)tbri + " => " + (String)tmp_pwm); + return tmp_pwm; } //********************************// -void read_eeprom_config() { - for (uint8_t light = 0; light < LIGHTS_COUNT; light++) { +void read_eeprom_config() +{ + for (uint8_t light = 0; light < LIGHTS_COUNT; light++) + { apply_scene(EEPROM.read(EEPROM_SCENE_ADDRESS), light); 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 } } 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; - } else if (tmp == TIMING_CONTROL_ENABLED) { + } else if (tmp == TIMING_CONTROL_ENABLED) + { tc_enabled = TIMING_CONTROL_ENABLED; } else { @@ -258,7 +276,8 @@ void loop() //********************************// -void handleNotFound() { +void handleNotFound() +{ String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); @@ -276,7 +295,8 @@ void handleNotFound() { //********************************// -void init_webserver() { +void init_webserver() +{ #ifndef DISABLE_WEB_CONTROL 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["bri"] = bri[light]; root["curbri"] = (int)current_bri[light]; + root["curpwm"] = current_pwm[light]; String output; serializeJson(root, output); server.send(200, "text/plain", output); @@ -520,6 +541,7 @@ void init_webserver() { http_content += "Light Setup"; http_content += ""; http_content += ""; + http_content += ""; http_content += ""; http_content += ""; http_content += ""; @@ -705,7 +727,7 @@ void init_webserver() { "}" "currenttime.push(data['currenttime']['hour']);" "currenttime.push(data['currenttime']['min']);" - //"console.log(currenttime);" + "console.log(currenttime);" // index suche für die vertikale linie "var currentTimeStr = currenttime[0] + ':' + (currenttime[1] < 10 ? '0' : '') + currenttime[1];" @@ -714,12 +736,30 @@ void init_webserver() { "var lowerIndex = -1;" "var upperIndex = -1;" "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;" "upperIndex = i + 1;" "break;" "}" "}" + + "console.log('lowerIndex='+lowerIndex);" + "console.log('upperIndex='+upperIndex);" "if (lowerIndex === -1 || upperIndex === -1) {" "console.log(\"Error: Current time not found in time array and not between two elements in time array.\");" "return;" @@ -728,15 +768,15 @@ void init_webserver() { "var upperTime = time[upperIndex].split(\":\");" "var timeDiff = (currenttime[0] - lowerTime[0]) + ((currenttime[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 {" - //"console.log(\"Index (integer): \" + index);" - //"console.log(\"Index (float): \" + index);" + "console.log(\"Index (integer): \" + index);" + "console.log(\"Index (float): \" + index);" "}" "if (indexFloat > index) {" "index = indexFloat;" "}" - //"console.log(\">>>\" + index);" + "console.log(\">>>\" + index);" "var trace1 = {" "x: time," diff --git a/firmware/timing_control.ino b/firmware/timing_control.ino index d77ee1f..7b598da 100644 --- a/firmware/timing_control.ino +++ b/firmware/timing_control.ino @@ -395,7 +395,7 @@ String tc_getJsonData() } if (tc_testOngoing == false) { - output += "], \"currenttime\":{\"hour\":" + (String)timeClient.getHours() + ",\"min\":" + (String)timeClient.getMinutes() + "}}"; + output += "], \"currenttime\":{\"hour\":" + (String)hour() + ",\"min\":" + (String)minute() + "}}"; } else { output += "], \"currenttime\":{\"hour\":16,\"min\":0}}"; }