Skip to content

Latest commit

 

History

History
53 lines (37 loc) · 3.67 KB

State_Machines_Implementation_In_C.md

File metadata and controls

53 lines (37 loc) · 3.67 KB

State Machines Implementation

1. Defining the State Table:

  • Represent the state table as a two-dimensional array in C.
  • Define constants for the maximum number of states (MAX_STATE) and the maximum number of event signals (MAX_SIG).
  • Initialize the state table with function pointers to action functions. Each cell in the array corresponds to a state-event combination, and the function pointer represents the action to be performed.

2. Implementing Action Functions:

  • Define action functions for each state and event combination. These functions execute the required actions based on the current state and the incoming event.
  • Entry and exit actions are implemented as separate functions associated with states. These functions perform actions upon entering or exiting a state.

3. Initializing the State Table:

  • Initialize the state table with function pointers corresponding to each state-event combination. Use the appropriate action functions for each cell in the table.
  • Ensure that the table is constant to prevent modifications at runtime and to optimize memory usage.

4. Dispatching Events:

  • In the dispatch function, index into the state table using the current state and the incoming event signal.
  • Call the corresponding action function based on the state-event combination.
  • Check the return status of the action function to determine if a transition occurred.
  • If a transition occurred, call the exit action for the previous state and the entry action for the new state.
  • Ensure that the state transitions are handled correctly, including the initial transition.

5. Handling Entry and Exit Actions:

  • Define entry and exit action functions for each state.
  • Move repeated actions from transitions to the entry and exit actions of the corresponding states.
  • Ensure that entry actions are executed upon entering a state, and exit actions are executed upon exiting a state.
  • Update the dispatch function to call entry and exit actions appropriately based on state transitions.

6. Returning Status from Action Functions:

  • Modify action functions to return status information indicating whether a transition occurred, an action was handled, or an event was ignored.
  • Update the dispatch function to handle the returned status and perform actions accordingly.

7. Building and Testing:

  • Build the code and load it onto the target board.
  • Test the state machine behavior to ensure that transitions and actions are executed correctly.

Guard Conditions

To implement guard conditions in your state machine, you'll need to follow these steps:

  1. Identify Decision Points: Determine where in your state machine logic you need to make decisions based on runtime conditions.

  2. Define Guard Conditions: For each decision point, define the guard conditions, which are boolean expressions evaluated at runtime. These conditions determine which transition should be taken.

  3. Add Choice Pseudostates: Add choice pseudostates to your state machine diagram at the decision points. Choice pseudostates are depicted as diamonds and represent decision points.

  4. Label Transitions with Guards: Outgoing transitions from choice pseudostates should be labeled with the guard conditions in square brackets. Each transition's guard condition determines whether that transition is enabled or disabled.

  5. Implement Guard Conditions in Code: In your code, implement the guard conditions using conditional statements (if-else). Evaluate the guard conditions at runtime to decide which transition to take.

  6. Handle Complementary Guards: If necessary, handle complementary guards to cover all possible conditions. These guards typically correspond to an "else" condition in your code.