diff --git a/api/mraa/led.h b/api/mraa/led.h index eec88e1..5233918 100644 --- a/api/mraa/led.h +++ b/api/mraa/led.h @@ -46,16 +46,24 @@ extern "C" { */ typedef struct _led* mraa_led_context; +/** + * Initialise led_context, based on led index. + * + * @param led ID of the LED + * @returns LED context or NULL + */ +mraa_led_context mraa_led_init(int led); + /** * Initialise led_context, based on led function name. * The structure of LED entry in sysfs is "devicename:colour:function" * This api expects only one unique LED identifier which would be - * "function" name most often. For instance, `mraa_led_init("user4");` + * "function" name most often. For instance, `mraa_led_init_raw("user4");` * - * @param led Name of the LED + * @param led_dev Name of the LED device * @returns LED context or NULL */ -mraa_led_context mraa_led_init(const char* led); +mraa_led_context mraa_led_init_raw(const char* led_dev); /** * Set LED brightness diff --git a/api/mraa/led.hpp b/api/mraa/led.hpp index 542e032..dcbc37a 100644 --- a/api/mraa/led.hpp +++ b/api/mraa/led.hpp @@ -44,9 +44,9 @@ class Led /** * Instantiates an LED object * - * @param led LED fuction name to use + * @param led LED index to use */ - Led(const char* led) + Led(int led) { m_led = mraa_led_init(led); @@ -55,6 +55,20 @@ class Led } } + /** + * Instantiates an LED object + * + * @param led_dev LED function name to use + */ + Led(std::string led_dev) + { + m_led = mraa_led_init_raw(led_dev.c_str()); + + if (m_led == NULL) { + throw std::invalid_argument("Invalid LED name specified"); + } + } + /** * LED Constructor, takes a pointer to a LED context and initialises * the LED class diff --git a/include/arm/96boards.h b/include/arm/96boards.h index 04d052b..dc95d0d 100644 --- a/include/arm/96boards.h +++ b/include/arm/96boards.h @@ -38,6 +38,7 @@ extern "C" { #define MRAA_96BOARDS_LS_SPI_COUNT 1 #define MRAA_96BOARDS_LS_UART_COUNT 2 #define MRAA_96BOARDS_LS_PIN_COUNT 40 +#define MRAA_96BOARDS_LED_COUNT 6 mraa_board_t* mraa_96boards(); diff --git a/include/mraa_internal_types.h b/include/mraa_internal_types.h index a64c7fa..beee8ad 100644 --- a/include/mraa_internal_types.h +++ b/include/mraa_internal_types.h @@ -46,7 +46,7 @@ #define MAX_AIO_COUNT 7 #define MAX_UART_COUNT 6 #define MAX_PWM_COUNT 6 - +#define MAX_LED_COUNT 12 // general status failures for internal functions #define MRAA_PLATFORM_NO_INIT -3 @@ -454,6 +454,16 @@ typedef struct { /*@}*/ } mraa_aio_dev_t; +/** + * Structure representing an LED device. + */ +typedef struct { + /*@{*/ + char *name; /**< LED device function name */ + unsigned int index; /**< Index as exposed in the platform */ + /*@}*/ +} mraa_led_dev_t; + /** * A Structure representing a platform/board. */ @@ -490,6 +500,8 @@ typedef struct _board_t { mraa_adv_func_t* adv_func; /**< Pointer to advanced function disptach table */ struct _board_t* sub_platform; /**< Pointer to sub platform */ mraa_boolean_t chardev_capable; /**< Decide what interface is being used: old sysfs or new char device*/ + mraa_led_dev_t led_dev[MAX_LED_COUNT]; /**< Array of LED devices */ + unsigned int led_dev_count; /**< Total onboard LED device count */ /*@}*/ } mraa_board_t; diff --git a/src/led/led.c b/src/led/led.c index b04cd9f..627d5f2 100644 --- a/src/led/led.c +++ b/src/led/led.c @@ -127,7 +127,48 @@ mraa_led_init_internal(const char* led) } mraa_led_context -mraa_led_init(const char* led) +mraa_led_init(int index) +{ + mraa_led_context dev = NULL; + char directory[MAX_SIZE]; + struct stat dir; + + if (plat == NULL) { + syslog(LOG_ERR, "led: init: platform not initialised"); + return NULL; + } + + if (plat->led_dev_count == 0) { + syslog(LOG_ERR, "led: init: no led device defined in platform"); + return NULL; + } + + if (index < 0) { + syslog(LOG_ERR, "led: init: led index cannot be negative"); + return NULL; + } + + if (index >= plat->led_dev_count) { + syslog(LOG_ERR, "led: init: requested led above led device count"); + return NULL; + } + + dev = mraa_led_init_internal((char*) plat->led_dev[index].name); + if (dev == NULL) { + return NULL; + } + + snprintf(directory, MAX_SIZE, "%s/%s", SYSFS_CLASS_LED, dev->led_name); + if (stat(directory, &dir) == 0 && S_ISDIR(dir.st_mode)) { + syslog(LOG_NOTICE, "led: init: current user doesn't have access rights for using LED %s", dev->led_name); + } + strncpy(dev->led_path, (const char*) directory, sizeof(directory)); + + return dev; +} + +mraa_led_context +mraa_led_init_raw(const char* led) { mraa_led_context dev = NULL; char directory[MAX_SIZE];