diff --git a/NERODevelopment/content/EfficiencyScreen.qml b/NERODevelopment/content/EfficiencyScreen.qml index b156dcf..2b12b86 100644 --- a/NERODevelopment/content/EfficiencyScreen.qml +++ b/NERODevelopment/content/EfficiencyScreen.qml @@ -15,10 +15,20 @@ Item { property int motorTemp: efficiencyController.motorTemp property int packTemp: efficiencyController.packTemp property int speed: efficiencyController.speed + property int timerValue: efficiencyController.currentTime + property int lastRunTime: efficiencyController.lastTime + property int fastestRunTime: efficiencyController.fastestTime width: 800 height: 480 + Keys.onPressed: (event) => { + if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) { + efficiencyController.enterButtonPressed(); + } + } + + HeaderView { id: header } @@ -65,17 +75,16 @@ Item { width: efficiency.width * 0.5 height: parent.height - Rectangle { - id: onRect - width: parent.width - height: parent.width * 0.1 - color: "transparent" - LabelText { - text: "ON" - color: "#18ff00" - anchors.centerIn: parent - font.pixelSize: 50 + TimerDisplay { + id: timerDisplay + anchors { + horizontalCenter: parent.horizontalCenter } + width: efficiency.width * 0.4 + height: efficiency.height * 0.3 + currentRunTime: efficiency.timerValue + lastRunTime: efficiency.lastRunTime + fastestRunTime: efficiency.fastestRunTime } Spedometer { @@ -84,14 +93,6 @@ Item { value: efficiency.speed anchors.top: onRect.bottom } - - DirectionView { - id: directionView - width: parent.width * 0.5 - height: parent.height * 0.15 - anchors.horizontalCenter: parent.horizontalCenter - anchors.top: spedometer.bottom - } } ColumnLayout { diff --git a/NERODevelopment/content/SpeedMode.qml b/NERODevelopment/content/SpeedMode.qml index 8ff5234..69b7b36 100644 --- a/NERODevelopment/content/SpeedMode.qml +++ b/NERODevelopment/content/SpeedMode.qml @@ -18,11 +18,11 @@ Item { property int currentSpeed: speedController.currentSpeed property int currentDraw: speedController.current - Keys.onPressed: { - if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) { - speedController.enterButtonPressed(); - } + Keys.onPressed: (event) => { + if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) { + speedController.enterButtonPressed(); } + } HeaderView { id: headerView diff --git a/NERODevelopment/src/controllers/efficiencycontroller.cpp b/NERODevelopment/src/controllers/efficiencycontroller.cpp index 4334f61..f05aa97 100644 --- a/NERODevelopment/src/controllers/efficiencycontroller.cpp +++ b/NERODevelopment/src/controllers/efficiencycontroller.cpp @@ -1,9 +1,13 @@ #include "efficiencycontroller.h" EfficiencyController::EfficiencyController(Model *model, QObject *parent) - : ButtonController{model, 3, parent} { + : ButtonController{model, 3, parent}, m_updateTimer(new QTimer(this)), + m_timerRunning(false) { connect(m_model, &Model::onCurrentDataChange, this, &EfficiencyController::currentDataDidChange); + connect(m_updateTimer, &QTimer::timeout, this, + &EfficiencyController::updateCurrentTime); + m_updateTimer->setInterval(1); } int EfficiencyController::currentMaxTorque() const { @@ -99,3 +103,61 @@ void EfficiencyController::currentDataDidChange() { setSpeed(*speed); } } + +int EfficiencyController::currentTime() const { return m_currentTime; } +void EfficiencyController::setCurrentTime(int currentTime) { + if (currentTime != m_currentTime) { + m_currentTime = currentTime; + emit currentTimeChanged(currentTime); + } +} +int EfficiencyController::fastestTime() const { return m_fastestTime; } +void EfficiencyController::setFastestTime(int fastTime) { + if (fastTime != m_fastestTime) { + m_fastestTime = fastTime; + emit fastestTimeChanged(fastTime); + } +} +int EfficiencyController::lastTime() const { return m_lastTime; } +void EfficiencyController::setLastTime(int lastTime) { + if (lastTime != m_lastTime) { + m_lastTime = lastTime; + emit lastTimeChanged(lastTime); + } +} + +void EfficiencyController::enterButtonPressed() { + if (m_timerRunning) { + int runTime = static_cast(m_timer.elapsed()); + m_timerRunning = false; + m_updateTimer->stop(); + + qDebug() << "Timer stopped. Run time:" << runTime + << " Last time:" << m_lastTime + << " Fastest time:" << m_fastestTime; + + setLastTime(runTime); + setCurrentTime(runTime); + + if (runTime < fastestTime() || fastestTime() == 0) { + setFastestTime(runTime); + qDebug() << "Fastest time overridden:" << runTime; + } + + m_timerRunning = true; + m_timer.start(); + m_updateTimer->start(); + qDebug() << "Timer started for new lap."; + } else { + m_timerRunning = true; + m_timer.start(); + m_updateTimer->start(); + qDebug() << "Timer started."; + } +} + +void EfficiencyController::updateCurrentTime() { + if (m_timerRunning) { + setCurrentTime(static_cast(m_timer.elapsed())); + } +} diff --git a/NERODevelopment/src/controllers/efficiencycontroller.h b/NERODevelopment/src/controllers/efficiencycontroller.h index 3c9cf2a..1b9a9bd 100644 --- a/NERODevelopment/src/controllers/efficiencycontroller.h +++ b/NERODevelopment/src/controllers/efficiencycontroller.h @@ -2,7 +2,9 @@ #define EFFICIENCYCONTROLLER_H #include "buttoncontroller.h" +#include #include +#include /** * @brief The EfficiencyController class @@ -25,6 +27,12 @@ class EfficiencyController : public ButtonController { setLowVoltageStateOfCharge NOTIFY lowVoltageStateOfChargeChanged FINAL) Q_PROPERTY(int speed READ speed WRITE setSpeed NOTIFY speedChanged FINAL) + Q_PROPERTY(int currentTime READ currentTime WRITE setCurrentTime NOTIFY + currentTimeChanged) + Q_PROPERTY(int fastestTime READ fastestTime WRITE setFastestTime NOTIFY + fastestTimeChanged) + Q_PROPERTY( + int lastTime READ lastTime WRITE setLastTime NOTIFY lastTimeChanged) public: explicit EfficiencyController(Model *model, QObject *parent = nullptr); @@ -35,6 +43,9 @@ class EfficiencyController : public ButtonController { int packTemp() const; int lowVoltageStateOfCharge() const; int speed() const; + int currentTime() const; + int fastestTime() const; + int lastTime() const; signals: void currentMaxTorqueChanged(int); @@ -44,6 +55,9 @@ class EfficiencyController : public ButtonController { void packTempChanged(int); void lowVoltageStateOfChargeChanged(int); void speedChanged(int); + void currentTimeChanged(int); + void fastestTimeChanged(int); + void lastTimeChanged(int); public slots: void setCurrentMaxTorque(int); @@ -54,6 +68,12 @@ public slots: void setLowVoltageStateOfCharge(int); void setSpeed(int); void currentDataDidChange(); + void setCurrentTime(int); + void setFastestTime(int); + void setLastTime(int); + + void enterButtonPressed() override; + void updateCurrentTime(); private: int m_currentMaxTorque = 0; // torque percentage [0,100] @@ -63,6 +83,12 @@ public slots: int m_packTemp = 0; // Celsius int m_lowVoltageStateOfCharge = 0; // charge percentage [0,100] int m_speed = 0; // speed in mph + int m_currentTime = 0; + int m_fastestTime = 0; + int m_lastTime = 0; + bool m_timerRunning = false; + QElapsedTimer m_timer; + QTimer *m_updateTimer; }; #endif // EFFICIENCYCONTROLLER_H