initial state machine
This commit is contained in:
parent
1f13ca71f0
commit
44fcc677bb
14 changed files with 282 additions and 12 deletions
|
@ -3,6 +3,8 @@
|
|||
#include <SPI.h>
|
||||
|
||||
#include "station.h"
|
||||
#include "statemachine.h"
|
||||
#include "state.h"
|
||||
|
||||
#define PN532_CS 10
|
||||
|
||||
|
@ -13,6 +15,8 @@ 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();
|
||||
|
||||
void setup(void) {
|
||||
setupNFC();
|
||||
|
||||
|
@ -42,7 +46,9 @@ void setupNFC() {
|
|||
void checkStations() {
|
||||
Station* station = detectStation();
|
||||
lcd.setCursor(0, 0);
|
||||
lcd.print("docked to station ");
|
||||
if (station != 0) {
|
||||
lcd.print("Station: " + station->getName());
|
||||
}
|
||||
}
|
||||
|
||||
Station* detectStation() {
|
||||
|
@ -51,10 +57,8 @@ Station* detectStation() {
|
|||
Station* found = 0;
|
||||
for (int i = 0; i < sizeof(stations); i++) {
|
||||
Station* currentStation = &stations[i];
|
||||
if (id != 0) {
|
||||
if(currentStation->check(id)) {
|
||||
found = currentStation;
|
||||
}
|
||||
if (currentStation->check(id)) {
|
||||
found = currentStation;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
|
|
18
main/state.h
18
main/state.h
|
@ -1,12 +1,16 @@
|
|||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include "station.h"
|
||||
|
||||
enum class State {
|
||||
|
||||
}
|
||||
|
||||
class GameState {
|
||||
private:
|
||||
class Station;
|
||||
|
||||
class State {
|
||||
public:
|
||||
virtual ~State() = default;
|
||||
|
||||
}
|
||||
virtual State* pickedUp() { return this; }
|
||||
virtual State* putDown(Station* newStation) { return this; }
|
||||
virtual State* update() { return this; }
|
||||
|
||||
virtual void updateDisplay(LiquidCrystal* lcd) { }
|
||||
};
|
||||
|
|
31
main/statemachine.cpp
Normal file
31
main/statemachine.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#include "statemachine.h"
|
||||
#include "station.h"
|
||||
|
||||
StateMachine::StateMachine(State* initialState)
|
||||
: currentState(initialState) {}
|
||||
|
||||
StateMachine::~StateMachine() {
|
||||
delete currentState;
|
||||
}
|
||||
|
||||
void StateMachine::updateState(State* newState) {
|
||||
if (newState != currentState) {
|
||||
delete currentState;
|
||||
currentState = newState;
|
||||
}
|
||||
}
|
||||
|
||||
void StateMachine::pickedUp() {
|
||||
State* newState = currentState->pickedUp();
|
||||
updateState(newState);
|
||||
}
|
||||
|
||||
void StateMachine::putDown(Station* newStation) {
|
||||
State* newState = currentState->putDown(newStation);
|
||||
updateState(newState);
|
||||
}
|
||||
|
||||
void StateMachine::update() {
|
||||
State* newState = currentState->update();
|
||||
updateState(newState);
|
||||
}
|
20
main/statemachine.h
Normal file
20
main/statemachine.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include "state.h"
|
||||
#include "station.h"
|
||||
|
||||
class StateMachine {
|
||||
private:
|
||||
State* currentState;
|
||||
void updateState(State* newState);
|
||||
|
||||
public:
|
||||
StateMachine(State* initialState);
|
||||
~StateMachine();
|
||||
|
||||
void pickedUp();
|
||||
void putDown(Station* newSation);
|
||||
void update();
|
||||
|
||||
void updateDisplay(LiquidCrystal* lcd);
|
||||
};
|
42
main/states.cpp
Normal file
42
main/states.cpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include "states.h"
|
||||
#include "state.h"
|
||||
#include "station.h"
|
||||
|
||||
//-------
|
||||
// On the Move
|
||||
//-------
|
||||
|
||||
|
||||
OnTheMove::OnTheMove(Station* targetStation) {
|
||||
this->targetStation = targetStation;
|
||||
}
|
||||
|
||||
State* OnTheMove::putDown(Station* newStation) {
|
||||
return new Hacking(newStation);
|
||||
}
|
||||
|
||||
|
||||
//-------
|
||||
// Hacking
|
||||
//-------
|
||||
|
||||
Hacking::Hacking(Station* currentStation) {
|
||||
this->currentStation = currentStation;
|
||||
}
|
||||
|
||||
State* Hacking::pickedUp() {
|
||||
return this;
|
||||
}
|
||||
|
||||
//-------
|
||||
// Waiting F orPickup
|
||||
//-------
|
||||
|
||||
WaitingForPickup::WaitingForPickup(Station* currentStation, Station* targetStation) {
|
||||
this->currentStation = currentStation;
|
||||
this->targetStation = targetStation;
|
||||
}
|
||||
|
||||
State* WaitingForPickup::pickedUp() {
|
||||
return this;
|
||||
}
|
30
main/states.h
Normal file
30
main/states.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include "state.h"
|
||||
#include "states.h"
|
||||
#include "station.h"
|
||||
|
||||
class OnTheMove : public State {
|
||||
private:
|
||||
Station* targetStation;
|
||||
public:
|
||||
OnTheMove(Station* targetStation);
|
||||
State* putDown(Station* newSation) override;
|
||||
};
|
||||
|
||||
class Hacking : public State {
|
||||
private:
|
||||
Station* currentStation;
|
||||
public:
|
||||
Hacking(Station* currentStation);
|
||||
State* pickedUp() override;
|
||||
};
|
||||
|
||||
class WaitingForPickup : public State {
|
||||
private:
|
||||
Station* currentStation;
|
||||
Station* targetStation;
|
||||
public:
|
||||
WaitingForPickup(Station* currentStation, Station* targetStation);
|
||||
State* pickedUp();
|
||||
};
|
|
@ -1,3 +1,4 @@
|
|||
#include <Arduino.h>
|
||||
#include "station.h"
|
||||
|
||||
Station::Station(uint32_t nfcTagId, String name) {
|
||||
|
@ -10,3 +11,7 @@ Station::Station(uint32_t nfcTagId, String name) {
|
|||
bool Station::check(uint32_t nfcTagId) {
|
||||
return nfcTagId == this->nfcTagId;
|
||||
}
|
||||
|
||||
String Station::getName() {
|
||||
return this->name;
|
||||
}
|
||||
|
|
|
@ -8,4 +8,5 @@ private:
|
|||
public:
|
||||
Station(uint32_t nfcTagId, String name);
|
||||
bool check(uint32_t nfcTagId);
|
||||
String getName();
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue