GCC Code Coverage Report


Directory: D:/ateliere/spledy/components/light_controller/src/
File: light_controller.c
Date: 2025-10-25 23:12:31
Coverage Exec Excl Total
Lines: 74.5% 35 0 47
Functions: 100.0% 5 0 5
Branches: 63.6% 7 0 11

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 10 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 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 10 static RGBColor getRGBColorWithBrightness(LightColor colorEnum, brightness_t brightness)
98 {
99 10 RGBColor color = OFF_COLOR;
100
1/5
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ Branch 2 → 5 not taken.
✓ Branch 2 → 6 taken 10 times.
✗ Branch 2 → 7 not taken.
10 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 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 10 return color;
121 }
122
123 /**
124 * ```{impl} Turn light off
125 * :id: SWIMPL_LC-002
126 * :implements: SWDD_LC-102
127 * ```
128 */
129 1 static void turnLightOff(void)
130 {
131 #if CONFIG_BLINKING
132 blinkState = FALSE;
133 #endif
134 1 RteSetLightValue(OFF_COLOR);
135 1 }
136
137 /**
138 * ```{impl} Turn light on
139 * :id: SWIMPL_LC-003
140 * :implements: SWDD_LC-102
141 * ```
142 */
143 10 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 10 light_colors_index = 0;
155 #endif
156
157 // Get the current color enum and brightness
158 10 LightColor currentColorEnum = light_colors[light_colors_index];
159 10 brightness_t currentBrightness = getBrightnessValue();
160
161 // Convert to RGBColor and set the light value
162 10 RGBColor color = getRGBColorWithBrightness(currentColorEnum, currentBrightness);
163
164 #if CONFIG_BLINKING
165 blinkState = TRUE;
166 #endif
167 10 RteSetLightValue(color);
168 10 }
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 SPLE_TESTABLE_STATIC unsigned int calculateBlinkPeriod(percentage_t mainKnobValue)
179 {
180 // Calculate blink period based on main knob value
181 unsigned int blinkPeriod = 100 - (mainKnobValue); // Adjust this formula as needed
182
183 // Ensure there's a minimum blink period
184 blinkPeriod = (blinkPeriod > 10) ? blinkPeriod : 10; // Adjust the minimum period as needed
185
186 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 31 void lightController(void)
202 {
203
204 31 PowerState powerState = RteGetPowerState();
205 #if CONFIG_BLINKING
206 percentage_t mainKnobValue = RteGetMainKnobValue();
207 unsigned int blinkPeriod = calculateBlinkPeriod(mainKnobValue);
208 #endif
209
210
2/2
✓ Branch 3 → 4 taken 21 times.
✓ Branch 3 → 8 taken 10 times.
31 switch (currentLightState)
211 {
212 21 case LIGHT_OFF:
213 #if CONFIG_BLINKING
214 blinkCounter = 0;
215 #endif
216
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 7 taken 20 times.
21 if (powerState != POWER_STATE_OFF)
217 {
218 1 turnLightOn();
219 1 currentLightState = LIGHT_ON;
220 }
221 21 break;
222
223 10 default: // LIGHT_ON
224
2/2
✓ Branch 8 → 9 taken 1 time.
✓ Branch 8 → 11 taken 9 times.
10 if (powerState == POWER_STATE_OFF)
225 {
226 1 turnLightOff();
227 1 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 9 turnLightOn();
252 }
253 #endif
254 10 break;
255 }
256 31 }
257