FreeRTOS Tetris
Functions | Variables
State Machine Module

State Machine handling changes between tasks. More...

Collaboration diagram for State Machine Module:

Functions

int iCheckStateInput (player_mode_t playerMode, bool isConnected, rotation_t rotationMode, bool gameOver)
 Function that checks if the player switched to a different state, depending on different parameters. More...
 
int iStateMachineInit ()
 Initialize the state machine. More...
 
static void changeState (volatile uint8_t *state, uint8_t forwards)
 Change the state, either forwards of backwards. More...
 
static void stateMachine ()
 Basic state machine with sequential states. More...
 

Variables

const uint8_t nextStateSignal = NEXT_TASK
 Signal to change to the next state.
 
const uint8_t prevStateSignal = PREV_TASK
 Signal to change to the previous state.
 
static TaskHandle_t StateMachine = NULL
 Task for the State Machine
 
QueueHandle_t StateQueue = NULL
 Queue for the different states
 

State Machine Definitions

#define STATE_QUEUE_LENGTH   1
 Length of the state queue.
 
#define STATE_COUNT   3
 Total number of states.
 
#define STATE_ONE   0
 State 1.
 
#define STATE_TWO   1
 State 2.
 
#define STATE_THREE   2
 State 3.
 
#define NEXT_TASK   0
 Next task.
 
#define PREV_TASK   1
 Previous task.
 
#define STARTING_STATE   STATE_ONE
 Initial state.
 
#define STATE_DEBOUNCE_DELAY   300
 Delay between state changes.
 

Detailed Description

State Machine handling changes between tasks.

This module contains a state machine task. This task is responsible for switching from one state (task) to another.

Function Documentation

◆ changeState()

static void changeState ( volatile uint8_t *  state,
uint8_t  forwards 
)
static

#include <stateMachine.c>

Change the state, either forwards of backwards.

Parameters
[in,out]state(uint8_t*): Current state, to be changed.
[in]forwards(uint8_t): Whether to change to the next or previous state.

◆ iCheckStateInput()

int iCheckStateInput ( player_mode_t  playerMode,
bool  isConnected,
rotation_t  rotationMode,
bool  gameOver 
)

#include <stateMachine.h>

Function that checks if the player switched to a different state, depending on different parameters.

  • If the game is running & Esc is pressed, switch to the next state (pause menu).
  • If the pause task is running, the game is not over, the opponent is connected & Esc is pressed, switch to the previous state (game task).
  • If the pause task is running & R is pressed, reset the game & switch to the previous state (game task).
  • If the pause task is running & M is pressed, reset the game & switch to the next state (Main Menu).
  • If the main menu task is running, valid settings have been selected & S is pressed, switch to the next state (game task).
Parameters
[in]playerMode( player_mode_t): The current player mode.
[in]isConnected(bool): The current connection status.
[in]rotationMode( rotation_t): The current rotation mode.
[in]gameOver(bool): Whether the game is over.
Returns
(int): 0 if state was successfully changed, -1 otherwise.

◆ iStateMachineInit()

int iStateMachineInit ( )

#include <stateMachine.h>

Initialize the state machine.

Returns
(int): 0 if initialization was successful, false otherwise.

◆ stateMachine()

static void stateMachine ( )
static

#include <stateMachine.c>

Basic state machine with sequential states.

  1. Read from StateQueue, whether to switch to the next or previous task.
  2. Calculate the current state by calling changeState().
  3. Depending on the current state, resume or suspend specific tasks.