You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
1.9 KiB
C++
94 lines
1.9 KiB
C++
5 years ago
|
#include "device.h"
|
||
5 years ago
|
#include "settings.h"
|
||
5 years ago
|
|
||
|
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
|
||
5 years ago
|
RTC_DATA_ATTR unsigned int bootCount = 0;
|
||
5 years ago
|
RTC_DATA_ATTR long config_DeepSleepInterval = 10; // sec
|
||
5 years ago
|
|
||
5 years ago
|
unsigned long bootTime = 0;
|
||
4 years ago
|
char DeviceId[21 + 1];
|
||
5 years ago
|
|
||
|
// private methods
|
||
|
void sleepDevice();
|
||
5 years ago
|
bool isBootTimeOver();
|
||
5 years ago
|
|
||
|
/**
|
||
|
* setup deep sleep mode
|
||
|
*/
|
||
|
void setupDevice()
|
||
|
{
|
||
5 years ago
|
// increment boot number and print it every reboot
|
||
|
bootCount++;
|
||
|
bootTime = millis();
|
||
4 years ago
|
|
||
|
// create anonymous device id
|
||
|
sprintf(DeviceId, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", UniqueID8[0], UniqueID8[1], UniqueID8[2], UniqueID8[3], UniqueID8[4], UniqueID8[5], UniqueID8[6], UniqueID8[7]);
|
||
5 years ago
|
}
|
||
|
|
||
|
void loopDevice()
|
||
|
{
|
||
5 years ago
|
if (NVS.getString("device_mode") == "passive")
|
||
|
{
|
||
|
// TODO give user a time window to switch the mode to active
|
||
|
if (true) // isBootTimeOver()
|
||
|
{
|
||
|
sleepDevice();
|
||
|
// device stop here
|
||
|
}
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
|
void sleepDevice()
|
||
|
{
|
||
5 years ago
|
Serial.println("Going to sleep now");
|
||
|
Serial.flush();
|
||
5 years ago
|
|
||
|
esp_sleep_enable_timer_wakeup(config_DeepSleepInterval * uS_TO_S_FACTOR);
|
||
5 years ago
|
esp_deep_sleep_start();
|
||
5 years ago
|
}
|
||
|
|
||
|
void deviceSetSleepInterval(long interval)
|
||
|
{
|
||
|
config_DeepSleepInterval = interval;
|
||
|
}
|
||
|
|
||
|
long deviceGetSleepInterval()
|
||
|
{
|
||
|
return config_DeepSleepInterval;
|
||
|
}
|
||
|
|
||
5 years ago
|
unsigned int deviceGetBootCount()
|
||
|
{
|
||
|
return bootCount;
|
||
|
}
|
||
|
|
||
5 years ago
|
bool isBootTimeOver()
|
||
5 years ago
|
{
|
||
5 years ago
|
return millis() - bootTime >= 60000;
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
String getWakeupReason()
|
||
|
{
|
||
|
esp_sleep_wakeup_cause_t wakeup_reason;
|
||
|
wakeup_reason = esp_sleep_get_wakeup_cause();
|
||
|
|
||
|
//return String(wakeup_reason);
|
||
|
|
||
|
switch (wakeup_reason)
|
||
|
{
|
||
|
case 1:
|
||
|
return String("Wakeup caused by external signal using RTC_IO");
|
||
|
case 2:
|
||
|
return String("Wakeup caused by external signal using RTC_CNTL");
|
||
|
case 3:
|
||
|
return String("Wakeup caused by timer");
|
||
|
case 4:
|
||
|
return String("Wakeup caused by touchpad");
|
||
|
case 5:
|
||
|
return String("Wakeup caused by ULP program");
|
||
|
default:
|
||
|
return String("Wakeup was not caused by deep sleep: " + String(wakeup_reason));
|
||
|
}
|
||
|
|
||
|
return String("unkown");
|
||
5 years ago
|
}
|