11// Copyright 2026 Jose M. Moya <jm.moya@upm.es>
22// SPDX-License-Identifier: MIT
33
4- #include <errno.h>
54#include <stdio.h>
65#include <stdlib.h>
7- #include <time.h>
86
7+ #include "rxnet/coop.h"
98#include "rxnet/fsm.h"
109
1110#include "app_driver.h"
1716#define LIGHT_C_GPIO 5
1817#define BUTTON_A_GPIO 0
1918#define BUTTON_B_GPIO 15
19+ #define PERIOD_US (10 * 1000)
2020
2121typedef struct {
2222 const rx_fsm_machine * light_a_machine ;
@@ -87,37 +87,8 @@ cmd_quit(rx_fsm_context *ctx, cli_machine_data *cli,
8787 exit (0 );
8888}
8989
90- static struct timespec
91- timespec_add_us (struct timespec t , long us )
92- {
93- t .tv_nsec += us * 1000L ;
94- while (t .tv_nsec >= 1000000000L ) {
95- t .tv_nsec -= 1000000000L ;
96- ++ t .tv_sec ;
97- }
98- return t ;
99- }
100-
101- static int
102- timespec_compare (struct timespec a , struct timespec b )
103- {
104- if (a .tv_sec < b .tv_sec ) {
105- return -1 ;
106- }
107- if (a .tv_sec > b .tv_sec ) {
108- return 1 ;
109- }
110- if (a .tv_nsec < b .tv_nsec ) {
111- return -1 ;
112- }
113- if (a .tv_nsec > b .tv_nsec ) {
114- return 1 ;
115- }
116- return 0 ;
117- }
118-
119- int
120- main (void )
90+ int
91+ main (void )
12192{
12293 rx_fsm_runtime runtime ;
12394 rx_fsm_machine light_a_machine ;
@@ -131,9 +102,7 @@ main(void)
131102 .light_c_machine = & light_c_machine ,
132103 };
133104 cli_machine_data cli_data ;
134- const long period_us = 10 * 1000 ;
135- struct timespec now ;
136- struct timespec next_tick ;
105+ rx_coop_exec ce ;
137106
138107 if (rx_fsm_runtime_init (& runtime , 4 ) != 0 ) {
139108 fprintf (stderr , "rx_fsm_runtime_init failed\n" );
@@ -159,10 +128,10 @@ main(void)
159128 }
160129 cli_fsm_create (& cli_machine , "cli" , & cli_data );
161130
162- if (rx_fsm_runtime_add_machine (& runtime , & light_a_machine , 0 , 0 ) != 0 ||
163- rx_fsm_runtime_add_machine (& runtime , & light_b_machine , 0 , 0 ) != 0 ||
164- rx_fsm_runtime_add_machine (& runtime , & light_c_machine , 0 , 0 ) != 0 ||
165- rx_fsm_runtime_add_machine (& runtime , & cli_machine , 0 , 0 ) != 0 ) {
131+ if (rx_fsm_runtime_add_machine (& runtime , & light_a_machine , PERIOD_US , 0 ) != 0 ||
132+ rx_fsm_runtime_add_machine (& runtime , & light_b_machine , PERIOD_US , 0 ) != 0 ||
133+ rx_fsm_runtime_add_machine (& runtime , & light_c_machine , PERIOD_US , 0 ) != 0 ||
134+ rx_fsm_runtime_add_machine (& runtime , & cli_machine , PERIOD_US , 0 ) != 0 ) {
166135 fprintf (stderr , "rx_fsm_runtime_add_machine failed\n" );
167136 rx_fsm_runtime_free (& runtime );
168137 return 1 ;
@@ -172,61 +141,9 @@ main(void)
172141 cmd_status (& runtime .context , & cli_data , "status" , & app_data );
173142 cli_fsm_print_prompt (& cli_data );
174143
175- if (clock_gettime (CLOCK_MONOTONIC , & next_tick ) != 0 ) {
176- fprintf (stderr , "clock_gettime failed\n" );
177- rx_fsm_runtime_free (& runtime );
178- return 1 ;
179- }
180- next_tick = timespec_add_us (next_tick , period_us );
181-
182- while (1 ) {
183- if (rx_fsm_tick (& runtime ) != 0 ) {
184- fprintf (stderr , "rx_fsm_tick failed\n" );
185- break ;
186- }
187-
188- if (clock_gettime (CLOCK_MONOTONIC , & now ) != 0 ) {
189- fprintf (stderr , "clock_gettime failed\n" );
190- break ;
191- }
192-
193- while (timespec_compare (now , next_tick ) >= 0 ) {
194- next_tick = timespec_add_us (next_tick , period_us );
195- }
196-
197- while (1 ) {
198- struct timespec sleep_time = {
199- .tv_sec = next_tick .tv_sec - now .tv_sec ,
200- .tv_nsec = next_tick .tv_nsec - now .tv_nsec ,
201- };
202-
203- if (sleep_time .tv_nsec < 0 ) {
204- sleep_time .tv_nsec += 1000000000L ;
205- -- sleep_time .tv_sec ;
206- }
207-
208- if (sleep_time .tv_sec < 0 ) {
209- break ;
210- }
211-
212- if (nanosleep (& sleep_time , NULL ) == 0 ) {
213- break ;
214- }
215-
216- if (errno != EINTR ) {
217- fprintf (stderr , "nanosleep failed\n" );
218- break ;
219- }
220-
221- if (clock_gettime (CLOCK_MONOTONIC , & now ) != 0 ) {
222- fprintf (stderr , "clock_gettime failed\n" );
223- break ;
224- }
225- }
226-
227- next_tick = timespec_add_us (next_tick , period_us );
228- }
144+ rx_coop_exec_init (& ce );
145+ rx_coop_exec_add (& ce , & runtime .runtime );
146+ rx_coop_exec_run (& ce ); /* never returns */
229147
230- rx_fsm_runtime_free (& runtime );
231148 return 0 ;
232149}
0 commit comments