Add portable Ticker @ 2.0.0 Library

This commit is contained in:
Aadi Desai 2021-06-05 13:10:51 +01:00
parent e07744478d
commit 99c3af355b
5 changed files with 188 additions and 1 deletions

View file

@ -0,0 +1,14 @@
#######################################
# Datatypes (KEYWORD1)
#######################################
Ticker KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
attach KEYWORD2
attach_ms KEYWORD2
once KEYWORD2
detach KEYWORD2

View file

@ -0,0 +1,9 @@
name=TickerV2
version=2.0.0
author=Bert Melis
maintainer=Hristo Gochkov <hristo@espressif.com>
sentence=Allows to call functions with a given interval.
paragraph=
category=Timing
url=
architectures=esp32

View file

@ -0,0 +1,58 @@
/*
Ticker.cpp - esp32 library that calls functions periodically
Copyright (c) 2017 Bert Melis. All rights reserved.
Based on the original work of:
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
The original version is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "TickerV2.h"
Ticker::Ticker() :
_timer(nullptr) {}
Ticker::~Ticker() {
detach();
}
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg) {
esp_timer_create_args_t _timerConfig;
_timerConfig.arg = reinterpret_cast<void*>(arg);
_timerConfig.callback = callback;
_timerConfig.dispatch_method = ESP_TIMER_TASK;
_timerConfig.name = "Ticker";
if (_timer) {
esp_timer_stop(_timer);
esp_timer_delete(_timer);
}
esp_timer_create(&_timerConfig, &_timer);
if (repeat) {
esp_timer_start_periodic(_timer, milliseconds * 1000ULL);
} else {
esp_timer_start_once(_timer, milliseconds * 1000ULL);
}
}
void Ticker::detach() {
if (_timer) {
esp_timer_stop(_timer);
esp_timer_delete(_timer);
_timer = nullptr;
}
}

View file

@ -0,0 +1,107 @@
/*
Ticker.h - esp32 library that calls functions periodically
Copyright (c) 2017 Bert Melis. All rights reserved.
Based on the original work of:
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
The original version is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TICKERV2_H
#define TICKERV2_H
extern "C" {
#include "esp_timer.h"
}
class Ticker
{
public:
Ticker();
~Ticker();
typedef void (*callback_t)(void);
typedef void (*callback_with_arg_t)(void*);
void attach(float seconds, callback_t callback)
{
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
void attach_ms(uint32_t milliseconds, callback_t callback)
{
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
template<typename TArg>
void attach(float seconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
// C-cast serves two purposes:
// 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>
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");
uint32_t arg32 = (uint32_t)arg;
_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();
bool active();
protected:
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);
protected:
esp_timer_handle_t _timer;
};
#endif // TICKERV2_H

View file

@ -23,7 +23,6 @@ build_flags =
build_type = debug build_type = debug
lib_deps = lib_deps =
plerup/EspSoftwareSerial@^6.12.6 plerup/EspSoftwareSerial@^6.12.6
sstaub/Ticker@^4.2.0
me-no-dev/ESP Async WebServer@^1.2.3 me-no-dev/ESP Async WebServer@^1.2.3
me-no-dev/AsyncTCP@^1.1.1 me-no-dev/AsyncTCP@^1.1.1
links2004/WebSockets@^2.3.6 links2004/WebSockets@^2.3.6