Skip to content

Fix Ticker callback casting. #6282

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

Merged
merged 2 commits into from
Jul 11, 2019
Merged
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
11 changes: 5 additions & 6 deletions libraries/Ticker/Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Ticker
Ticker();
~Ticker();

typedef void (*callback_with_arg_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)
Expand Down Expand Up @@ -64,14 +64,14 @@ class Ticker
// C-cast serves two purposes:
// static_cast for smaller integer types,
// reinterpret_cast + const_cast for pointer types
_attach_s(seconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
_attach_s(seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
}

template<typename TArg>
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
}

void once_scheduled(float seconds, callback_function_t callback)
Expand Down Expand Up @@ -100,14 +100,14 @@ class Ticker
void once(float seconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
}

template<typename TArg>
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
}

void detach();
Expand All @@ -122,7 +122,6 @@ class Ticker

private:
void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg);
//char _etsTimerMem[sizeof(ETSTimer)];
ETSTimer _etsTimer;
};

Expand Down
10 changes: 8 additions & 2 deletions libraries/Ticker/examples/TickerParameter/TickerParameter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@
#include <Ticker.h>

Ticker tickerSetHigh;
Ticker tickerSetAnalog;
Ticker tickerSetLow;

void setPin(int state) {
digitalWrite(LED_BUILTIN, state);
}

void setPinChar(char state) {
digitalWrite(LED_BUILTIN, state);
}

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(1, LOW);

// every 25 ms, call setPin(0)
tickerSetLow.attach_ms(25, setPin, 0);

// every 26 ms, call setPin(1)
tickerSetHigh.attach_ms(26, setPin, 1);
// every 26 ms, call setPinChar(1)
tickerSetHigh.attach_ms(26, setPinChar, (char)1);

}

void loop() {
Expand Down