@@ -187,9 +187,10 @@ static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
187
187
* This optional routine sets the brightness of a LED to the given value.
188
188
* Calling this function after led_blink() won't affect blinking.
189
189
*
190
- * LEDs which can only be turned on or off may provide this function.
190
+ * LEDs which can only be turned on or off do not need to provide this
191
+ * function.
191
192
* These should simply turn the LED on if @p value is nonzero, and off
192
- * if @p value is zero.
193
+ * if @p value is zero using the on/off APIs automatically .
193
194
*
194
195
* @param dev LED device
195
196
* @param led LED number
@@ -206,14 +207,23 @@ static inline int z_impl_led_set_brightness(const struct device *dev,
206
207
const struct led_driver_api * api =
207
208
(const struct led_driver_api * )dev -> api ;
208
209
209
- if (api -> set_brightness == NULL ) {
210
+ if (api -> set_brightness == NULL &&
211
+ api -> on == NULL && api -> off == NULL ) {
210
212
return - ENOSYS ;
211
213
}
212
214
213
215
if (value > LED_BRIGTHNESS_MAX ) {
214
216
return - EINVAL ;
215
217
}
216
218
219
+ if (api -> set_brightness == NULL ) {
220
+ if (value ) {
221
+ return api -> on (dev , led );
222
+ } else {
223
+ return api -> off (dev , led );
224
+ }
225
+ }
226
+
217
227
return api -> set_brightness (dev , led , value );
218
228
}
219
229
@@ -307,6 +317,9 @@ static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
307
317
*
308
318
* This routine turns on an LED
309
319
*
320
+ * LEDs which implements brightness control do not need to implement this, the
321
+ * set_brightness API is used automatically.
322
+ *
310
323
* @param dev LED device
311
324
* @param led LED number
312
325
* @return 0 on success, negative on error
@@ -318,6 +331,14 @@ static inline int z_impl_led_on(const struct device *dev, uint32_t led)
318
331
const struct led_driver_api * api =
319
332
(const struct led_driver_api * )dev -> api ;
320
333
334
+ if (api -> set_brightness == NULL && api -> on == NULL ) {
335
+ return - ENOSYS ;
336
+ }
337
+
338
+ if (api -> on == NULL ) {
339
+ return api -> set_brightness (dev , led , LED_BRIGTHNESS_MAX );
340
+ }
341
+
321
342
return api -> on (dev , led );
322
343
}
323
344
@@ -326,6 +347,9 @@ static inline int z_impl_led_on(const struct device *dev, uint32_t led)
326
347
*
327
348
* This routine turns off an LED
328
349
*
350
+ * LEDs which implements brightness control do not need to implement this, the
351
+ * set_brightness API is used automatically.
352
+ *
329
353
* @param dev LED device
330
354
* @param led LED number
331
355
* @return 0 on success, negative on error
@@ -337,6 +361,14 @@ static inline int z_impl_led_off(const struct device *dev, uint32_t led)
337
361
const struct led_driver_api * api =
338
362
(const struct led_driver_api * )dev -> api ;
339
363
364
+ if (api -> set_brightness == NULL && api -> off == NULL ) {
365
+ return - ENOSYS ;
366
+ }
367
+
368
+ if (api -> off == NULL ) {
369
+ return api -> set_brightness (dev , led , 0 );
370
+ }
371
+
340
372
return api -> off (dev , led );
341
373
}
342
374
0 commit comments