Skip to content

Commit b22a803

Browse files
committed
Ticker library
1 parent 4cf6737 commit b22a803

6 files changed

Lines changed: 223 additions & 0 deletions

File tree

libraries/Ticker/Ticker.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stddef.h>
2+
#include <stdint.h>
3+
4+
extern "C" {
5+
#include "c_types.h"
6+
#include "eagle_soc.h"
7+
#include "ets_sys.h"
8+
#include "osapi.h"
9+
}
10+
11+
#include "Ticker.h"
12+
13+
Ticker::Ticker()
14+
: _timer(0)
15+
{
16+
}
17+
18+
Ticker::~Ticker()
19+
{
20+
detach();
21+
}
22+
23+
void Ticker::_attach_ms(callback_with_arg_t callback, uint32_t milliseconds, void* arg)
24+
{
25+
const int REPEAT = 1;
26+
27+
if (_timer)
28+
{
29+
os_timer_disarm(_timer);
30+
}
31+
else
32+
{
33+
_timer = new ETSTimer;
34+
}
35+
36+
os_timer_setfn(_timer, reinterpret_cast<ETSTimerFunc*>(callback), arg);
37+
os_timer_arm(_timer, milliseconds, REPEAT);
38+
}
39+
40+
void Ticker::detach()
41+
{
42+
if (!_timer)
43+
return;
44+
45+
os_timer_disarm(_timer);
46+
delete _timer;
47+
_timer = 0;
48+
}

libraries/Ticker/Ticker.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef TICKER_H
2+
#define TICKER_H
3+
4+
#include <stdint.h>
5+
#include <stddef.h>
6+
7+
extern "C" {
8+
typedef struct _ETSTIMER_ ETSTimer;
9+
}
10+
11+
class Ticker
12+
{
13+
public:
14+
Ticker();
15+
~Ticker();
16+
typedef void (*callback_t)(void);
17+
typedef void (*callback_with_arg_t)(void*);
18+
19+
void attach(callback_t callback, float seconds)
20+
{
21+
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), seconds * 1000, 0);
22+
}
23+
24+
void attach_ms(callback_t callback, uint32_t milliseconds)
25+
{
26+
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), milliseconds, 0);
27+
}
28+
29+
template<typename TArg>
30+
void attach(void (*callback)(TArg), float seconds, TArg arg)
31+
{
32+
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
33+
uint32_t arg32 = static_cast<uint32_t>(arg);
34+
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), seconds * 1000, reinterpret_cast<void*>(arg32));
35+
}
36+
37+
template<typename TArg>
38+
void attach_ms(void (*callback)(TArg), uint32_t milliseconds, TArg arg)
39+
{
40+
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
41+
uint32_t arg32 = static_cast<uint32_t>(arg);
42+
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), milliseconds, reinterpret_cast<void*>(arg32));
43+
}
44+
45+
void detach();
46+
47+
protected:
48+
void _attach_ms(callback_with_arg_t callback, uint32_t milliseconds, void* arg);
49+
50+
51+
protected:
52+
ETSTimer* _timer;
53+
};
54+
55+
56+
#endif//TICKER_H
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Basic Ticker usage
3+
4+
Ticker is an object that will call a given function with a certain period.
5+
Each Ticker calls one function. You can have as many Tickers as you like,
6+
memory being the only limitation.
7+
8+
A function may be attached to a ticker and detached from the ticker.
9+
There are two variants of the attach function: attach and attach_ms.
10+
The first one takes period in seconds, the second one in milliseconds.
11+
12+
An LED connected to GPIO1 will be blinking. Use a built-in LED on ESP-01
13+
or connect an external one to TXD on other boards.
14+
*/
15+
16+
#include <Ticker.h>
17+
18+
Ticker flipper;
19+
20+
int count = 0;
21+
22+
void flip()
23+
{
24+
int state = digitalRead(1); // get the current state of GPIO1 pin
25+
digitalWrite(1, !state); // set pin to the opposite state
26+
27+
++count;
28+
// when the counter reaches a certain value, start blinking like crazy
29+
if (count == 20)
30+
{
31+
flipper.attach(&flip, 0.1);
32+
}
33+
// when the counter reaches yet another value, stop blinking
34+
else if (count == 120)
35+
{
36+
flipper.detach();
37+
}
38+
}
39+
40+
void setup() {
41+
pinMode(1, OUTPUT);
42+
digitalWrite(1, LOW);
43+
44+
// flip the pin every 0.3s
45+
flipper.attach(&flip, 0.3);
46+
}
47+
48+
void loop() {
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Passing paramters to Ticker callbacks
3+
4+
Apart from void(void) functions, the Ticker library supports
5+
functions taking one argument. This argument's size has to be less or
6+
equal to 4 bytes (so char, short, int, float, void*, char* types will do).
7+
8+
This sample runs two tickers that both call one callback function,
9+
but with different arguments.
10+
11+
An LED connected to GPIO1 will be pulsing. Use a built-in LED on ESP-01
12+
or connect an external one to TXD on other boards.
13+
*/
14+
15+
#include <Ticker.h>
16+
17+
Ticker tickerSetHigh;
18+
Ticker tickerSetLow;
19+
20+
void setPin(int state) {
21+
digitalWrite(1, state);
22+
}
23+
24+
void setup() {
25+
pinMode(1, OUTPUT);
26+
digitalWrite(1, LOW);
27+
28+
// call setPin(0) every 25 ms
29+
tickerSetLow.attach_ms(&setPin, 25, 0);
30+
31+
// call setPin(1) every 26 ms
32+
tickerSetHigh.attach_ms(&setPin, 26, 1);
33+
}
34+
35+
void loop() {
36+
}

libraries/Ticker/keywords.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#######################################
2+
# Syntax Coloring Map For Wire
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
#######################################
10+
# Methods and Functions (KEYWORD2)
11+
#######################################
12+
13+
attach KEYWORD2
14+
attach_ms KEYWORD2
15+
detach KEYWORD2
16+
17+
#######################################
18+
# Instances (KEYWORD2)
19+
#######################################
20+
21+
Ticker KEYWORD2
22+
23+
#######################################
24+
# Constants (LITERAL1)
25+
#######################################
26+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name=Ticker
2+
version=1.0
3+
author=Ivan Grokhtokov <igrokhotkov@gmail.com>
4+
maintainer=Ivan Grokhtokov <igrokhotkov@gmail.com>
5+
sentence=Allows to call functions with a given interval.
6+
paragraph=
7+
url=
8+
architectures=esp8266

0 commit comments

Comments
 (0)