Skip to content

Instantly share code, notes, and snippets.

@tuanpmt
Created May 2, 2022 09:00
Show Gist options
  • Save tuanpmt/2b8958bae06befbd24f2f2ab8409854d to your computer and use it in GitHub Desktop.
Save tuanpmt/2b8958bae06befbd24f2f2ab8409854d to your computer and use it in GitHub Desktop.
#define AWAIT(a) if (a != OK) continue;
status_t _connect(state_t *state)
{
if (state >= CONNECTED) {
return OK;
}
// do connect, return FAIL if failed
state = CONNECTED;
return OK;
}
status_t _register(state_t *state)
{
if (state < CONNECTED) {
return FAIL;
}
if (state > CONNECTED) {
return OK;
}
// do register, return FAIL if failed
state = DATA;
return OK;
}
status_t _exchange_data(state_t *state)
{
if (state != DATA) {
return FAIL;
}
return OK;
}
void main() {
state_t state = INITILIZED;
while (true) {
AWAIT(_connect(&state));
AWAIT(_register(&state));
AWAIT(_exchange_data(&state));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment