mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
Ticker library update
This commit is contained in:
parent
9ba2fcd29f
commit
687e472b4b
@ -29,6 +29,9 @@ extern "C" {
|
|||||||
#include "osapi.h"
|
#include "osapi.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const int ONCE = 0;
|
||||||
|
const int REPEAT = 1;
|
||||||
|
|
||||||
#include "Ticker.h"
|
#include "Ticker.h"
|
||||||
|
|
||||||
Ticker::Ticker()
|
Ticker::Ticker()
|
||||||
@ -41,10 +44,8 @@ Ticker::~Ticker()
|
|||||||
detach();
|
detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ticker::_attach_ms(callback_with_arg_t callback, uint32_t milliseconds, void* arg)
|
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg)
|
||||||
{
|
{
|
||||||
const int REPEAT = 1;
|
|
||||||
|
|
||||||
if (_timer)
|
if (_timer)
|
||||||
{
|
{
|
||||||
os_timer_disarm(_timer);
|
os_timer_disarm(_timer);
|
||||||
@ -54,8 +55,8 @@ void Ticker::_attach_ms(callback_with_arg_t callback, uint32_t milliseconds, voi
|
|||||||
_timer = new ETSTimer;
|
_timer = new ETSTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
os_timer_setfn(_timer, reinterpret_cast<ETSTimerFunc*>(callback), arg);
|
os_timer_setfn(_timer, reinterpret_cast<ETSTimerFunc*>(callback), reinterpret_cast<void*>(arg));
|
||||||
os_timer_arm(_timer, milliseconds, REPEAT);
|
os_timer_arm(_timer, milliseconds, (repeat)?REPEAT:ONCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ticker::detach()
|
void Ticker::detach()
|
||||||
|
@ -37,36 +37,65 @@ public:
|
|||||||
typedef void (*callback_t)(void);
|
typedef void (*callback_t)(void);
|
||||||
typedef void (*callback_with_arg_t)(void*);
|
typedef void (*callback_with_arg_t)(void*);
|
||||||
|
|
||||||
void attach(callback_t callback, float seconds)
|
void attach(float seconds, callback_t callback)
|
||||||
{
|
{
|
||||||
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), seconds * 1000, 0);
|
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void attach_ms(callback_t callback, uint32_t milliseconds)
|
void attach_ms(uint32_t milliseconds, callback_t callback)
|
||||||
{
|
{
|
||||||
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), milliseconds, 0);
|
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TArg>
|
template<typename TArg>
|
||||||
void attach(void (*callback)(TArg), float seconds, TArg arg)
|
void attach(float seconds, void (*callback)(TArg), TArg arg)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
|
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
|
||||||
uint32_t arg32 = static_cast<uint32_t>(arg);
|
// C-cast serves two purposes:
|
||||||
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), seconds * 1000, reinterpret_cast<void*>(arg32));
|
// static_cast for smaller integer types,
|
||||||
|
// reinterpret_cast + const_cast for pointer types
|
||||||
|
uint32_t arg32 = (uint32_t)arg;
|
||||||
|
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TArg>
|
template<typename TArg>
|
||||||
void attach_ms(void (*callback)(TArg), uint32_t milliseconds, TArg arg)
|
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
|
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
|
||||||
uint32_t arg32 = static_cast<uint32_t>(arg);
|
uint32_t arg32 = (uint32_t)arg;
|
||||||
_attach_ms(reinterpret_cast<callback_with_arg_t>(callback), milliseconds, reinterpret_cast<void*>(arg32));
|
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
|
||||||
|
}
|
||||||
|
|
||||||
|
void once(float seconds, callback_t callback)
|
||||||
|
{
|
||||||
|
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void once_ms(uint32_t milliseconds, callback_t callback)
|
||||||
|
{
|
||||||
|
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TArg>
|
||||||
|
void once(float seconds, void (*callback)(TArg), TArg arg)
|
||||||
|
{
|
||||||
|
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
|
||||||
|
uint32_t arg32 = (uint32_t)(arg);
|
||||||
|
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TArg>
|
||||||
|
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
|
||||||
|
{
|
||||||
|
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
|
||||||
|
uint32_t arg32 = (uint32_t)(arg);
|
||||||
|
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
|
||||||
}
|
}
|
||||||
|
|
||||||
void detach();
|
void detach();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void _attach_ms(callback_with_arg_t callback, uint32_t milliseconds, void* arg);
|
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -28,7 +28,7 @@ void flip()
|
|||||||
// when the counter reaches a certain value, start blinking like crazy
|
// when the counter reaches a certain value, start blinking like crazy
|
||||||
if (count == 20)
|
if (count == 20)
|
||||||
{
|
{
|
||||||
flipper.attach(&flip, 0.1);
|
flipper.attach(0.1, flip);
|
||||||
}
|
}
|
||||||
// when the counter reaches yet another value, stop blinking
|
// when the counter reaches yet another value, stop blinking
|
||||||
else if (count == 120)
|
else if (count == 120)
|
||||||
@ -42,7 +42,7 @@ void setup() {
|
|||||||
digitalWrite(1, LOW);
|
digitalWrite(1, LOW);
|
||||||
|
|
||||||
// flip the pin every 0.3s
|
// flip the pin every 0.3s
|
||||||
flipper.attach(&flip, 0.3);
|
flipper.attach(0.3, flip);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@ -25,11 +25,11 @@ void setup() {
|
|||||||
pinMode(1, OUTPUT);
|
pinMode(1, OUTPUT);
|
||||||
digitalWrite(1, LOW);
|
digitalWrite(1, LOW);
|
||||||
|
|
||||||
// call setPin(0) every 25 ms
|
// every 25 ms, call setPin(0)
|
||||||
tickerSetLow.attach_ms(&setPin, 25, 0);
|
tickerSetLow.attach_ms(25, setPin, 0);
|
||||||
|
|
||||||
// call setPin(1) every 26 ms
|
// every 26 ms, call setPin(1)
|
||||||
tickerSetHigh.attach_ms(&setPin, 26, 1);
|
tickerSetHigh.attach_ms(26, setPin, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
|
|
||||||
attach KEYWORD2
|
attach KEYWORD2
|
||||||
attach_ms KEYWORD2
|
attach_ms KEYWORD2
|
||||||
|
once KEYWORD2
|
||||||
|
once_ms KEYWORD2
|
||||||
detach KEYWORD2
|
detach KEYWORD2
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
|
Loading…
x
Reference in New Issue
Block a user