rcc 1.0
RCC - RGB LED Color Cube Documentation
Loading...
Searching...
No Matches
led.c File Reference

Implementation of LED control functions using SPI interface. More...

#include "led.h"
Include dependency graph for led.c:

Functions

void led_init (void)
 Initialize the LED control interface and hardware.
void led_disable (void)
 Disable the LED hardware interface.
void led_xof (unsigned char value)
 Transmit a specified value repeatedly over SPI to form a data frame.
void led_data (LED_Data data)
 Send an LED data frame with specified color and intensity.
LED_Data led_status_color (LED_Status status, unsigned char intensity)
 Generate a color configuration for an LED based on its status and intensity.
void leds_off (void)
 Turn off all LEDs by sending zero-intensity frames.
void led_color (LED_Position position, LED_Data color)
 Set the colors of specified LEDs based on position flags.
void led_blink (LED_Position position, LED_Data color, LED_Delay delay, unsigned char repeat)
 Blink LEDs on specified positions with given color, delay and repeat count.

Detailed Description

Implementation of LED control functions using SPI interface.

This source file provides functions to initialize and control addressable LEDs, including setting colors, blinking patterns, and turning LEDs on or off. It utilizes the SPI communication peripheral to send LED data frames.

Author
g.raf
Date
2025-09-28
Version
1.0 Release
Note
This file is part of a larger embedded systems project and subject to the license specified in the repository. For updates and the complete revision history, see the GitHub repository.
See also
https://github.com/0x007e/rcc "AVR ATmega GitHub Repository"

Function Documentation

◆ led_blink()

void led_blink ( LED_Position position,
LED_Data color,
LED_Delay delay,
unsigned char repeat )

Blink LEDs on specified positions with given color, delay and repeat count.

Parameters
positionBitwise flags indicating LED positions and blinking modes to be used.
colorThe color and intensity to blink on the LEDs.
delayEnumeration specifying delay duration between blink states.
repeatThe number of times to repeat the blink sequence.

This function controls blinking patterns for LEDs by alternately activating the specified LED positions and their alternating blinking counterparts. It uses the function led_color to set the color, and led_delay to wait between blinking states. The blinking sequence runs for repeat cycles, then turns off all LEDs using leds_off().

Note
Ensure the SPI interface and LED hardware are properly initialized before calling this function. The delay parameter controls brightness on/off period in milliseconds.
See also
led_color() for setting LED colors.
led_delay() for insertion of delay intervals.
leds_off() to turn off LEDs after blinking completes.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ led_color()

void led_color ( LED_Position position,
LED_Data color )

Set the colors of specified LEDs based on position flags.

Parameters
positionBitwise flags indicating which LEDs to color and their blinking modes.
colorThe LED_Data structure specifying color and intensity for the LEDs.

This function initiates an LED data frame sequence by sending a start-of-frame (SOF) signal. It then iterates over all configured LEDs and determines, based on their position and the position flags, whether to set the LED color to the specified color or turn the LED off.

For odd numbers of LEDs, the middle LED is always turned off. The function supports left and right LED positions as well as alternating blinking flags. The LED data or off frames are sent accordingly. Finally, an end-of-frame (EOF) signal is sent to mark the completion of the LED update.

Note
Ensure the SPI interface is initialized and the LED hardware supports the specified frame format.
See also
LED_SOF(), LED_EOF() for frame delimiters.
led_data() and led_off() for sending LED on/off frames.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ led_data()

void led_data ( LED_Data data)

Send an LED data frame with specified color and intensity.

Parameters
dataLED_Data structure containing intensity and RGB color values.

This function constructs and transmits a single LED data frame over SPI, combining the LED enable flag with the masked intensity value, followed by the blue, green, and red color components. The intensity value is masked with 0x3F to limit it to valid bits.

The frame format is:

  • Mode byte: LED_ENABLE_FLAG OR'ed with intensity (0x3F mask).
  • Blue color byte.
  • Green color byte.
  • Red color byte.
Note
Ensure the LED is initialized before calling this function.
Here is the caller graph for this function:

◆ led_disable()

void led_disable ( void )

Disable the LED hardware interface.

This function sends multiple sleep commands to all configured LEDs to put them into a low power state. It transmits four repeated LED sleep frames using SPI and then disables the SPI peripheral itself.

This operation is used to safely turn off the LEDs and reduce power consumption when LED functionality is not needed.

Note
Ensure no ongoing LED data transmission occurs before calling this function to avoid communication issues.
See also
spi_disable() for disabling the SPI communication peripheral.
Here is the call graph for this function:

◆ led_init()

void led_init ( void )

Initialize the LED control interface and hardware.

This function initializes the SPI peripheral to communicate with the LEDs using most significant bit (MSB) first, with clock polarity and phase set to rising edges. After SPI initialization, it sends a start-of-frame (SOF) signal followed by initializing all configured LEDs with the enable flag and zero color data (LEDs initially off). Finally, it sends an end-of-frame (EOF) signal to mark completion of the initialization sequence. This setup prepares the LEDs for subsequent color and blink control operations by configuring the communication and initial states.

Note
This function must be called before any other LED control operations. It assumes SPI_MSB, SPI_Rising macros and LED frame configurations are correctly defined.
See also
spi_init() for SPI communication setup.
LED_SOF() and LED_EOF() macros for frame delimiters.
led_frame() for sending individual LED data frames.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ led_status_color()

LED_Data led_status_color ( LED_Status status,
unsigned char intensity )

Generate a color configuration for an LED based on its status and intensity.

Parameters
statusThe LED status indicating the system state (e.g., ready, warning, error).
intensityThe brightness intensity level for the LED (0-255).
Returns
Returns an LED_Data structure populated with the RGB color values corresponding to the specified status and intensity.

This function initializes an LED_Data structure with the provided intensity and default black color (all zero). It then assigns color values based on the status:

  • LED_Status_Ready assigns full green brightness,
  • LED_Status_Warning assigns full red and green brightness (yellow),
  • LED_Status_Error assigns full red brightness.

This mapping follows common LED indicator color conventions to visually represent different states.

Note
The returned color values are maximum brightness (0x3F) for the color channels, regardless of the input intensity which only affects overall brightness.
Here is the caller graph for this function:

◆ led_xof()

void led_xof ( unsigned char value)

Transmit a specified value repeatedly over SPI to form a data frame.

Parameters
valueThe byte value to be sent repeatedly.

This function sends the given value repeatedly for LED_FRAME_SIZE times via SPI using the spi_transfer function. It is commonly used to send start or stop frames for LED data sequences to synchronize communication with the LED hardware.

Note
The function blocks until all bytes are transmitted. Ensure the SPI interface is initialized before calling this function.
Here is the call graph for this function:

◆ leds_off()

void leds_off ( void )

Turn off all LEDs by sending zero-intensity frames.

This function sends a start-of-frame signal followed by zero-intensity data frames to all configured LEDs, effectively turning them off. It concludes by sending an end-of-frame signal. This ensures all LEDs are turned off cleanly by resetting their color and intensity data.

Note
This function assumes the SPI interface is already initialized and ready for communication. Use led_off() internally to send individual LED off frames.
See also
LED_SOF(), LED_EOF() for frame delimiters.
led_off() for turning off a single LED.
Here is the caller graph for this function: