Merge sleep command

This commit is contained in:
andrethomas 2018-12-01 18:47:25 +02:00
parent 694c29508d
commit 6050cd40f3
5 changed files with 40 additions and 33 deletions

View File

@ -73,7 +73,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu
uint32_t use_wifi_rescan : 1; // bit 7 (v6.3.0.10)
uint32_t receive_raw : 1; // bit 8 (v6.3.0.11)
uint32_t hass_tele_as_result : 1; // bit 9 (v6.3.0.13)
uint32_t spare10 : 1;
uint32_t sleep_normal : 1; // SetOption60 - Enable normal sleep instead of dynamic sleep
uint32_t spare11 : 1;
uint32_t spare12 : 1;
uint32_t spare13 : 1;

View File

@ -417,7 +417,6 @@ void SettingsDefaultSet2(void)
// Settings.flag.stop_flash_rotate = 0;
Settings.save_data = SAVE_DATA;
Settings.sleep = APP_SLEEP;
Settings.param[P_LOOP_SLEEP_DELAY] = LOOP_SLEEP_DELAY;
// Module
// Settings.flag.interlock = 0;
@ -855,9 +854,10 @@ void SettingsDelta(void)
if (Settings.version < 0x06030004) {
memset(&Settings.drivers, 0xFF, 32); // Enable all possible monitors, displays, drivers and sensors
}
if (Settings.version < 0x0603000A) {
Settings.param[P_LOOP_SLEEP_DELAY] = LOOP_SLEEP_DELAY;
Settings.sleep = 0; // We do not want sleep enabled when SetOption36 is active
if (Settings.version < 0x0603000F) {
if (Settings.sleep < 50) {
Settings.sleep = 50; // Default to 50 for sleep, for now
}
}
if (Settings.version < 0x0603000E) {
Settings.flag2.calc_resolution = CALC_RESOLUTION;

View File

@ -222,7 +222,7 @@ enum ButtonStates { PRESSED, NOT_PRESSED };
enum Shortcuts { SC_CLEAR, SC_DEFAULT, SC_USER };
enum SettingsParmaIndex {P_HOLD_TIME, P_MAX_POWER_RETRY, P_TUYA_DIMMER_ID, P_MDNS_DELAYED_START, P_LOOP_SLEEP_DELAY, P_MAX_PARAM8}; // Max is PARAM8_SIZE (18) - SetOption32 until SetOption49
enum SettingsParmaIndex {P_HOLD_TIME, P_MAX_POWER_RETRY, P_TUYA_DIMMER_ID, P_MDNS_DELAYED_START, P_MAX_PARAM8}; // Max is PARAM8_SIZE (18) - SetOption32 until SetOption49
enum DomoticzSensors {DZ_TEMP, DZ_TEMP_HUM, DZ_TEMP_HUM_BARO, DZ_POWER_ENERGY, DZ_ILLUMINANCE, DZ_COUNT, DZ_VOLTAGE, DZ_CURRENT, DZ_AIRQUALITY, DZ_MAX_SENSORS};

View File

@ -587,11 +587,6 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len)
}
else if (CMND_SLEEP == command_code) {
if ((payload >= 0) && (payload < 251)) {
if (payload > 0) {
snprintf_P(log_data, sizeof(log_data), PSTR("*** WARNING *** - Disabling SetOption36 (Dynamic Sleep) in favour of sleep"));
AddLog(LOG_LEVEL_INFO);
Settings.param[P_LOOP_SLEEP_DELAY] = 0; // We do not want SetOption36 to be active along with traditional sleep
}
Settings.sleep = payload;
sleep = payload;
WiFiSetSleepMode();
@ -750,6 +745,13 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len)
else if (1 == ptype) { // SetOption50 .. 81
if (payload <= 1) {
bitWrite(Settings.flag3.data, pindex, payload);
if (60 == ptype) { // SetOption60 enable or disable traditional sleep
if (payload == 0) { // Dynamic Sleep
WiFiSetSleepMode(); // Update WiFi sleep mode accordingly
} else { // Traditional Sleep //AT
WiFiSetSleepMode(); // Update WiFi sleep mode accordingly
}
}
}
}
else { // SetOption32 .. 49
@ -761,14 +763,6 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len)
param_low = 1;
param_high = 250;
break;
case P_LOOP_SLEEP_DELAY:
if (payload > 0) {
snprintf_P(log_data, sizeof(log_data), PSTR("*** WARNING *** - Disabling sleep in favour of SetOption36 (Dynamic Sleep)"));
AddLog(LOG_LEVEL_INFO);
Settings.sleep = 0; // We do not want traditional sleep to be enabled along side SetOption36
sleep = 0;
WiFiSetSleepMode();
}
}
if ((payload >= param_low) && (payload <= param_high)) {
Settings.param[pindex] = payload;
@ -1590,13 +1584,16 @@ void MqttShowState(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_JSON_VCC "\":%s"), mqtt_data, stemp1);
#endif
if (Settings.param[P_LOOP_SLEEP_DELAY]) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"LoopSet\":%u"), mqtt_data, (uint32_t)Settings.param[P_LOOP_SLEEP_DELAY]); // Add current loop delay target to telemetry
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"LoadAvg\":%u"), mqtt_data, loop_load_avg); // Add LoadAvg to telemetry data
char _sleepmode[8];
if (Settings.flag3.sleep_normal) {
sprintf(_sleepmode,"Normal");
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"Sleep\":%u"), mqtt_data, (uint32_t)sleep); // Add current sleep setting so we know Dynamic Sleep is not enabled
sprintf(_sleepmode,"Dynamic");
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"SleepMode\":\"%s\""), mqtt_data, _sleepmode); // Add current sleep mode setting to telemetry
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"Sleep\":%u"), mqtt_data, sleep); // Add current sleep setting to telemetry
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"LoadAvg\":%u"), mqtt_data, loop_load_avg); // Add LoadAvg to telemetry
for (byte i = 0; i < devices_present; i++) {
if (i == light_device -1) {
LightState(1);
@ -2758,6 +2755,8 @@ void setup(void)
XsnsCall(FUNC_INIT);
}
uint32_t _counter = 0;
void loop(void)
{
uint32_t my_sleep = millis();
@ -2800,22 +2799,25 @@ void loop(void)
while (arduino_ota_triggered) ArduinoOTA.handle();
#endif // USE_ARDUINO_OTA
// yield(); // yield == delay(0), delay contains yield, auto yield in loop
delay(sleep); // https://github.com/esp8266/Arduino/issues/2021
uint32_t my_activity = millis() - my_sleep;
if (my_activity < (uint32_t)Settings.param[P_LOOP_SLEEP_DELAY]) {
delay((uint32_t)Settings.param[P_LOOP_SLEEP_DELAY] - my_activity); // Provide time for background tasks like wifi
if (Settings.flag3.sleep_normal) {
// yield(); // yield == delay(0), delay contains yield, auto yield in loop
delay(sleep); // https://github.com/esp8266/Arduino/issues/2021
} else {
if (global_state.wifi_down) {
delay(my_activity /2); // If wifi down and my_activity > setoption36 then force loop delay to 1/3 of my_activity period
if (my_activity < (uint32_t)sleep) {
delay((uint32_t)sleep - my_activity); // Provide time for background tasks like wifi
} else {
if (global_state.wifi_down) {
delay(my_activity /2); // If wifi down and my_activity > setoption36 then force loop delay to 1/3 of my_activity period
}
}
}
if (!my_activity) { my_activity++; } // We cannot divide by 0
uint32_t loop_delay = Settings.param[P_LOOP_SLEEP_DELAY];
uint32_t loop_delay = sleep;
if (!loop_delay) { loop_delay++; } // We cannot divide by 0
uint32_t loops_per_second = 1000 / loop_delay; // We need to keep track of this many loops per second
uint32_t this_cycle_ratio = 100 * my_activity / loop_delay;
loop_load_avg = loop_load_avg - (loop_load_avg / loops_per_second) + (this_cycle_ratio / loops_per_second); // Take away one loop average away and add the new one
loop_load_avg = loop_load_avg - (loop_load_avg / loops_per_second) + (this_cycle_ratio / loops_per_second); // Take away one loop average away and add the new one
}

View File

@ -181,6 +181,11 @@ void WiFiSetSleepMode(void)
#if defined(ARDUINO_ESP8266_RELEASE_2_4_1) || defined(ARDUINO_ESP8266_RELEASE_2_4_2)
#else // Enabled in 2.3.0, 2.4.0 and stage
if (sleep) {
if (Settings.flag3.sleep_normal) {
WiFi.setSleepMode(WIFI_LIGHT_SLEEP); // Allow light sleep during idle times
} else {
WiFi.setSleepMode(WIFI_MODEM_SLEEP); // Disable sleep (Esp8288/Arduino core and sdk default)
}
WiFi.setSleepMode(WIFI_LIGHT_SLEEP); // Allow light sleep during idle times
} else {
WiFi.setSleepMode(WIFI_MODEM_SLEEP); // Disable sleep (Esp8288/Arduino core and sdk default)