shengine pre-release
shengine by mrsinho
Loading...
Searching...
No Matches
shTime.h
Go to the documentation of this file.
1/**
2 * @file shTime.h
3 * @brief Handling time-related functionalities in the `shengine` framework.
4 *
5 * The shTime.h file contains structures and functions related to handling time in the `shengine` framework.
6 */
7
8#ifndef SH_TIME_H
9#define SH_TIME_H
10
11#ifdef __cplusplus
12extern "C" {
13#endif//__cplusplus
14
15
16
17/**
18 * @def SH_TIME_MAX_TICK_COUNT
19 * @brief Maximum number of ticks in the time structure.
20 *
21 * SH_TIME_MAX_TICK_COUNT defines the maximum number of ticks that can be tracked in the ShTime structure.
22 */
23#define SH_TIME_MAX_TICK_COUNT 1024
24
25
26
27/**
28 * @typedef ShTimeFunc
29 * @brief Function type for time-related callbacks.
30 *
31 * The ShTimeFunc typedef defines a function type for time-related callbacks, taking a void pointer as an argument and
32 * returning a uint64_t.
33 */
34typedef uint64_t ShTimeFunc(void*);
35
36
37
38/**
39 * @enum ShTickStatus
40 * @brief Enumerates possible tick statuses.
41 *
42 * The ShTickStatus enumeration defines possible statuses of a tick in the ShTime structure, including undefined,
43 * initialized, waiting, executing, and finished executing.
44 */
45typedef enum ShTickStatus {
46 SH_TICK_UNDEFINED = 0, /**< Tick status undefined. */
47 SH_TICK_INITIALIZED = 1, /**< Tick status initialized. */
48 SH_TICK_WAITING = 2, /**< Tick status waiting. */
49 SH_TICK_EXECUTING = 3, /**< Tick status executing. */
50 SH_TICK_FINISHED_EXECUTING = 4, /**< Tick status finished executing. */
53
54
55
56/**
57 * @struct ShTime
58 * @brief Structure representing time in the `shengine` framework.
59 *
60 * The ShTime structure represents time in the `shengine` framework, including properties such as the current time,
61 * delta time, last time, tick count, tick last time, tick status, and tick functions.
62 */
63typedef struct ShTime {
64 double now; /**< Current time. */
65 double delta_time; /**< Delta time. */
66 double last_time; /**< Last time. */
67
68 uint32_t tick_count; /**< Tick count. */
69 double ticks_last_time[SH_TIME_MAX_TICK_COUNT]; /**< Tick last time. */
73
74
75
76/**
77 * @brief Gets the current time in the `shengine` framework.
78 *
79 * @param p_time Pointer to a valid @ref ShTime structure.
80 * @return 1 on success, 0 on failure.
81 */
82extern uint8_t shGetTime(
83 ShTime* p_time
84);
85
86/**
87 * @brief Sets the time in the `shengine` framework.
88 *
89 * @param now Current time to set.
90 * @param p_time Pointer to a valid @ref ShTime structure.
91 * @return 1 on success, 0 on failure.
92 */
93extern uint8_t shSetTime(
94 double now,
95 ShTime* p_time
96);
97
98/**
99 * @brief Calls a function on a tick in the `shengine` framework.
100 *
101 * @param p_time Pointer to a valid @ref ShTime structure.
102 * @param seconds Time interval in seconds for the tick.
103 * @param tick_idx Index of the tick.
104 * @param p_func Pointer to the time function to call.
105 * @param p_arg Pointer to an argument for the time function.
106 * @param p_return_value Pointer to store the return value of the time function.
107 * @return 1 on success, 0 on failure.
108 */
109extern uint8_t shCallOnTick(
110 ShTime* p_time,
111 double seconds,
112 uint32_t tick_idx,
113 ShTimeFunc* p_func,
114 void* p_arg,
115 uint64_t* p_return_value
116);
117
118/**
119 * @def shOnTick
120 * @brief Macro to execute an expression on a tick in the `shengine` framework.
121 *
122 * The shOnTick macro executes the specified expression when a tick condition is met in the ShTime structure.
123 *
124 * Example:
125 * @code
126 * shOnTick(time, 0.5, 1, printf("Tick 1 executed!"))
127 * @endcode
128 *
129 * @param time ShTime structure to check for ticks.
130 * @param seconds_d Time interval in seconds for the tick.
131 * @param tick_idx Index of the tick.
132 * @param expression Expression to execute when the tick condition is met.
133 */
134#define shOnTick(time, seconds_d, tick_idx, expression)\
135 if ((time).ticks_status[tick_idx] == SH_TICK_UNDEFINED) {\
136 (time).ticks_status[tick_idx] = SH_TICK_INITIALIZED;\
137 }\
138 if (\
139 (\
140 ((time).now - (time).ticks_last_time[tick_idx]) >= (double)(seconds_d)\
141 ) &&\
142 (tick_idx < SH_TIME_MAX_TICK_COUNT) &&\
143 (\
144 (time).ticks_status[tick_idx] != SH_TICK_UNDEFINED &&\
145 (time).ticks_status[tick_idx] != SH_TICK_EXECUTING\
146 )\
147 ) {\
148 (time).ticks_status[tick_idx] = SH_TICK_EXECUTING;\
149 expression;\
150 (time).ticks_last_time[tick_idx] = (time).now;\
151 (time).ticks_status[tick_idx] = SH_TICK_FINISHED_EXECUTING;\
152 }\
153 else {\
154 (time).ticks_status[tick_idx] = SH_TICK_WAITING;\
155 }
156
157/**
158 * @brief Sleeps for a specified number of milliseconds.
159 *
160 * @param ms Number of milliseconds to sleep.
161 */
162extern void shSleep(
163 uint32_t ms
164);
165
166
167
168#ifdef __cplusplus
169}
170#endif//__cplusplus
171
172#endif//SH_TIME_H
uint8_t shCallOnTick(ShTime *p_time, double seconds, uint32_t tick_idx, ShTimeFunc *p_func, void *p_arg, uint64_t *p_return_value)
Calls a function on a tick in the shengine framework.
Definition shTime.c:46
uint8_t shGetTime(ShTime *p_time)
Gets the current time in the shengine framework.
Definition shTime.c:19
ShTickStatus
Enumerates possible tick statuses.
Definition shTime.h:45
@ SH_TICK_UNDEFINED
Definition shTime.h:46
@ SH_TICK_WAITING
Definition shTime.h:48
@ SH_TICK_FINISHED_EXECUTING
Definition shTime.h:50
@ SH_TICK_INITIALIZED
Definition shTime.h:47
@ SH_TICK_EXECUTING
Definition shTime.h:49
@ SH_TICK_STATUS_MAX_ENUM
Definition shTime.h:51
uint64_t ShTimeFunc(void *)
Function type for time-related callbacks.
Definition shTime.h:34
#define SH_TIME_MAX_TICK_COUNT
Maximum number of ticks in the time structure.
Definition shTime.h:23
void shSleep(uint32_t ms)
Sleeps for a specified number of milliseconds.
Definition shTime.c:93
uint8_t shSetTime(double now, ShTime *p_time)
Sets the time in the shengine framework.
Definition shTime.c:31
Structure representing time in the shengine framework.
Definition shTime.h:63
ShTickStatus ticks_status[SH_TIME_MAX_TICK_COUNT]
Definition shTime.h:70
double delta_time
Definition shTime.h:65
double ticks_last_time[SH_TIME_MAX_TICK_COUNT]
Definition shTime.h:69
ShTimeFunc * p_ticks_funcs[SH_TIME_MAX_TICK_COUNT]
Definition shTime.h:71
double now
Definition shTime.h:64
uint32_t tick_count
Definition shTime.h:68
double last_time
Definition shTime.h:66