#include<stdio.h>
#include<stdlib.h> // Include all libraries
void intiate_state_machine_fun(void); //function included here
void Gear_Down_fun(void);
void Checking_before_takeoff_func(void);
void Raising_Gear_fun(void);
void Gear_up_fun(void);
void Checking_before_landing_fun(void);
void Lower_gear_fun(void);
//included all functions here in array so that it can help to chose function according to the current state
static void(*statetable[])(void)={Gear_Down_fun,Checking_before_takeoff_func,Raising_Gear_fun,Gear_up_fun, Checking_before_landing_fun,Lower_gear_fun};
// created all states as per the project
typedef enum State{
GEAR_DOWN,
CHECKING_BEFORE_TAKE_OFF,
RISING_GEAR,
GEAR_UP,
CHECKING_BEFORE_LANDING,
LOWERING_GEAR,
}State;
// Pilot lever position defined in structure tpedef
typedef enum Pilot_lever{
Raising,
Falling,
Up,
Down
}Pilot_lever;
//Limit swich cases defined here
typedef enum Limit_switch{
Closed,
Opened
}Limit_switch;
//Squat switch cases defined here
typedef enum Squat_Switch{
Open,
Close
} Squat_Switch;
//landing gear Positions defined here
typedef enum Control_for_landing_gear {
ENABLE,
Disable
}Control_for_landing_gear;
State current_state;
//outputs created in structure to define in every state output action
typedef struct {
char* current_state_indication;
char* Light;
char* Pilot_lever_status;
char* landing_gear_control_status;
char* Gas_pressure_spring_system[2];
}structure_for_output;
// created state machine output to display for every state.
static structure_for_output State_machine[6]=
{{"Gear Down","Green", "NULL","NULL", {NULL,NULL}},
{"checking before Take off", "Red", "Raising", " NULL", {NULL, NULL}},
{"Rising Gear", "Red","Up", "Disabled", {NULL,NULL}},
{"Gear Up", "OFF","NULL", "Disabled", {NULL, NULL}},
{"Checking Before Landing", "Red", "Falling", "Enabled", {NULL,NULL }},
{"Lowering Gear","Red", "Down", "NULL",{"Disable", "Enable"}}
};
//initiated main
int main()
{
printf("Entered to state machine\n");
current_state=GEAR_DOWN;// took first state as Gear down to initiate the state machine
while(1){
statetable[current_state]();
}
}
static volatile Pilot_lever PL;
static volatile Squat_Switch SS;
static volatile Control_for_landing_gear CFC;
static volatile Limit_switch LS;
//function for gear Down
void Gear_Down_fun(void)
{
printf("current state is %s\n",State_machine->current_state_indication);
printf("please enter Lever position 0.Raising \t 1.Falling \t 2.Up \t 3.Down \n squatswitch : 1.Open\t 2.Close\n");
scanf("%d %d", &PL,&SS);
if(PL== Raising && SS== Open)
{
current_state=CHECKING_BEFORE_TAKE_OFF;
}
else{
Gear_Down_fun();
}
}
//Checking before take off function based on inputs it will select and change the current state
void Checking_before_takeoff_func()
{
printf("\nchanging state to Checking before Take off\n");
printf("Current status:%s\n",State_machine[current_state].current_state_indication);
printf("LED :%s\n", State_machine->Light);
printf("\nEnter pilots Lever position 0.Raising 1.Falling 2.Up 3.Down");
scanf("%d",&PL);
if (PL==Up){
current_state=RISING_GEAR;
}
else if(PL==Falling){
current_state= GEAR_DOWN;
}
else{
Checking_before_takeoff_func();
}
}
// Function for Rising gear down and set the current position.
void Raising_Gear_fun(){
printf("\ncurrent state is %s", State_machine[current_state].current_state_indication);
printf("\nLED: %s",State_machine[current_state].Light);
printf("\n%s",State_machine[current_state].Light);
printf("\nEnter Pilot Lever position, Limit switch\n Enter pilots Lever position 0.Raising 1.Falling 2.Up 3.Down\n Limit Switch : 0. Closed 1.opened");
scanf("%d %d",&PL,&LS);
if(PL==Falling)
{
current_state=CHECKING_BEFORE_LANDING;
}
else if(LS==Closed&&(PL==Up||PL==Raising)){
current_state=GEAR_UP;
}
else{
Raising_Gear_fun();
}
}
//Function for Gear Up function based on input it selects and changes the state.
void Gear_up_fun()
{
printf("\ncurrent state is %s\n", State_machine[current_state].current_state_indication);
printf("lights %s\n", State_machine[current_state].Light);
printf("landing gear %s\n", State_machine[current_state].landing_gear_control_status);
printf("Enter pilots Lever position 0.Raising 1.Falling 2.Up 3.Down");
scanf("%d", &PL);
if (PL==Falling){
current_state=CHECKING_BEFORE_LANDING;
}
else{
Gear_up_fun();
}
}
//function for Checking before landing. Based on input it will select the current state and changes.
void Checking_before_landing_fun(void)
{
printf("\ncurrent state: %s\n", State_machine[current_state].current_state_indication);
printf("LED: %s\n",State_machine[current_state].Light);
printf("Control for landing Gear is %s\n", State_machine[current_state].landing_gear_control_status);
printf("Enter Pilot lever position 0.Raising 1.Falling 2.Up 3.Down");
scanf("%d", &PL);
if (PL==Down)
{
current_state=LOWERING_GEAR;
}
else{
Checking_before_landing_fun();
}
}
//Function for Lower_gear state, based on input it will changes current state.
void Lower_gear_fun(void)
{
printf("\ncurrent state: %s\n", State_machine[current_state].current_state_indication);
printf("Enter Pilot lever position, Limtit Switch\n Pilot lever position: 0.Raising 1.Falling 2.Up 3.Down\n Limit switch : 0.Closed 1.Open");
scanf("%d %d", &PL,&LS);
if (LS==Opened&&(PL==Falling|| PL==Up||PL==Down))
{
current_state=GEAR_DOWN;
}
else if(PL==Raising&&LS==Closed)
{
current_state=CHECKING_BEFORE_TAKE_OFF;
}
else
{
Lower_gear_fun();
}
}
Output :