Chain Captain RGB LED related APIs and sample programs.
3.3.10-aM5ChainCaptain0.3.00.3.01.0.9The Chain Captain RGB LED is controlled by three PWM outputs from M5IOE1:
| RGB LED | M5IOE1 Pin | PWM Channel |
|---|---|---|
| R | IO10 | PWM4 |
| G | IO8 | PWM2 |
| B | IO9 | PWM1 |
analogWrite() is called without configuring the drive mode, the PWM pins cannot output a high level and the LED will not light. Configure IO8, IO9, and IO10 as push-pull outputs with setDriveMode() first.Press KEYB / KEYC to select the previous / next color, and KEYA to confirm (turn the LED off or restore it).
#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);
}After uploading, the RGB LED shows red by default. Press KEYB / KEYC to switch to the previous / next color, and press KEYA to turn the LED off or restore it. The screen shows the current color in sync.
The Chain Captain RGB LED is controlled by the PWM outputs of M5IOE1. For more related APIs and information, refer to the following documents: