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 += "