|
| 1 | +/* |
| 2 | + * microtimer — Software timer manager for embedded systems. |
| 3 | + * |
| 4 | + * Register oneshot and periodic timers, tick from SysTick or main loop. |
| 5 | + * Replaces scattered `if (now - last > interval)` patterns. |
| 6 | + * |
| 7 | + * C99 · Zero dependencies · Zero allocations · Portable |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: MIT |
| 10 | + * https://github.com/Vanderhell/microtimer |
| 11 | + */ |
| 12 | + |
| 13 | +#ifndef MTIMER_H |
| 14 | +#define MTIMER_H |
| 15 | + |
| 16 | +#ifdef __cplusplus |
| 17 | +extern "C" { |
| 18 | +#endif |
| 19 | + |
| 20 | +#include <stdint.h> |
| 21 | +#include <stdbool.h> |
| 22 | +#include <stddef.h> |
| 23 | + |
| 24 | +/* ── Configuration ─────────────────────────────────────────────────────── */ |
| 25 | + |
| 26 | +#ifndef MTIMER_MAX_TIMERS |
| 27 | +#define MTIMER_MAX_TIMERS 8 |
| 28 | +#endif |
| 29 | + |
| 30 | +/* ── Error codes ───────────────────────────────────────────────────────── */ |
| 31 | + |
| 32 | +typedef enum { |
| 33 | + MTIMER_OK = 0, |
| 34 | + MTIMER_ERR_NULL = -1, |
| 35 | + MTIMER_ERR_FULL = -2, |
| 36 | + MTIMER_ERR_NOT_FOUND = -3, |
| 37 | + MTIMER_ERR_INVALID = -4, |
| 38 | +} mtimer_err_t; |
| 39 | + |
| 40 | +const char *mtimer_err_str(mtimer_err_t err); |
| 41 | + |
| 42 | +/* ── Timer mode ────────────────────────────────────────────────────────── */ |
| 43 | + |
| 44 | +typedef enum { |
| 45 | + MTIMER_ONESHOT = 0, /**< Fires once, then auto-stops. */ |
| 46 | + MTIMER_PERIODIC = 1, /**< Fires repeatedly at interval. */ |
| 47 | +} mtimer_mode_t; |
| 48 | + |
| 49 | +/* ── Timer state ───────────────────────────────────────────────────────── */ |
| 50 | + |
| 51 | +typedef enum { |
| 52 | + MTIMER_STOPPED = 0, |
| 53 | + MTIMER_RUNNING = 1, |
| 54 | + MTIMER_PAUSED = 2, |
| 55 | +} mtimer_state_t; |
| 56 | + |
| 57 | +const char *mtimer_state_str(mtimer_state_t state); |
| 58 | + |
| 59 | +/* ── Platform callback ─────────────────────────────────────────────────── */ |
| 60 | + |
| 61 | +typedef uint32_t (*mtimer_clock_fn)(void); |
| 62 | + |
| 63 | +/* ── Timer callback ────────────────────────────────────────────────────── */ |
| 64 | + |
| 65 | +/** |
| 66 | + * Timer expiry callback. |
| 67 | + * |
| 68 | + * @param timer_id Timer index. |
| 69 | + * @param ctx User context. |
| 70 | + */ |
| 71 | +typedef void (*mtimer_cb_fn)(uint8_t timer_id, void *ctx); |
| 72 | + |
| 73 | +/* ── Timer descriptor ──────────────────────────────────────────────────── */ |
| 74 | + |
| 75 | +typedef struct { |
| 76 | + const char *name; /**< Timer name (static string). */ |
| 77 | + uint32_t interval_ms; /**< Timer interval / delay. */ |
| 78 | + mtimer_mode_t mode; /**< ONESHOT or PERIODIC. */ |
| 79 | + mtimer_cb_fn callback; /**< Expiry callback. */ |
| 80 | + void *ctx; /**< User context for callback. */ |
| 81 | + |
| 82 | + /* Runtime state */ |
| 83 | + mtimer_state_t state; |
| 84 | + uint32_t start_ms; /**< When timer was (re)started. */ |
| 85 | + uint32_t remaining_ms; /**< For pause/resume. */ |
| 86 | + uint32_t fire_count; /**< Times this timer has fired. */ |
| 87 | + bool allocated; /**< Slot in use? */ |
| 88 | +} mtimer_entry_t; |
| 89 | + |
| 90 | +/* ── Timer manager instance ────────────────────────────────────────────── */ |
| 91 | + |
| 92 | +typedef struct { |
| 93 | + mtimer_entry_t timers[MTIMER_MAX_TIMERS]; |
| 94 | + mtimer_clock_fn clock; |
| 95 | + uint32_t tick_count; /**< Total tick() calls. */ |
| 96 | + uint32_t fire_count; /**< Total callbacks fired. */ |
| 97 | +} mtimer_t; |
| 98 | + |
| 99 | +/* ── Init ──────────────────────────────────────────────────────────────── */ |
| 100 | + |
| 101 | +mtimer_err_t mtimer_init(mtimer_t *tm, mtimer_clock_fn clock); |
| 102 | + |
| 103 | +/* ── Create / Destroy ──────────────────────────────────────────────────── */ |
| 104 | + |
| 105 | +/** |
| 106 | + * Create a timer. |
| 107 | + * |
| 108 | + * @param tm Timer manager. |
| 109 | + * @param name Timer name (static/const, for debug). |
| 110 | + * @param interval_ms Interval or delay in ms. |
| 111 | + * @param mode MTIMER_ONESHOT or MTIMER_PERIODIC. |
| 112 | + * @param callback Expiry callback. |
| 113 | + * @param ctx User context. |
| 114 | + * @return Timer ID (0-based) or negative error. |
| 115 | + */ |
| 116 | +int mtimer_create(mtimer_t *tm, const char *name, uint32_t interval_ms, |
| 117 | + mtimer_mode_t mode, mtimer_cb_fn callback, void *ctx); |
| 118 | + |
| 119 | +/** |
| 120 | + * Destroy a timer (free the slot). |
| 121 | + */ |
| 122 | +mtimer_err_t mtimer_destroy(mtimer_t *tm, uint8_t id); |
| 123 | + |
| 124 | +/* ── Control ───────────────────────────────────────────────────────────── */ |
| 125 | + |
| 126 | +/** Start (or restart) a timer. */ |
| 127 | +mtimer_err_t mtimer_start(mtimer_t *tm, uint8_t id); |
| 128 | + |
| 129 | +/** Stop a timer. */ |
| 130 | +mtimer_err_t mtimer_stop(mtimer_t *tm, uint8_t id); |
| 131 | + |
| 132 | +/** Pause a running timer (saves remaining time). */ |
| 133 | +mtimer_err_t mtimer_pause(mtimer_t *tm, uint8_t id); |
| 134 | + |
| 135 | +/** Resume a paused timer. */ |
| 136 | +mtimer_err_t mtimer_resume(mtimer_t *tm, uint8_t id); |
| 137 | + |
| 138 | +/** Restart with a new interval. */ |
| 139 | +mtimer_err_t mtimer_set_interval(mtimer_t *tm, uint8_t id, uint32_t interval_ms); |
| 140 | + |
| 141 | +/* ── Tick ──────────────────────────────────────────────────────────────── */ |
| 142 | + |
| 143 | +/** |
| 144 | + * Process all timers. Call from main loop or periodic interrupt. |
| 145 | + * |
| 146 | + * Checks each running timer against the clock. Fires callbacks for |
| 147 | + * expired timers. Periodic timers auto-restart. Oneshot timers stop. |
| 148 | + * |
| 149 | + * @return Number of timers that fired this tick. |
| 150 | + */ |
| 151 | +int mtimer_tick(mtimer_t *tm); |
| 152 | + |
| 153 | +/* ── Query ─────────────────────────────────────────────────────────────── */ |
| 154 | + |
| 155 | +uint8_t mtimer_count(const mtimer_t *tm); |
| 156 | +const mtimer_entry_t *mtimer_at(const mtimer_t *tm, uint8_t id); |
| 157 | +int mtimer_find(const mtimer_t *tm, const char *name); |
| 158 | +mtimer_state_t mtimer_state(const mtimer_t *tm, uint8_t id); |
| 159 | +uint32_t mtimer_remaining(const mtimer_t *tm, uint8_t id); |
| 160 | +uint32_t mtimer_fire_count(const mtimer_t *tm, uint8_t id); |
| 161 | +uint32_t mtimer_total_fires(const mtimer_t *tm); |
| 162 | +uint32_t mtimer_total_ticks(const mtimer_t *tm); |
| 163 | + |
| 164 | +#ifdef __cplusplus |
| 165 | +} |
| 166 | +#endif |
| 167 | + |
| 168 | +#endif /* MTIMER_H */ |
0 commit comments