-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathbatterycontroller.cpp
42 lines (33 loc) · 1 KB
/
batterycontroller.cpp
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
#include "batterycontroller.h"
#include <random>
#include <QDateTime>
#include <QTimer>
#include <QDebug>
// defines how often (milisec) the charge changes
#define TIME_INTERVAL 2000
BatteryController::BatteryController(QObject *parent)
: QObject(parent), _charge(100)
{
// start generating random values for _charge
QTimer::singleShot(TIME_INTERVAL, this, SLOT(_tick()));
}
void BatteryController::_tick()
{
// generate a random number between 0-100 every 5 seconds
unsigned int ms = static_cast<unsigned>(QDateTime::currentMSecsSinceEpoch());
std::mt19937 gen(ms);
std::uniform_int_distribution<> uid(0, 100); // min, max
_charge = uid(gen);
qDebug() << "_tick: charge=" << _charge;
emit chargeValueChanged();
QTimer::singleShot(TIME_INTERVAL, this, SLOT(_tick()));
}
int BatteryController::chargeValue()
{
return _charge;
}
void BatteryController::setChargeValue(int value)
{
_charge = value;
emit chargeValueChanged();
}