Implement main update loop
This commit is contained in:
parent
2704bb2636
commit
46e0e8d0a6
@ -1,5 +1,10 @@
|
|||||||
#include "main_window.hpp"
|
#include "main_window.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <numeric>
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include <QtDebug>
|
#include <QtDebug>
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
|
|
||||||
@ -100,4 +105,66 @@ void MainWindow::loadIcons()
|
|||||||
void MainWindow::updateState()
|
void MainWindow::updateState()
|
||||||
{
|
{
|
||||||
qDebug(ltr("Updating state"));
|
qDebug(ltr("Updating state"));
|
||||||
|
|
||||||
|
bool sensorError = false;
|
||||||
|
bool monitorError = false;
|
||||||
|
size_t iconBrightness = 0;
|
||||||
|
|
||||||
|
const auto getAverageSensorValue = [this](float& brightness) {
|
||||||
|
if(m_sensors.empty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<float> values;
|
||||||
|
|
||||||
|
for(auto& sensor: m_sensors) {
|
||||||
|
const auto sensorValues = sensor.readValues();
|
||||||
|
if(!sensor) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
values.insert(values.end(), sensorValues.begin(), sensorValues.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
brightness = std::accumulate(values.begin(), values.end(), 0.f) / values.size();
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto setAllMonitorsBrightness = [this](float& brightness) {
|
||||||
|
if(m_monitors.empty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool errorOccurred = false;
|
||||||
|
for(auto& monitor: m_monitors) {
|
||||||
|
if(!monitor.setBrightness(brightness)) {
|
||||||
|
errorOccurred = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return !errorOccurred;
|
||||||
|
};
|
||||||
|
|
||||||
|
if(!getAverageSensorValue(m_brightness)) {
|
||||||
|
m_sensors = enumerateSensors();
|
||||||
|
qInfo(ltr("At least one sensor became invalid, re-enumerating sensors found %1 valid sensor(s)").arg(m_sensors.size()));
|
||||||
|
sensorError = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto roundedBrightness = std::round(utils::map(m_brightness, 0.f, 1.f, 0, 8));
|
||||||
|
iconBrightness = std::clamp<size_t>(roundedBrightness, 0, 8);
|
||||||
|
|
||||||
|
if(!setAllMonitorsBrightness(m_brightness)) {
|
||||||
|
m_monitors = enumerateMonitors();
|
||||||
|
qInfo(ltr("At least one monitor became invalid, re-enumerating monitors found %1 valid monitor(s)").arg(m_monitors.size()));
|
||||||
|
monitorError = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!sensorError && !monitorError && m_trayIcon) {
|
||||||
|
m_trayIcon->setIcon(*m_sensorStateIcons[iconBrightness]);
|
||||||
|
}
|
||||||
|
else if(sensorError && !monitorError && m_trayIcon) {
|
||||||
|
m_trayIcon->setIcon(*m_noSensorStateIcons[iconBrightness]);
|
||||||
|
}
|
||||||
|
else if(m_trayIcon) {
|
||||||
|
m_trayIcon->setIcon(*m_errorIcon);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <QAction>
|
#include <QAction>
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
@ -40,7 +41,7 @@ class MainWindow : public QMainWindow {
|
|||||||
|
|
||||||
QTimer* m_timer = nullptr;
|
QTimer* m_timer = nullptr;
|
||||||
|
|
||||||
float m_brightness;
|
float m_brightness = 0.5f;
|
||||||
|
|
||||||
std::vector<Sensor> m_sensors;
|
std::vector<Sensor> m_sensors;
|
||||||
std::vector<Monitor> m_monitors;
|
std::vector<Monitor> m_monitors;
|
||||||
|
Loading…
Reference in New Issue
Block a user