L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1/*
2 * utils.c
3 *
4 * Created on: May 5, 2024
5 * Author: Milan
6 */
7
8#include "utils.h"
9#include "main.h"
10
11////////////////////////////////////////////////////////////////
12// Sleeper /////////////////////////////////////////////////////
13void sleeper_Init(sleeper_t *v, uint32_t sleepMS) //
14{
15 v->InicTime = 0;
16 v->SleepMS = 0;
17 v->Stop = 0;
18 sleeper_SetSleepMS(v, sleepMS);
19}
20
22{
23 return !v->Stop && (v->SleepMS == 0 || HAL_GetTick() - v->InicTime > v->SleepMS);
24}
25
27{
28 int isTime = sleeper_IsElapsed(v);
29
30 if (isTime)
31 sleeper_Next(v);
32 return isTime;
33}
34
36{
37 v->InicTime = HAL_GetTick();
38 v->Stop = 0;
39}
40
41void sleeper_SetSleepMS(sleeper_t *v, uint32_t sleepMS) //
42{
43 sleeper_Next(v);
44 v->SleepMS = sleepMS;
45}
46
48{
49 int isTime = sleeper_IsElapsed(v);
50
51 if (isTime)
52 sleeper_Stop(v);
53 return isTime;
54}
55
57{
58 v->Stop = 1;
59}
60////////////////////////////////////////////////////////////////
61
62////////////////////////////////////////////////////////////////
63// valueChanger_Inic /////////////////////////////////////////////////////
64void valueChanger_Inic(valueChanger_t *v, TVAL inicValue, uint32_t timeMS) //
65{
66 v->LastValue = inicValue;
67 sleeper_Init(&v->Timer, timeMS);
68 v->IsLocked = 0;
69}
70
72{
73 if (newValue != v->LastValue) // the values are different
74 {
75 v->LastValue = newValue;
76 v->IsLocked = 0;
78 }
79 return !v->IsLocked && sleeper_IsElapsed(&v->Timer);
80}
81
83{
84 v->IsLocked = 1;
85}
86
88{
89 return v->LastValue;
90}
91
92
93/////////////////////////////////////////////////////////////////////
94void clearFlash() //
95{
96 /*
97 HAL_FLASH_Unlock();
98
99 FLASH_EraseInitTypeDef erase;
100 erase.NbPages = 1;
101 erase.TypeErase = FLASH_TYPEERASE_MASSERASE;
102 erase.PageAddress = FLASH_BASE;
103
104 uint32_t err;
105 HAL_FLASHEx_Erase(&erase, &err);
106
107 HAL_FLASH_Lock();
108
109 NVIC_SystemReset();
110 */
111}
112
113/////////////////////////////////////////////////////////////////////
: Header for main.c file. This file contains the common defines of the application.
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
uint32_t HAL_GetTick(void)
Definition sys_app.c:337
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
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
uint8_t TVAL
Alias for the value type used by valueChanger_t (default: uint8_t).
Definition utils.h:95