pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

M5Unified LED Class

setLedInstance

Syntax:

void setLedInstance(std::shared_ptr<LED_Base> instance);

Description:

  • Set the LED instance

Parameters:

  • instance:
    • LED instance pointer

Return:

  • null

getLedInstancePtr

Syntax:

LED_Base* getLedInstancePtr(void);

Description:

  • Get the pointer to the LED instance

Parameters:

  • null

Return:

  • LED_Base*:
    • LED instance pointer

begin

Syntax:

bool begin(void);

Description:

  • Initialize the LED

Parameters:

  • null

Return:

  • bool:
    • true: Initialization succeeded
    • false: Initialization failed

display

Syntax:

void display(void);

Description:

  • Update LED display

Parameters:

  • null

Return:

  • null

setAutoDisplay

Syntax:

void setAutoDisplay(bool enable);

Description:

  • Set auto update LED display status

Parameters:

  • enable:
    • true: Enable auto update
    • false: Disable auto update

Return:

  • null

getCount

Syntax:

size_t getCount(void);

Description:

  • Get the number of LEDs

Parameters:

  • null

Return:

  • size_t:
    • Number of LEDs

getBuffer

Syntax:

RGBColor* getBuffer(void);

Description:

  • Get LED color buffer

Parameters:

  • null

Return:

  • RGBColor*:
    • Pointer to LED color buffer

setBrightness

Syntax:

void setBrightness(uint8_t brightness);

Description:

  • Set LED brightness

Parameters:

  • brightness:
    • Brightness (0-255)

Return:

  • null

setColor

Syntax 1:

void setColor(size_t index, const RGBColor& color);

Syntax 2:

void setColor(size_t index, uint8_t red, uint8_t green, uint8_t blue);

Syntax 3:

void setColor(size_t index, T c);

Description:

  • Set a specific LED color

Parameters:

  • index:
    • LED index position (starting from 0)
  • color / red, green, blue / c:
    • Color / RGB colors / generic color

Return:

  • null

setColors

Syntax:

void setColors(const T*s, size_t index = 0, size_t length = INT32_MAX);

Description:

  • Set multiple LED colors in batch

Parameters:

-s:

  • Pointer to the color array
  • index:
    • Starting LED index position (starting from 0)
  • length:
    • Length of the color array

Return:

  • null

setAllColor

Syntax:

void setAllColor(T c);

Description:

  • Set color for all LEDs

Parameters:

  • c:
    • Color code

Return:

  • null

isEnabled

Syntax:

bool isEnabled(void);

Description:

  • Get LED activation status

Parameters:

  • null

Return:

  • bool:
    • true: Enabled
    • false: Disabled

Example

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include "M5Unified.h"
#include "utility/led/LED_Strip_Class.hpp"
#include "utility/led/LED_Base.hpp"

RGBColor colors[10] = {
  { 255, 0, 0 },      // Red 
  { 0, 255, 0 },      // Green
  { 0, 0, 255 },      // Blue 
  { 255, 255, 0 },    // Yellow 
  { 0, 255, 255 },    // Cyan
  { 255, 0, 255 },    // Magenta
  { 128, 128, 128 },  // Gray
  { 255, 128, 0 },    // Orange
  { 0, 128, 255 },    // Sky Blue
  { 128, 0, 255 }     // Purple-Blue
};

void setup() {
  M5.begin();

  // Configure RMT bus for LED strip (data pin: GPIO26)
  auto busled = std::make_shared<m5::LedBus_RMT>();
  auto buscfg = busled->getConfig();
  buscfg.pin_data = 26;
  busled->setConfig(buscfg);

  // Configure LED strip (30 LEDs, 3 bytes/LED, GRB order)
  auto led_strip = std::make_shared<m5::LED_Strip_Class>();
  auto ledcfg = led_strip->getConfig();
  ledcfg.led_count = 30;
  ledcfg.byte_per_led = 3;
  ledcfg.color_order = m5::LED_Strip_Class::config_t::color_order_grb;
  led_strip->setConfig(ledcfg);

  led_strip->setBus(busled);  // Link bus to LED strip
  M5.Led.setLedInstance(led_strip);  // Register to M5 system
  M5.Led.begin();  // Initialize LED strip

  M5.Display.clear();
  M5.Display.setFont(&fonts::FreeMonoBoldOblique12pt7b);
  M5.Display.setCursor(20, 50);
  M5.Display.printf("LED Cnt: %d", M5.Led.getCount());
  M5.Display.setCursor(20, 70);
  M5.Display.printf("LED Status: %s", M5.Led.isEnabled() ? "Enabled" : "Disabled");
  M5.Led.setBrightness(100);
}

void loop() {
  static int offset = 0;  // Scrolling offset (persists between loops)
  RGBColor* buf = M5.Led.getBuffer();  // Get LED color buffer

  // Assign colors to each LED with offset (cyclic scrolling)
  for (int i = 0; i < 30; ++i) {
    buf[i] = colors[(i + offset) % 10];
  }

  M5.Led.setColors(buf, 0, 30);  // Update LED strip with buffer data
  offset = (offset + 1) % 10;    // Increment offset (cycle 0-9)
}

Using M5GO to run the above code, connect a 30-LED RGB strip to PORT.B, the effect is as follows:

On This Page