Private
Public Access
2
0

led: Add on board LED support

This patch adds support for using on board LED through sysfs.
Commonly available LED parameters are supported.

Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Manivannan Sadhasivam
2017-08-25 18:01:19 +05:30
committed by Brendan Le Foll
parent e7b51f3b66
commit e1382ad845
8 changed files with 653 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ extern "C" {
#include "mraa/i2c.h"
#include "mraa/uart.h"
#include "mraa/uart_ow.h"
#include "mraa/led.h"
#ifdef __cplusplus
}

View File

@@ -31,3 +31,4 @@
#include "mraa/i2c.hpp"
#include "mraa/spi.hpp"
#include "mraa/uart.hpp"
#include "mraa/led.hpp"

111
api/mraa/led.h Normal file
View File

@@ -0,0 +1,111 @@
/*
* Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
* Copyright (c) 2017 Linaro Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
/**
* @file
* @brief LED module
*
* LED is the Light Emitting Diode interface to libmraa. It is used to
* access the on board LED's via sysfs.
*
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "common.h"
#include <stdio.h>
/**
* Opaque pointer definition to the internal struct _led
*/
typedef struct _led* mraa_led_context;
/**
* 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");`
*
* @param led Name of the LED
* @returns LED context or NULL
*/
mraa_led_context mraa_led_init(const char* led);
/**
* Set LED brightness
*
* @param dev LED context
* @param value Integer value to write
* @returns Result of operation
*/
mraa_result_t mraa_led_set_brightness(mraa_led_context dev, int value);
/**
* Read LED brightness
*
* @param dev LED context
* @returns Brightness value
*/
int mraa_led_read_brightness(mraa_led_context dev);
/**
* Read LED maximum brightness
*
* @param dev LED context
* @returns Maximum brightness value
*/
int mraa_led_read_max_brightness(mraa_led_context dev);
/**
* Set LED trigger
*
* @param dev LED context
* @param trigger Type of trigger to set
* @returns Result of operation
*/
mraa_result_t mraa_led_set_trigger(mraa_led_context dev, const char* trigger);
/**
* Clear active LED trigger
*
* @param dev LED context
* @returns Result of operation
*/
mraa_result_t mraa_led_clear_trigger(mraa_led_context dev);
/**
* Close LED file descriptors and free the context memory
*
* @param dev LED context
* @returns Result of operation
*/
mraa_result_t mraa_led_close(mraa_led_context dev);
#ifdef __cplusplus
}
#endif

139
api/mraa/led.hpp Normal file
View File

@@ -0,0 +1,139 @@
/*
* Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
* Copyright (c) 2017 Linaro Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "led.h"
#include "types.hpp"
#include <stdexcept>
namespace mraa
{
/**
* @brief API to Light Emitting Diode
*
* This file defines the LED interface for libmraa
*
*/
class Led
{
public:
/**
* Instantiates an LED object
*
* @param led LED fuction name to use
*/
Led(const char* led)
{
m_led = mraa_led_init(led);
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
*
* @param led_context void * to LED context
*/
Led(void* led_context)
{
m_led = (mraa_led_context) led_context;
if (m_led == NULL) {
throw std::invalid_argument("Invalid LED name specified");
}
}
/**
* LED object destructor
*/
~Led()
{
mraa_led_close(m_led);
}
/**
* Set LED brightness value
*
* @param value Value to set LED brightness
* @return Result of operation
*/
Result
setBrightness(int value)
{
return (Result) mraa_led_set_brightness(m_led, value);
}
/**
* Read LED brightness value
*
* @return LED brightness value
*/
int
readBrightness()
{
return mraa_led_read_brightness(m_led);
}
/**
* Read LED maximum brightness value
*
* @return LED max brightness value
*/
int
readMaxBrightness()
{
return mraa_led_read_max_brightness(m_led);
}
/**
* Set LED trigger
*
* @param trigger Type of trigger to set
* @return Result of operation
*/
Result
trigger(const char* trigger)
{
return (Result) mraa_led_set_trigger(m_led, trigger);
}
/**
* Clear active LED trigger
*
* @return Result of operation
*/
Result
clearTrigger()
{
return (Result) mraa_led_clear_trigger(m_led);
}
private:
mraa_led_context m_led;
};
}