|
| 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 | + |
| 32 | +class Ticker |
| 33 | +{ |
| 34 | +public: |
| 35 | + Ticker(); |
| 36 | + ~Ticker(); |
| 37 | + typedef void (*callback_t)(void); |
| 38 | + typedef void (*callback_with_arg_t)(void*); |
| 39 | + |
| 40 | + void attach(float seconds, callback_t callback) |
| 41 | + { |
| 42 | + _attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0); |
| 43 | + } |
| 44 | + |
| 45 | + void attach_ms(uint32_t milliseconds, callback_t callback) |
| 46 | + { |
| 47 | + _attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0); |
| 48 | + } |
| 49 | + |
| 50 | + template<typename TArg> |
| 51 | + void attach(float seconds, void (*callback)(TArg), TArg arg) |
| 52 | + { |
| 53 | + static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes"); |
| 54 | + // C-cast serves two purposes: |
| 55 | + // static_cast for smaller integer types, |
| 56 | + // reinterpret_cast + const_cast for pointer types |
| 57 | + uint32_t arg32 = (uint32_t)arg; |
| 58 | + _attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), arg32); |
| 59 | + } |
| 60 | + |
| 61 | + template<typename TArg> |
| 62 | + void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) |
| 63 | + { |
| 64 | + static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes"); |
| 65 | + uint32_t arg32 = (uint32_t)arg; |
| 66 | + _attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32); |
| 67 | + } |
| 68 | + |
| 69 | + void once(float seconds, callback_t callback) |
| 70 | + { |
| 71 | + _attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0); |
| 72 | + } |
| 73 | + |
| 74 | + void once_ms(uint32_t milliseconds, callback_t callback) |
| 75 | + { |
| 76 | + _attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0); |
| 77 | + } |
| 78 | + |
| 79 | + template<typename TArg> |
| 80 | + void once(float seconds, void (*callback)(TArg), TArg arg) |
| 81 | + { |
| 82 | + static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes"); |
| 83 | + uint32_t arg32 = (uint32_t)(arg); |
| 84 | + _attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), arg32); |
| 85 | + } |
| 86 | + |
| 87 | + template<typename TArg> |
| 88 | + void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) |
| 89 | + { |
| 90 | + static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes"); |
| 91 | + uint32_t arg32 = (uint32_t)(arg); |
| 92 | + _attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), arg32); |
| 93 | + } |
| 94 | + |
| 95 | + void detach(); |
| 96 | + bool active(); |
| 97 | + |
| 98 | +protected: |
| 99 | + void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg); |
| 100 | + |
| 101 | + |
| 102 | +protected: |
| 103 | + esp_timer_handle_t _timer; |
| 104 | +}; |
| 105 | + |
| 106 | + |
| 107 | +#endif // TICKER_H |
0 commit comments