diff --git a/main/context.cpp b/main/context.cpp index 9b404b3..346df91 100644 --- a/main/context.cpp +++ b/main/context.cpp @@ -1 +1,6 @@ #include "Context.h" + +Context::Context(Station* stations, unsigned long delta) { + this->stations = stations; + this->delta = delta; +} diff --git a/main/context.h b/main/context.h index 85be7a2..33db2c9 100644 --- a/main/context.h +++ b/main/context.h @@ -4,5 +4,8 @@ class Context { public: - Station* stations[4]; + Context(Station* stations, unsigned long delta); + + Station* stations; + unsigned long delta; }; diff --git a/main/main.ino b/main/main.ino index 6521f16..f92ce37 100644 --- a/main/main.ino +++ b/main/main.ino @@ -6,6 +6,7 @@ #include "statemachine.h" #include "state.h" #include "states.h" +#include "timer.h" #define PN532_CS 10 @@ -15,21 +16,36 @@ const int rs = 9, en = 8, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); Station stations[4] = { Station(1680767519, "Yellow"), Station(3346823711, "Green"), Station(3569318175, "Pink"), Station(2174777887, "Blue") }; +Timer stationDetectionTimer(1000, true); StateMachine stateMachine(new Startup()); +unsigned long lastLoopTime = 0; +unsigned long currentLoopTime = 0; +unsigned long deltaTime = 0; + void setup(void) { setupNFC(); lcd.begin(16, 2); lcd.setCursor(0, 0); lcd.print("Test"); + stationDetectionTimer.start(); } void loop(void) { - lcd.clear(); - checkStations(); - delay(1000); + currentLoopTime = millis(); + deltaTime = currentLoopTime - lastLoopTime; + + bool doCheck = stationDetectionTimer.update(deltaTime); + if (doCheck) { + lcd.clear(); + checkStations(); + } + + stateMachine.update(Context(stations, deltaTime)); + + lastLoopTime = currentLoopTime; } void setupNFC() { @@ -48,7 +64,7 @@ void checkStations() { Station* station = detectStation(); lcd.setCursor(0, 0); if (station != 0) { - lcd.print("Station: " + station->getName()); + lcd.print("Station: " + station->getName()); } } diff --git a/main/states.cpp b/main/states.cpp index 03da931..e2f7ad7 100644 --- a/main/states.cpp +++ b/main/states.cpp @@ -58,5 +58,5 @@ NewTargetFinder::NewTargetFinder(Station* currentStation) { } State* NewTargetFinder::update(Context context) { - return new WaitingForPickup(this->currentStation, context.stations[0]); + return new WaitingForPickup(this->currentStation, (&context.stations)[0]); } \ No newline at end of file diff --git a/main/timer.cpp b/main/timer.cpp new file mode 100644 index 0000000..4692ea2 --- /dev/null +++ b/main/timer.cpp @@ -0,0 +1,33 @@ +#include "timer.h" + +Timer::Timer(unsigned long duration, bool autoRestart) { + this->autoRestart = autoRestart; + this->duration = duration; + this->endTime = duration; +} + +void Timer::start() { + this->running = true; +} + +bool Timer::update(unsigned long delta) { + if (!running) { + return false; + } + bool timedOut = false; + this->currentTime += delta; + if (currentTime > this->endTime) { + timedOut = true; + } + if (timedOut && this->autoRestart) { + this->endTime += this->duration; + } + if (timedOut && !this->autoRestart) { + running = false; + } + return timedOut; +} + +bool Timer::isRunning() { + return this->running; +} diff --git a/main/timer.h b/main/timer.h new file mode 100644 index 0000000..11dbdbb --- /dev/null +++ b/main/timer.h @@ -0,0 +1,18 @@ +#pragma once +#include + +class Timer { +private: + unsigned long duration; + bool autoRestart; + + unsigned long currentTime; + unsigned long endTime; + bool running; + +public: + Timer(unsigned long duration, bool autoRestart); + void start(); + bool update(unsigned long delta); + bool isRunning(); +};