text: /**
   * @file ControllerFsm.cpp
   * @author Vinicius Pimenta Bernardo (viniciuspibi@gmail.com)
   * @brief ControllerFsm class definition
   * @version 0.1
   * @date 2021-03-25
   * 
   * @copyright Copyright (c) 2021
   * 
   */
  
  #include "ControllerFsm.h"
  
  char *eventToString(int event)
  {
      if (event == event_something_opened)
      {
          return (char *)"event_something_opened";
      }
      else if (event == event_everything_closed)
      {
          return (char *)"event_everything_closed";
      }
      else if (event == event_btn_pressed)
      {
          return (char *)"event_btn_pressed";
      }
      return (char *)"event_none";
  }
  
  char *actionToString(int action)
  {
      if (action == action_relay_on)
      {
          return (char *)"action_relay_on";
      }
      else if (action == action_relay_off)
      {
          return (char *)"action_relay_off";
      }
      else if (action == action_led_on)
      {
          return (char *)"action_led_on";
      }
      else if (action == action_led_off)
      {
          return (char *)"action_led_off";
      }
      else if (action == action_led_on + action_relay_off)
      {
          return (char *)"action_led_on + action_relay_off";
      }
      return (char *)"action_none";
  }
  
  char *stateToString(int state)
  {
      if (state == state_led_off_realy_off)
      {
          return (char *)"state_led_off_realy_off";
      }
      else if (state == state_led_off_realy_on)
      {
          return (char *)"state_led_off_realy_on";
      }
      else if (state == state_led_on_realy_off)
      {
          return (char *)"state_led_on_realy_off";
      }
      return (char *)"state_error";
  }
  
  /**
   * @brief Construct a new Controller Fsm:: Controller Fsm object
   */
  ControllerFsm::ControllerFsm(void)
  {
      state = state_led_off_realy_off;
  }
  
  /**
   * @brief Helper function to get current FSM state.
   * 
   * @return int The state.
   */
  int ControllerFsm::getState(void)
  {
      return state;
  }
  
  /**
   * @brief Process the event, changing state (if needed) and returning output as result.
   * 
   * @param event The input event to process.
   * @return int The output actions.
   */
  int ControllerFsm::processEvent(int event)
  {
      int output_actions = action_none;
      switch (state)
      {
      case state_led_off_realy_off:
          if (event == event_btn_pressed)
          {
              state = state_led_off_realy_on;
              output_actions += action_relay_on;
          }
          else if (event == event_something_opened)
          {
              state = state_led_on_realy_off;
              output_actions += action_led_on;
          }
          break;
      case state_led_off_realy_on:
          if (event == event_something_opened)
          {
              state = state_led_on_realy_off;
              output_actions += action_led_on + action_relay_off;
          }
          break;
      case state_led_on_realy_off:
          if (event == event_everything_closed)
          {
              state = state_led_off_realy_off;
              output_actions += action_led_off;
          }
          break;
      default:
          break;
      }
      return output_actions;
  }