Skip to content

Commit

Permalink
Merge pull request #20783 from mguetschow/led-functions
Browse files Browse the repository at this point in the history
drivers/led: add LED_NUMOF and convenience inline functions
  • Loading branch information
benpicco authored Jul 11, 2024
2 parents 3e712a5 + 0ca0ab5 commit 2d50bd4
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 50 deletions.
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_cpu.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
57 changes: 13 additions & 44 deletions tests/leds/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
#include <stdint.h>

#include "clk.h"
#include "board.h"
#include "periph_conf.h"
#include "led.h"
#include "periph/gpio.h"

#define DELAY_SHORT (coreclk() / 50)
Expand All @@ -38,41 +37,11 @@ void dumb_delay(uint32_t delay)

int main(void)
{
int numof = 0;

/* get the number of available LED's and turn them all off*/
#ifdef LED0_ON
++numof;
LED0_OFF;
#endif
#ifdef LED1_ON
++numof;
LED1_OFF;
#endif
#ifdef LED2_ON
++numof;
LED2_OFF;
#endif
#ifdef LED3_ON
++numof;
LED3_OFF;
#endif
#ifdef LED4_ON
++numof;
LED4_OFF;
#endif
#ifdef LED5_ON
++numof;
LED5_OFF;
#endif
#ifdef LED6_ON
++numof;
LED6_OFF;
#endif
#ifdef LED7_ON
++numof;
LED7_OFF;
#endif
unsigned numof = LED_NUMOF;
for (unsigned i = 0; i < numof; i++) {
led_off(i);
}

puts("On-board LED test\n");
/* cppcheck-suppress knownConditionTrueFalse
Expand All @@ -86,7 +55,7 @@ int main(void)
}

for (unsigned i = 0; i < 4; ++i) {
#ifdef LED0_ON
#ifdef LED0_IS_PRESENT
LED0_ON;
dumb_delay(DELAY_LONG);
LED0_OFF;
Expand All @@ -100,7 +69,7 @@ int main(void)
LED0_TOGGLE;
dumb_delay(DELAY_LONG);
#endif
#ifdef LED1_ON
#ifdef LED1_IS_PRESENT
LED1_ON;
dumb_delay(DELAY_LONG);
LED1_OFF;
Expand All @@ -114,7 +83,7 @@ int main(void)
LED1_TOGGLE;
dumb_delay(DELAY_LONG);
#endif
#ifdef LED2_ON
#ifdef LED2_IS_PRESENT
LED2_ON;
dumb_delay(DELAY_LONG);
LED2_OFF;
Expand All @@ -128,7 +97,7 @@ int main(void)
LED2_TOGGLE;
dumb_delay(DELAY_LONG);
#endif
#ifdef LED3_ON
#ifdef LED3_IS_PRESENT
LED3_ON;
dumb_delay(DELAY_LONG);
LED3_OFF;
Expand All @@ -142,7 +111,7 @@ int main(void)
LED3_TOGGLE;
dumb_delay(DELAY_LONG);
#endif
#ifdef LED4_ON
#ifdef LED4_IS_PRESENT
LED4_ON;
dumb_delay(DELAY_LONG);
LED4_OFF;
Expand All @@ -156,7 +125,7 @@ int main(void)
LED4_TOGGLE;
dumb_delay(DELAY_LONG);
#endif
#ifdef LED5_ON
#ifdef LED5_IS_PRESENT
LED5_ON;
dumb_delay(DELAY_LONG);
LED5_OFF;
Expand All @@ -170,7 +139,7 @@ int main(void)
LED5_TOGGLE;
dumb_delay(DELAY_LONG);
#endif
#ifdef LED6_ON
#ifdef LED6_IS_PRESENT
LED6_ON;
dumb_delay(DELAY_LONG);
LED6_OFF;
Expand All @@ -184,7 +153,7 @@ int main(void)
LED6_TOGGLE;
dumb_delay(DELAY_LONG);
#endif
#ifdef LED7_ON
#ifdef LED7_IS_PRESENT
LED7_ON;
dumb_delay(DELAY_LONG);
LED7_OFF;
Expand Down

0 comments on commit 2d50bd4

Please sign in to comment.