#include <stdio.h>
enum turnstile_state {
LOCKED,
UNLOCKED
};
enum turnstile_event {
COIN,
PUSH
};
struct turnstile {
enum turnstile_state state;
};
void transition(struct turnstile *t, enum turnstile_event event) {
switch (t->state) {
case LOCKED:
if (event == COIN) {
printf("Unlocking turnstile...\n");
t->state = UNLOCKED;
} else {
printf("Turnstile is locked. Please insert a coin to unlock.\n");
}
break;
case UNLOCKED:
if (event == PUSH) {
printf("Passing through turnstile...\n");
t->state = LOCKED;
} else {
printf("Turnstile is already unlocked. Please proceed.\n");
}
break;
}
}
extern "C" void app_main (){
struct turnstile t = { .state = LOCKED };
enum turnstile_event events[] = {COIN, PUSH, PUSH, COIN};
for (int i = 0; i < sizeof(events) / sizeof(events[0]); i++) {
transition(&t, events[i]);
}
}