LED driver.
The LED driver is provided for easy access to common LED functionality. All functionality can be replicated using the GPIO.h and PWM.h APIs.
LED driver simplifies using an LED (may be GPIO or PWM controlled) available on board and supports following operations -
1. To Turn ON/OFF 2. Blink with requested delay, stop when requested 3. Vary brightness (can only be done to a PWM controlled LED) 4. Toggle
There are also APIs to open and close an LED handle and also one to get current state of a LED. User can request to set a LED into particular state while opening itself i.e. to start blink as part of LED_open() call.
LED_init() must be called before using LED_open().
To use the LED driver, an application has to indicate how many LEDs it wants to operate, of what type (PWM or GPIO controlled), and which GPIO or PWM to index for each LED.
Each structure must be defined by the application. The following example is for an MSP432P401R platform in which four LEDs are available on board. The following declarations are placed in "ti_drivers_config.c". How the gpio indices are defined is detailed in the next section.
The following code snippet shows how a GPIO pin controlling an LED is configured. The index the user provides to LED_open() corresponds to an entry in the GPIO_PinConfig array which will source the LED. It is the user's responsibility to ensure that the pin is configured properly in the pin array. Typically this means configuring the pin as an output.
The following definitions are in "ti_drivers_config.h" and "ti_drivers_config.c" respectively. This example uses GPIO pins 1.0 and 2.0 which control LED1 and RED LED on LaunchPad respectively. In addition to the structures shown below, the other GPIO configuration data must exist. See GPIO.h.
"ti_drivers_config.h"
"ti_drivers_config.c"
The LED driver allows for an LED to be driven by the PWM driver. This allows the user to set a brightness level in addition to the other LED features. The user must specify in the LED_HWAttrs of each LED_Config entry which PWM_Config the LED instance is allowed to use. LED instances cannot share a PWM instance.
The user may specify the period of the PWM signal in the LED_Params passed to LED_open(). This is not to be confused with LED_Params.blinkPeriod which specifies the default blink period.
We will borrow the 3rd LED_config entry from the ti_drivers_LED_Examples_config_array
In "ti_drivers_config.h"
In application code:
#include <stdint.h>
#include <stdbool.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/PWM.h>
#include <ti/drivers/dpl/ClockP.h>
Go to the source code of this file.
Data Structures | |
struct | LED_Config |
LED configuration. More... | |
struct | LED_HWAttrs |
Hardware specific settings for a LED module. More... | |
struct | LED_Object |
LED Object structure. More... | |
struct | LED_Params |
LED Parameters. More... | |
Macros | |
#define | LED_BRIGHTNESS_MAX 100U /* Max brightness in % is 100%*/ |
#define | LED_BRIGHTNESS_MIN 0U /* Max brightness in % is 0%*/ |
#define | LED_ON 1U |
#define | LED_OFF 0U |
#define | LED_BLINK_FOREVER 0xFFFF |
Typedefs | |
typedef LED_Config * | LED_Handle |
A handle that is returned from a LED_open() call. More... | |
Enumerations | |
enum | LED_Type { LED_NONE = 0, LED_GPIO_CONTROLLED, LED_PWM_CONTROLLED } |
LED types based on control source. More... | |
enum | LED_State { LED_STATE_OFF = 0, LED_STATE_ON, LED_STATE_BLINKING } |
LED State. More... | |
Functions | |
void | LED_close (LED_Handle ledHandle) |
Function to close a LED specified by the LED handle. More... | |
LED_State | LED_getState (LED_Handle ledHandle) |
Function to get LED state. More... | |
void | LED_init (void) |
Function to initialize LED driver. More... | |
LED_Handle | LED_open (uint_least8_t index, LED_Params *params) |
Function to open an instance of LED. More... | |
void | LED_Params_init (LED_Params *params) |
Function to initialize a LED_Params struct to its defaults. More... | |
bool | LED_setBrightnessLevel (LED_Handle ledHandle, uint8_t level) |
Function to set brightness level of a LED. More... | |
bool | LED_setOff (LED_Handle ledHandle) |
Function to turn off an LED. More... | |
bool | LED_setOn (LED_Handle ledHandle, uint8_t brightness) |
Function to turn on an LED. More... | |
void | LED_startBlinking (LED_Handle ledHandle, uint16_t blinkPeriod, uint16_t blinkCount) |
Function to start an LED blinking. More... | |
void | LED_stopBlinking (LED_Handle ledHandle) |
Function to stop an LED blinking. More... | |
void | LED_toggle (LED_Handle ledHandle) |
Function to toggle an LED. More... | |
void | LED_write (LED_Handle ledHandle, bool value) |
Specify binary state of an LED. More... | |
Variables | |
const uint_least8_t | LED_count |
#define LED_BRIGHTNESS_MAX 100U /* Max brightness in % is 100%*/ |
#define LED_BRIGHTNESS_MIN 0U /* Max brightness in % is 0%*/ |
#define LED_ON 1U |
#define LED_OFF 0U |
#define LED_BLINK_FOREVER 0xFFFF |
typedef LED_Config* LED_Handle |
A handle that is returned from a LED_open() call.
enum LED_Type |
LED types based on control source.
A LED can be controlled by GPIO or PWM. Only a PWM controlled LED can be operated to show brightness variation. An unopened instance will be of type LED_NONE.
Enumerator | |
---|---|
LED_NONE | |
LED_GPIO_CONTROLLED | |
LED_PWM_CONTROLLED |
enum LED_State |
void LED_close | ( | LED_Handle | ledHandle | ) |
Function to close a LED specified by the LED handle.
This call will destruct associated clock, turn off LED, and close the PWM instance if applicable.
ledHandle | A LED_Handle returned from LED_open() |
LED_State LED_getState | ( | LED_Handle | ledHandle | ) |
Function to get LED state.
This function may be useful in scenarios if a LED state(ON/OFF/BLINKING) is tied with some system warning/alerts
ledHandle | A LED_Handle returned from LED_open() |
void LED_init | ( | void | ) |
Function to initialize LED driver.
This function will initialize the LED driver.
LED_Handle LED_open | ( | uint_least8_t | index, |
LED_Params * | params | ||
) |
Function to open an instance of LED.
Function to open an LED instance in the LED_config array. The GPIO or PWM configuartions must already exist before this function is called. The LED_Params input can be used to specify the run time options of the LED instance.
index | Index into the LED_config array specifying the LED_Config that is to be used to open the LED. |
*params | A pointer to LED_Params structure. If NULL, it will use default values. |
void LED_Params_init | ( | LED_Params * | params | ) |
Function to initialize a LED_Params struct to its defaults.
params | A pointer to LED_Params structure for initialization. |
The default parameters are:
bool LED_setBrightnessLevel | ( | LED_Handle | ledHandle, |
uint8_t | level | ||
) |
Function to set brightness level of a LED.
Ignores brightness settings if LED is not PWM controlled. Fails if requested brightness is above 100%.
ledHandle | A LED_Handle |
level | Brightness level in terms of percentage (0-100) |
bool LED_setOff | ( | LED_Handle | ledHandle | ) |
Function to turn off an LED.
ledHandle | An LED_Handle |
bool LED_setOn | ( | LED_Handle | ledHandle, |
uint8_t | brightness | ||
) |
Function to turn on an LED.
ledHandle | An LED_Handle |
brightness | Brightness level in terms of percentage 0-100%. Is ignored for non PWM LEDs. |
void LED_startBlinking | ( | LED_Handle | ledHandle, |
uint16_t | blinkPeriod, | ||
uint16_t | blinkCount | ||
) |
Function to start an LED blinking.
ledHandle | An LED_Handle |
blinkPeriod | Value in ms which determines how often the LED blinks. A value of 1000 will cause the LED to blink once a second. The maximum value is ~65 seconds or 0xFFFF ms. |
blinkCount | If not set to LED_BLINK_FOREVER, the LED will blink the specified number of times and then will turn off. A value of zero will stop the LED blinking. Maximum number of blinks is 0x7FFF or 32,767 blinks. An input exceeding this value will be truncated to 0x7FFF. |
void LED_stopBlinking | ( | LED_Handle | ledHandle | ) |
Function to stop an LED blinking.
ledHandle | An LED_Handle |
void LED_toggle | ( | LED_Handle | ledHandle | ) |
Function to toggle an LED.
ledHandle | An LED_Handle |
void LED_write | ( | LED_Handle | ledHandle, |
bool | value | ||
) |
Specify binary state of an LED.
ledHandle | An LED_Handle |
value | TRUE for on, FALSE for off |
const uint_least8_t LED_count |