#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include <driver/gpio.h>
#include <freertos/task.h>
#include <string.h>
typedef enum {
Locked,
Unlocked
} State;
typedef enum {
Coin,
Push
} Event;
typedef struct {
State old_state;
Event trigger;
State new_state;
} Transition;
Transition transitions[] = {
{Locked,Push,Locked},
{Locked,Coin,Unlocked},
{Unlocked,Push,Locked},
{Unlocked,Coin,Unlocked},
};
void init_pins(){
gpio_reset_pin(GPIO_NUM_26);
gpio_set_direction(GPIO_NUM_26, GPIO_MODE_INPUT);
gpio_reset_pin(GPIO_NUM_2);
gpio_set_direction(GPIO_NUM_26, GPIO_MODE_INPUT);
}
const char * enum_to_string(State state){
switch(state){
case Locked: return "Locked";
case Unlocked: return "Unlocked";
}
}
void event_handler(Event current_event, State *current_state){
//printf("reaching handler\n");
for(int i = 0; i < 9; i++){
if(*current_state == transitions[i].old_state && current_event == transitions[i].trigger){
if(*current_state == Unlocked && transitions[i].trigger == Push){
printf("Passing through!\n");
}
*current_state = transitions[i].new_state;
printf("%s\n", enum_to_string(transitions[i].new_state));
}
}
}
void turnstile(void *(arg)){
State current_state = Locked;
while(1){
//printf("%d\n", gpio_get_level(GPIO_NUM_2));
if(gpio_get_level(GPIO_NUM_26) == 0){
printf("Turnstile is Unlocked! Please move forward!\n");
event_handler(Coin,¤t_state);
}
if(gpio_get_level(GPIO_NUM_2) == 1){
printf("Pushing....\n");
event_handler(Push,¤t_state);
}
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
extern "C" void app_main(){
init_pins();
printf("Hello!\nWelcome to the turnstile simulator, to enter a coin please press the button.\n");
printf("To simulate movement, a motion sensor has been added.\nPlease interact with the sensor to move through the turnstile!\n");
xTaskCreate(&turnstile,"turnstile", 2048, NULL, 5, NULL);
}