Skip to content

Commit 482516e

Browse files
dok-netdevyte
authored andcommitted
Minimize header use, move Ticker function definitions into cpp file (#6496)
* Backport from ESP32 * Use new library layout (.../src) * Cleanup test case. * C++ style cast required. * Whitespace * Inlining via header has better baseline ROM footprint. * Reordered functions for better code-compare to master * Reduces ROM footprint some more. * Avoid unnecessary parameter passing - refactoring, same generated footprint. * Reformat example sources
1 parent af85bd2 commit 482516e

File tree

5 files changed

+173
-186
lines changed

5 files changed

+173
-186
lines changed

libraries/Ticker/Ticker.h

-129
This file was deleted.

libraries/Ticker/examples/TickerParameter/TickerParameter.ino

+16-7
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@
1313

1414
#include <Ticker.h>
1515

16-
Ticker tickerSetHigh;
17-
Ticker tickerSetAnalog;
1816
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+
}
1927

2028
void setPin(int state) {
2129
digitalWrite(LED_BUILTIN, state);
@@ -27,14 +35,15 @@ void setPinChar(char state) {
2735

2836
void setup() {
2937
pinMode(LED_BUILTIN, OUTPUT);
30-
digitalWrite(1, LOW);
3138

32-
// every 25 ms, call setPin(0)
33-
tickerSetLow.attach_ms(25, setPin, 0);
39+
// every 25 ms, call setPinLow()
40+
tickerSetLow.attach_ms(25, setPinLow);
3441

35-
// every 26 ms, call setPinChar(1)
36-
tickerSetHigh.attach_ms(26, setPinChar, (char)1);
42+
// every 26 ms, call setPinHigh()
43+
tickerSetHigh.attach_ms(26, setPinHigh);
3744

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

4049
void loop() {

libraries/Ticker/keywords.txt

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#######################################
2-
# Syntax Coloring Map For Wire
3-
#######################################
4-
51
#######################################
62
# Datatypes (KEYWORD1)
73
#######################################
84

5+
Ticker KEYWORD1
6+
97
#######################################
108
# Methods and Functions (KEYWORD2)
119
#######################################
@@ -16,14 +14,3 @@ once KEYWORD2
1614
once_ms KEYWORD2
1715
detach KEYWORD2
1816
active KEYWORD2
19-
20-
#######################################
21-
# Instances (KEYWORD2)
22-
#######################################
23-
24-
Ticker KEYWORD2
25-
26-
#######################################
27-
# Constants (LITERAL1)
28-
#######################################
29-

libraries/Ticker/Ticker.cpp renamed to libraries/Ticker/src/Ticker.cpp

+22-35
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,47 @@
2525

2626
#include "Ticker.h"
2727

28-
namespace
29-
{
30-
constexpr int ONCE = 0;
31-
constexpr int REPEAT = 1;
32-
}
33-
3428
Ticker::Ticker()
35-
: _timer(nullptr)
36-
{
37-
}
29+
: _timer(nullptr) {}
3830

3931
Ticker::~Ticker()
4032
{
41-
detach();
42-
}
43-
44-
void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg)
45-
{
46-
_attach_ms(1000 * seconds, repeat, callback, arg);
33+
detach();
4734
}
4835

4936
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg)
5037
{
51-
if (_timer)
52-
{
53-
os_timer_disarm(_timer);
54-
}
55-
else
56-
{
57-
_timer = &_etsTimer;
58-
}
59-
60-
os_timer_setfn(_timer, callback, arg);
61-
os_timer_arm(_timer, milliseconds, (repeat) ? REPEAT : ONCE);
38+
if (_timer)
39+
{
40+
os_timer_disarm(_timer);
41+
}
42+
else
43+
{
44+
_timer = &_etsTimer;
45+
}
46+
47+
os_timer_setfn(_timer, callback, arg);
48+
os_timer_arm(_timer, milliseconds, repeat);
6249
}
6350

6451
void Ticker::detach()
6552
{
66-
if (!_timer)
67-
return;
53+
if (!_timer)
54+
return;
6855

69-
os_timer_disarm(_timer);
70-
_timer = nullptr;
71-
_callback_function = nullptr;
56+
os_timer_disarm(_timer);
57+
_timer = nullptr;
58+
_callback_function = nullptr;
7259
}
7360

7461
bool Ticker::active() const
7562
{
76-
return _timer;
63+
return _timer;
7764
}
7865

7966
void Ticker::_static_callback(void* arg)
8067
{
81-
Ticker* _this = reinterpret_cast<Ticker*>(arg);
82-
if (_this && _this->_callback_function)
83-
_this->_callback_function();
68+
Ticker* _this = reinterpret_cast<Ticker*>(arg);
69+
if (_this && _this->_callback_function)
70+
_this->_callback_function();
8471
}

0 commit comments

Comments
 (0)