Skip to content

Instantly share code, notes, and snippets.

@NF1198
Created January 19, 2023 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NF1198/dc0bb3bd7a375b1a13cfee568dbd4845 to your computer and use it in GitHub Desktop.
Save NF1198/dc0bb3bd7a375b1a13cfee568dbd4845 to your computer and use it in GitHub Desktop.
Timer Class for Arduino or other Embedded System
/*
* Copyright (c) 2022, 2023 Nicholas Folse
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This software may be included in a binary form, without the need to include
* the above copyright notice in the binary distribution.
*/
#include <functional>
#include "timer.h"
Timer::Timer(timer_callback callback, counter_t delay_ms) : callback(callback), delay_ms(delay_ms), enabled(false) {}
void Timer::start(counter_t now) {
if (enabled == true)
return;
enabled = true;
last_execution_time = now - delay_ms;
update(now);
}
void Timer::start_delayed(counter_t now) {
if (enabled == true)
return;
enabled = true;
last_execution_time = now;
}
void Timer::stop() {
enabled = false;
}
void Timer::set_delay(counter_t value) {
delay_ms = value;
}
bool get_is_running() {
return enabled;
}
counter_t get_last_execution_time() {
return last_execution_time;
}
counter_t get_delay_ms() {
return delay_ms;
}
void Timer::update(counter_t now) {
if (enabled) {
counter_t ms_since_last = now - last_execution_time;
if (ms_since_last >= delay_ms) {
enabled = callback(now, ms_since_last);
counter_t deviation_ms = ms_since_last % delay_ms;
last_execution_time = now - deviation_ms;
}
}
}
#ifndef FCBA12DF_61F5_4114_95A1_A7F54BCFD308
#define FCBA12DF_61F5_4114_95A1_A7F54BCFD308
/*
* Copyright (c) 2022, 2023 Nicholas Folse
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This software may be included in a binary form, without the need to include
* the above copyright notice in the binary distribution.
*/
#include <functional>
typedef uint32_t counter_t;
// bool timer_callback(uint32_t now, uint32_t ms_since_last_execution)
// returns TRUE if the timer should continue executing.
typedef std::function<bool(counter_t, counter_t)> timer_callback;
class Timer
{
private:
const timer_callback callback;
counter_t delay_ms;
bool enabled;
counter_t last_execution_time;
public:
// Create a new timer with a callback function and delay; The timer will not be started.
Timer(timer_callback callback, counter_t delay_ms);
// Start a timer, with first execution immediately. This method does nothing if the timer is already running.
void start(counter_t now);
// Start a timer, with first execution in (now + delay_ms). This method does nothing if the timer is already running.
void start_delayed(counter_t now);
// Stops a timer. This does nothing if the timer is already stopped.
void stop();
// Call this method continuously in a loop, passing in the current time.
void update(counter_t now);
// Change the delay time of a previously running timer. Follow this call with update() to ensure the timer is executed using the new delay time.
void set_delay(counter_t value);
// Check if the timer is running.
bool get_is_running();
// Return the last execution time (corrected for any deviation)
counter_t get_last_execution_time();
// Return the delay time
counter_t get_delay_ms();
//
};
#endif /* FCBA12DF_61F5_4114_95A1_A7F54BCFD308 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment