Skip to content
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

delay() refactoring #6212

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ unsigned long millis(void);
unsigned long micros(void);
uint64_t micros64(void);
void delay(unsigned long);
void interruptable_delay(unsigned long ms);
void delayMicroseconds(unsigned int us);
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
Expand Down
19 changes: 18 additions & 1 deletion cores/esp8266/core_esp8266_wiring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "osapi.h"
#include "user_interface.h"
#include "cont.h"
#include <PolledTimeout.h>

extern "C" {

Expand All @@ -43,7 +44,7 @@ void delay_end(void* arg) {
esp_schedule();
}

void delay(unsigned long ms) {
void interruptable_delay(unsigned long ms) {
if(ms) {
os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0);
os_timer_arm(&delay_timer, ms, ONCE);
Expand All @@ -56,6 +57,22 @@ void delay(unsigned long ms) {
}
}

void delay(unsigned long ms) {
if (ms) {
using esp8266::polledTimeout::oneShotMs;
oneShotMs waitDone(ms);
while(!waitDone) {
os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0);
os_timer_arm(&delay_timer, 1, ONCE);
esp_yield();
os_timer_disarm(&delay_timer);
}
} else {
esp_schedule();
esp_yield();
}
}

void micros_overflow_tick(void* arg) {
(void) arg;
uint32_t m = system_get_time();
Expand Down
7 changes: 6 additions & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,11 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
aResult = IPAddress(&addr);
} else if(err == ERR_INPROGRESS) {
_dns_lookup_pending = true;
delay(timeout_ms);
// Following delay will be interrupted by dns found callback
for (decltype(timeout_ms) i = 0; _dns_lookup_pending && i < timeout_ms; i++) {
// let a chance to recurrent scheduled functions (ethernet)
delay(1);
}
_dns_lookup_pending = false;
// will return here when dns_found_callback fires
if(aResult.isSet()) {
Expand Down Expand Up @@ -597,6 +601,7 @@ void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *ca
if(ipaddr) {
(*reinterpret_cast<IPAddress*>(callback_arg)) = IPAddress(ipaddr);
}
_dns_lookup_pending = false;
esp_schedule(); // resume the hostByName function
}

Expand Down