QDriverStation
The free, cross-platform FRC Driver Station
utilities.h
1 /*
2  * Copyright (c) 2015-2016 Alex Spataru <alex_spataru@outlook.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef _QDS_UTILITIES_H
24 #define _QDS_UTILITIES_H
25 
26 #include <QProcess>
27 
28 #if defined Q_OS_LINUX
29  #include <QPair>
30 #endif
31 
32 class QSettings;
33 
37 class Utilities : public QObject {
38  Q_OBJECT
39 
40  public:
41  explicit Utilities();
42 
43  Q_INVOKABLE int cpuUsage();
44  Q_INVOKABLE int batteryLevel();
45  Q_INVOKABLE qreal scaleRatio();
46  Q_INVOKABLE bool isConnectedToAC();
47 
48  public slots:
49  void copy (const QVariant& data);
50  void setAutoScaleEnabled (const bool enabled);
51 
52  private slots:
53  void updateCpuUsage();
54  void updateBatteryLevel();
55  void updateConnectedToAC();
56  void calculateScaleRatio();
57  void readCpuUsageProcess (int exit_code = 0);
58  void readBatteryLevelProcess (int exit_code = 0);
59  void readConnectedToACProcess (int exit_code = 0);
60 
61  private:
62 #if defined (Q_OS_LINUX)
63  QPair<quint64, quint64> getCpuJiffies();
64  QPair<quint64, quint64> m_pastCpuJiffies {0, 0};
65 #endif
66 
67  qreal m_ratio;
68  int m_cpuUsage;
69  int m_batteryLevel;
70  bool m_connectedToAC;
71 
72  QSettings* m_settings;
73  QProcess m_cpuProcess;
74  QProcess m_batteryLevelProcess;
75  QProcess m_connectedToACProcess;
76 };
77 
78 #endif
Utilities()
Definition: utilities.cpp:91
Provides CPU and Battery information to the QML interface.
Definition: utilities.h:37
void copy(const QVariant &data)
Definition: utilities.cpp:169
void calculateScaleRatio()
Definition: utilities.cpp:245
Q_INVOKABLE int batteryLevel()
Definition: utilities.cpp:149
Q_INVOKABLE qreal scaleRatio()
Definition: utilities.cpp:127
void readConnectedToACProcess(int exit_code=0)
Definition: utilities.cpp:329
void updateBatteryLevel()
Definition: utilities.cpp:210
Q_INVOKABLE int cpuUsage()
Definition: utilities.cpp:137
void setAutoScaleEnabled(const bool enabled)
Definition: utilities.cpp:177
void readBatteryLevelProcess(int exit_code=0)
Definition: utilities.cpp:300
void updateCpuUsage()
Definition: utilities.cpp:184
Q_INVOKABLE bool isConnectedToAC()
Definition: utilities.cpp:162
void readCpuUsageProcess(int exit_code=0)
Definition: utilities.cpp:273
void updateConnectedToAC()
Definition: utilities.cpp:227