-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattery.cpp
More file actions
44 lines (35 loc) · 1.08 KB
/
Battery.cpp
File metadata and controls
44 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "Battery.h"
#include <QDebug>
Battery::Battery(QObject *parent) : QObject(parent), batteryLevel(100) {
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Battery::consumePower);
}
Battery::~Battery() {
if (timer) timer->stop();
}
void Battery::startBatteryConsumption() {
timer->start(15000); // 60000 milliseconds = 60 seconds
// we can adjust this later
qDebug ("Battery consumption started.");
}
void Battery::stopBatteryConsumption() {
if (timer->isActive()) {
timer->stop();
}
}
void Battery::consumePower() {
batteryLevel -= 10; // Decrease battery by 10 percent
if(batteryLevel <= 0) {
batteryLevel = 0;
emit batteryDepleted(); // Emit the batteryDepleted signal
timer->stop(); // Stop the timer as the battery is dead
}
else if (batteryLevel <= 20) {
emit lowBatteryWarning();
}
emit batteryLevelChanged(batteryLevel);
qDebug() << "Battery level is now at" << batteryLevel << "%";
}
int Battery::getBatteryLevel() const {
return batteryLevel;
}