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

Arduino 上手教程

2. 设备开发 & 案例程序

5. 扩展模块

6. 应用案例

Chain Captain RGB LED 灯效

Chain Captain RGB LED 相关 API 与案例程序。

案例程序

编译要求

  • M5Stack 板管理版本 >= 3.3.10-a
  • 开发板选项 = M5ChainCaptain
  • M5Unified 库版本 >= 0.3.0
  • M5GFX 库版本 >= 0.3.0
  • M5IOE1 库版本 >= 1.0.9

Chain Captain 的 RGB LED 由 M5IOE1 的三路 PWM 输出控制:

RGB LED M5IOE1 引脚 PWM 通道
R IO10 PWM4
G IO8 PWM2
B IO9 PWM1
注意
M5IOE1 的 GPIO 和 PWM 输出默认是开漏模式。RGB LED 没有外部上拉,如果只调用 analogWrite() 而不配置驱动模式,PWM 引脚无法输出高电平,LED 不会点亮。需要先通过 setDriveMode() 将 IO8、IO9、IO10 配置为推挽输出。

按动 KEYB / KEYC 选择上一个 / 下一个颜色,KEYA 确认(关闭或恢复 LED)。

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#include <M5Unified.h>
#include <M5IOE1.h>

M5IOE1 ioExpander;

static const uint8_t COLORS[][3] = {
  {255, 0, 0},
  {0, 255, 0},
  {0, 0, 255},
  {255, 255, 0},
  {0, 255, 255},
  {255, 0, 255},
  {255, 255, 255},
};

static const char* COLOR_NAMES[] = {
  "Red", "Green", "Blue", "Yellow", "Cyan", "Magenta", "White",
};

static const size_t COLOR_COUNT = sizeof(COLORS) / sizeof(COLORS[0]);

static size_t colorIndex = 0;
static bool ledEnabled = true;

static void setRgb(uint8_t r, uint8_t g, uint8_t b)
{
  // R=IO10/PWM4, G=IO8/PWM2, B=IO9/PWM1
  ioExpander.analogWrite(M5IOE1_PWM_CH4, r);
  ioExpander.analogWrite(M5IOE1_PWM_CH2, g);
  ioExpander.analogWrite(M5IOE1_PWM_CH1, b);
}

static void drawStatus()
{
  M5.Display.startWrite();
  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.setTextDatum(middle_center);
  M5.Display.setFont(&fonts::FreeSansBold12pt7b);
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
  M5.Display.drawString("RGB LED", 120, 36);
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
  M5.Display.setFont(&fonts::FreeSansBold18pt7b);
  M5.Display.drawString(ledEnabled ? COLOR_NAMES[colorIndex] : "Off", 120, 104);

  if (ledEnabled) {
    const auto& color = COLORS[colorIndex];
    M5.Display.fillRect(44, 142, 152, 44,
                        M5.Display.color565(color[0], color[1], color[2]));
  } else {
    M5.Display.drawRect(44, 142, 152, 44, TFT_WHITE);
  }

  M5.Display.setFont(&fonts::FreeSans9pt7b);
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
  M5.Display.drawString("A: OK   B: prev   C: next", 120, 214);
  M5.Display.endWrite();
}

void setup()
{
  delay(500);
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setRotation(0);
  M5.Display.setBrightness(128);

  if (ioExpander.begin(&M5.In_I2C, 0x4F, 100000,
                       M5IOE1_INT_MODE_DISABLED) != M5IOE1_OK) {
    Serial.println("M5IOE1 init failed");
    M5.Display.setTextDatum(middle_center);
    M5.Display.drawString("M5IOE1 init failed", 120, 120);
    return;
  }

  // PWM outputs are open-drain by default; use push-pull for the RGB LED.
  ioExpander.setDriveMode(M5IOE1_PIN_10, M5IOE1_DRIVE_PUSHPULL); // R: IO10
  ioExpander.setDriveMode(M5IOE1_PIN_8, M5IOE1_DRIVE_PUSHPULL);  // G: IO8
  ioExpander.setDriveMode(M5IOE1_PIN_9, M5IOE1_DRIVE_PUSHPULL);  // B: IO9

  ioExpander.setPwmFrequency(5000);
  setRgb(COLORS[colorIndex][0], COLORS[colorIndex][1], COLORS[colorIndex][2]);
  drawStatus();
}

void loop()
{
  M5.update();

  if (M5.BtnB.wasClicked()) {
    colorIndex = (colorIndex + COLOR_COUNT - 1) % COLOR_COUNT;
    ledEnabled = true;
    setRgb(COLORS[colorIndex][0], COLORS[colorIndex][1], COLORS[colorIndex][2]);
    drawStatus();
  }

  if (M5.BtnC.wasClicked()) {
    colorIndex = (colorIndex + 1) % COLOR_COUNT;
    ledEnabled = true;
    setRgb(COLORS[colorIndex][0], COLORS[colorIndex][1], COLORS[colorIndex][2]);
    drawStatus();
  }

  if (M5.BtnA.wasClicked()) {
    ledEnabled = !ledEnabled;
    if (ledEnabled) {
      setRgb(COLORS[colorIndex][0], COLORS[colorIndex][1], COLORS[colorIndex][2]);
    } else {
      setRgb(0, 0, 0);
    }
    drawStatus();
  }

  delay(20);
}

上传后,RGB LED 默认显示红色。按动 KEYB / KEYC 可切换到上一个 / 下一个颜色,按动 KEYA 可关闭或恢复 LED;屏幕会同步显示当前颜色。

API

Chain Captain RGB LED 使用 M5IOE1 的 PWM 输出控制,更多相关 API 及信息可以参考下方文档:

Page Tools
PDF
On This Page