1+ #include "u_tx_timers.h"
2+ #include "u_tx_debug.h"
3+
4+ // clang-format off
5+
6+ /* Check if a timer is active. */
7+ int _is_timer_active (timer_t * timer , bool * active ) {
8+ /* Get the active indication. */
9+ UINT is_active ;
10+ int status = tx_timer_info_get (& timer -> TX_TIMER_ , (CHAR * * )TX_NULL , & is_active , (ULONG * )TX_NULL , (ULONG * )TX_NULL , (TX_TIMER * * )TX_NULL );
11+ if (status != TX_SUCCESS ) {
12+ PRINTLN_ERROR ("Failed to call tx_timer_info_get (Status: %d/%s, Timer: %s)." , status , tx_status_toString (status ), timer -> name );
13+ return U_ERROR ;
14+ }
15+
16+ /* Check the indication. */
17+ if (is_active == TX_TRUE ) {
18+ * active = true;
19+ } else {
20+ * active = false;
21+ }
22+
23+ return U_SUCCESS ;
24+ }
25+
26+ /* Initializes a timer. */
27+ int timer_init (timer_t * timer ) {
28+ /* Set reschedule ticks setting. */
29+ ULONG reschedule_ticks ;
30+ if (timer -> type == ONESHOT ) {
31+ reschedule_ticks = 0 ; // If it's a one-shot timer, don't reschedule the timer.
32+ } else {
33+ reschedule_ticks = timer -> duration ; // If it's a periodic timer, set the timer to go off every 'duration' ticks.
34+ }
35+
36+ /* Set auto-activate setting. */
37+ UINT auto_activate ;
38+ if (timer -> auto_activate ) {
39+ auto_activate = TX_AUTO_ACTIVATE ; // The timer will start automatically at initialization.
40+ } else {
41+ auto_activate = TX_NO_ACTIVATE ; // The timer must be manually started after initialization.
42+ }
43+
44+ /* Create the timer. */
45+ int status = tx_timer_create (& timer -> TX_TIMER_ , (CHAR * )timer -> name , timer -> callback , timer -> callback_input , timer -> duration , reschedule_ticks , auto_activate );
46+ if (status != TX_SUCCESS ) {
47+ PRINTLN_ERROR ("Failed to create timer (Status: %d/%s, Timer: %s)." , status , tx_status_toString (status ), timer -> name );
48+ return U_ERROR ;
49+ }
50+
51+ /* Return successful. */
52+ PRINTLN_INFO ("Ran Timer::init() (Timer: %s)." , timer -> name );
53+ return U_SUCCESS ;
54+ }
55+
56+ /* Starts a timer. */
57+ int timer_start (timer_t * timer ) {
58+ /* Get active status. */
59+ bool is_active = false;
60+ int status = _is_timer_active (timer , & is_active );
61+ if (status != U_SUCCESS ) {
62+ PRINTLN_ERROR ("Failed to get the activation status of a timer (Timer: %s)." , timer -> name );
63+ return U_ERROR ;
64+ }
65+
66+ /* Check active status. */
67+ if (is_active ) {
68+ PRINTLN_ERROR ("Tried to start a timer that is already active (Timer: %s)." , timer -> name );
69+ return U_ERROR ;
70+ }
71+
72+ /* Activate the timer. */
73+ status = tx_timer_activate (& timer -> TX_TIMER_ );
74+ if (status != TX_SUCCESS ) {
75+ PRINTLN_ERROR ("Failed to call tx_timer_activate() (Status: %d/%s, Timer: %s)." , status , tx_status_toString (status ), timer -> name );
76+ return U_ERROR ;
77+ }
78+
79+ return U_SUCCESS ;
80+ }
81+
82+ /* Stops a timer. If the timer is already deactivated, this function will have no effect. */
83+ int timer_stop (timer_t * timer ) {
84+ /* Deactivate the timer. */
85+ int status = tx_timer_deactivate (& timer -> TX_TIMER_ );
86+ if (status != TX_SUCCESS ) {
87+ PRINTLN_ERROR ("Failed to call tx_timer_deactivate() (Status: %d/%s, Timer: %s)." , status , tx_status_toString (status ), timer -> name );
88+ return U_ERROR ;
89+ }
90+ return U_SUCCESS ;
91+ }
92+
93+ /* Resets a timer back to its starting position. Does not automatically start the timer after resetting (use Timer::restart() for that.) */
94+ int timer_reset (timer_t * timer ) {
95+ /* Deactivate the timer. */
96+ int status = tx_timer_deactivate (& timer -> TX_TIMER_ );
97+ if (status != TX_SUCCESS ) {
98+ PRINTLN_ERROR ("Failed to call tx_timer_deactivate() (Status: %d/%s, Timer: %s)." , status , tx_status_toString (status ), timer -> name );
99+ return U_ERROR ;
100+ }
101+
102+ /* Set reschedule ticks setting. */
103+ ULONG reschedule_ticks ;
104+ if (timer -> type == ONESHOT ) {
105+ reschedule_ticks = 0 ; // If it's a one-shot timer, don't reschedule the timer.
106+ }
107+ else {
108+ reschedule_ticks = timer -> duration ; // If it's a periodic timer, set the timer to go off every 'duration' ticks.
109+ }
110+
111+ /* Change the timer. */
112+ status = tx_timer_change (& timer -> TX_TIMER_ , timer -> duration , reschedule_ticks );
113+ if (status != TX_SUCCESS ) {
114+ PRINTLN_ERROR ("Failed to call tx_timer_change() (Status: %d/%s, Timer: %s)." , status , tx_status_toString (status ), timer -> name );
115+ return U_ERROR ;
116+ }
117+
118+ return U_SUCCESS ;
119+ }
120+
121+ /* Restarts a timer. */
122+ int timer_restart (timer_t * timer ) {
123+ /* Reset the timer. */
124+ int status = timer_reset (timer );
125+ if (status != U_SUCCESS ) {
126+ PRINTLN_ERROR ("Failed to call timer_reset() when restarting timer (Timer: %s)." , timer -> name );
127+ return U_ERROR ;
128+ }
129+
130+ /* Start the timer. */
131+ status = timer_start (timer );
132+ if (status != U_SUCCESS ) {
133+ PRINTLN_ERROR ("Failed to call timer_start() when restarting timer (Timer: %s)." , timer -> name );
134+ return U_ERROR ;
135+ }
136+
137+ return U_SUCCESS ;
138+ }
139+
140+ /* Gets the remaining number of ticks until the timer's expiration. */
141+ int timer_getRemainingTicks (timer_t * timer , uint32_t * remaining ) {
142+ /* Get the remaining ticks status. */
143+ ULONG remaining_ticks ;
144+ int status = tx_timer_info_get (& timer -> TX_TIMER_ , (CHAR * * )TX_NULL , (UINT * )TX_NULL , & remaining_ticks , (ULONG * )TX_NULL , (TX_TIMER * * )TX_NULL );
145+ if (status != TX_SUCCESS ) {
146+ PRINTLN_ERROR ("Failed to call tx_timer_info_get (Status: %d/%s, Timer: %s)." , status , tx_status_toString (status ), timer -> name );
147+ return U_ERROR ;
148+ }
149+
150+ /* Set the remaining ticks. */
151+ * remaining = (uint32_t )(remaining_ticks );
152+
153+ return U_SUCCESS ;
154+ }
155+
156+ // clang-format on
0 commit comments