简体中文
English
简体中文
日本語

Arduino 上手教程

2. 设备开发 & 案例程序

5. 扩展模块

6. 应用案例

Core2 RGB LED

Core2 搭配 Base M5GO Bottom2 v1.3 时,可通过 Core2 的 GPIO25 控制底座上的 10 颗 SK6812 RGB LED。

案例程序

编译要求

  • M5Stack 板管理版本 >= 3.2.0
  • 开发板选项 = M5Core2
  • M5Unified 库版本 >= 0.2.11
  • M5GFX 库版本 >= 0.2.8
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 60 61
#include <M5Unified.h>
#include "utility/led/LED_Strip_Class.hpp"

constexpr int LED_DATA_PIN = 25;
constexpr size_t LED_COUNT = 10;

void setup()
{
    auto cfg = M5.config();
    M5.begin(cfg);

    auto busled = std::make_shared<m5::LedBus_RMT>();
    auto buscfg = busled->getConfig();
    buscfg.pin_data = LED_DATA_PIN;
    busled->setConfig(buscfg);

    auto led_strip = std::make_shared<m5::LED_Strip_Class>();
    auto ledcfg = led_strip->getConfig();
    ledcfg.led_count = LED_COUNT;
    ledcfg.byte_per_led = 3;
    ledcfg.color_order = m5::LED_Strip_Class::config_t::color_order_grb;
    led_strip->setBus(busled);
    led_strip->setConfig(ledcfg);

    M5.Led.setLedInstance(led_strip);
    M5.Led.setAutoDisplay(false);
    M5.Led.begin();
    M5.Led.setBrightness(80);

    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextFont(&fonts::FreeMonoBold12pt7b);
    M5.Display.setTextSize(1);
    M5.Display.drawString("RGB LED Test", M5.Display.width() / 2,
                         M5.Display.height() / 2);

}

void loop()
{
    M5.update();

    M5.Led.setAllColor(255, 0, 0);
    M5.Led.display();
    delay(1000);

    M5.Led.setAllColor(0, 255, 0);
    M5.Led.display();
    delay(1000);

    M5.Led.setAllColor(0, 0, 255);
    M5.Led.display();
    delay(1000);

    M5.Led.setAllColor(255, 255, 255);
    M5.Led.display();
    delay(1000);

    M5.Led.setAllColor(0, 0, 0);
    M5.Led.display();
    delay(1000);
}

程序启动后,Base M5GO Bottom2 v1.3 上的 10 颗 RGB LED 会同时依次显示红、绿、蓝、白色,最后熄灭,每种状态持续 1 秒并循环运行。

API

Core2 RGB LED 部分使用了 M5Unified 库中的 LED_Class,更多相关 API 可以参考下方文档:

Page Tools
PDF
On This Page