Skip to content

Commit

Permalink
wip: making states
Browse files Browse the repository at this point in the history
  • Loading branch information
enzoevers committed Jun 29, 2024
1 parent 63260a3 commit 7f66460
Show file tree
Hide file tree
Showing 24 changed files with 990 additions and 57 deletions.
31 changes: 18 additions & 13 deletions Code/Applications/Clock/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
add_executable(${APPLICATION_NAME} main.cpp)
add_subdirectory(StateMachine)

target_link_libraries(${APPLICATION_NAME}
Common
HAL
Displays
TextProvider
)
if(NOT TEST_ON_PC)
add_executable(${APPLICATION_NAME} main.cpp)

if(USE_STM32)
add_subdirectory(HardwareSetup)

stm32_print_size_of_target(${APPLICATION_NAME})
stm32_generate_binary_file(${APPLICATION_NAME})
stm32_generate_hex_file(${APPLICATION_NAME})
target_link_libraries(${APPLICATION_NAME}
Common
HAL
Displays
TextProvider
StateMachine
)

if(USE_STM32)
add_subdirectory(HardwareSetup)

stm32_print_size_of_target(${APPLICATION_NAME})
stm32_generate_binary_file(${APPLICATION_NAME})
stm32_generate_hex_file(${APPLICATION_NAME})
endif()
endif()
24 changes: 24 additions & 0 deletions Code/Applications/Clock/StateMachine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
add_library(StateMachine STATIC
${CMAKE_CURRENT_SOURCE_DIR}/src/StateMachine.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/States/ShowingClock.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/States/SettingClock.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/States/SettingHour.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/States/SettingMinute.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/States/SettingSecond.cpp
)

target_include_directories(StateMachine PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)

target_link_libraries(StateMachine
Common
HAL
Displays
TextProvider
)

if(TEST_ON_PC)
add_subdirectory(test)
endif()

46 changes: 46 additions & 0 deletions Code/Applications/Clock/StateMachine/include/IState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "States/States.h"

namespace Clock {

/*!
@brief Interface to define a state of the state machine.
*/
class IState {
public:
virtual ~IState() = default;

virtual auto GetStateName() const -> const States = 0;

/*!
@brief Enter the state. Must be called only once.
*/
virtual auto Enter() -> void = 0;

/*!
@brief Update the state. Must be called once every loop.
*/
virtual auto Update() -> void = 0;

/*!
@brief Exit the state. Must be called only once.
*/
virtual auto Exit() -> void = 0;

/*!
@brief Check if the state should transition to another state.
@return States::None if should not transition, otherwise the state to transition to.
*/
virtual auto ShouldTransitionToState() -> States = 0;

virtual auto HasSubState() const -> bool = 0;

/*!
@brief Get the sub state if it exists.
@return The sub state or nullptr if it does not exist.
*/
virtual auto GetSubState() const -> const IState* = 0;
};

} // namespace Clock
31 changes: 31 additions & 0 deletions Code/Applications/Clock/StateMachine/include/IStateMachine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "IState.h"

namespace Clock {

/*!
@brief Interface to define the main state machine that handles the internal state transitions.
*/
class IStateMachine {
public:
virtual ~IStateMachine() = default;

/*!
@brief Start the state machine at the initial state.
This initial state is defined by the implementation.
*/
virtual auto Start() -> void = 0;

/*!
@brief To be called once every iteratoin
*/
virtual auto Update() -> void = 0;

/*!
@brief Get the current state.
*/
virtual auto GetCurrentState() const -> const IState* = 0;
};

} // namespace Clock
55 changes: 55 additions & 0 deletions Code/Applications/Clock/StateMachine/include/StateMachine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include "IStateMachine.h"

// Displays
#include "IDisplay.h"
#include "ITextProvider.h"
#include "Monochrome.h"

// Time
#include "IDateTime.h"

// Delay
#include "IDelay.h"

// Control input
#include "IGPIOInput.h"

//-----

#include <memory>

namespace Clock {

/*!
@brief Interface to define the main state machine that handles the internal state transitions.
*/
class StateMachine : public IStateMachine {
public:
StateMachine(IDisplay<Monochrome>& display, ITextProvider& textProvider, HAL::IDateTime& dateTime,
HAL::IDelay& delay, HAL::IGPIOInput& buttonA, HAL::IGPIOInput& buttonB);
~StateMachine() override;

//---------------
// Clock::IStateMachine
//---------------
auto Start() -> void override;
auto Update() -> void override;
auto GetCurrentState() const -> const IState* override;

private:
auto TransitionToState(const States newState) -> void;
auto HandleStateTransition(std::unique_ptr<IState> newState) -> void;

IDisplay<Monochrome>& m_display;
ITextProvider& m_textProvider;
HAL::IDateTime& m_dateTime;
HAL::IDelay& m_delay;
HAL::IGPIOInput& m_buttonA;
HAL::IGPIOInput& m_buttonB;

std::unique_ptr<IState> m_currentState;
};

} // namespace Clock
42 changes: 42 additions & 0 deletions Code/Applications/Clock/StateMachine/include/States/SettingClock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "IState.h"

// Context
#include "IDateTime.h"
#include "IDisplay.h"
#include "IGPIOInput.h"
#include "ITextProvider.h"
#include "Monochrome.h"

namespace Clock {

class SettingClock : public IState {
public:
struct Context {
IDisplay<Monochrome> &display;
ITextProvider &textProvider;
HAL::IDateTime &dateTime;
HAL::IGPIOInput &buttonA;
HAL::IGPIOInput &buttonB;
};

SettingClock(const Context &&context);
~SettingClock() override = default;

//---------------
// Clock::IState
//---------------
auto GetStateName() const -> const States override;
auto Enter() -> void override;
auto Update() -> void override;
auto Exit() -> void override;
auto ShouldTransitionToState() -> States override;
auto HasSubState() const -> bool override;
auto GetSubState() const -> const IState * override;

private:
const Context m_context;
};

} // namespace Clock
23 changes: 23 additions & 0 deletions Code/Applications/Clock/StateMachine/include/States/SettingHour.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "IState.h"

namespace Clock {

class SettingHour : public IState {
public:
~SettingHour() override = default;

//---------------
// Clock::IState
//---------------
auto GetStateName() const -> const States override;
auto Enter() -> void override;
auto Update() -> void override;
auto Exit() -> void override;
auto ShouldTransitionToState() -> States override;
auto HasSubState() const -> bool override;
auto GetSubState() const -> const IState* override;
};

} // namespace Clock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "IState.h"

namespace Clock {

class SettingMinute : public IState {
public:
~SettingMinute() override = default;

//---------------
// Clock::IState
//---------------
auto GetStateName() const -> const States override;
auto Enter() -> void override;
auto Update() -> void override;
auto Exit() -> void override;
auto ShouldTransitionToState() -> States override;
auto HasSubState() const -> bool override;
auto GetSubState() const -> const IState* override;
};

} // namespace Clock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "IState.h"

namespace Clock {

class SettingSecond : public IState {
public:
~SettingSecond() override = default;

//---------------
// Clock::IState
//---------------
auto GetStateName() const -> const States override;
auto Enter() -> void override;
auto Update() -> void override;
auto Exit() -> void override;
auto ShouldTransitionToState() -> States override;
auto HasSubState() const -> bool override;
auto GetSubState() const -> const IState* override;
};

} // namespace Clock
52 changes: 52 additions & 0 deletions Code/Applications/Clock/StateMachine/include/States/ShowingClock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include "IState.h"
//----------
#include "Types/DateTimeData.h"
#include "VecXD.h"

// Context
#include "IDateTime.h"
#include "IDisplay.h"
#include "IGPIOInput.h"
#include "ITextProvider.h"
#include "Monochrome.h"

namespace Clock {

class ShowingClock : public IState {
public:
struct Context {
IDisplay<Monochrome> &display;
ITextProvider &textProvider;
HAL::IDateTime &dateTime;
HAL::IGPIOInput &buttonA;
HAL::IGPIOInput &buttonB;
uint8_t secondsForBtnPressTillChangeToSetTime;
};

ShowingClock(const Context &&context);
~ShowingClock() override = default;

//---------------
// Clock::IState
//---------------
auto GetStateName() const -> const States override;
auto Enter() -> void override;
auto Update() -> void override;
auto Exit() -> void override;
auto ShouldTransitionToState() -> States override;
auto HasSubState() const -> bool override;
auto GetSubState() const -> const IState * override;

private:
const Context m_context;
Vec2D m_screenResolution;
Common::Types::DateTime m_lastDateTime;
bool m_lastTimeIncreaseState;
bool m_lastTimeDecreaseState;

Common::Types::Time m_startTimeForSetTimeCheck;
};

} // namespace Clock
Loading

0 comments on commit 7f66460

Please sign in to comment.