Skip to content
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
21 changes: 21 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"

#include "c_types.h"
#include "eagle_soc.h"
Expand Down Expand Up @@ -71,3 +72,23 @@ bool Ticker::active()
{
return (bool)_timer;
}

void Ticker::internalCallback(void* arg)
{
Ticker* pTicker = (Ticker*)arg;
if (pTicker == nullptr)
{
return;
}
if (pTicker->internalTicker)
{
if (pTicker->scheduleTicker)
{
schedule_function(pTicker->internalTicker);
}
else
{
pTicker->internalTicker();
}
}
}
33 changes: 24 additions & 9 deletions libraries/Ticker/Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <functional>
#include <Schedule.h>

extern "C" {
typedef struct _ETSTIMER_ ETSTimer;
Expand All @@ -35,17 +37,22 @@ class Ticker
public:
Ticker();
~Ticker();
typedef void (*callback_t)(void);

typedef void (*callback_with_arg_t)(void*);
typedef std::function<void(void)> TickerFunction;

void attach(float seconds, callback_t callback)
void attach(float seconds, TickerFunction tf, bool reqSchedule = false)
{
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
internalTicker = tf;
scheduleTicker = reqSchedule;
attach(seconds, internalCallback, (void*)this);
}

void attach_ms(uint32_t milliseconds, callback_t callback)
void attach_ms(uint32_t milliseconds, TickerFunction tf, bool reqSchedule = false)
{
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
internalTicker = tf;
scheduleTicker = reqSchedule;
attach_ms(milliseconds, internalCallback, (void*)this);
}

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

void once(float seconds, callback_t callback)
void once(float seconds, TickerFunction tf, bool reqSchedule = false)
{
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
internalTicker = tf;
scheduleTicker = reqSchedule;
once(seconds, internalCallback, (void*)this);
}

void once_ms(uint32_t milliseconds, callback_t callback)
void once_ms(uint32_t milliseconds, TickerFunction tf, bool reqSchedule = false)
{
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
internalTicker = tf;
scheduleTicker = reqSchedule;
once_ms(milliseconds, internalCallback, (void*)this);
}

template<typename TArg>
Expand All @@ -98,10 +109,14 @@ 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;
bool scheduleTicker = false;

};


Expand Down