I have a main function that runs a few functions during initialization and then runs a while loop that waits for commands from the UART.
When I see a specific command (let's say reset), I call a function that returns a value. I want to do the following things:
- Save the returned value
- Start the main function again with the returned value. The returned value is required during initialization of the functions in main.
I am newbie in C and I am not able to figure out a way save variable value in main.
The way I understand things, you essentially have the following setup:
Here's one approach you could take:
This essentially lets you "escape" from the event loop and perform the initialization all over again.
In theory this is possible, but it kind of breaks paradigms, and repetitively calling a function without ever letting it finish and return will quickly fill up your call stack, unless you take measures to unwind it behind the compiler's back.
A more common solution would be to write your main() function as one giant infinite while {1} loop. You can do all of your operation in an innner loop or whatever, and have clauses such that if you get your desired new value you can fall through to the bottom and loop back, effectively re-running the body of main with the new state.
just wrap your
main
into infinity-loop.