Skip to content

Return error in class deepSleep() when max sleeptime is exceeded #4936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,22 @@ void EspClass::wdtFeed(void)

extern "C" void esp_yield();

void EspClass::deepSleep(uint64_t time_us, WakeMode mode)
bool EspClass::deepSleep(uint64_t time_us, WakeMode mode)
{
system_deep_sleep_set_option(static_cast<int>(mode));
system_deep_sleep(time_us);
esp_yield();
if (time_us > deepSleepMax())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be more useful (and help fix peoples code which is broken already) if, instead of possibly printing an error and return false; , you just printed a warning and adjusted the time down to deepSleepMax()?

Existing code will not expect to ever get a return value and potentially go off into lala-land, since this was a void.

"Be conservative in what you do, be liberal in what you accept from others."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@earlephilhower Point taken. I'll make some changes to the code.

{
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("Error: max sleeptime exceeded");
#endif
return 0; // error: max sleeptime exceeded
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type is bool, but returned value is int => return false here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right.
Strange though that this is not detected by git code check.

}
else
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove else and braces.

{
system_deep_sleep_set_option(static_cast<int>(mode));
system_deep_sleep(time_us);
esp_yield();
return 1; // never gets called
}
}

//this calculation was taken verbatim from the SDK api reference for SDK 2.1.0.
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class EspClass {
void wdtDisable();
void wdtFeed();

void deepSleep(uint64_t time_us, RFMode mode = RF_DEFAULT);
bool deepSleep(uint64_t time_us, RFMode mode = RF_DEFAULT);
uint64_t deepSleepMax();

bool rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size);
Expand Down