Computer Engineering

profileAlkhawaa
lab05_state_machnine_code_snippet.c

// Code snippet 1 /* USER CODE BEGIN Includes */ #include <stdbool.h> /* USER CODE END Includes */ // Code snippet 2 /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ #ifdef __GNUC__ /* With GCC, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */ extern uint32_t read_a_bit_of_a_port(__IO uint32_t *pIDR, uint32_t pinMask); extern void set_a_bit_of_a_port(__IO uint32_t *pODR, uint32_t pinMask); extern void clear_a_bit_of_a_port(__IO uint32_t *pODR, uint32_t pinMask); #define LD_R_ON // Add the appropriate function here #define LD_G_ON // Add the appropriate function here #define LD_R_OFF // Add the appropriate function here #define LD_G_OFF // Add the appropriate function here #define JOY_C_IS_PRESSED // Add the appropriate function here #define JOY_L_IS_PRESSED // Add the appropriate function here #define JOY_R_IS_PRESSED // Add the appropriate function here /* USER CODE END PFP */ // Code snippet 3 enum progState{State1 = 1, State2, State3, State4}; enum progState currentState = State1; bool stateChanged = true; bool ledON = true; uint32_t bDelay = 100; // Code snippet 4 if (stateChanged) printf("The current state is State%1d.\n\r", currentState); switch (currentState) { case State1: if (stateChanged) { printf("Press the center key to go to State 2.\n\r"); stateChanged = false; } if (JOY_C_IS_PRESSED) { currentState = State2; stateChanged = true; break; } if (ledON) { LD_R_ON; LD_G_OFF; } else { LD_R_OFF; LD_G_ON; } HAL_Delay(bDelay); ledON = !ledON; break; case State2: if (stateChanged) { // provide your hint to the user and set your "state changed" flag } if (JOY_C_IS_PRESSED) { // provide your code to handle the center Joy key input. } if (JOY_L_IS_PRESSED) { // provide your code to handle the left Joy key input. } if (JOY_R_IS_PRESSED) { // provide your code to handle the right Joy key input. } LD_R_OFF; LD_G_OFF; break; case State3: if (stateChanged) { // your code } // your state transition code if (ledON) { LD_R_ON; } else { LD_R_OFF; } LD_G_OFF; HAL_Delay(2*bDelay); ledON = !ledON; break; case State4: if (stateChanged) { // your code } // your state transition code // your LED code break; default: printf("The program should not print this line---something is deadly wrong\n\r"); break; }