light_controller.c

Functions

getBrightnessValue

76static brightness_t getBrightnessValue()
77{
78#ifdef CONFIG_BRIGHTNESS_ADJUSTMENT_ENABLED
79    /**
80     * ```{impl} Variable brightness
81     *    :id: SWIMPL_LC-005
82     *    :implements: SWDD_LC-204
83     * ```
84     */
85    return RteGetBrightnessValue();
86#else
87    return 128;
88#endif
89}

getRGBColorWithBrightness

@brief Converts a LightColor enum and brightness to an RGBColor struct. @param colorEnum The LightColor enum value. @param brightness The brightness value (0-255). @return The corresponding RGBColor struct.

 97static RGBColor getRGBColorWithBrightness(LightColor colorEnum, brightness_t brightness)
 98{
 99    RGBColor color = OFF_COLOR;
100    switch (colorEnum)
101    {
102    case COLOR_VAL_GREEN:
103        color.green = brightness;
104        break;
105    case COLOR_VAL_BLUE:
106        color.blue = brightness;
107        break;
108    case COLOR_VAL_RED:
109        color.red = brightness;
110        break;
111    case COLOR_VAL_PURPLE:
112        color.red = brightness / 2; // Assuming purple is half red, full blue
113        color.blue = brightness;
114        break;
115    case COLOR_VAL_OFF:
116    default:
117        color = OFF_COLOR;
118        break;
119    }
120    return color;
121}

turnLightOff

Implementation: Turn light off SWIMPL_LC-002 ../../../../../../_images/arrow-right-circle.svg
implements: SWDD_LC-102
129static void turnLightOff(void)
130{
131#if CONFIG_BLINKING
132    blinkState = FALSE;
133#endif
134    RteSetLightValue(OFF_COLOR);
135}

turnLightOn

Implementation: Turn light on SWIMPL_LC-003 ../../../../../../_images/arrow-right-circle.svg
implements: SWDD_LC-102
143static void turnLightOn(void)
144{
145#ifdef CONFIG_BRIGHTNESS_ADJUSTMENT_PERIOD
146    // Check if the brightness adjustment counter is zero to switch colors
147    unsigned int counter = 0;
148    RteGetBrightnessAdjustmentCounter(&counter);
149    if (counter == 0)
150    {
151        light_colors_index = (light_colors_index + 1) % light_colors_count;
152    }
153#else
154    light_colors_index = 0;
155#endif
156
157    // Get the current color enum and brightness
158    LightColor currentColorEnum = light_colors[light_colors_index];
159    brightness_t currentBrightness = getBrightnessValue();
160
161    // Convert to RGBColor and set the light value
162    RGBColor color = getRGBColorWithBrightness(currentColorEnum, currentBrightness);
163
164#if CONFIG_BLINKING
165    blinkState = TRUE;
166#endif
167    RteSetLightValue(color);
168}

lightController

Implementation: Light Controller's main function SWIMPL_LC-006 ../../../../../../_images/arrow-right-circle.svg
implements: SWDD_LC-100

@brief Controls the light state.

Uses a state machine to determine the light state based on several inputs, e.g., the system’s power state.

201void lightController(void)
202{
203
204    PowerState powerState = RteGetPowerState();
205#if CONFIG_BLINKING
206    percentage_t mainKnobValue = RteGetMainKnobValue();
207    unsigned int blinkPeriod = calculateBlinkPeriod(mainKnobValue);
208#endif
209
210    switch (currentLightState)
211    {
212    case LIGHT_OFF:
213#if CONFIG_BLINKING
214        blinkCounter = 0;
215#endif
216        if (powerState != POWER_STATE_OFF)
217        {
218            turnLightOn();
219            currentLightState = LIGHT_ON;
220        }
221        break;
222
223    default: // LIGHT_ON
224        if (powerState == POWER_STATE_OFF)
225        {
226            turnLightOff();
227            currentLightState = LIGHT_OFF;
228        }
229#if CONFIG_BLINKING
230        else
231        {
232            blinkCounter++;
233            if (blinkCounter >= blinkPeriod)
234            {
235                // Toggle the LED state
236                if (blinkState == TRUE)
237                {
238                    turnLightOff();
239                }
240                else
241                {
242                    turnLightOn();
243                }
244                blinkCounter = 0;
245            }
246        }
247#endif
248#ifdef CONFIG_BRIGHTNESS_ADJUSTMENT_ENABLED
249        else
250        {
251            turnLightOn();
252        }
253#endif
254        break;
255    }
256}