From 32bc51caaa01472a3f6125ce0c2ab6d53c8d6a03 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 15 May 2023 14:04:45 +0200 Subject: [PATCH] Fixed some timing controller functionality - handling of the data sets should be better now. Also fixed the funciton which writes the datablocks to the eeprom, it resets the tc engine and also reads out the new written data. --- firmware/timing_control.ino | 64 ++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/firmware/timing_control.ino b/firmware/timing_control.ino index 1b1a5be..2d1a4fb 100644 --- a/firmware/timing_control.ino +++ b/firmware/timing_control.ino @@ -142,12 +142,13 @@ void tc_update_loop() return; } - if ((hour() % 10) != 0 || last_min_check == minute()) // && tc_testOngoing == false + if ((minute() % 10) != 0 || last_min_check == minute()) // && tc_testOngoing == false { - last_min_check = minute(); return; // only run every 10 minutes } + last_min_check = minute(); + tc_update_main(); } @@ -162,12 +163,18 @@ void tc_update_main() tc_updateTime(); + // calculate the current time as minutes + uint16_t time_now = (((uint16_t)hour()) * 60) + minute(); + // search for the current active time slot for (int i = NUMBER_OF_TIMER_DATA_BLOCKS-1; i >= 0 && target_data_block == 255; --i) { //Serial.println((String)i + " - " + (String)tc_data[i].hh + ":" + (String)tc_data[i].mm); - if (((tc_data[i].hh * 60) + tc_data[i].mm) <= ((hour() * 60) + minute())) + // calculate the time of the data block to minutes + uint16_t time_tc_data = (((uint16_t)tc_data[i].hh) * 60) + tc_data[i].mm; + + if (time_now >= time_tc_data) { target_data_block = i+1; // found the next block to load //Serial.println((String)i + " => " + target_data_block); @@ -179,14 +186,13 @@ void tc_update_main() // no new predecessor or successor found, start over current_target_data_block = 255; - Serial.println("No predecessor or successor found, start over..."); + Serial.println("No predecessor or successor found."); // disable the lights for (uint8_t i = 0; i < LIGHTS_COUNT; i++) { - light_state[i] = false; bri[i] = 0; - current_bri[i] = 4; // set it to a value which forces the light wngine to switch of the pwm signal pf the gpio + current_bri[i] = 1; // set it to a value to force the light engine to fix the current brightness transitiontime[i] = default_transitiontime; process_lightdata(i, transitiontime[i]); } @@ -206,19 +212,16 @@ void tc_update_main() if (target_data_block >= NUMBER_OF_TIMER_DATA_BLOCKS) { - // we are not between two valid data points - // we are at the last element of the data block list - Serial.println("tdb is beyond data blocks length, abort operation"); - + // we are not between two valid data points, do nothing + Serial.println("tdb is >= num data blocks, abort operation..."); for (uint8_t i = 0; i < LIGHTS_COUNT; i++) { light_state[i] = false; bri[i] = 0; - current_bri[i] = 4; // set it to a value which forces the light wngine to switch of the pwm signal pf the gpio + current_bri[i] = 1; // set it to a value to force the light engine to fix the current brightness transitiontime[i] = default_transitiontime; process_lightdata(i, transitiontime[i]); } - target_data_block = 255; current_target_data_block = 255; return; @@ -251,21 +254,23 @@ void tc_update_main() bri[1] = tc_data[target_data_block].ch2; bri[2] = tc_data[target_data_block].ch3; bri[3] = tc_data[target_data_block].ch4; + + // make sure that the current brightness is correct if (tc_data[target_data_block-1].ch1 != current_bri[0]) { - current_bri[0] = tc_data[target_data_block-1].ch1 + ((tc_data[target_data_block].ch1 == 0) ? 1 : -1); + current_bri[0] = tc_data[target_data_block-1].ch1; } if (tc_data[target_data_block-1].ch2 != current_bri[1]) { - current_bri[1] = tc_data[target_data_block-1].ch2 + ((tc_data[target_data_block].ch2 == 0) ? 1 : -1); + current_bri[1] = tc_data[target_data_block-1].ch2; } if (tc_data[target_data_block-1].ch3 != current_bri[2]) { - current_bri[2] = tc_data[target_data_block-1].ch3 + ((tc_data[target_data_block].ch3 == 0) ? 1 : -1); + current_bri[2] = tc_data[target_data_block-1].ch3; } if (tc_data[target_data_block-1].ch4 != current_bri[3]) { - current_bri[3] = tc_data[target_data_block-1].ch4 + ((tc_data[target_data_block].ch4 == 0) ? 1 : -1); + current_bri[3] = tc_data[target_data_block-1].ch4; } for (uint8_t i = 0; i < LIGHTS_COUNT; i++) @@ -285,24 +290,34 @@ void tc_update_main() } // set the transition time - int t_time = 0; + int t_time = default_transitiontime; if (target_data_block > 0) { // hours as seconds from now on to the next enabled block - t_time = ((uint16_t)tc_data[target_data_block].hh * 60 * 60) - ((uint16_t)hour() * 60 * 60); + t_time = ((uint32_t)tc_data[target_data_block].hh * 60 * 60) - ((uint32_t)hour() * 60 * 60); // add the left over seconds to the next enabled block t_time += ((uint16_t)tc_data[target_data_block].mm * 60) - ((uint16_t)minute() * 60); + if (t_time <= 0) + { + t_time = 1; // 0 could lead to a division by zero + } + } + for (uint8_t i = 0; i < LIGHTS_COUNT; i++) + { + transitiontime[i] = t_time; } - transitiontime[0] = t_time; - transitiontime[1] = t_time; - transitiontime[2] = t_time; - transitiontime[3] = t_time; // calculate the step level for (uint8_t i = 0; i < LIGHTS_COUNT; i++) { process_lightdata(i, transitiontime[i]); + + // set the PWM for the channel + current_pwm[i] = calcPWM(current_bri[i]); + //Serial.println("lon: pin" + (String)i + " = PWM(" + (String)tmp_pwm + ")"); + analogWrite(pins[i], current_pwm[i]); + Serial.println("transitiontime[" + (String)i + "] = " + (String)transitiontime[i]); } for (uint8_t i = 0; i < LIGHTS_COUNT; i++) @@ -530,6 +545,11 @@ void tc_jsonDataBlocksToEEPROM(String json_data_string) } EEPROM.commit(); + + // reset the prograss in the timing control engine + tc_reset(); + // call the function which reads out the new set data + tc_update_main(); } //********************************//