adds new states and context

This commit is contained in:
Marcel 2024-05-17 15:37:03 +02:00
parent 44fcc677bb
commit 8b16bcbee5
8 changed files with 57 additions and 7 deletions

1
main/context.cpp Normal file
View file

@ -0,0 +1 @@
#include "Context.h"

8
main/context.h Normal file
View file

@ -0,0 +1,8 @@
#pragma once
#include <Arduino.h>
#include "station.h"
class Context {
public:
Station* stations[4];
};

View file

@ -5,6 +5,7 @@
#include "station.h" #include "station.h"
#include "statemachine.h" #include "statemachine.h"
#include "state.h" #include "state.h"
#include "states.h"
#define PN532_CS 10 #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") }; Station stations[4] = { Station(1680767519, "Yellow"), Station(3346823711, "Green"), Station(3569318175, "Pink"), Station(2174777887, "Blue") };
StateMachine stateMachine(); StateMachine stateMachine(new Startup());
void setup(void) { void setup(void) {
setupNFC(); setupNFC();

View file

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include <LiquidCrystal.h>
#include "station.h" #include "station.h"
#include "context.h"
class Station; class Station;
@ -10,7 +12,7 @@ public:
virtual State* pickedUp() { return this; } virtual State* pickedUp() { return this; }
virtual State* putDown(Station* newStation) { 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) { } virtual void updateDisplay(LiquidCrystal* lcd) { }
}; };

View file

@ -1,5 +1,6 @@
#include "statemachine.h" #include "statemachine.h"
#include "station.h" #include "station.h"
#include "context.h"
StateMachine::StateMachine(State* initialState) StateMachine::StateMachine(State* initialState)
: currentState(initialState) {} : currentState(initialState) {}
@ -25,7 +26,7 @@ void StateMachine::putDown(Station* newStation) {
updateState(newState); updateState(newState);
} }
void StateMachine::update() { void StateMachine::update(Context context) {
State* newState = currentState->update(); State* newState = currentState->update(context);
updateState(newState); updateState(newState);
} }

View file

@ -1,7 +1,9 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include <LiquidCrystal.h>
#include "state.h" #include "state.h"
#include "station.h" #include "station.h"
#include "context.h"
class StateMachine { class StateMachine {
private: private:
@ -14,7 +16,7 @@ public:
void pickedUp(); void pickedUp();
void putDown(Station* newSation); void putDown(Station* newSation);
void update(); void update(Context context);
void updateDisplay(LiquidCrystal* lcd); void updateDisplay(LiquidCrystal* lcd);
}; };

View file

@ -40,3 +40,23 @@ WaitingForPickup::WaitingForPickup(Station* currentStation, Station* targetStati
State* WaitingForPickup::pickedUp() { State* WaitingForPickup::pickedUp() {
return this; 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]);
}

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include "context.h"
#include "state.h" #include "state.h"
#include "states.h" #include "states.h"
#include "station.h" #include "station.h"
@ -26,5 +27,19 @@ private:
Station* targetStation; Station* targetStation;
public: public:
WaitingForPickup(Station* currentStation, Station* targetStation); WaitingForPickup(Station* currentStation, Station* targetStation);
State* pickedUp(); 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;
};