L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/*
2 * utils.h
3 *
4 * General-purpose timing and value-stability utilities.
5 *
6 * Created on: May 5, 2024
7 * Author: Milan
8 */
9
10#ifndef UTILS_UTILS_H_
11#define UTILS_UTILS_H_
12
13#include <stdint.h>
14
15//////////////////////////////////////////////////////////////////////////////////
16/////////// sleeper_t ////////////////////////////////////////////////////////////
17
18/**
19 * @brief Non-blocking timer utility – similar to HAL_Delay but without CPU blocking.
20 *
21 * The caller polls sleeper_IsElapsed() or sleeper_IsElapsedNext() inside its
22 * main loop to detect when the configured time interval has elapsed.
23 * The timer can also be stopped; in that case all _IsElapsedXX functions
24 * return 0 regardless of elapsed time.
25 */
26#pragma pack(1)
27typedef struct
28{
29 uint32_t SleepMS; /**< Duration (ms) to wait; 0 means no pause */
30 uint32_t InicTime; /**< Reference timestamp captured at (re)start, used for comparison */
31 uint8_t Stop; /**< 1 – timer is stopped (all IsElapsed checks return 0); 0 – running */
32} sleeper_t;
33#pragma pack()
34
35/**
36 * @brief Initialise the sleeper and start timing from now.
37 * @param v Pointer to the sleeper_t instance to initialise.
38 * @param sleepMS Duration in milliseconds after which IsElapsed returns 1.
39 * Pass 0 to create a stopped / instant timer.
40 */
41void sleeper_Init(sleeper_t *v, uint32_t sleepMS);
42
43/**
44 * @brief Check whether the configured time interval has elapsed.
45 * @param v Pointer to a const sleeper_t instance.
46 * @retval 1 The time has elapsed (or SleepMS == 0 and Stop == 0).
47 * @retval 0 The time has not yet elapsed, or the timer is stopped.
48 */
49int sleeper_IsElapsed(const sleeper_t *v);
50
51/**
52 * @brief Check whether the time has elapsed and, if so, restart the timer for
53 * the next period (InicTime advances by SleepMS to avoid drift).
54 * @param v Pointer to the sleeper_t instance.
55 * @retval 1 The time has elapsed; timer restarted for the next cycle.
56 * @retval 0 The time has not yet elapsed, or the timer is stopped.
57 */
59
60/**
61 * @brief Restart the timer so the next period begins from now.
62 * InicTime is updated to the current HAL tick.
63 * @param v Pointer to the sleeper_t instance.
64 */
65void sleeper_Next(sleeper_t *v);
66
67/**
68 * @brief Change the sleep duration and restart the timer from now.
69 * @param v Pointer to the sleeper_t instance.
70 * @param sleepMS New duration in milliseconds.
71 */
72void sleeper_SetSleepMS(sleeper_t *v, uint32_t sleepMS);
73
74/**
75 * @brief Check whether the time has elapsed and, if so, stop the timer.
76 * @note Call sleeper_Next() to resume timing after this function returns 1.
77 * @param v Pointer to the sleeper_t instance.
78 * @retval 1 The time has elapsed; timer is now stopped.
79 * @retval 0 The time has not yet elapsed, or the timer was already stopped.
80 */
82
83/**
84 * @brief Stop the timer so that all subsequent IsElapsed checks return 0.
85 * @note Call sleeper_Next() or sleeper_Init() to resume timing.
86 * @param v Pointer to the sleeper_t instance.
87 */
88void sleeper_Stop(sleeper_t *v);
89
90//////////////////////////////////////////////////////////////////////////////////
91
92/**
93 * @brief Alias for the value type used by valueChanger_t (default: uint8_t).
94 */
95typedef uint8_t TVAL;
96
97/**
98 * @brief Value-stability detector – determines when a periodically updated
99 * value has remained unchanged for a configured time window.
100 *
101 * Call valueChanger_SetValue() each time a new sample arrives.
102 * The function returns 1 only when the value has been stable (unchanged)
103 * for the full duration specified at initialisation.
104 *
105 * When IsLocked is set, valueChanger_SetValue() always returns 0 until a
106 * different value is provided, at which point the lock is automatically cleared.
107 */
108typedef struct
109{
110 TVAL LastValue; /**< Most recently accepted value */
111 sleeper_t Timer; /**< Stability timer – restarted whenever the value changes */
112 uint8_t IsLocked; /**< 1 – locked (SetValue always returns 0); 0 – normal */
114
115/**
116 * @brief Initialise a valueChanger_t instance.
117 * @param v Pointer to the valueChanger_t to initialise.
118 * @param inicValue Initial value stored in LastValue.
119 * @param timeMS Stability window in milliseconds; SetValue returns 1 only
120 * after the value has been unchanged for this duration.
121 */
122void valueChanger_Inic(valueChanger_t *v, TVAL inicValue, uint32_t timeMS);
123
124/**
125 * @brief Submit a new value sample and check for stability.
126 *
127 * If newValue differs from LastValue, LastValue is updated, the stability
128 * timer is restarted, and the instance is unlocked.
129 * If newValue equals LastValue and the stability timer has elapsed, 1 is
130 * returned to signal that the value has been stable for the full window.
131 *
132 * @param v Pointer to the valueChanger_t instance.
133 * @param newValue The latest sampled value.
134 * @retval 1 The value has remained unchanged for the full stability window.
135 * @retval 0 The value changed, the timer has not elapsed, or the instance is locked.
136 */
138
139/**
140 * @brief Lock the valueChanger_t so that SetValue always returns 0.
141 * The lock is cleared automatically the next time SetValue receives
142 * a value different from LastValue.
143 * @param v Pointer to the valueChanger_t instance.
144 */
146
147/**
148 * @brief Return the most recently stored value.
149 * @param v Pointer to a const valueChanger_t instance.
150 * @return Current value held in LastValue.
151 */
153
154/////////////////////////////////////////////////////////////
155
156/**
157 * @brief Erase the external flash memory signature so that the next boot
158 * forces a full re-initialisation (used when flashing a new firmware
159 * version that changes persistent data structures).
160 */
161void clearFlash();
162
163/////////////////////////////////////////////////////////////
164
165#endif /* UTILS_UTILS_H_ */
Non-blocking timer utility – similar to HAL_Delay but without CPU blocking.
Definition utils.h:28
uint8_t Stop
Definition utils.h:31
uint32_t SleepMS
Definition utils.h:29
uint32_t InicTime
Definition utils.h:30
Value-stability detector – determines when a periodically updated value has remained unchanged for a ...
Definition utils.h:109
sleeper_t Timer
Definition utils.h:111
uint8_t IsLocked
Definition utils.h:112
TVAL LastValue
Definition utils.h:110
void valueChanger_Lock(valueChanger_t *v)
Lock the valueChanger_t so that SetValue always returns 0. The lock is cleared automatically the next...
Definition utils.c:82
void sleeper_SetSleepMS(sleeper_t *v, uint32_t sleepMS)
Change the sleep duration and restart the timer from now.
Definition utils.c:41
int sleeper_IsElapsedNext(sleeper_t *v)
Check whether the time has elapsed and, if so, restart the timer for the next period (InicTime advanc...
Definition utils.c:26
void sleeper_Init(sleeper_t *v, uint32_t sleepMS)
Initialise the sleeper and start timing from now.
Definition utils.c:13
void clearFlash()
Erase the external flash memory signature so that the next boot forces a full re-initialisation (used...
Definition utils.c:94
int sleeper_IsElapsedStop(sleeper_t *v)
Check whether the time has elapsed and, if so, stop the timer.
Definition utils.c:47
uint8_t TVAL
Alias for the value type used by valueChanger_t (default: uint8_t).
Definition utils.h:95
void sleeper_Stop(sleeper_t *v)
Stop the timer so that all subsequent IsElapsed checks return 0.
Definition utils.c:56
void sleeper_Next(sleeper_t *v)
Restart the timer so the next period begins from now. InicTime is updated to the current HAL tick.
Definition utils.c:35
void valueChanger_Inic(valueChanger_t *v, TVAL inicValue, uint32_t timeMS)
Initialise a valueChanger_t instance.
Definition utils.c:64
TVAL valueChanger_GetValue(const valueChanger_t *v)
Return the most recently stored value.
Definition utils.c:87
int valueChanger_SetValue(valueChanger_t *v, TVAL newValue)
Submit a new value sample and check for stability.
Definition utils.c:71
int sleeper_IsElapsed(const sleeper_t *v)
Check whether the configured time interval has elapsed.
Definition utils.c:21