-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ShiftController state machine boilerplate
- Loading branch information
Showing
3 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
635E684B79701B039C64EA45C3F84D30=D2A22CBB9C10176830AB359222B664EE | ||
66BE74F758C12D739921AEA421D593D3=0 | ||
8DF89ED150041C4CBC7CB9A9CAA90856=31748B60D9E165745BACD0E80BDC496C | ||
DC22A860405A8BF2F2C095E5B6529F12=31748B60D9E165745BACD0E80BDC496C | ||
8DF89ED150041C4CBC7CB9A9CAA90856=D8432134309552137BE43E7913439EC6 | ||
DC22A860405A8BF2F2C095E5B6529F12=0D5CC8A688ECAD96876BD908EA9873D4 | ||
eclipse.preferences.version=1 |
86 changes: 86 additions & 0 deletions
86
Firmware/Shifter_System/Program/Src/Application/ShiftController/shift_controller.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
#include "shift_controller.hpp" | ||
|
||
namespace application { | ||
|
||
ShiftController::ShiftController() { } | ||
|
||
ShiftController::~ShiftController() { } | ||
|
||
void ShiftController::SetState(State* new_state) { | ||
if (new_state != nullptr) { | ||
current_state_->Exit(*this); | ||
current_state_ = new_state; | ||
current_state_->Enter(*this); | ||
} | ||
} | ||
|
||
void ShiftController::Run() { | ||
current_state_->Compute(*this); | ||
} | ||
|
||
|
||
//************************************************* | ||
// LowGear State | ||
//************************************************* | ||
void ShiftController::LowGear::Enter(ShiftController& context) { | ||
printf("[ShiftController] Entering LowGear state.\n"); | ||
} | ||
|
||
void ShiftController::LowGear::Compute(ShiftController& context) { | ||
|
||
} | ||
|
||
void ShiftController::LowGear::Exit(ShiftController& context) { | ||
printf("[ShiftController] Leaving LowGear state.\n"); | ||
} | ||
|
||
|
||
//************************************************* | ||
// Neutral State | ||
//************************************************* | ||
void ShiftController::Neutral::Enter(ShiftController& context) { | ||
printf("[ShiftController] Entering Neutral state.\n"); | ||
} | ||
|
||
void ShiftController::Neutral::Compute(ShiftController& context) { | ||
|
||
} | ||
|
||
void ShiftController::Neutral::Exit(ShiftController& context) { | ||
printf("[ShiftController] Leaving Neutral state.\n"); | ||
} | ||
|
||
|
||
//************************************************* | ||
// MidGear State | ||
//************************************************* | ||
void ShiftController::MidGear::Enter(ShiftController& context) { | ||
printf("[ShiftController] Entering MidGear state.\n"); | ||
} | ||
|
||
void ShiftController::MidGear::Compute(ShiftController& context) { | ||
|
||
} | ||
|
||
void ShiftController::MidGear::Exit(ShiftController& context) { | ||
printf("[ShiftController] Leaving MidGear state.\n"); | ||
} | ||
|
||
|
||
//************************************************* | ||
// HighGear State | ||
//************************************************* | ||
void ShiftController::HighGear::Enter(ShiftController& context) { | ||
printf("[ShiftController] Entering HighGear state.\n"); | ||
} | ||
|
||
void ShiftController::HighGear::Compute(ShiftController& context) { | ||
|
||
} | ||
|
||
void ShiftController::HighGear::Exit(ShiftController& context) { | ||
printf("[ShiftController] Leaving HighGear state.\n"); | ||
} | ||
|
||
} // namespace application |
81 changes: 81 additions & 0 deletions
81
Firmware/Shifter_System/Program/Src/Application/ShiftController/shift_controller.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
#ifndef SHIFT_CONTROLLER_H | ||
#define SHIFT_CONTROLLER_H | ||
|
||
// Standard Libraries | ||
#include <cstdio> | ||
|
||
namespace application { | ||
|
||
class ShiftController { | ||
public: | ||
|
||
ShiftController(); | ||
|
||
~ShiftController(); | ||
|
||
void Run(); | ||
|
||
|
||
protected: | ||
class State { | ||
public: | ||
virtual ~State() = default; | ||
|
||
virtual void Enter(ShiftController& context) = 0; | ||
virtual void Compute(ShiftController& context) = 0; | ||
virtual void Exit(ShiftController& context) = 0; | ||
|
||
protected: | ||
State() = default; | ||
}; | ||
|
||
class LowGear : public State { | ||
public: | ||
LowGear() = default; | ||
virtual void Enter(ShiftController& context) override; | ||
virtual void Compute(ShiftController& context) override; | ||
virtual void Exit(ShiftController& context) override; | ||
}; | ||
|
||
class Neutral : public State { | ||
public: | ||
Neutral() = default; | ||
|
||
virtual void Enter(ShiftController& context) override; | ||
virtual void Compute(ShiftController& context) override; | ||
virtual void Exit(ShiftController& context) override; | ||
}; | ||
|
||
class MidGear : public State { | ||
public: | ||
MidGear() = default; | ||
|
||
virtual void Enter(ShiftController& context) override; | ||
virtual void Compute(ShiftController& context) override; | ||
virtual void Exit(ShiftController& context) override; | ||
}; | ||
|
||
class HighGear : public State { | ||
public: | ||
HighGear() = default; | ||
|
||
virtual void Enter(ShiftController& context) override; | ||
virtual void Compute(ShiftController& context) override; | ||
virtual void Exit(ShiftController& context) override; | ||
}; | ||
|
||
private: | ||
void SetState(State* new_state); | ||
|
||
|
||
LowGear low_gear_state_; | ||
Neutral neutral_state_; | ||
MidGear mid_gear_state_; | ||
HighGear high_gear_state_; | ||
State* current_state_{&neutral_state_}; | ||
}; | ||
|
||
} // namespace application | ||
|
||
#endif // SHIFT_CONTROLLER_H |