Simple RGB PWM LED Driver

Description

Simple Red/Green/Blue PWM LED Driver.


Introduction

The Simple RGB PWM LED Driver is a module for the LED driver that provides functionality for controlling Red/Green/Blue LEDs that are driven by PWM.


RGB PWM LED Configuration

RGB PWM LEDs use the sl_led_t struct, and their own structs sl_simple_rgb_pwm_led_context_t and sl_led_rgb_pwm_t. These are automatically generated into the following files, as well as instance specific headers with macro definitions in them. The samples below are for a single instance called "inst0".

// sl_simple_rgb_pwm_led_instances.c
#include "em_gpio.h"
#include "sl_simple_rgb_pwm_led.h"
#include "sl_simple_rgb_pwm_led_inst0_config.h"
sl_led_pwm_t red_inst0 = {
.port = SIMPLE_RGB_PWM_LED_INST0_PORT,
.pin = SIMPLE_RGB_PWM_LED_INST0_PIN,
.polarity = SIMPLE_RGB_PWM_LED_INST0_POLARITY,
.channel = SIMPLE_RGB_PWM_LED_INST0_CHANNEL,
#if defined(SL_SIMPLE_RGB_PWM_LED_INST0_RED_LOC)
.location = SIMPLE_RGB_PWM_LED_INST0_LOC,
#endif
};
sl_led_pwm_t green_inst0 = {
.port = SIMPLE_RGB_PWM_LED_INST0_PORT,
.pin = SIMPLE_RGB_PWM_LED_INST0_PIN,
.polarity = SIMPLE_RGB_PWM_LED_INST0_POLARITY,
.channel = SIMPLE_RGB_PWM_LED_INST0_CHANNEL,
#if defined(SL_SIMPLE_RGB_PWM_LED_INST0_RED_LOC)
.location = SIMPLE_RGB_PWM_LED_INST0_LOC,
#endif
};
sl_led_pwm_t blue_inst0 = {
.port = SIMPLE_RGB_PWM_LED_INST0_PORT,
.pin = SIMPLE_RGB_PWM_LED_INST0_PIN,
.polarity = SIMPLE_RGB_PWM_LED_INST0_POLARITY,
.channel = SIMPLE_RGB_PWM_LED_INST0_CHANNEL,
#if defined(SL_SIMPLE_RGB_PWM_LED_INST0_RED_LOC)
.location = SIMPLE_RGB_PWM_LED_INST0_LOC,
#endif
};
sl_simple_rgb_pwm_led_context_t simple_rgb_pwm_inst0_context = {
.red = &red_inst0,
.green = &green_inst0,
.blue = &blue_inst0,
.timer = SL_SIMPLE_RGB_PWM_LED_INST0_PERIPHERAL,
.frequency = SL_SIMPLE_RGB_PWM_LED_INST0_FREQUENCY,
.resolution = SL_SIMPLE_RGB_PWM_LED_INST0_RESOLUTION,
};
const sl_led_rgb_pwm_t sl_inst0 = {
.led_common.context = &simple_rgb_pwm_inst0_context,
.led_common.init = sl_simple_rgb_pwm_led_init,
.led_common.turn_on = sl_simple_rgb_pwm_led_turn_on,
.led_common.turn_off = sl_simple_rgb_pwm_led_turn_off,
.led_common.toggle = sl_simple_rgb_pwm_led_toggle,
.led_common.get_state = sl_simple_rgb_pwm_led_get_state,
};
void sl_simple_rgb_pwm_led_init_instances(void)
{
sl_led_init((sl_led_t *)&sl_inst0);
}
Note
The sl_simple_rgb_pwm_led_instances.c file is shown with only one instance, but if more were in use they would all appear in this .c file.
// sl_simple_rgb_pwm_led_instances.h
#ifndef SL_SIMPLE_RGB_PWM_LED_INSTANCES_H
#define SL_SIMPLE_RGB_PWM_LED_INSTANCES_H
#include "sl_simple_rgb_pwm_led.h"
extern const sl_led_rgb_pwm_t sl_inst0;
void sl_simple_rgb_pwm_led_init_instances(void);
#endif // SL_SIMPLE_RGB_PWM_LED_INIT_H
Note
The sl_simple_rgb_pwm_led_instances.h file is shown with only one instance, but if more were in use they would all appear in this .h file.


RGB PWM LED Usage

The RGB PWM Led driver provides functionality for controlling Red/Green/Blue/White LEDs that are driven by PWM. The LEDs can be turned on and off and toggled, and remember their color and brightness state when being turned back on. The color and brightness can be set using values of 0-65535 for red, green, blue, and white. Retrieving the state gives the on/off value, while retrieving the color gives the rgb values. The following code shows how to control these LEDs. An LED should always be initialized before calling any other functions with it.

// initialize rgb LED
sl_led_init(&rgb_led_inst0);
// turn on LED, set color to purple, turn off, toggle (would maintain purple color)
sl_led_turn_on(&rgb_led_inst0);
uint16_t red = 65535; // max red
uint16_t green = 0; // no green
uint16_t blue = 65535; // max blue
sl_led_set_rgb_color(&rgb_led_inst0, red, green, blue);
sl_led_turn_off(&rgb_led_inst0);
sl_led_toggle(&rgb_led_inst0);
// get the state of the led
sl_led_state_t state = sl_led_get_state(&rgb_led_inst0);

Data Structures

struct  sl_simple_rgb_pwm_led_context_t
 A Simple RGB LED context.
 
struct  sl_led_rgb_pwm_t
 A Simple RGB LED PWM instance.
 

Functions

sl_status_t sl_simple_rgb_pwm_led_init (void *rgb)
 Initialize an RGB PWM LED driver.
 
void sl_simple_rgb_pwm_led_turn_on (void *rgb)
 Turn on an RBG LED.
 
void sl_simple_rgb_pwm_led_turn_off (void *rgb)
 Turn off an RGB LED.
 
void sl_simple_rgb_pwm_led_toggle (void *rgb)
 Toggle an RGB LED.
 
sl_led_state_t sl_simple_rgb_pwm_led_get_state (void *rgb)
 Get status of an RGB LED.
 
void sl_simple_rgb_pwm_led_set_color (void *rgb, uint16_t red, uint16_t green, uint16_t blue)
 Set color mixing and dimming level of an RGB LED.
 
void sl_simple_rgb_pwm_led_get_color (void *rgb, uint16_t *red, uint16_t *green, uint16_t *blue)
 Get current color mixing and dimming level of an RGB LED.
 
void sl_led_set_rgb_color (const sl_led_rgb_pwm_t *rgb, uint16_t red, uint16_t green, uint16_t blue)
 LED set RGB color.
 
void sl_led_get_rgb_color (const sl_led_rgb_pwm_t *rgb, uint16_t *red, uint16_t *green, uint16_t *blue)
 LED get RGB setting.
 

Macros

#define SL_SIMPLE_RGB_PWM_LED_POLARITY_ACTIVE_HIGH   0U
 LED Active polarity High.
 
#define SL_SIMPLE_RGB_PWM_LED_POLARITY_ACTIVE_LOW   1U
 LED Active polarity Low.
 
#define SL_SIMPLE_RGB_PWM_LED_COLOR_R   0U
 LED Red.
 
#define SL_SIMPLE_RGB_PWM_LED_COLOR_G   1U
 LED Green.
 
#define SL_SIMPLE_RGB_PWM_LED_COLOR_B   2U
 LED Blue.
 
#define SL_SIMPLE_RGB_PWM_LED_NUM_CC_REQUIRED   3U
 Number of Timer Capture Channels required (1 for each RGB color)
 

Function Documentation

◆ sl_simple_rgb_pwm_led_init()

sl_status_t sl_simple_rgb_pwm_led_init ( void *  rgb)

Initialize an RGB PWM LED driver.

Parameters
[in]led_handlePointer to rgb-pwm-led specific data.
Returns
Status Code:
  • SL_STATUS_OK Success
  • SL_STATUS_FAIL Init error

◆ sl_simple_rgb_pwm_led_turn_on()

void sl_simple_rgb_pwm_led_turn_on ( void *  rgb)

Turn on an RBG LED.

Turns on at previously set color levels If no previous levels set, turns on at max level for all RGB LEDs

Parameters
[in]led_handlePointer to rgb_pwm-led specific data.

◆ sl_simple_rgb_pwm_led_turn_off()

void sl_simple_rgb_pwm_led_turn_off ( void *  rgb)

Turn off an RGB LED.

Parameters
[in]led_handlePointer to rgb-pwm-led specific data.

◆ sl_simple_rgb_pwm_led_toggle()

void sl_simple_rgb_pwm_led_toggle ( void *  rgb)

Toggle an RGB LED.

The toggle "ON" behavior is as defined for sl_simple_rgb_pwm_led_turn_on()

Parameters
[in]led_handlePointer to rgb-pwm-led specific data.

◆ sl_simple_rgb_pwm_led_get_state()

sl_led_state_t sl_simple_rgb_pwm_led_get_state ( void *  rgb)

Get status of an RGB LED.

Parameters
[in]led_handlePointer to rgb-pwm-led specific data.
Returns
sl_led_state_t Current state of RGB LED. 0 for Red, Green, Blue and White LEDs are all OFF 1 for Red, Green, Blue or White LED is ON

◆ sl_simple_rgb_pwm_led_set_color()

void sl_simple_rgb_pwm_led_set_color ( void *  rgb,
uint16_t  red,
uint16_t  green,
uint16_t  blue 
)

Set color mixing and dimming level of an RGB LED.

Parameters
[in]led_handlePointer to rgb-pwm-led specific data:
[in]redRed color level (PWM duty-cycle [0-65535])
[in]greenGreen color level (PWM duty-cycle [0-65535])
[in]blueBlue color level (PWM duty-cycle [0-65535])

◆ sl_simple_rgb_pwm_led_get_color()

void sl_simple_rgb_pwm_led_get_color ( void *  rgb,
uint16_t *  red,
uint16_t *  green,
uint16_t *  blue 
)

Get current color mixing and dimming level of an RGB LED.

Note
Will return the last stored levels regardless of the current ON/OFF state. Call sl_simple_rgb_pwm_led_get_state() to determine if the RGB LED is actually ON or OFF.
Parameters
[in]led_handlePointer to rgb-pwm-led specific data:
[out]redRed color level (PWM duty-cycle [0-65535])
[out]greenGreen color level (PWM duty-cycle [0-65535])
[out]blueBlue color level (PWM duty-cycle [0-65535])

◆ sl_led_set_rgb_color()

void sl_led_set_rgb_color ( const sl_led_rgb_pwm_t rgb,
uint16_t  red,
uint16_t  green,
uint16_t  blue 
)

LED set RGB color.

Parameters
rgbLED Instance handle
redLED red intensity
greenLED green intensity
blueLED blue intensity

◆ sl_led_get_rgb_color()

void sl_led_get_rgb_color ( const sl_led_rgb_pwm_t rgb,
uint16_t *  red,
uint16_t *  green,
uint16_t *  blue 
)

LED get RGB setting.

Parameters
rgbLED Instance handle
redLED red intensity
greenLED green intensity
blueLED blue intensity

Macro Definition Documentation

◆ SL_SIMPLE_RGB_PWM_LED_POLARITY_ACTIVE_HIGH

#define SL_SIMPLE_RGB_PWM_LED_POLARITY_ACTIVE_HIGH   0U

LED Active polarity High.

◆ SL_SIMPLE_RGB_PWM_LED_POLARITY_ACTIVE_LOW

#define SL_SIMPLE_RGB_PWM_LED_POLARITY_ACTIVE_LOW   1U

LED Active polarity Low.

◆ SL_SIMPLE_RGB_PWM_LED_COLOR_R

#define SL_SIMPLE_RGB_PWM_LED_COLOR_R   0U

LED Red.

◆ SL_SIMPLE_RGB_PWM_LED_COLOR_G

#define SL_SIMPLE_RGB_PWM_LED_COLOR_G   1U

LED Green.

◆ SL_SIMPLE_RGB_PWM_LED_COLOR_B

#define SL_SIMPLE_RGB_PWM_LED_COLOR_B   2U

LED Blue.

◆ SL_SIMPLE_RGB_PWM_LED_NUM_CC_REQUIRED

#define SL_SIMPLE_RGB_PWM_LED_NUM_CC_REQUIRED   3U

Number of Timer Capture Channels required (1 for each RGB color)