From 89837fcea5e6f0e2f97b2fb19c0de8b82d280750 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Fri, 5 Jan 2018 04:04:53 +0000 Subject: [PATCH] lib/Ticker: add bool active() (#2722) * lib/Ticker: add bool active() Makes it easier to self detach, and check if a timer is still operating. Signed-off-by: Karl Palsson * Code cleanup Ticker.cpp --- libraries/Ticker/Ticker.cpp | 15 +++++++++------ libraries/Ticker/Ticker.h | 2 ++ libraries/Ticker/keywords.txt | 1 + 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/libraries/Ticker/Ticker.cpp b/libraries/Ticker/Ticker.cpp index b348798c6..dee3a6363 100644 --- a/libraries/Ticker/Ticker.cpp +++ b/libraries/Ticker/Ticker.cpp @@ -22,20 +22,18 @@ #include #include -extern "C" { #include "c_types.h" #include "eagle_soc.h" #include "ets_sys.h" #include "osapi.h" -} -const int ONCE = 0; -const int REPEAT = 1; +static const int ONCE = 0; +static const int REPEAT = 1; #include "Ticker.h" Ticker::Ticker() -: _timer(0) +: _timer(nullptr) { } @@ -66,5 +64,10 @@ void Ticker::detach() os_timer_disarm(_timer); delete _timer; - _timer = 0; + _timer = nullptr; +} + +bool Ticker::active() +{ + return (bool)_timer; } diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/Ticker.h index ea3f59f1c..74725fb76 100644 --- a/libraries/Ticker/Ticker.h +++ b/libraries/Ticker/Ticker.h @@ -23,6 +23,7 @@ #define TICKER_H #include +#include #include extern "C" { @@ -93,6 +94,7 @@ public: } void detach(); + bool active(); protected: void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg); diff --git a/libraries/Ticker/keywords.txt b/libraries/Ticker/keywords.txt index 89cca9a0c..b5701b5be 100644 --- a/libraries/Ticker/keywords.txt +++ b/libraries/Ticker/keywords.txt @@ -15,6 +15,7 @@ attach_ms KEYWORD2 once KEYWORD2 once_ms KEYWORD2 detach KEYWORD2 +active KEYWORD2 ####################################### # Instances (KEYWORD2)