mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
Ticker library
This commit is contained in:
parent
4cf6737823
commit
b22a803d53
48
libraries/Ticker/Ticker.cpp
Normal file
48
libraries/Ticker/Ticker.cpp
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "c_types.h"
|
||||||
|
#include "eagle_soc.h"
|
||||||
|
#include "ets_sys.h"
|
||||||
|
#include "osapi.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "Ticker.h"
|
||||||
|
|
||||||
|
Ticker::Ticker()
|
||||||
|
: _timer(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Ticker::~Ticker()
|
||||||
|
{
|
||||||
|
detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ticker::_attach_ms(callback_with_arg_t callback, uint32_t milliseconds, void* arg)
|
||||||
|
{
|
||||||
|
const int REPEAT = 1;
|
||||||
|
|
||||||
|
if (_timer)
|
||||||
|
{
|
||||||
|
os_timer_disarm(_timer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_timer = new ETSTimer;
|
||||||
|
}
|
||||||
|
|
||||||
|
os_timer_setfn(_timer, reinterpret_cast<ETSTimerFunc*>(callback), arg);
|
||||||
|
os_timer_arm(_timer, milliseconds, REPEAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ticker::detach()
|
||||||
|
{
|
||||||
|
if (!_timer)
|
||||||
|
return;
|
||||||
|
|
||||||
|
os_timer_disarm(_timer);
|
||||||
|
delete _timer;
|
||||||
|
_timer = 0;
|
||||||
|
}
|
56
libraries/Ticker/Ticker.h
Normal file
56
libraries/Ticker/Ticker.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#ifndef TICKER_H
|
||||||
|
#define TICKER_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
typedef struct _ETSTIMER_ ETSTimer;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Ticker
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Ticker();
|
||||||
|
~Ticker();
|
||||||
|
typedef void (*callback_t)(void);
|
||||||
|
typedef void (*callback_with_arg_t)(void*);
|
||||||
|
|
||||||
|
void attach(callback_t callback, float seconds)
|
||||||
|
{
|
||||||
|
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), seconds * 1000, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void attach_ms(callback_t callback, uint32_t milliseconds)
|
||||||
|
{
|
||||||
|
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), milliseconds, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TArg>
|
||||||
|
void attach(void (*callback)(TArg), float seconds, TArg arg)
|
||||||
|
{
|
||||||
|
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
|
||||||
|
uint32_t arg32 = static_cast<uint32_t>(arg);
|
||||||
|
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), seconds * 1000, reinterpret_cast<void*>(arg32));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TArg>
|
||||||
|
void attach_ms(void (*callback)(TArg), uint32_t milliseconds, TArg arg)
|
||||||
|
{
|
||||||
|
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
|
||||||
|
uint32_t arg32 = static_cast<uint32_t>(arg);
|
||||||
|
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), milliseconds, reinterpret_cast<void*>(arg32));
|
||||||
|
}
|
||||||
|
|
||||||
|
void detach();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void _attach_ms(callback_with_arg_t callback, uint32_t milliseconds, void* arg);
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ETSTimer* _timer;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif//TICKER_H
|
49
libraries/Ticker/examples/TickerBasic/TickerBasic.ino
Normal file
49
libraries/Ticker/examples/TickerBasic/TickerBasic.ino
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
Basic Ticker usage
|
||||||
|
|
||||||
|
Ticker is an object that will call a given function with a certain period.
|
||||||
|
Each Ticker calls one function. You can have as many Tickers as you like,
|
||||||
|
memory being the only limitation.
|
||||||
|
|
||||||
|
A function may be attached to a ticker and detached from the ticker.
|
||||||
|
There are two variants of the attach function: attach and attach_ms.
|
||||||
|
The first one takes period in seconds, the second one in milliseconds.
|
||||||
|
|
||||||
|
An LED connected to GPIO1 will be blinking. Use a built-in LED on ESP-01
|
||||||
|
or connect an external one to TXD on other boards.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
|
Ticker flipper;
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
void flip()
|
||||||
|
{
|
||||||
|
int state = digitalRead(1); // get the current state of GPIO1 pin
|
||||||
|
digitalWrite(1, !state); // set pin to the opposite state
|
||||||
|
|
||||||
|
++count;
|
||||||
|
// when the counter reaches a certain value, start blinking like crazy
|
||||||
|
if (count == 20)
|
||||||
|
{
|
||||||
|
flipper.attach(&flip, 0.1);
|
||||||
|
}
|
||||||
|
// when the counter reaches yet another value, stop blinking
|
||||||
|
else if (count == 120)
|
||||||
|
{
|
||||||
|
flipper.detach();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(1, OUTPUT);
|
||||||
|
digitalWrite(1, LOW);
|
||||||
|
|
||||||
|
// flip the pin every 0.3s
|
||||||
|
flipper.attach(&flip, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
Passing paramters to Ticker callbacks
|
||||||
|
|
||||||
|
Apart from void(void) functions, the Ticker library supports
|
||||||
|
functions taking one argument. This argument's size has to be less or
|
||||||
|
equal to 4 bytes (so char, short, int, float, void*, char* types will do).
|
||||||
|
|
||||||
|
This sample runs two tickers that both call one callback function,
|
||||||
|
but with different arguments.
|
||||||
|
|
||||||
|
An LED connected to GPIO1 will be pulsing. Use a built-in LED on ESP-01
|
||||||
|
or connect an external one to TXD on other boards.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
|
Ticker tickerSetHigh;
|
||||||
|
Ticker tickerSetLow;
|
||||||
|
|
||||||
|
void setPin(int state) {
|
||||||
|
digitalWrite(1, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(1, OUTPUT);
|
||||||
|
digitalWrite(1, LOW);
|
||||||
|
|
||||||
|
// call setPin(0) every 25 ms
|
||||||
|
tickerSetLow.attach_ms(&setPin, 25, 0);
|
||||||
|
|
||||||
|
// call setPin(1) every 26 ms
|
||||||
|
tickerSetHigh.attach_ms(&setPin, 26, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
}
|
26
libraries/Ticker/keywords.txt
Normal file
26
libraries/Ticker/keywords.txt
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#######################################
|
||||||
|
# Syntax Coloring Map For Wire
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Datatypes (KEYWORD1)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Methods and Functions (KEYWORD2)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
attach KEYWORD2
|
||||||
|
attach_ms KEYWORD2
|
||||||
|
detach KEYWORD2
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Instances (KEYWORD2)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
Ticker KEYWORD2
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Constants (LITERAL1)
|
||||||
|
#######################################
|
||||||
|
|
8
libraries/Ticker/library.properties
Normal file
8
libraries/Ticker/library.properties
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
name=Ticker
|
||||||
|
version=1.0
|
||||||
|
author=Ivan Grokhtokov <igrokhotkov@gmail.com>
|
||||||
|
maintainer=Ivan Grokhtokov <igrokhotkov@gmail.com>
|
||||||
|
sentence=Allows to call functions with a given interval.
|
||||||
|
paragraph=
|
||||||
|
url=
|
||||||
|
architectures=esp8266
|
Loading…
x
Reference in New Issue
Block a user