diff --git a/main/context.cpp b/main/context.cpp new file mode 100644 index 0000000..9b404b3 --- /dev/null +++ b/main/context.cpp @@ -0,0 +1 @@ +#include "Context.h" diff --git a/main/context.h b/main/context.h new file mode 100644 index 0000000..85be7a2 --- /dev/null +++ b/main/context.h @@ -0,0 +1,8 @@ +#pragma once +#include +#include "station.h" + +class Context { +public: + Station* stations[4]; +}; diff --git a/main/main.ino b/main/main.ino index 36a3570..6521f16 100644 --- a/main/main.ino +++ b/main/main.ino @@ -5,6 +5,7 @@ #include "station.h" #include "statemachine.h" #include "state.h" +#include "states.h" #define PN532_CS 10 @@ -15,7 +16,7 @@ LiquidCrystal lcd(rs, en, d4, d5, d6, d7); Station stations[4] = { Station(1680767519, "Yellow"), Station(3346823711, "Green"), Station(3569318175, "Pink"), Station(2174777887, "Blue") }; -StateMachine stateMachine(); +StateMachine stateMachine(new Startup()); void setup(void) { setupNFC(); diff --git a/main/state.h b/main/state.h index 6e03ecd..3697703 100644 --- a/main/state.h +++ b/main/state.h @@ -1,6 +1,8 @@ #pragma once #include +#include #include "station.h" +#include "context.h" class Station; @@ -10,7 +12,7 @@ public: virtual State* pickedUp() { return this; } virtual State* putDown(Station* newStation) { return this; } - virtual State* update() { return this; } + virtual State* update(Context context) { return this; } virtual void updateDisplay(LiquidCrystal* lcd) { } }; diff --git a/main/statemachine.cpp b/main/statemachine.cpp index d890679..cdbf893 100644 --- a/main/statemachine.cpp +++ b/main/statemachine.cpp @@ -1,5 +1,6 @@ #include "statemachine.h" #include "station.h" +#include "context.h" StateMachine::StateMachine(State* initialState) : currentState(initialState) {} @@ -25,7 +26,7 @@ void StateMachine::putDown(Station* newStation) { updateState(newState); } -void StateMachine::update() { - State* newState = currentState->update(); +void StateMachine::update(Context context) { + State* newState = currentState->update(context); updateState(newState); } \ No newline at end of file diff --git a/main/statemachine.h b/main/statemachine.h index 4a6aaef..91b7db5 100644 --- a/main/statemachine.h +++ b/main/statemachine.h @@ -1,7 +1,9 @@ #pragma once #include +#include #include "state.h" #include "station.h" +#include "context.h" class StateMachine { private: @@ -14,7 +16,7 @@ public: void pickedUp(); void putDown(Station* newSation); - void update(); + void update(Context context); void updateDisplay(LiquidCrystal* lcd); }; diff --git a/main/states.cpp b/main/states.cpp index a861ba2..03da931 100644 --- a/main/states.cpp +++ b/main/states.cpp @@ -40,3 +40,23 @@ WaitingForPickup::WaitingForPickup(Station* currentStation, Station* targetStati State* WaitingForPickup::pickedUp() { return this; } + +//------- +// Startup +//------- + +State* Startup::putDown(Station* newStation) { + return new NewTargetFinder(newStation); +} + +//------- +// NewTargetFinder +//------- + +NewTargetFinder::NewTargetFinder(Station* currentStation) { + this->currentStation = currentStation; +} + +State* NewTargetFinder::update(Context context) { + return new WaitingForPickup(this->currentStation, context.stations[0]); +} \ No newline at end of file diff --git a/main/states.h b/main/states.h index 8969b23..cf9bf46 100644 --- a/main/states.h +++ b/main/states.h @@ -1,5 +1,6 @@ #pragma once #include +#include "context.h" #include "state.h" #include "states.h" #include "station.h" @@ -26,5 +27,19 @@ private: Station* targetStation; public: WaitingForPickup(Station* currentStation, Station* targetStation); - State* pickedUp(); -}; \ No newline at end of file + State* pickedUp() override; +}; + +class Startup : public State { +public: + State* putDown(Station* newSation) override; +}; + +class NewTargetFinder : public State { +private: + Station* currentStation; +public: + NewTargetFinder(Station* currentStation); + State* update(Context context) override; +}; +