
Arduino 上手教程
Chain Captain RGB LED 相关 API 与案例程序。
3.3.10-aM5ChainCaptain0.3.00.3.01.0.9Chain Captain 的 RGB LED 由 M5IOE1 的三路 PWM 输出控制:
| RGB LED | M5IOE1 引脚 | PWM 通道 |
|---|---|---|
| R | IO10 | PWM4 |
| G | IO8 | PWM2 |
| B | IO9 | PWM1 |
analogWrite() 而不配置驱动模式,PWM 引脚无法输出高电平,LED 不会点亮。需要先通过 setDriveMode() 将 IO8、IO9、IO10 配置为推挽输出。按动 KEYB / KEYC 选择上一个 / 下一个颜色,KEYA 确认(关闭或恢复 LED)。
#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;屏幕会同步显示当前颜色。
Chain Captain RGB LED 使用 M5IOE1 的 PWM 输出控制,更多相关 API 及信息可以参考下方文档: