|
| 1 | +/* |
| 2 | + Ticker.h - esp32 library that calls functions periodically |
| 3 | +
|
| 4 | + Copyright (c) 2017 Bert Melis. All rights reserved. |
| 5 | +
|
| 6 | + Based on the original work of: |
| 7 | + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. |
| 8 | + The original version is part of the esp8266 core for Arduino environment. |
| 9 | +
|
| 10 | + This library is free software; you can redistribute it and/or |
| 11 | + modify it under the terms of the GNU Lesser General Public |
| 12 | + License as published by the Free Software Foundation; either |
| 13 | + version 2.1 of the License, or (at your option) any later version. |
| 14 | +
|
| 15 | + This library is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | + Lesser General Public License for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU Lesser General Public |
| 21 | + License along with this library; if not, write to the Free Software |
| 22 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | +*/ |
| 24 | + |
| 25 | +#ifndef TICKER_H |
| 26 | +#define TICKER_H |
| 27 | + |
| 28 | +extern "C" { |
| 29 | +#include "esp_timer.h" |
| 30 | +} |
| 31 | +#include <functional> |
| 32 | +#include <Schedule.h> |
| 33 | + |
| 34 | +class Ticker |
| 35 | +{ |
| 36 | +public: |
| 37 | + Ticker(); |
| 38 | + ~Ticker(); |
| 39 | + |
| 40 | + typedef void (*callback_with_arg_t)(void*); |
| 41 | + typedef std::function<void(void)> callback_function_t; |
| 42 | + |
| 43 | + void attach_scheduled(float seconds, callback_function_t callback) |
| 44 | + { |
| 45 | + attach(seconds, [callback]() { schedule_function(callback); }); |
| 46 | + } |
| 47 | + |
| 48 | + void attach(float seconds, callback_function_t callback) |
| 49 | + { |
| 50 | + _callback_function = std::move(callback); |
| 51 | + _attach_ms(seconds * 1000, true, _static_callback, this); |
| 52 | + } |
| 53 | + |
| 54 | + void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) |
| 55 | + { |
| 56 | + attach_ms(milliseconds, [callback]() { schedule_function(callback); }); |
| 57 | + } |
| 58 | + |
| 59 | + void attach_ms(uint32_t milliseconds, callback_function_t callback) |
| 60 | + { |
| 61 | + _callback_function = std::move(callback); |
| 62 | + _attach_ms(milliseconds, true, _static_callback, this); |
| 63 | + } |
| 64 | + |
| 65 | + template<typename TArg> |
| 66 | + void attach(float seconds, void (*callback)(TArg), TArg arg) |
| 67 | + { |
| 68 | + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); |
| 69 | + // C-cast serves two purposes: |
| 70 | + // static_cast for smaller integer types, |
| 71 | + // reinterpret_cast + const_cast for pointer types |
| 72 | + _attach_ms(seconds * 1000, true, callback, arg); |
| 73 | + } |
| 74 | + |
| 75 | + template<typename TArg> |
| 76 | + void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) |
| 77 | + { |
| 78 | + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); |
| 79 | + _attach_ms(milliseconds, true, callback, arg); |
| 80 | + } |
| 81 | + |
| 82 | + void once_scheduled(float seconds, callback_function_t callback) |
| 83 | + { |
| 84 | + once(seconds, [callback]() { schedule_function(callback); }); |
| 85 | + } |
| 86 | + |
| 87 | + void once(float seconds, callback_function_t callback) |
| 88 | + { |
| 89 | + _callback_function = std::move(callback); |
| 90 | + _attach_ms(seconds * 1000, false, _static_callback, this); |
| 91 | + } |
| 92 | + |
| 93 | + void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) |
| 94 | + { |
| 95 | + once_ms(milliseconds, [callback]() { schedule_function(callback); }); |
| 96 | + } |
| 97 | + |
| 98 | + void once_ms(uint32_t milliseconds, callback_function_t callback) |
| 99 | + { |
| 100 | + _callback_function = std::move(callback); |
| 101 | + _attach_ms(milliseconds, false, _static_callback, this); |
| 102 | + } |
| 103 | + |
| 104 | + template<typename TArg> |
| 105 | + void once(float seconds, void (*callback)(TArg), TArg arg) |
| 106 | + { |
| 107 | + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); |
| 108 | + _attach_ms(seconds * 1000, false, callback, arg); |
| 109 | + } |
| 110 | + |
| 111 | + template<typename TArg> |
| 112 | + void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) |
| 113 | + { |
| 114 | + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); |
| 115 | + _attach_ms(milliseconds, false, callback, arg); |
| 116 | + } |
| 117 | + |
| 118 | + void detach(); |
| 119 | + bool active() const; |
| 120 | + |
| 121 | +protected: |
| 122 | + void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg); |
| 123 | + static void _static_callback(void* arg); |
| 124 | + |
| 125 | + callback_function_t _callback_function = nullptr; |
| 126 | + |
| 127 | +protected: |
| 128 | + esp_timer_handle_t _timer; |
| 129 | +}; |
| 130 | + |
| 131 | + |
| 132 | +#endif//TICKER_H |
0 commit comments