Skip to content

Flags and conditional execution

Konstantin Khait edited this page Jul 22, 2026 · 1 revision

Because Loviisa is cooperative and fully built as C execution environment with minimum dedicated operations, it needs a special techniques to code things related to waiting or delays. It is mostly valuable for tasks that shall yield others in the middle.

It is recommended to implement event handlers wherever it is possible. In this case you normally don't need any wait primitives and process events upon its generation. It is the fastest and most relevant programming technique for embedded systems.

If you use tasks, you shall not use delays and long loops, but use conditional execution instead of it.

Flags are special bit-size variables identified by its numbers and dedicated to indicate that there's something in the application that has to be conditionally processed of waited for. The overall number of flags is defined in lvs_config.h as LVS_FLAGS_USED and must be more than the maximum flag number that the application is used.

The following primitives are used to manipulate with flags:

LVS_GET_FLAG(flag_no) - to get 0 or 1 depending on the state of the flag with given number

LVS_SET_FLAG(flag_no) - set flag with the given number to 1

LVS_RESET_FLAG(flag_no) - reset flag with the given number to 0

The following primitives can be only used in event handlers or with maximum care and understanding of the consequences:

LVS_DELAY(ms) - unconditionally waits for the given time in milliseconds. Stops entire system except interrupts and not recommended for use anywhere

LVS_WAIT_FOR(condition) - waits for the given condition. Shall not be used in tasks (use LVS_IF instead).

LVS_WAIT_FLAG(flag_no) - waits until the given flag is set and resets the flag. Shall not be used in tasks (use LVS_IF_FLAG instead)

The following primitives can be used everywhere and designed primarily for tasks to skip execution cycles if conditions are not yet ready:

LVS_IF(condition) the following block until LVS_ENDIF() or LVS_ELSE() is executed only if condition is true.

LVS_IF_FLAG(flag_no) the following block until LVS_ENDIF() or LVS_ELSE() is executed only if the given flag is set (flag resets automatically).

LVS_IF_PASSED(ms) the following block until LVS_ENDIF() or LVS_ELSE() is executed only if more than ms milliseconds passed since last execution. Recommended for use instead of delays.

LVS_ELSE() executed if previous LVS_IF_... is false. Can be used with LVS_IF, LVS_IF_FLAG, LVS_IF_PASSED.

LVS_ENDIF() closes LVS_IF - LVS_ELSE block.

Clone this wiki locally