Skip to content

Commit 8c37601

Browse files
earlephilhowerdevyte
authored andcommitted
Fix Ticker callback casting. (#6282)
Fixes errors seen in #6281 and adds a slight test case to the examples to ensure no compiler errors.
1 parent 7c67015 commit 8c37601

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

libraries/Ticker/Ticker.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Ticker
3232
Ticker();
3333
~Ticker();
3434

35-
typedef void (*callback_with_arg_t)(void*);
35+
typedef void (*callback_with_arg_t)(void *);
3636
typedef std::function<void(void)> callback_function_t;
3737

3838
void attach_scheduled(float seconds, callback_function_t callback)
@@ -64,14 +64,14 @@ class Ticker
6464
// C-cast serves two purposes:
6565
// static_cast for smaller integer types,
6666
// reinterpret_cast + const_cast for pointer types
67-
_attach_s(seconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
67+
_attach_s(seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
6868
}
6969

7070
template<typename TArg>
7171
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
7272
{
7373
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
74-
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
74+
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
7575
}
7676

7777
void once_scheduled(float seconds, callback_function_t callback)
@@ -100,14 +100,14 @@ class Ticker
100100
void once(float seconds, void (*callback)(TArg), TArg arg)
101101
{
102102
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
103-
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
103+
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
104104
}
105105

106106
template<typename TArg>
107107
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
108108
{
109109
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
110-
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
110+
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
111111
}
112112

113113
void detach();
@@ -122,7 +122,6 @@ class Ticker
122122

123123
private:
124124
void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg);
125-
//char _etsTimerMem[sizeof(ETSTimer)];
126125
ETSTimer _etsTimer;
127126
};
128127

libraries/Ticker/examples/TickerParameter/TickerParameter.ino

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,27 @@
1414
#include <Ticker.h>
1515

1616
Ticker tickerSetHigh;
17+
Ticker tickerSetAnalog;
1718
Ticker tickerSetLow;
1819

1920
void setPin(int state) {
2021
digitalWrite(LED_BUILTIN, state);
2122
}
2223

24+
void setPinChar(char state) {
25+
digitalWrite(LED_BUILTIN, state);
26+
}
27+
2328
void setup() {
2429
pinMode(LED_BUILTIN, OUTPUT);
2530
digitalWrite(1, LOW);
2631

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

30-
// every 26 ms, call setPin(1)
31-
tickerSetHigh.attach_ms(26, setPin, 1);
35+
// every 26 ms, call setPinChar(1)
36+
tickerSetHigh.attach_ms(26, setPinChar, (char)1);
37+
3238
}
3339

3440
void loop() {

0 commit comments

Comments
 (0)