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

Add Functional and Scheduled callback option to Ticker #4062

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions libraries/Ticker/Ticker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <stddef.h>
#include <stdint.h>
#include "Arduino.h"

extern "C" {
#include "c_types.h"
Expand Down Expand Up @@ -67,4 +68,18 @@ void Ticker::detach()
os_timer_disarm(_timer);
delete _timer;
_timer = 0;
_callback_function = nullptr;
}

void Ticker::_static_callback(void* arg)
{
Ticker* _this = (Ticker*)arg;
if (_this == nullptr)
{
return;
}
if (_this->_callback_function)
{
_this->_callback_function();
}
}
49 changes: 39 additions & 10 deletions libraries/Ticker/Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <stdint.h>
#include <stddef.h>
#include <functional>
#include <Schedule.h>
Copy link
Member

Choose a reason for hiding this comment

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

This header file is probably only needed in Ticker.cpp


extern "C" {
typedef struct _ETSTIMER_ ETSTimer;
Expand All @@ -34,17 +36,31 @@ class Ticker
public:
Ticker();
~Ticker();

typedef void (*callback_t)(void);
Copy link
Member

Choose a reason for hiding this comment

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

It might be better to keep this definition for compatibility. I ran grep on my sketchbook and found a few uses of Ticker::callback_t. Perhaps others could be using it as well.

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_t callback)
void attach(float seconds, callback_function_t callback)
{
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
_callback_function = callback;
attach(seconds, _static_callback, (void*)this);
}

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

void attach_ms(uint32_t milliseconds, callback_function_t callback)
{
_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)
{
once(seconds, std::bind(schedule_function, callback));
}

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

void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
{
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
once_ms(milliseconds, std::bind(schedule_function, callback));
}

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

template<typename TArg>
Expand All @@ -96,11 +124,12 @@ 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:
ETSTimer* _timer;
};
callback_function_t _callback_function = nullptr;

};

#endif//TICKER_H