Permalink
Browse files

led: Add support for initializing onboard LEDs based on board definition

1. Add support for initializing onboard LEDs based on board definition.
Maximum LED count has been set to 12.

2. Introduce mraa_led_init_raw API for initializing LEDs based on
function name. This API can be used by platforms which doesn't have mapping
in board definition.

Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
  • Loading branch information...
Mani-Sadhasivam committed Aug 30, 2018
1 parent 6c0ec10 commit 6fcd882d5867955c861e8a6b2a0a628d181bef47
Showing with 83 additions and 7 deletions.
  1. +11 −3 api/mraa/led.h
  2. +16 −2 api/mraa/led.hpp
  3. +1 −0 include/arm/96boards.h
  4. +13 −1 include/mraa_internal_types.h
  5. +42 −1 src/led/led.c
View
@@ -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
View
@@ -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
View
@@ -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();
@@ -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;
View
@@ -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];

0 comments on commit 6fcd882

Please sign in to comment.