Skip to content

Commit 3417f08

Browse files
committed
Avoid unnecessary parameter passing - refactoring, same generated footprint.
1 parent f78c774 commit 3417f08

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

libraries/Ticker/examples/TickerParameter/TickerParameter.ino

+16-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@
1313

1414
#include <Ticker.h>
1515

16-
Ticker tickerSetHigh;
1716
Ticker tickerSetLow;
17+
Ticker tickerSetHigh;
18+
Ticker tickerSetChar;
19+
20+
void setPinLow() {
21+
digitalWrite(LED_BUILTIN, 0);
22+
}
23+
24+
void setPinHigh() {
25+
digitalWrite(LED_BUILTIN, 1);
26+
}
1827

1928
void setPin(int state) {
2029
digitalWrite(LED_BUILTIN, state);
@@ -27,12 +36,14 @@ void setPinChar(char state) {
2736
void setup() {
2837
pinMode(LED_BUILTIN, OUTPUT);
2938

30-
// every 25 ms, call setPin(0)
31-
tickerSetLow.attach_ms(25, setPin, 0);
39+
// every 25 ms, call setPinLow()
40+
tickerSetLow.attach_ms(25, setPinLow);
3241

33-
// every 26 ms, call setPinChar(1)
34-
tickerSetHigh.attach_ms(26, setPinChar, (char)1);
42+
// every 26 ms, call setPinHigh()
43+
tickerSetHigh.attach_ms(26, setPinHigh);
3544

45+
// every 54 ms, call setPinChar(1)
46+
tickerSetChar.attach_ms(26, setPinChar, (char)1);
3647
}
3748

3849
void loop() {

libraries/Ticker/src/Ticker.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,25 @@ class Ticker
3838
void attach_scheduled(float seconds, callback_function_t callback)
3939
{
4040
_callback_function = [callback]() { schedule_function(callback); };
41-
_attach_ms(1000UL * seconds, true, _static_callback, this);
41+
_attach_ms(1000UL * seconds, true);
4242
}
4343

4444
void attach(float seconds, callback_function_t callback)
4545
{
4646
_callback_function = std::move(callback);
47-
_attach_ms(1000UL * seconds, true, _static_callback, this);
47+
_attach_ms(1000UL * seconds, true);
4848
}
4949

5050
void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
5151
{
5252
_callback_function = [callback]() { schedule_function(callback); };
53-
_attach_ms(milliseconds, true, _static_callback, this);
53+
_attach_ms(milliseconds, true);
5454
}
5555

5656
void attach_ms(uint32_t milliseconds, callback_function_t callback)
5757
{
5858
_callback_function = std::move(callback);
59-
_attach_ms(milliseconds, true, _static_callback, this);
59+
_attach_ms(milliseconds, true);
6060
}
6161

6262
template<typename TArg>
@@ -76,25 +76,25 @@ class Ticker
7676
void once_scheduled(float seconds, callback_function_t callback)
7777
{
7878
_callback_function = [callback]() { schedule_function(callback); };
79-
_attach_ms(1000UL * seconds, false, _static_callback, this);
79+
_attach_ms(1000UL * seconds, false);
8080
}
8181

8282
void once(float seconds, callback_function_t callback)
8383
{
8484
_callback_function = std::move(callback);
85-
_attach_ms(1000UL * seconds, false, _static_callback, this);
85+
_attach_ms(1000UL * seconds, false);
8686
}
8787

8888
void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
8989
{
9090
_callback_function = [callback]() { schedule_function(callback); };
91-
_attach_ms(milliseconds, false, _static_callback, this);
91+
_attach_ms(milliseconds, false);
9292
}
9393

9494
void once_ms(uint32_t milliseconds, callback_function_t callback)
9595
{
9696
_callback_function = std::move(callback);
97-
_attach_ms(milliseconds, false, _static_callback, this);
97+
_attach_ms(milliseconds, false);
9898
}
9999

100100
template<typename TArg>
@@ -117,6 +117,10 @@ class Ticker
117117
protected:
118118
static void _static_callback(void* arg);
119119
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg);
120+
void _attach_ms(uint32_t milliseconds, bool repeat)
121+
{
122+
_attach_ms(milliseconds, repeat, _static_callback, this);
123+
}
120124

121125
ETSTimer* _timer;
122126
callback_function_t _callback_function = nullptr;

0 commit comments

Comments
 (0)