Skip to content

RGB LED

Ashish Jaiswal edited this page Jul 21, 2026 · 1 revision

RGB-LED.h

The RGB LED API drives an addressable WS2812B LED strip from a selectable data pin. It supports direct per-LED colour control, a set of built-in non-blocking animations, and an ownership switch that hands the strip between your code and the firmware's built-in flight-status indicator.

Only one strip is active at a time, chosen at init. Up to RGB_MAX_LEDS (64) LEDs are supported.

Enumerations

peripheral_rgb_pin_e

Selects which vetted {pin, timer, DMA} slot drives the strip. Pass one to RGB_Init().

typedef enum {
  RGB_1 = 0,   // PA8   TIM1_CH1   DMA1_Ch2   PPM RC-IN / 5th motor pin
  RGB_2,       // PB6   TIM4_CH1   DMA1_Ch1   shares DMA with ADC1
  RGB_3,       // PB14  TIM15_CH1  DMA1_Ch5   ! disables flash/blackbox
  RGB_4,       // PA13  TIM4_CH3   DMA1_Ch5   ! SWDIO debug pin
  RGB_5,       // PA14  TIM8_CH2   DMA2_Ch5   ! SWCLK debug pin
  RGB_6,       // PB4   TIM3_CH1   DMA1_Ch6   free pin (recommended)
  RGB_7,       // PB5   TIM17_CH1  DMA1_Ch1   free pin, shares ADC1 DMA
  RGB_8,       // PA15  TIM8_CH1   DMA2_Ch3   default flight-status pin
} peripheral_rgb_pin_e;

Pin conflicts: slots marked ! collide with SWD debug (RGB_4/RGB_5) or flash/blackbox (RGB_3). RGB_6 is the recommended free pin; RGB_8 (PA15) is the default flight-status pin. See docs/WS2812_RGB.md for the full matrix.

rgb_control_e

Selects who owns the strip.

typedef enum {
  RGB_USER = 0,   // user code drives the strip via RGB_* calls
  RGB_SYSTEM      // firmware draws the flight-status indicator
} rgb_control_e;

RGB_Animation_e

The built-in non-blocking animation to play. Pass one to RGB_StartAnimation().

RGB_ANIM_NONE, RGB_ANIM_CHASE, RGB_ANIM_METEOR, RGB_ANIM_DUAL_CHASE,
RGB_ANIM_BOUNCE, RGB_ANIM_COLOR_WIPE, RGB_ANIM_FILL_DRAIN, RGB_ANIM_DROP,
RGB_ANIM_HOURGLASS, RGB_ANIM_SWEEP_STACK, RGB_ANIM_SWEEP_UNSTACK,
RGB_ANIM_CONVERGE, RGB_ANIM_EXPAND, RGB_ANIM_BREATHE, RGB_ANIM_BLINK,
RGB_ANIM_ALTERNATE_BLINK, RGB_ANIM_SPARKLE, RGB_ANIM_FIRE, RGB_ANIM_RAINBOW,
RGB_ANIM_THEATER_CHASE, RGB_ANIM_WAVE, RGB_ANIM_DUAL_SCAN

RGB_Direction_e

Controls which way patterns travel.

typedef enum {
  RGB_DIR_FORWARD = 0,   // LED 0 -> LED N
  RGB_DIR_REVERSE        // LED N -> LED 0
} RGB_Direction_e;

Setup & Ownership

void RGB_Init(peripheral_rgb_pin_e pin, uint8_t led_count)

Initialises the strip on the selected pin slot with led_count LEDs (1 to RGB_MAX_LEDS). Call once in plutoInit(). The strip starts in RGB_SYSTEM mode (firmware paints the flight-status indicator).

void RGB_Control(rgb_control_e mode)

Switches ownership. RGB_USER lets your RGB_* calls drive the strip; RGB_SYSTEM hands it back to the built-in flight-status indicator, which the firmware updates automatically every control loop.

void RGB_Release(void)

Releases the strip and frees its pin/timer/DMA slot.

uint8_t RGB_GetLedCount(void)

Returns the number of LEDs configured at init.

Colour Control

All colour edits write to a back buffer and are not visible until RGB_Show().

void RGB_SetColor(uint8_t index, uint8_t r, uint8_t g, uint8_t b)

Sets a single LED's colour.

void RGB_SetColorAll(uint8_t r, uint8_t g, uint8_t b)

Sets every LED to the same colour.

void RGB_SetColorHSV(uint8_t index, uint16_t h, uint8_t s, uint8_t v)

Sets a single LED from HSV (hue 0-359, saturation/value 0-255).

void RGB_FillColor(uint8_t start, uint8_t end, uint8_t r, uint8_t g, uint8_t b)

Fills a range of LEDs [start, end] with one colour.

void RGB_SetBrightness(uint8_t percent)

Scales overall brightness (0-100%).

void RGB_Clear(void)

Clears the buffer (all LEDs off). Still requires RGB_Show() to take effect.

void RGB_Show(void)

Pushes the buffer to the physical strip via DMA.

Animations

Animations are non-blocking: start one, then advance it every loop.

void RGB_StartAnimation(RGB_Animation_e animation, uint16_t speed_ms, RGB_Direction_e direction, uint8_t r, uint8_t g, uint8_t b)

Starts an animation at speed_ms per frame in the given direction and base colour. Colour is ignored by palette-driven effects (e.g. RGB_ANIM_RAINBOW).

void RGB_UpdateAnimation(void)

Advances the active animation if its frame is due. Call every plutoLoop() iteration. Calls RGB_Show() internally - do not call RGB_Show() yourself while an animation is running.

void RGB_StopAnimation(void)

Stops the animation and turns all LEDs off immediately.

bool RGB_IsAnimating(void)

Returns true while an animation is active.

Usage Example

#include "PlutoPilot.h"

void plutoInit ( void ) {
    RGB_Init ( RGB_6, 8 );          // 8-LED strip on PB4 (recommended free pin)
}

void onLoopStart ( void ) {
    RGB_Control ( RGB_USER );       // take the strip in Developer Mode
    RGB_StartAnimation ( RGB_ANIM_RAINBOW, 100, RGB_DIR_FORWARD, 0, 0, 0 );
}

void plutoLoop ( void ) {
    RGB_UpdateAnimation();          // advance the animation each loop
}

void onLoopFinish ( void ) {
    RGB_StopAnimation();
    RGB_Control ( RGB_SYSTEM );     // hand the strip back to flight status
}

Static colours instead of an animation:

RGB_Control ( RGB_USER );
RGB_SetColor ( 0, 255, 0, 0 );      // LED 0 red
RGB_SetColor ( 1, 0, 255, 0 );      // LED 1 green
RGB_SetBrightness ( 50 );           // 50% brightness
RGB_Show();                          // push to the strip

Clone this wiki locally