| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file light_controller.c | ||
| 3 | * @brief Module to control light based on power state. | ||
| 4 | */ | ||
| 5 | |||
| 6 | #include "light_controller.h" | ||
| 7 | #include "rte.h" | ||
| 8 | #include "autoconf.h" | ||
| 9 | |||
| 10 | /** | ||
| 11 | * @enum LightColor | ||
| 12 | * @brief Represents the possible light colors. | ||
| 13 | */ | ||
| 14 | typedef enum | ||
| 15 | { | ||
| 16 | COLOR_VAL_OFF, | ||
| 17 | COLOR_VAL_GREEN, | ||
| 18 | COLOR_VAL_BLUE, | ||
| 19 | COLOR_VAL_RED, | ||
| 20 | COLOR_VAL_PURPLE | ||
| 21 | } LightColor; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * ```{impl} Light state | ||
| 25 | * :id: SWIMPL_LC-001 | ||
| 26 | * :implements: SWDD_LC-100 | ||
| 27 | * ``` | ||
| 28 | * | ||
| 29 | * @enum LightState | ||
| 30 | * @brief Represents the states of the light. | ||
| 31 | */ | ||
| 32 | typedef enum | ||
| 33 | { | ||
| 34 | LIGHT_OFF, /**< Represents a state where the light is turned off. */ | ||
| 35 | LIGHT_ON /**< Represents a state where the light is turned on with a specific color. */ | ||
| 36 | } LightState; | ||
| 37 | |||
| 38 | static LightState currentLightState = LIGHT_OFF; /**< Current state of the light. */ | ||
| 39 | #if CONFIG_BLINKING | ||
| 40 | static int blinkCounter = 0; | ||
| 41 | static boolean blinkState = FALSE; | ||
| 42 | #endif | ||
| 43 | const RGBColor OFF_COLOR = {.red = 0, .green = 0, .blue = 0}; | ||
| 44 | |||
| 45 | const LightColor light_colors[] = { | ||
| 46 | #if CONFIG_COLOR_GREEN | ||
| 47 | COLOR_VAL_GREEN | ||
| 48 | #elif CONFIG_COLOR_BLUE | ||
| 49 | COLOR_VAL_BLUE | ||
| 50 | #elif CONFIG_COLOR_RED | ||
| 51 | COLOR_VAL_RED | ||
| 52 | #elif CONFIG_COLOR_PURPLE | ||
| 53 | COLOR_VAL_PURPLE | ||
| 54 | #else | ||
| 55 | COLOR_VAL_OFF | ||
| 56 | #endif | ||
| 57 | #if CONFIG_COLOR_1_IS_ENABLED | ||
| 58 | , | ||
| 59 | #if CONFIG_COLOR_1_GREEN | ||
| 60 | COLOR_VAL_GREEN | ||
| 61 | #elif CONFIG_COLOR_1_BLUE | ||
| 62 | COLOR_VAL_BLUE | ||
| 63 | #elif CONFIG_COLOR_1_RED | ||
| 64 | COLOR_VAL_RED | ||
| 65 | #elif CONFIG_COLOR_1_PURPLE | ||
| 66 | COLOR_VAL_PURPLE | ||
| 67 | #else | ||
| 68 | COLOR_VAL_OFF | ||
| 69 | #endif | ||
| 70 | #endif // CONFIG_COLOR_1_IS_ENABLED | ||
| 71 | }; | ||
| 72 | const int light_colors_count = sizeof(light_colors) / sizeof(light_colors[0]); | ||
| 73 | // Used to iterate through the light colors | ||
| 74 | int light_colors_index = 0; | ||
| 75 | |||
| 76 | 14 | static 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 | 10 | return RteGetBrightnessValue(); | |
| 86 | #else | ||
| 87 | 4 | return 128; | |
| 88 | #endif | ||
| 89 | } | ||
| 90 | |||
| 91 | /** | ||
| 92 | * @brief Converts a LightColor enum and brightness to an RGBColor struct. | ||
| 93 | * @param colorEnum The LightColor enum value. | ||
| 94 | * @param brightness The brightness value (0-255). | ||
| 95 | * @return The corresponding RGBColor struct. | ||
| 96 | */ | ||
| 97 | 14 | static RGBColor getRGBColorWithBrightness(LightColor colorEnum, brightness_t brightness) | |
| 98 | { | ||
| 99 | 14 | RGBColor color = OFF_COLOR; | |
| 100 |
2/5✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 4 not taken.
✗ Branch 2 → 5 not taken.
✓ Branch 2 → 6 taken 10 times.
✗ Branch 2 → 7 not taken.
|
14 | switch (colorEnum) |
| 101 | { | ||
| 102 | 4 | case COLOR_VAL_GREEN: | |
| 103 | 4 | color.green = brightness; | |
| 104 | 4 | break; | |
| 105 | ✗ | case COLOR_VAL_BLUE: | |
| 106 | ✗ | color.blue = brightness; | |
| 107 | ✗ | break; | |
| 108 | ✗ | case COLOR_VAL_RED: | |
| 109 | ✗ | color.red = brightness; | |
| 110 | ✗ | break; | |
| 111 | 10 | case COLOR_VAL_PURPLE: | |
| 112 | 10 | color.red = brightness / 2; // Assuming purple is half red, full blue | |
| 113 | 10 | color.blue = brightness; | |
| 114 | 10 | break; | |
| 115 | ✗ | case COLOR_VAL_OFF: | |
| 116 | default: | ||
| 117 | ✗ | color = OFF_COLOR; | |
| 118 | ✗ | break; | |
| 119 | } | ||
| 120 | 14 | return color; | |
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 | * ```{impl} Turn light off | ||
| 125 | * :id: SWIMPL_LC-002 | ||
| 126 | * :implements: SWDD_LC-102 | ||
| 127 | * ``` | ||
| 128 | */ | ||
| 129 | 3 | static void turnLightOff(void) | |
| 130 | { | ||
| 131 | #if CONFIG_BLINKING | ||
| 132 | 2 | blinkState = FALSE; | |
| 133 | #endif | ||
| 134 | 3 | RteSetLightValue(OFF_COLOR); | |
| 135 | 3 | } | |
| 136 | |||
| 137 | /** | ||
| 138 | * ```{impl} Turn light on | ||
| 139 | * :id: SWIMPL_LC-003 | ||
| 140 | * :implements: SWDD_LC-102 | ||
| 141 | * ``` | ||
| 142 | */ | ||
| 143 | 14 | static 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 | 14 | light_colors_index = 0; | |
| 155 | #endif | ||
| 156 | |||
| 157 | // Get the current color enum and brightness | ||
| 158 | 14 | LightColor currentColorEnum = light_colors[light_colors_index]; | |
| 159 | 14 | brightness_t currentBrightness = getBrightnessValue(); | |
| 160 | |||
| 161 | // Convert to RGBColor and set the light value | ||
| 162 | 14 | RGBColor color = getRGBColorWithBrightness(currentColorEnum, currentBrightness); | |
| 163 | |||
| 164 | #if CONFIG_BLINKING | ||
| 165 | 4 | blinkState = TRUE; | |
| 166 | #endif | ||
| 167 | 14 | RteSetLightValue(color); | |
| 168 | 14 | } | |
| 169 | |||
| 170 | #if CONFIG_BLINKING | ||
| 171 | /** | ||
| 172 | * | ||
| 173 | * ```{impl} Calculate blink period | ||
| 174 | * :id: SWIMPL_LC-004 | ||
| 175 | * :implements: SWDD_LC-101 | ||
| 176 | * ``` | ||
| 177 | */ | ||
| 178 | 148 | SPLE_TESTABLE_STATIC unsigned int calculateBlinkPeriod(percentage_t mainKnobValue) | |
| 179 | { | ||
| 180 | // Calculate blink period based on main knob value | ||
| 181 | 148 | unsigned int blinkPeriod = 100 - (mainKnobValue); // Adjust this formula as needed | |
| 182 | |||
| 183 | // Ensure there's a minimum blink period | ||
| 184 | 148 | blinkPeriod = (blinkPeriod > 10) ? blinkPeriod : 10; // Adjust the minimum period as needed | |
| 185 | |||
| 186 | 148 | return blinkPeriod; | |
| 187 | } | ||
| 188 | #endif | ||
| 189 | |||
| 190 | /** | ||
| 191 | * ```{impl} Light Controller's main function | ||
| 192 | * :id: SWIMPL_LC-006 | ||
| 193 | * :implements: SWDD_LC-100 | ||
| 194 | * ``` | ||
| 195 | * | ||
| 196 | * @brief Controls the light state. | ||
| 197 | * | ||
| 198 | * Uses a state machine to determine the light state based on several inputs, | ||
| 199 | * e.g., the system's power state. | ||
| 200 | */ | ||
| 201 | 173 | void lightController(void) | |
| 202 | { | ||
| 203 | |||
| 204 | 173 | PowerState powerState = RteGetPowerState(); | |
| 205 | #if CONFIG_BLINKING | ||
| 206 | 142 | percentage_t mainKnobValue = RteGetMainKnobValue(); | |
| 207 | 142 | unsigned int blinkPeriod = calculateBlinkPeriod(mainKnobValue); | |
| 208 | #endif | ||
| 209 | |||
| 210 |
4/4✓ Branch 3 → 4 taken 21 times.
✓ Branch 3 → 8 taken 10 times.
✓ Branch 5 → 6 taken 32 times.
✓ Branch 5 → 10 taken 110 times.
|
173 | switch (currentLightState) |
| 211 | { | ||
| 212 | 53 | case LIGHT_OFF: | |
| 213 | #if CONFIG_BLINKING | ||
| 214 | 32 | blinkCounter = 0; | |
| 215 | #endif | ||
| 216 |
4/4✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 7 taken 20 times.
✓ Branch 6 → 7 taken 3 times.
✓ Branch 6 → 9 taken 29 times.
|
53 | if (powerState != POWER_STATE_OFF) |
| 217 | { | ||
| 218 | 4 | turnLightOn(); | |
| 219 | 4 | currentLightState = LIGHT_ON; | |
| 220 | } | ||
| 221 | 53 | break; | |
| 222 | |||
| 223 | 120 | default: // LIGHT_ON | |
| 224 |
4/4✓ Branch 8 → 9 taken 1 time.
✓ Branch 8 → 11 taken 9 times.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 13 taken 109 times.
|
120 | if (powerState == POWER_STATE_OFF) |
| 225 | { | ||
| 226 | 2 | turnLightOff(); | |
| 227 | 2 | currentLightState = LIGHT_OFF; | |
| 228 | } | ||
| 229 | #if CONFIG_BLINKING | ||
| 230 | else | ||
| 231 | { | ||
| 232 | 109 | blinkCounter++; | |
| 233 |
2/2✓ Branch 13 → 14 taken 2 times.
✓ Branch 13 → 18 taken 107 times.
|
109 | if (blinkCounter >= blinkPeriod) |
| 234 | { | ||
| 235 | // Toggle the LED state | ||
| 236 |
2/2✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 16 taken 1 time.
|
2 | if (blinkState == TRUE) |
| 237 | { | ||
| 238 | 1 | turnLightOff(); | |
| 239 | } | ||
| 240 | else | ||
| 241 | { | ||
| 242 | 1 | turnLightOn(); | |
| 243 | } | ||
| 244 | 2 | blinkCounter = 0; | |
| 245 | } | ||
| 246 | } | ||
| 247 | #endif | ||
| 248 | #ifdef CONFIG_BRIGHTNESS_ADJUSTMENT_ENABLED | ||
| 249 | else | ||
| 250 | { | ||
| 251 | 9 | turnLightOn(); | |
| 252 | } | ||
| 253 | #endif | ||
| 254 | 120 | break; | |
| 255 | } | ||
| 256 | 173 | } | |
| 257 |