mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
use a scheduled function for settimeofday_cb (#6600)
* use a scheduled function for settimeofday_cb * per review * use a generic and clear name for trivial functional variable type name used for callbacks
This commit is contained in:
parent
d1b70dfc1d
commit
c55f49bd61
@ -17,7 +17,6 @@ extern bool timeshift64_is_set;
|
|||||||
void esp_yield();
|
void esp_yield();
|
||||||
void esp_schedule();
|
void esp_schedule();
|
||||||
void tune_timeshift64 (uint64_t now_us);
|
void tune_timeshift64 (uint64_t now_us);
|
||||||
void settimeofday_cb (void (*cb)(void));
|
|
||||||
void disable_extra4k_at_link_time (void) __attribute__((noinline));
|
void disable_extra4k_at_link_time (void) __attribute__((noinline));
|
||||||
|
|
||||||
uint32_t sqrt32 (uint32_t n);
|
uint32_t sqrt32 (uint32_t n);
|
||||||
@ -25,6 +24,14 @@ uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff);
|
|||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
using TrivialCB = std::function<void()>;
|
||||||
|
|
||||||
|
void settimeofday_cb (TrivialCB&& cb);
|
||||||
|
void settimeofday_cb (const TrivialCB& cb);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // __COREDECLS_H
|
#endif // __COREDECLS_H
|
||||||
|
@ -42,16 +42,22 @@
|
|||||||
#include <osapi.h>
|
#include <osapi.h>
|
||||||
#include <os_type.h>
|
#include <os_type.h>
|
||||||
#include "coredecls.h"
|
#include "coredecls.h"
|
||||||
|
#include "Schedule.h"
|
||||||
|
|
||||||
extern "C" {
|
static TrivialCB _settimeofday_cb;
|
||||||
|
|
||||||
static void (*_settimeofday_cb)(void) = NULL;
|
void settimeofday_cb (TrivialCB&& cb)
|
||||||
|
{
|
||||||
|
_settimeofday_cb = std::move(cb);
|
||||||
|
}
|
||||||
|
|
||||||
void settimeofday_cb (void (*cb)(void))
|
void settimeofday_cb (const TrivialCB& cb)
|
||||||
{
|
{
|
||||||
_settimeofday_cb = cb;
|
_settimeofday_cb = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
#if LWIP_VERSION_MAJOR == 1
|
#if LWIP_VERSION_MAJOR == 1
|
||||||
|
|
||||||
#include <pgmspace.h>
|
#include <pgmspace.h>
|
||||||
@ -478,7 +484,7 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
|
|||||||
sntp_set_system_time(tv->tv_sec);
|
sntp_set_system_time(tv->tv_sec);
|
||||||
|
|
||||||
if (_settimeofday_cb)
|
if (_settimeofday_cb)
|
||||||
_settimeofday_cb();
|
schedule_function(_settimeofday_cb);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -152,6 +152,10 @@ void printTm(const char* what, const tm* tm) {
|
|||||||
void time_is_set_scheduled() {
|
void time_is_set_scheduled() {
|
||||||
// everything is allowed in this function
|
// everything is allowed in this function
|
||||||
|
|
||||||
|
if (time_machine_days == 0) {
|
||||||
|
time_machine_running = !time_machine_running;
|
||||||
|
}
|
||||||
|
|
||||||
// time machine demo
|
// time machine demo
|
||||||
if (time_machine_running) {
|
if (time_machine_running) {
|
||||||
if (time_machine_days == 0)
|
if (time_machine_days == 0)
|
||||||
@ -178,19 +182,6 @@ void time_is_set_scheduled() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void time_is_set_callback() {
|
|
||||||
// As in an ISR,
|
|
||||||
// it is not allowed to call "heavy" core API
|
|
||||||
// like yield()/delay()/print()/network/...
|
|
||||||
// If this is needed, use a scheduled function.
|
|
||||||
|
|
||||||
// This scheduled function is used for the demo, it is normaly unneeded
|
|
||||||
schedule_function(time_is_set_scheduled);
|
|
||||||
if (time_machine_days == 0) {
|
|
||||||
time_machine_running = !time_machine_running;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println("\nStarting...\n");
|
Serial.println("\nStarting...\n");
|
||||||
@ -204,7 +195,7 @@ void setup() {
|
|||||||
|
|
||||||
// install callback - called when settimeofday is called (by SNTP or us)
|
// install callback - called when settimeofday is called (by SNTP or us)
|
||||||
// once enabled (by DHCP), SNTP is updated every hour
|
// once enabled (by DHCP), SNTP is updated every hour
|
||||||
settimeofday_cb(time_is_set_callback);
|
settimeofday_cb(time_is_set_scheduled);
|
||||||
|
|
||||||
// NTP servers may be overriden by your DHCP server for a more local one
|
// NTP servers may be overriden by your DHCP server for a more local one
|
||||||
// (see below)
|
// (see below)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user