C
Qt Quick Ultralite multitask Example
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
#include "hardwarecontrol.h"
HardwareControl::HardwareControl()
: fanSpeed(1)
, ledCycleCount(0)
{
ledCycleTimer.onTimeout([this]() { ledToggle(); });
}
void HardwareControl::updateSpeed(int newSpeed)
{
fanSpeed.setValue(newSpeed);
updateFanSpeed();
updateLedSpeed();
}
void HardwareControl::updateLedSpeed()
{
int interval = speedToLedToggleInterval(fanSpeed.value());
if (interval > 0) {
ledCycleTimer.setInterval(interval);
ledCycleTimer.start();
} else {
ledCycleTimer.stop();
}
}
void HardwareControl::updateFanSpeed()
{
fanRotationPeriodChanged(fanSpeed.value() == 0 ? 0 : 5000 / (fanSpeed.value() * 3));
}
static HardwareControlEventQueue eventQueue;
void postEventsToUI(HardwareEvent &event)
{
eventQueue.postEvent(event);
}
void HardwareControl::ledToggle()
{
static bool ledOn = false;
static int cycleCount = 0;
static HardwareEvent event;
if (ledOn) {
cycleCount++;
cycleCount = cycleCount < 0 ? 0 : cycleCount;
}
ledOn = !ledOn;
event.id = HardwareEventId::LedCycleCount;
event.data = cycleCount;
postEventsToUI(event);
}
void HardwareControlEventQueue::onEvent(const HardwareEvent &event)
{
if (event.id == HardwareEventId::LedCycleCount)
HardwareControl::instance().ledCycleCount.setValue(event.data);
else if (event.id == HardwareEventId::FanRotationPeriod)
HardwareControl::instance().fanRotationPeriodChanged(event.data);
}