C

Qt Quick Ultralite Automotive Cluster Demo

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
#include "simulation/lights.h"
#include <algorithm>

namespace Simulation {
Lights::Lights()
{
    for (int i = 0; i < LIGHTS_NUM; ++i) {
        resetLight((LightType) i);
    }
}

void Lights::update(uint32_t tick)
{
    for (int i = 0; i < LIGHTS_NUM; ++i) {
        if (_lightTimers[i] > 0) {
            _lightTimers[i] -= std::min(tick, _lightTimers[i]);
            if (_lightTimers[i] == 0) {
                _lightStates[i] = !_lightStates[i];
            }
        }
    }
}

void Lights::resetLight(LightType light, bool enabled)
{
    _lightStates[light] = enabled;
    _lightTimers[light] = 0;
}

void Lights::setLightState(LightType light, bool enabled, uint32_t duration)
{
    _lightStates[light] = enabled;
    _lightTimers[light] = duration;

    if (light == TurnLeft && enabled) {
        // Turn signals are mutually exclusive
        resetLight(TurnRight);
    } else if (light == TurnRight && enabled) {
        // Turn signals are mutually exclusive
        resetLight(TurnLeft);
    } else if (light == ParkingLights && !enabled) {
        // Low beam headlights must also go off when parking lights are set off
        resetLight(LowBeamHeadlights);
    } else if (light == LowBeamHeadlights && enabled) {
        // Parking lights must be also on when low beam headlights are set on
        if (!getLightState(ParkingLights)) {
            setLightState(ParkingLights, enabled, duration);
        }
    }
}

bool Lights::getLightState(LightType light) const
{
    return _lightStates[light];
}
} // namespace Simulation