Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/led: add LED_NUMOF and convenience inline functions #20783

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boards/adafruit-grand-central-m4-express/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define BOARD_H

#include "cpu.h"
#include "periph/gpio.h"
Copy link
Contributor

@benpicco benpicco Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add this to led.h instead to avoid changing all board definitions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we could, but if we want to follow the include-what-you-use paradigm (see #20570), it doesn't make sense to add it to led.h. That file does not use the content of periph/gpio.h, while the board.h files indeed do (e.g., GPIO_PIN).

Copy link
Contributor Author

@mguetschow mguetschow Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, running make -C examples/hello-world BOARD=adafruit-grand-central-m4-express all with the following diff also fails on master due to the missing include in board.h, completely independent of led.h:

diff --git a/examples/hello-world/main.c b/examples/hello-world/main.c
index 213128ac64..a34fcc00bf 100644
--- a/examples/hello-world/main.c
+++ b/examples/hello-world/main.c
@@ -21,6 +21,8 @@
 
 #include <stdio.h>
 
+#include "board.h"
+
 int main(void)
 {
     puts("Hello World!");
@@ -28,5 +30,7 @@ int main(void)
     printf("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
     printf("This board features a(n) %s CPU.\n", RIOT_CPU);
 
+    printf("board.h defined PIN: %d\n", WS281X_PARAM_PIN);
+
     return 0;
 }

This now only gets apparent because the macros are used in the inline functions.

But anyway, I feel like the board.h include structure could benefit from a bigger overhaul in any case, so I'm not too strictly opposed to put the include in led.h instead for now. What do you think?


#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/adafruit-itsybitsy-m4/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define BOARD_H

#include "cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/arduino-mkr1000/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "periph_conf.h"
#include "board_common.h"
#include "arduino_pinmap.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/arduino-mkrfox1200/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "periph_conf.h"
#include "board_common.h"
#include "arduino_pinmap.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/arduino-mkrwan1300/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "periph_conf.h"
#include "board_common.h"
#include "arduino_pinmap.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/arduino-mkrzero/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "periph_conf.h"
#include "board_common.h"
#include "arduino_pinmap.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/arduino-nano-33-iot/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define BOARD_H

#include "cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/bastwan/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define BOARD_H

#include "cpu.h"
#include "periph/gpio.h"
#include "periph/pm.h"

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions boards/common/arduino-due/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "cpu.h"
#include "arduino_pinmap.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/common/arduino-zero/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "periph_conf.h"
#include "periph_cpu.h"
#include "arduino_pinmap.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/common/saml1x/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define BOARD_H

#include "cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/feather-m0/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "cpu.h"
#include "periph_conf.h"
#include "periph_cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/gd32vf103c-start/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define BOARD_H

#include "board_common.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/hamilton/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "cpu.h"
#include "periph_conf.h"
#include "periph_cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/samd10-xmini/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "cpu.h"
#include "periph_conf.h"
#include "periph_cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/samd20-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "cpu.h"
#include "periph_conf.h"
#include "periph_cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/samd21-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "cpu.h"
#include "periph_conf.h"
#include "periph_cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/same54-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define BOARD_H

#include "cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/saml21-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define BOARD_H

#include "cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/samr21-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "cpu.h"
#include "periph_conf.h"
#include "periph_cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/samr30-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define BOARD_H

#include "cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/samr34-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define BOARD_H

#include "cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -44,7 +45,7 @@
* @name Board specific configuration
* @{
*/
#define TCXO_PWR_PIN GPIO_PIN(PA, 9) /**< 32 MHz oscillator for radio enable */

Check warning on line 48 in boards/samr34-xpro/include/board.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
#define TX_OUTPUT_SEL_PIN GPIO_PIN(PA, 13) /**< BAND_SEL */
/** @}*/

Expand Down
1 change: 1 addition & 0 deletions boards/seeedstudio-gd32/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define BOARD_H

#include "board_common.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/seeeduino_xiao/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "cpu.h"
#include "periph_conf.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/sensebox_samd21/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "cpu.h"
#include "periph_conf.h"
#include "periph_cpu.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/serpente/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "cpu.h"
#include "periph_conf.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/sipeed-longan-nano/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define BOARD_H

#include "board_common.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/sodaq-autonomo/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "cpu.h"
#include "board_common.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/sodaq-explorer/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "cpu.h"
#include "board_common.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/sodaq-one/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "cpu.h"
#include "board_common.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/sodaq-sara-aff/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "cpu.h"
#include "board_common.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions boards/sodaq-sara-sff/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "cpu.h"
#include "board_common.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
Expand Down
99 changes: 93 additions & 6 deletions drivers/include/led.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @defgroup drivers_led Control on-board LEDs
* @ingroup drivers_actuators
* @brief Access macros to control the on-board LEDs
* @brief Access macros and functions to control the on-board LEDs
*
* This header contains a set of macros for controlling the on-board LEDs of
* a board. The LEDs are enumerated, starting from LED0 to LED7. As most
Expand All @@ -24,7 +24,7 @@
* @{
*
* @file
* @brief Macros for controlling the on-board LEDs
* @brief Macros and inline functions for controlling the on-board LEDs
*
* @author Hauke Petersen <[email protected]>
*/
Expand Down Expand Up @@ -106,15 +106,102 @@ extern "C" {
#define LED7_IS_PRESENT /**< indicate that LED7 is present */
#endif

/**
* Number of LEDs available on the current board.
*/
#if defined(LED7_IS_PRESENT)
#define LED_NUMOF 8
#elif defined(LED6_IS_PRESENT)
#define LED_NUMOF 7
#elif defined(LED5_IS_PRESENT)
#define LED_NUMOF 6
#elif defined(LED4_IS_PRESENT)
#define LED_NUMOF 5
#elif defined(LED3_IS_PRESENT)
#define LED_NUMOF 4
#elif defined(LED2_IS_PRESENT)
#define LED_NUMOF 3
#elif defined(LED1_IS_PRESENT)
#define LED_NUMOF 2
#elif defined(LED0_IS_PRESENT)
#define LED_NUMOF 1
#else
#define LED_NUMOF 0
#endif

/** @} */

/**
* @name Convenience LED control macros
* @name Convenience LED control functions and macros
* @{
*/
#define LED_ON(x) LED ## x ##_ON /**< Turn on led x */
#define LED_OFF(x) LED ## x ## _OFF /**< Turn off led x */
#define LED_TOGGLE(x) LED ## x ##_TOGGLE /**< Toggle led x */
#define LED_ON(id) LED ## id ##_ON /**< Turn on an LED */
#define LED_OFF(id) LED ## id ## _OFF /**< Turn off an LED */
#define LED_TOGGLE(id) LED ## id ##_TOGGLE /**< Toggle an LED */

/**
* Turn on an LED.
*
* @note If id is a compile-time constant, consider using @ref LED_ON(id) instead.
*
* @param id id of LED between 0 and 7
*/
static inline void led_on(unsigned id)
{
switch (id) {
case 0: LED0_ON; break;
case 1: LED1_ON; break;
case 2: LED2_ON; break;
case 3: LED3_ON; break;
case 4: LED4_ON; break;
case 5: LED5_ON; break;
case 6: LED6_ON; break;
case 7: LED7_ON; break;
}
}

/**
* Turn off an LED.
*
* @note If id is a compile-time constant, consider using @ref LED_OFF(id) instead.
*
* @param id id of LED between 0 and 7
*/
static inline void led_off(unsigned id)
{
switch (id) {
case 0: LED0_OFF; break;
case 1: LED1_OFF; break;
case 2: LED2_OFF; break;
case 3: LED3_OFF; break;
case 4: LED4_OFF; break;
case 5: LED5_OFF; break;
case 6: LED6_OFF; break;
case 7: LED7_OFF; break;
}
}

/**
* Toggle an LED.
*
* @note If id is a compile-time constant, consider using @ref LED_TOGGLE(id) instead.
*
* @param id id of LED between 0 and 7
*/
static inline void led_toggle(unsigned id)
{
switch (id) {
case 0: LED0_TOGGLE; break;
case 1: LED1_TOGGLE; break;
case 2: LED2_TOGGLE; break;
case 3: LED3_TOGGLE; break;
case 4: LED4_TOGGLE; break;
case 5: LED5_TOGGLE; break;
case 6: LED6_TOGGLE; break;
case 7: LED7_TOGGLE; break;
}
}

/** @} */

#ifdef __cplusplus
Expand Down
Loading
Loading