From c2c242528386b315fd3e921eadae75f4b63a5192 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Thu, 11 May 2023 16:48:59 +0200 Subject: [PATCH] Added a pwm test function --- firmware/config.h | 10 ++ firmware/data/bottom.js | 28 +++ firmware/data/index_template_top.html | 1 + firmware/firmware.ino | 236 ++++++++++++++++++++++++-- firmware/html/bottom.js | 43 ++++- firmware/html/index_template_top.html | 1 + firmware/timing_control.ino | 2 +- 7 files changed, 305 insertions(+), 16 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index 625d9d1..d925ab8 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -24,3 +24,13 @@ #define TIME_LIGHTENGINE_INTERVAL_MS (1000UL / BRI_MOD_STEPS_PER_SEC) // BRI_MOD_STEPS_PER_SEC steps per second to in-/decrease the brightness #define MY_NTP_SERVER "de.pool.ntp.org" + +// 10 bit PWM +#define PWM_FREQ (50000UL) +#define PWM_OFF 0 // 0V +#define PWM_MIN 0 // 0V - minimum light amount (~1%) +#define PWM_MAX 255 // 24V - maximum light amount (100%) +#define BRI_TO_PWM_FACTOR 1.0 // 24V-0V = 24V range + +#define PWM_TEST_INTERVA_MS 1000 +#define TEST_PWM_CHG_CNT 5 diff --git a/firmware/data/bottom.js b/firmware/data/bottom.js index 3b0a739..db867c4 100644 --- a/firmware/data/bottom.js +++ b/firmware/data/bottom.js @@ -581,3 +581,31 @@ toast.style.opacity = 0; }, 5000); } } +function sendPWMTestRequest(event) { +event.preventDefault(); +var button = event.target; +var xhr = new XMLHttpRequest(); +xhr.open("GET", 'http://{{IP_ADDRESS}}/test_pwm', true); +xhr.onload = function () { +if (xhr.status == 200 && xhr.responseText === "OK") { +button.innerHTML = "PWM test started"; +button.style.backgroundColor = "green"; +} else { +button.innerHTML = "Error!"; +button.style.backgroundColor = "red"; +} +setTimeout(function () { +button.innerHTML = "PWM Test"; +button.style.backgroundColor = ""; +}, 2000); +}; +xhr.onerror = function () { +button.innerHTML = "Error!"; +button.style.backgroundColor = "red"; +setTimeout(function () { +button.innerHTML = "PWM Test"; +button.style.backgroundColor = ""; +}, 2000); +}; +xhr.send(); +} diff --git a/firmware/data/index_template_top.html b/firmware/data/index_template_top.html index 7345687..5deb421 100644 --- a/firmware/data/index_template_top.html +++ b/firmware/data/index_template_top.html @@ -22,6 +22,7 @@ reset light reset timing control data alert +PWM Test
diff --git a/firmware/firmware.ino b/firmware/firmware.ino index ef60651..b87bf44 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -55,17 +55,25 @@ IPAddress dns (192, 168, 0, 1); #define SCENE_BRIGHT 1 #define SCENE_NIGHTLY 2 -// 10 bit PWM -#define PWM_FREQ (50000UL) -#define PWM_OFF 0 // 0V -#define PWM_MIN 0 // 0V - minimum light amount (~1%) -#define PWM_MAX 255 // 24V - maximum light amount (100%) -#define BRI_TO_PWM_FACTOR 1.0 // 24V-0V = 24V range +#define TEST_PWM_STATE_INIT 0 +#define TEST_PWM_STATE_CH1_INC 1 +#define TEST_PWM_STATE_CH1_DEC 2 +#define TEST_PWM_STATE_CH2_INC 3 +#define TEST_PWM_STATE_CH2_DEC 4 +#define TEST_PWM_STATE_CH3_INC 5 +#define TEST_PWM_STATE_CH3_DEC 6 +#define TEST_PWM_STATE_CH4_INC 7 +#define TEST_PWM_STATE_CH4_DEC 8 //********************************// uint8_t scene; uint8_t tc_enabled; +uint8_t tc_enabled_old; + +bool test_pwm = false; +uint32_t test_pwm_lastcheck_ms = 0; +uint8_t test_pwm_state = TEST_PWM_STATE_INIT; bool light_state[LIGHTS_COUNT]; bool in_transition; @@ -345,6 +353,9 @@ void setup() void loop() { server.handleClient(); + + ESP.wdtFeed(); + lightEngine(); if (tc_enabled == TIMING_CONTROL_ENABLED) @@ -352,6 +363,11 @@ void loop() //Serial.println("tc_enabled = " + (String)tc_enabled); tc_update_loop(); } + + ESP.wdtFeed(); + + test_pwm_main(); + delay(100); } //********************************// @@ -463,18 +479,27 @@ void init_webserver() 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); - root["name"] = LIGHT_NAME; - root["lights"] = LIGHTS_COUNT; + root["name"] = LIGHT_NAME; + root["lights"] = LIGHTS_COUNT; root["protocol"] = "native_multi"; - root["modelid"] = "LWB010"; - root["type"] = "dimmable_light"; - root["mac"] = String(macString); - root["version"] = LIGHT_VERSION; + root["modelid"] = "LWB010"; + root["type"] = "dimmable_light"; + root["mac"] = String(macString); + root["version"] = LIGHT_VERSION; String output; serializeJson(root, output); server.send(200, "text/plain", output); }); + server.on("/test_pwm", []() + { + test_pwm = true; + tc_enabled_old = tc_enabled; + tc_enabled == TIMING_CONTROL_DISABLED; + + server.send(200, "text/html", "OK"); + }); + server.on("/tc_data_blocks_read", []() { String output = tc_getJsonData(); @@ -935,3 +960,190 @@ String formatBytes(size_t bytes) } //********************************// + +void test_pwm_main() +{ + if (test_pwm == false) + { + return; + } + + if ((test_pwm_lastcheck_ms + PWM_TEST_INTERVA_MS) <= millis()) + { + test_pwm_lastcheck_ms = millis(); + + switch (test_pwm_state) + { + // ----------------------- // + case TEST_PWM_STATE_INIT: + // disable the lights + for (uint8_t i = 0; i < LIGHTS_COUNT; i++) + { + light_state[i] = false; + bri[i] = 0; + current_bri[i] = 0; + current_pwm[i] = 0; + transitiontime[i] = 0; + process_lightdata(i, transitiontime[i]); + } + + light_state[0] = true; + test_pwm_state = TEST_PWM_STATE_CH1_INC; + break; + + // ----------------------- // + case TEST_PWM_STATE_CH1_INC: + + if (current_bri[0] < 255) + { + bri[0] += TEST_PWM_CHG_CNT; + transitiontime[0] = 1; + process_lightdata(0, transitiontime[0]); + + } else { + + test_pwm_state = TEST_PWM_STATE_CH1_DEC; + } + break; + + case TEST_PWM_STATE_CH1_DEC: + + if (current_bri[0] > 0) + { + bri[0] -= TEST_PWM_CHG_CNT; + transitiontime[0] = 1; + process_lightdata(0, transitiontime[0]); + + } else { + + light_state[0] = false; + transitiontime[0] = 0; + light_state[1] = true; + test_pwm_state = TEST_PWM_STATE_CH2_INC; + } + break; + + // ----------------------- // + case TEST_PWM_STATE_CH2_INC: + + if (current_bri[1] < 255) + { + bri[1] += TEST_PWM_CHG_CNT; + transitiontime[1] = 1; + process_lightdata(1, transitiontime[1]); + + } else { + + test_pwm_state = TEST_PWM_STATE_CH2_DEC; + } + break; + + case TEST_PWM_STATE_CH2_DEC: + + if (current_bri[1] > 0) + { + bri[1] -= TEST_PWM_CHG_CNT; + transitiontime[1] = 1; + process_lightdata(1, transitiontime[1]); + + } else { + + light_state[1] = false; + transitiontime[1] = 0; + light_state[2] = true; + test_pwm_state = TEST_PWM_STATE_CH3_INC; + } + break; + + // ----------------------- // + case TEST_PWM_STATE_CH3_INC: + + if (current_bri[2] < 255) + { + bri[2] += TEST_PWM_CHG_CNT; + transitiontime[2] = 1; + process_lightdata(2, transitiontime[2]); + + } else { + + test_pwm_state = TEST_PWM_STATE_CH3_DEC; + } + break; + + case TEST_PWM_STATE_CH3_DEC: + + if (current_bri[2] > 0) + { + bri[2] -= TEST_PWM_CHG_CNT; + transitiontime[2] = 1; + process_lightdata(2, transitiontime[2]); + + } else { + + light_state[2] = false; + transitiontime[2] = 0; + light_state[3] = true; + test_pwm_state = TEST_PWM_STATE_CH4_INC; + } + break; + + // ----------------------- // + case TEST_PWM_STATE_CH4_INC: + + if (current_bri[3] < 255) + { + bri[3] += TEST_PWM_CHG_CNT; + transitiontime[3] = 1; + process_lightdata(3, transitiontime[3]); + + } else { + + test_pwm_state = TEST_PWM_STATE_CH4_DEC; + } + break; + + case TEST_PWM_STATE_CH4_DEC: + + if (current_bri[3] > 0) + { + bri[3] -= TEST_PWM_CHG_CNT; + transitiontime[3] = 1; + process_lightdata(3, transitiontime[3]); + + } else { + + test_pwm = false; + tc_enabled = tc_enabled_old; + for (uint8_t i = 0; i < LIGHTS_COUNT; i++) + { + light_state[i] = false; + bri[i] = 0; + current_bri[i] = 0; + current_pwm[i] = 0; + transitiontime[i] = 0; + process_lightdata(i, transitiontime[i]); + } + } + test_pwm_state = TEST_PWM_STATE_INIT; + break; + + // ----------------------- // + default: + test_pwm_state = TEST_PWM_STATE_INIT; + } + + Serial.println("---"); + for (uint8_t i = 0; i < LIGHTS_COUNT; i++) + { + Serial.println("light_state[" + (String)i + "] = " + (String)light_state[i]); + Serial.println("bri[" + (String)i + "] = " + (String)bri[i]); + Serial.println("current_bri[" + (String)i + "] = " + (String)current_bri[i]); + Serial.println("current_pwm[" + (String)i + "] = " + (String)current_pwm[i]); + Serial.println("transitiontime[" + (String)i + "] = " + (String)transitiontime[i]); + } + + } + +} + +//********************************// diff --git a/firmware/html/bottom.js b/firmware/html/bottom.js index 20deffc..91a7463 100644 --- a/firmware/html/bottom.js +++ b/firmware/html/bottom.js @@ -328,8 +328,6 @@ function updateLightState() { 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); - //console.log('data.on ' + i + ' = ' + data.on); - //console.log('data light ' + i + ' = ' + data); if (data.on == true) { onLinkOn.classList.add('pure-button-primary'); onLinkOff.classList.remove('pure-button-primary'); @@ -708,4 +706,43 @@ function showToast(message, type) { }, 5000); } } - \ No newline at end of file + +function sendPWMTestRequest(event) { + + event.preventDefault(); // Verhindert das Standardverhalten des Links + + var button = event.target; // Der geklickte Button + + var xhr = new XMLHttpRequest(); // Erstellt eine XMLHttpRequest-Instanz + xhr.open("GET", 'http://{{IP_ADDRESS}}/test_pwm', true); // Sendet eine GET-Anfrage an die angegebene URL + + xhr.onload = function () { + if (xhr.status == 200 && xhr.responseText === "OK") { + button.innerHTML = "PWM test started"; + button.style.backgroundColor = "green"; // Setzt die Hintergrundfarbe auf Grün + } else { + button.innerHTML = "Error!"; + button.style.backgroundColor = "red"; // Setzt die Hintergrundfarbe auf Rot + } + + // Nach 2 Sekunden den ursprünglichen Text wiederherstellen + setTimeout(function () { + button.innerHTML = "PWM Test"; + button.style.backgroundColor = ""; // Entfernt die Hintergrundfarbe + }, 2000); + }; + + xhr.onerror = function () { + button.innerHTML = "Error!"; + button.style.backgroundColor = "red"; // Setzt die Hintergrundfarbe auf Rot + + // Nach 2 Sekunden den ursprünglichen Text wiederherstellen + setTimeout(function () { + button.innerHTML = "PWM Test"; + button.style.backgroundColor = ""; // Entfernt die Hintergrundfarbe + }, 2000); + }; + + xhr.send(); // Sendet die Anfrage +} + diff --git a/firmware/html/index_template_top.html b/firmware/html/index_template_top.html index 48515ca..9392287 100644 --- a/firmware/html/index_template_top.html +++ b/firmware/html/index_template_top.html @@ -22,6 +22,7 @@ reset light reset timing control data alert + PWM Test

diff --git a/firmware/timing_control.ino b/firmware/timing_control.ino index 67e1258..17fea0a 100644 --- a/firmware/timing_control.ino +++ b/firmware/timing_control.ino @@ -171,7 +171,7 @@ void tc_update_main() bri[i] = 0; current_bri[i] = 0; current_pwm[i] = 0; - transitiontime[i] = 4; + transitiontime[i] = 0; process_lightdata(i, transitiontime[i]); }