Skip to content

Commit

Permalink
Fix Ticker callback casting. (#6282)
Browse files Browse the repository at this point in the history
Fixes errors seen in #6281 and adds a slight test case to the examples
to ensure no compiler errors.
  • Loading branch information
earlephilhower authored and devyte committed Jul 11, 2019
1 parent 7c67015 commit 8c37601
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
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

0 comments on commit 8c37601

Please sign in to comment.