Skip to content

Add Schedule and Functional/Scheduled Ticker #1344

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
78 changes: 78 additions & 0 deletions cores/esp32/Schedule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "Schedule.h"

struct scheduled_fn_t
{
scheduled_fn_t* mNext;
std::function<void(void)> mFunc;
};

static scheduled_fn_t* sFirst = 0;
static scheduled_fn_t* sLast = 0;

static scheduled_fn_t* sFirstUnused = 0;
static scheduled_fn_t* sLastUnused = 0;

static int sCount = 0;

static scheduled_fn_t* get_fn() {
scheduled_fn_t* result = NULL;
// try to get an item from unused items list
if (sFirstUnused) {
result = sFirstUnused;
sFirstUnused = result->mNext;
if (sFirstUnused == NULL) {
sLastUnused = NULL;
}
}
// if no unused items, and count not too high, allocate a new one
else if (sCount != SCHEDULED_FN_MAX_COUNT) {
result = new scheduled_fn_t;
result->mNext = NULL;
++sCount;
}
return result;
}

static void recycle_fn(scheduled_fn_t* fn)
{
if (!sLastUnused) {
sFirstUnused = fn;
}
else {
sLastUnused->mNext = fn;
}
fn->mNext = NULL;
sLastUnused = fn;
}

bool schedule_function(std::function<void(void)> fn)
{
scheduled_fn_t* item = get_fn();
if (!item) {
return false;
}
item->mFunc = fn;
item->mNext = NULL;
if (!sFirst) {
sFirst = item;
}
else {
sLast->mNext = item;
}
sLast = item;
return true;
}

void run_scheduled_functions()
{
scheduled_fn_t* rFirst = sFirst;
sFirst = NULL;
sLast = NULL;
while (rFirst) {
scheduled_fn_t* item = rFirst;
rFirst = item->mNext;
item->mFunc();
item->mFunc = std::function<void(void)>();
recycle_fn(item);
}
}
27 changes: 27 additions & 0 deletions cores/esp32/Schedule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef ESP_SCHEDULE_H
#define ESP_SCHEDULE_H

#include <functional>

#define SCHEDULED_FN_MAX_COUNT 32
#define SCHEDULED_FN_INITIAL_COUNT 4

// Warning
// This API is not considered stable.
// Function signatures will change.
// You have been warned.

// Run given function next time `loop` function returns,
// or `run_scheduled_functions` is called.
// Use std::bind to pass arguments to a function, or call a class member function.
// Note: there is no mechanism for cancelling scheduled functions.
// Keep that in mind when binding functions to objects which may have short lifetime.
// Returns false if the number of scheduled functions exceeds SCHEDULED_FN_MAX_COUNT.
bool schedule_function(std::function<void(void)> fn);

// Run all scheduled functions.
// Use this function if your are not using `loop`, or `loop` does not return
// on a regular basis.
void run_scheduled_functions();

#endif //ESP_SCHEDULE_H
2 changes: 2 additions & 0 deletions cores/esp32/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Arduino.h"
#include "Schedule.h"

#if CONFIG_AUTOSTART_ARDUINO

Expand All @@ -16,6 +17,7 @@ void loopTask(void *pvParameters)
for(;;) {
micros(); //update overflow
loop();
run_scheduled_functions();
}
}

Expand Down
13 changes: 13 additions & 0 deletions libraries/Ticker/src/Ticker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,18 @@ void Ticker::detach() {
esp_timer_stop(_timer);
esp_timer_delete(_timer);
_timer = nullptr;
_callback_function = nullptr;
}
}

void Ticker::_static_callback(void* arg){
Ticker* _this = (Ticker*)arg;
if (_this == nullptr)
{
return;
}
if (_this->_callback_function)
{
_this->_callback_function();
}
}
47 changes: 38 additions & 9 deletions libraries/Ticker/src/Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#ifndef TICKER_H
#define TICKER_H

#include <functional>
#include "Schedule.h"

extern "C" {
#include "esp_timer.h"
}
Expand All @@ -36,15 +39,28 @@ class Ticker
~Ticker();
typedef void (*callback_t)(void);
typedef void (*callback_with_arg_t)(void*);
typedef std::function<void(void)> callback_function_t;

void attach_scheduled(float seconds, callback_function_t callback)
{
attach(seconds,std::bind(schedule_function, callback));
}

void attach(float seconds, callback_function_t callback)
{
_callback_function = callback;
attach(seconds, _static_callback, (void*)this);
}

void attach(float seconds, callback_t callback)
void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
{
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
attach_ms(milliseconds, std::bind(schedule_function, callback));
}

void attach_ms(uint32_t milliseconds, callback_t callback)
void attach_ms(uint32_t milliseconds, callback_function_t callback)
{
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
_callback_function = callback;
attach_ms(milliseconds, _static_callback, (void*)this);
}

template<typename TArg>
Expand All @@ -66,14 +82,26 @@ class Ticker
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}

void once(float seconds, callback_t callback)
void once_scheduled(float seconds, callback_function_t callback)
{
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
once(seconds, std::bind(schedule_function, callback));
}

void once_ms(uint32_t milliseconds, callback_t callback)
void once(float seconds, callback_function_t callback)
{
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
_callback_function = callback;
once(seconds, _static_callback, (void*)this);
}

void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
{
once_ms(milliseconds, std::bind(schedule_function, callback));
}

void once_ms(uint32_t milliseconds, callback_function_t callback)
{
_callback_function = callback;
once_ms(milliseconds, _static_callback, (void*)this);
}

template<typename TArg>
Expand All @@ -97,10 +125,11 @@ class Ticker

protected:
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);

static void _static_callback (void* arg);

protected:
esp_timer_handle_t _timer;
callback_function_t _callback_function = nullptr;
};


Expand Down