Last active
June 22, 2016 19:00
-
-
Save embedded-creations/0c22db0f5deddd4f30bc2652ae0e1261 to your computer and use it in GitHub Desktop.
Demonstrate bug in System.sleep() - will sleep infinitely long with small values of `seconds`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "application.h" | |
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY)); | |
// if you want to change sleepTimeSeconds or reset sleepTimeSeconds back to maxSleepTimeSeconds, change the value of magicWordComparison and reflash application | |
const uint32_t magicWordComparison = 0xDEADBEEF; | |
const unsigned int maxSleepTimeSeconds = 10; | |
retained unsigned int sleepTimeSeconds = maxSleepTimeSeconds; | |
retained uint32_t magicWord = magicWordComparison; | |
void setup() { | |
unsigned int currentSleepTimeSeconds; | |
// simple check for valid retained memory | |
if(magicWord != magicWordComparison) { | |
magicWord = magicWordComparison; | |
sleepTimeSeconds = maxSleepTimeSeconds; | |
} | |
currentSleepTimeSeconds = sleepTimeSeconds; | |
if(--sleepTimeSeconds == 0) | |
sleepTimeSeconds = maxSleepTimeSeconds; | |
// wait for serial | |
delay(10000); | |
Serial.print("connected, going to sleep for "); | |
Serial.print(currentSleepTimeSeconds); | |
Serial.println(" seconds"); | |
System.sleep(SLEEP_MODE_DEEP, currentSleepTimeSeconds); | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment