Skip to content

Commit

Permalink
#97: added lap times to efficiency mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrwang committed Jun 6, 2024
1 parent 6a5da73 commit b90e713
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 23 deletions.
37 changes: 19 additions & 18 deletions NERODevelopment/content/EfficiencyScreen.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions NERODevelopment/content/SpeedMode.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 63 additions & 1 deletion NERODevelopment/src/controllers/efficiencycontroller.cpp
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<int>(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<int>(m_timer.elapsed()));
}
}
26 changes: 26 additions & 0 deletions NERODevelopment/src/controllers/efficiencycontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define EFFICIENCYCONTROLLER_H

#include "buttoncontroller.h"
#include <QElapsedTimer>
#include <QObject>
#include <QTimer>

/**
* @brief The EfficiencyController class
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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]
Expand All @@ -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

0 comments on commit b90e713

Please sign in to comment.