adds timer to control nfc check duration
This commit is contained in:
parent
8b16bcbee5
commit
b0778df859
6 changed files with 81 additions and 6 deletions
|
@ -1 +1,6 @@
|
|||
#include "Context.h"
|
||||
|
||||
Context::Context(Station* stations, unsigned long delta) {
|
||||
this->stations = stations;
|
||||
this->delta = delta;
|
||||
}
|
||||
|
|
|
@ -4,5 +4,8 @@
|
|||
|
||||
class Context {
|
||||
public:
|
||||
Station* stations[4];
|
||||
Context(Station* stations, unsigned long delta);
|
||||
|
||||
Station* stations;
|
||||
unsigned long delta;
|
||||
};
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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]);
|
||||
}
|
33
main/timer.cpp
Normal file
33
main/timer.cpp
Normal file
|
@ -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;
|
||||
}
|
18
main/timer.h
Normal file
18
main/timer.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
#include <Arduino.h>
|
||||
|
||||
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();
|
||||
};
|
Loading…
Reference in a new issue