diff --git a/code/espurna/main.cpp b/code/espurna/main.cpp index da88645a..31c05bb2 100644 --- a/code/espurna/main.cpp +++ b/code/espurna/main.cpp @@ -142,11 +142,13 @@ void loop() { // One-time callbacks, registered some time during runtime // Notice that callback container is LIFO, most recently added // callback is called first. Copy to allow container modifications. - decltype(internal::once_callbacks) once_callbacks; - once_callbacks.swap(internal::once_callbacks); + if (!internal::once_callbacks.empty()) { + decltype(internal::once_callbacks) once_callbacks; + once_callbacks.swap(internal::once_callbacks); - for (const auto& callback : once_callbacks) { - callback(); + for (const auto& callback : once_callbacks) { + callback(); + } } espurna::time::delay(internal::loop_delay); diff --git a/code/espurna/system.cpp b/code/espurna/system.cpp index ee4559cd..4eef4ade 100644 --- a/code/espurna/system.cpp +++ b/code/espurna/system.cpp @@ -341,9 +341,9 @@ void SystemTimer::start(Duration duration, Callback callback, bool repeat) { if (!_timer) { _timer.reset(new os_timer_t{}); } + _armed = _timer.get(); _callback = std::move(callback); - _armed = _timer.get(); _repeat = repeat; os_timer_setfn(_timer.get(), @@ -372,10 +372,14 @@ void SystemTimer::start(Duration duration, Callback callback, bool repeat) { void SystemTimer::stop() { if (_armed) { os_timer_disarm(_armed); - _armed = nullptr; - _callback = nullptr; - _tick = nullptr; } + reset(); +} + +void SystemTimer::reset() { + _armed = nullptr; + _callback = Callback(); + _tick = nullptr; } void SystemTimer::callback() { @@ -395,7 +399,7 @@ void SystemTimer::callback() { return; } - stop(); + reset(); } void SystemTimer::schedule_once(Duration duration, Callback callback) { diff --git a/code/espurna/system.h b/code/espurna/system.h index 622792c2..5644f8ad 100644 --- a/code/espurna/system.h +++ b/code/espurna/system.h @@ -243,6 +243,7 @@ private: // with current implementation we use division by 2 until we reach value less than this one static constexpr Duration DurationMax = Duration(6870947); + void reset(); void start(Duration, Callback, bool repeat); void callback(); @@ -251,7 +252,8 @@ private: size_t count; }; - Callback _callback { nullptr }; + Callback _callback; + os_timer_t* _armed { nullptr }; bool _repeat { false };