Skip to content

Add functional callbacks to ticker #2738

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
wants to merge 4 commits into from
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
14 changes: 14 additions & 0 deletions libraries/Ticker/Ticker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,18 @@ void Ticker::detach()
os_timer_disarm(_timer);
delete _timer;
_timer = 0;
internalTicker = nullptr;
}

void Ticker::internalCallback(void* arg)
{
Ticker* pTicker = (Ticker*)arg;
if (pTicker == nullptr)
{
return;
}
if (pTicker->internalTicker)
{
pTicker->internalTicker();
}
}
29 changes: 29 additions & 0 deletions libraries/Ticker/Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <stdint.h>
#include <stddef.h>
#include <functional>

extern "C" {
typedef struct _ETSTIMER_ ETSTimer;
Expand All @@ -36,12 +37,25 @@ class Ticker
~Ticker();
typedef void (*callback_t)(void);
typedef void (*callback_with_arg_t)(void*);
typedef std::function<void(void)> TickerFunction;

void attach(float seconds, TickerFunction tf)
{
internalTicker = tf;
attach(seconds, internalCallback, (void*)this);
}

void attach(float seconds, callback_t callback)
{
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
}

void attach_ms(uint32_t milliseconds, TickerFunction tf)
{
internalTicker = tf;
attach_ms(milliseconds, internalCallback, (void*)this);
}

void attach_ms(uint32_t milliseconds, callback_t callback)
{
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
Expand All @@ -66,11 +80,23 @@ class Ticker
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}

void once(float seconds, TickerFunction tf)
{
internalTicker = tf;
once(seconds, internalCallback, (void*)this);
}

void once(float seconds, callback_t callback)
{
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
}

void once_ms(uint32_t milliseconds, TickerFunction tf)
{
internalTicker = tf;
once_ms(milliseconds, internalCallback, (void*)this);
}

void once_ms(uint32_t milliseconds, callback_t callback)
{
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
Expand All @@ -96,10 +122,13 @@ class Ticker

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


protected:
ETSTimer* _timer;
TickerFunction internalTicker = nullptr;

};


Expand Down