Skip to content

Add Functional and Scheduled callback option to Ticker #4062

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 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,25 @@ 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)
{
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 @@ -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,22 @@ 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)> TickerFunction;

void attach(float seconds, callback_t callback)
void attach(float seconds, TickerFunction tf, bool reqSchedule = false)
Copy link
Member

Choose a reason for hiding this comment

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

I suggest making the case for the scheduled function more explicit.

E.g. attach_scheduled(10, myfunc) reads much better than attach(10, myfunc, true).

Copy link
Member

Choose a reason for hiding this comment

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

Also, why did void attach(float seconds, void (*callback)(TArg), TArg arg) overload not get the version with "scheduled" option?

I would expect to see the product of {attach, once} x {sec, msec} x {no arg, arg} x {normal, scheduled}, i.e. 16 functions.

Copy link

@bertmelis bertmelis Jan 20, 2018

Choose a reason for hiding this comment

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

There's no point in repeatedly attach the same callback as the callback will run after the loop and there no way (yet?) to stop that. Or do I miss something?

Just asking because I'm using the esp8266's interface in my esp32's version of the Ticker lib.

I like the more explicit version!

{
_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 @@ -66,14 +73,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 @@ -96,10 +107,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;
Copy link
Member

Choose a reason for hiding this comment

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

For consistency with the rest of this file, please use snake_case for member and function names, and prefix the member names with an underscore. Also, perhaps _callback would be a better name for this member? internalTicker makes it look like there is an instance of Ticker here.

bool scheduleTicker = false;
Copy link
Member

Choose a reason for hiding this comment

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

Instead of introducing a flag, can we do something like this for the "scheduled" case?

internalTicker = std::bind(schedule_function, tf);

and

internalTicker = tf;

for the normal (not scheduled)?


};


Expand Down