Skip to content

Commit adde93b

Browse files
hreintkedevyte
authored andcommitted
Implementation of Functional and Scheduled option in Ticker lib (esp8266#5030)
* Implementation of Functional and Scheduled option in Ticker lib * Update example formatting * More example updates * More updates to example * More updates to example
1 parent cbcefa7 commit adde93b

File tree

3 files changed

+115
-9
lines changed

3 files changed

+115
-9
lines changed

libraries/Ticker/Ticker.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,23 @@ void Ticker::detach()
6565
os_timer_disarm(_timer);
6666
delete _timer;
6767
_timer = nullptr;
68+
_callback_function = nullptr;
6869
}
6970

7071
bool Ticker::active()
7172
{
7273
return (bool)_timer;
7374
}
75+
76+
void Ticker::_static_callback(void* arg)
77+
{
78+
Ticker* _this = (Ticker*)arg;
79+
if (_this == nullptr)
80+
{
81+
return;
82+
}
83+
if (_this->_callback_function)
84+
{
85+
_this->_callback_function();
86+
}
87+
}

libraries/Ticker/Ticker.h

+37-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <stdint.h>
2626
#include <stdbool.h>
2727
#include <stddef.h>
28+
#include <functional>
29+
#include <Schedule.h>
2830

2931
extern "C" {
3032
typedef struct _ETSTIMER_ ETSTimer;
@@ -37,15 +39,28 @@ class Ticker
3739
~Ticker();
3840
typedef void (*callback_t)(void);
3941
typedef void (*callback_with_arg_t)(void*);
42+
typedef std::function<void(void)> callback_function_t;
4043

41-
void attach(float seconds, callback_t callback)
44+
void attach_scheduled(float seconds, callback_function_t callback)
4245
{
43-
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
46+
attach(seconds,std::bind(schedule_function, callback));
4447
}
4548

46-
void attach_ms(uint32_t milliseconds, callback_t callback)
49+
void attach(float seconds, callback_function_t callback)
4750
{
48-
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
51+
_callback_function = callback;
52+
attach(seconds, _static_callback, (void*)this);
53+
}
54+
55+
void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
56+
{
57+
attach_ms(milliseconds, std::bind(schedule_function, callback));
58+
}
59+
60+
void attach_ms(uint32_t milliseconds, callback_function_t callback)
61+
{
62+
_callback_function = callback;
63+
attach_ms(milliseconds, _static_callback, (void*)this);
4964
}
5065

5166
template<typename TArg>
@@ -67,14 +82,26 @@ class Ticker
6782
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
6883
}
6984

70-
void once(float seconds, callback_t callback)
85+
void once_scheduled(float seconds, callback_function_t callback)
7186
{
72-
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
87+
once(seconds, std::bind(schedule_function, callback));
7388
}
7489

75-
void once_ms(uint32_t milliseconds, callback_t callback)
90+
void once(float seconds, callback_function_t callback)
7691
{
77-
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
92+
_callback_function = callback;
93+
once(seconds, _static_callback, (void*)this);
94+
}
95+
96+
void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
97+
{
98+
once_ms(milliseconds, std::bind(schedule_function, callback));
99+
}
100+
101+
void once_ms(uint32_t milliseconds, callback_function_t callback)
102+
{
103+
_callback_function = callback;
104+
once_ms(milliseconds, _static_callback, (void*)this);
78105
}
79106

80107
template<typename TArg>
@@ -98,10 +125,11 @@ class Ticker
98125

99126
protected:
100127
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);
101-
128+
static void _static_callback (void* arg);
102129

103130
protected:
104131
ETSTimer* _timer;
132+
callback_function_t _callback_function = nullptr;
105133
};
106134

107135

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "Arduino.h"
2+
#include "Ticker.h"
3+
4+
#define LED1 2
5+
#define LED2 4
6+
#define LED3 12
7+
#define LED4 14
8+
#define LED5 15
9+
10+
11+
class ExampleClass {
12+
public:
13+
ExampleClass(int pin, int duration) : _pin(pin), _duration(duration) {
14+
pinMode(_pin, OUTPUT);
15+
_myTicker.attach_ms(_duration, std::bind(&ExampleClass::classBlink, this));
16+
}
17+
~ExampleClass() {};
18+
19+
int _pin, _duration;
20+
Ticker _myTicker;
21+
22+
void classBlink() {
23+
digitalWrite(_pin, !digitalRead(_pin));
24+
}
25+
};
26+
27+
void staticBlink() {
28+
digitalWrite(LED2, !digitalRead(LED2));
29+
}
30+
31+
void scheduledBlink() {
32+
digitalWrite(LED3, !digitalRead(LED2));
33+
}
34+
35+
void parameterBlink(int p) {
36+
digitalWrite(p, !digitalRead(p));
37+
}
38+
39+
Ticker staticTicker;
40+
Ticker scheduledTicker;
41+
Ticker parameterTicker;
42+
Ticker lambdaTicker;
43+
44+
ExampleClass example(LED1, 100);
45+
46+
47+
void setup() {
48+
pinMode(LED2, OUTPUT);
49+
staticTicker.attach_ms(100, staticBlink);
50+
51+
pinMode(LED3, OUTPUT);
52+
scheduledTicker.attach_ms_scheduled(100, scheduledBlink);
53+
54+
pinMode(LED4, OUTPUT);
55+
parameterTicker.attach_ms(100, std::bind(parameterBlink, LED4));
56+
57+
pinMode(LED5, OUTPUT);
58+
lambdaTicker.attach_ms(100, []() {
59+
digitalWrite(LED5, !digitalRead(LED5));
60+
});
61+
}
62+
63+
void loop() {
64+
}

0 commit comments

Comments
 (0)