Merge branch 'arendst:development' into rename_partitions

This commit is contained in:
Jason2866 2022-05-04 21:06:22 +02:00 committed by GitHub
commit 82e79dffb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,6 +37,7 @@
#endif #endif
const uint32_t DEEPSLEEP_MAX = 10 * 366 * 24 * 60 * 60; // Allow max 10 years sleep const uint32_t DEEPSLEEP_MAX = 10 * 366 * 24 * 60 * 60; // Allow max 10 years sleep
const uint32_t DEEPSLEEP_MAX_CYCLE = 60 * 60; // Maximum time for a deepsleep as defined by chip hardware
const uint32_t DEEPSLEEP_MIN_TIME = 5; // Allow 5 seconds skew const uint32_t DEEPSLEEP_MIN_TIME = 5; // Allow 5 seconds skew
const uint32_t DEEPSLEEP_START_COUNTDOWN = 4; // Allow 4 seconds to update web console before deepsleep const uint32_t DEEPSLEEP_START_COUNTDOWN = 4; // Allow 4 seconds to update web console before deepsleep
@ -46,6 +47,7 @@ const char kDeepsleepCommands[] PROGMEM = D_PRFX_DEEPSLEEP "|"
void (* const DeepsleepCommand[])(void) PROGMEM = { void (* const DeepsleepCommand[])(void) PROGMEM = {
&CmndDeepsleepTime }; &CmndDeepsleepTime };
uint32_t deepsleep_sleeptime = 0;
uint8_t deepsleep_flag = 0; uint8_t deepsleep_flag = 0;
bool DeepSleepEnabled(void) bool DeepSleepEnabled(void)
@ -59,9 +61,34 @@ bool DeepSleepEnabled(void)
pinMode(Pin(GPIO_DEEPSLEEP), INPUT_PULLUP); pinMode(Pin(GPIO_DEEPSLEEP), INPUT_PULLUP);
return (digitalRead(Pin(GPIO_DEEPSLEEP))); // Disable DeepSleep if user holds pin GPIO_DEEPSLEEP low return (digitalRead(Pin(GPIO_DEEPSLEEP))); // Disable DeepSleep if user holds pin GPIO_DEEPSLEEP low
} }
return true; // Enabled return true; // Enabled
} }
void DeepSleepReInit(void)
{
if ((ResetReason() == REASON_DEEP_SLEEP_AWAKE) && DeepSleepEnabled()) {
if ((RtcSettings.ultradeepsleep > DEEPSLEEP_MAX_CYCLE) && (RtcSettings.ultradeepsleep < 1700000000)) {
// Go back to sleep after 60 minutes if requested deepsleep has not been reached
RtcSettings.ultradeepsleep = RtcSettings.ultradeepsleep - DEEPSLEEP_MAX_CYCLE;
AddLog(LOG_LEVEL_ERROR, PSTR("DSL: Remain DeepSleep %d"), RtcSettings.ultradeepsleep);
RtcSettingsSave();
RtcRebootReset();
#ifdef ESP8266
ESP.deepSleep(100 * RtcSettings.deepsleep_slip * (DEEPSLEEP_MAX_CYCLE < RtcSettings.ultradeepsleep ? DEEPSLEEP_MAX_CYCLE : RtcSettings.ultradeepsleep), WAKE_RF_DEFAULT);
#endif // ESP8266
#ifdef ESP32
esp_sleep_enable_timer_wakeup(100 * RtcSettings.deepsleep_slip * (DEEPSLEEP_MAX_CYCLE < RtcSettings.ultradeepsleep ? DEEPSLEEP_MAX_CYCLE : RtcSettings.ultradeepsleep));
esp_deep_sleep_start();
#endif // ESP32
yield();
// Sleeping
}
}
// Stay awake
RtcSettings.ultradeepsleep = 0;
}
void DeepSleepPrepare(void) void DeepSleepPrepare(void)
{ {
// Deepsleep_slip is ideally 10.000 == 100% // Deepsleep_slip is ideally 10.000 == 100%
@ -79,7 +106,7 @@ void DeepSleepPrepare(void)
// Timeslip in 0.1 seconds between the real wakeup and the calculated wakeup // Timeslip in 0.1 seconds between the real wakeup and the calculated wakeup
// Because deepsleep is in second and timeslip in 0.1 sec the compare always check if the slip is in the 10% range // Because deepsleep is in second and timeslip in 0.1 sec the compare always check if the slip is in the 10% range
int32_t timeslip = ((int32_t)RtcSettings.nextwakeup + millis() / 1000 - (int32_t)LocalTime()) * 10; int16_t timeslip = (int16_t)(RtcSettings.nextwakeup + millis() / 1000 - LocalTime()) * 10;
// Allow 10% of deepsleep error to count as valid deepsleep; expecting 3-4% // Allow 10% of deepsleep error to count as valid deepsleep; expecting 3-4%
// if more then 10% timeslip = 0 == non valid wakeup; maybe manual // if more then 10% timeslip = 0 == non valid wakeup; maybe manual
@ -89,7 +116,6 @@ void DeepSleepPrepare(void)
RtcSettings.deepsleep_slip = (RtcSettings.nextwakeup - LocalTime()) * RtcSettings.deepsleep_slip / tmax((Settings->deepsleep - (millis() / 1000)),5); RtcSettings.deepsleep_slip = (RtcSettings.nextwakeup - LocalTime()) * RtcSettings.deepsleep_slip / tmax((Settings->deepsleep - (millis() / 1000)),5);
// Avoid crazy numbers. Again maximum 10% deviation. // Avoid crazy numbers. Again maximum 10% deviation.
RtcSettings.deepsleep_slip = tmin(tmax(RtcSettings.deepsleep_slip, 9000), 11000); RtcSettings.deepsleep_slip = tmin(tmax(RtcSettings.deepsleep_slip, 9000), 11000);
} }
// It may happen that wakeup in just <5 seconds in future // It may happen that wakeup in just <5 seconds in future
@ -100,23 +126,29 @@ void DeepSleepPrepare(void)
} }
String dt = GetDT(RtcSettings.nextwakeup); // 2017-03-07T11:08:02 String dt = GetDT(RtcSettings.nextwakeup); // 2017-03-07T11:08:02
// Limit sleeptime to DEEPSLEEP_MAX_CYCLE
// uint32_t deepsleep_sleeptime = DEEPSLEEP_MAX_CYCLE < (RtcSettings.nextwakeup - LocalTime()) ? (uint32_t)DEEPSLEEP_MAX_CYCLE : RtcSettings.nextwakeup - LocalTime();
deepsleep_sleeptime = tmin((uint32_t)DEEPSLEEP_MAX_CYCLE ,RtcSettings.nextwakeup - LocalTime());
// stat/tasmota/DEEPSLEEP = {"DeepSleep":{"Time":"2019-11-12T21:33:45","Epoch":1573590825}} // stat/tasmota/DEEPSLEEP = {"DeepSleep":{"Time":"2019-11-12T21:33:45","Epoch":1573590825}}
Response_P(PSTR("{\"" D_PRFX_DEEPSLEEP "\":{\"" D_JSON_TIME "\":\"%s\",\"Epoch\":%d}}"), (char*)dt.c_str(), RtcSettings.nextwakeup); Response_P(PSTR("{\"" D_PRFX_DEEPSLEEP "\":{\"" D_JSON_TIME "\":\"%s\",\"Epoch\":%d}}"), (char*)dt.c_str(), RtcSettings.nextwakeup);
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, PSTR(D_PRFX_DEEPSLEEP)); MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, PSTR(D_PRFX_DEEPSLEEP));
// Response_P(S_LWT_OFFLINE);
// MqttPublishPrefixTopicRulesProcess_P(TELE, PSTR(D_LWT), true); // Offline or remove previous retained topic
} }
void DeepSleepStart(void) void DeepSleepStart(void)
{ {
WifiShutdown(); WifiShutdown();
RtcSettings.ultradeepsleep = RtcSettings.nextwakeup - LocalTime();
RtcSettingsSave(); RtcSettingsSave();
RtcRebootReset(); RtcRebootReset();
uint64_t deepsleepticker = 100 * (uint64_t)RtcSettings.deepsleep_slip * (uint64_t)(RtcSettings.nextwakeup>LocalTime()?RtcSettings.nextwakeup - LocalTime():RtcSettings.nextwakeup);
#ifdef ESP8266 #ifdef ESP8266
ESP.deepSleep(deepsleepticker); ESP.deepSleep(100 * RtcSettings.deepsleep_slip * deepsleep_sleeptime);
#endif // ESP8266 #endif // ESP8266
#ifdef ESP32 #ifdef ESP32
esp_sleep_enable_timer_wakeup(deepsleepticker); esp_sleep_enable_timer_wakeup(100 * RtcSettings.deepsleep_slip * deepsleep_sleeptime);
esp_deep_sleep_start(); esp_deep_sleep_start();
#endif // ESP32 #endif // ESP32
yield(); yield();
@ -179,13 +211,16 @@ bool Xdrv29(uint8_t function)
DeepSleepEverySecond(); DeepSleepEverySecond();
break; break;
case FUNC_AFTER_TELEPERIOD: case FUNC_AFTER_TELEPERIOD:
if (DeepSleepEnabled() && !deepsleep_flag && (Settings->tele_period == 10 || Settings->tele_period == 300 || millis() > 20000 )) { if (DeepSleepEnabled() && !deepsleep_flag && (Settings->tele_period == 10 || Settings->tele_period == 300 || millis() > 20000)) {
deepsleep_flag = DEEPSLEEP_START_COUNTDOWN; // Start deepsleep in 4 seconds deepsleep_flag = DEEPSLEEP_START_COUNTDOWN; // Start deepsleep in 4 seconds
} }
break; break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = DecodeCommand(kDeepsleepCommands, DeepsleepCommand); result = DecodeCommand(kDeepsleepCommands, DeepsleepCommand);
break; break;
case FUNC_PRE_INIT:
DeepSleepReInit();
break;
} }
return result; return result;
} }