#include "phase.h" int timer_id_valid(int64_t id) { return id >= 0 && id < TIMER_CHANNELS; } int timer_coord_valid(int64_t value) { return value >= -TIMER_LIMIT && value <= TIMER_LIMIT; } int timer_period_valid(int64_t period) { return period >= 1 && period <= TIMER_MAX_PERIOD; } int timer_mode_valid(TimerMode mode) { return mode == TIMER_COUNT || mode == TIMER_LATEST; } int64_t timer_residue(int64_t value, int64_t period) { int64_t result = value % period; return result; } int64_t timer_next(int64_t now, int64_t epoch, int64_t phase, int64_t period) { int64_t elapsed = now - epoch - phase; int64_t position = timer_residue(elapsed, period); int64_t remaining = period - position; return now + remaining; } int64_t timer_due(int64_t next, int64_t target, int64_t period) { if (next > target) { return 0; } return (target - next) / period + 1; } const char *timer_mode_name(TimerMode mode) { switch (mode) { case TIMER_COUNT: return "COUNT"; case TIMER_LATEST: return "LATEST"; default: return "INVALID"; } } const char *timer_error_name(int code) { switch (code) { case TIMER_SYNTAX: return "syntax"; case TIMER_RANGE: return "range"; case TIMER_MISSING: return "missing"; case TIMER_BACKWARD: return "backward"; default: return "syntax"; } }