



#include "M5Chain.h"
#define RXD_PIN GPIO_NUM_5 // 47 for the other side of Chain DualKey
#define TXD_PIN GPIO_NUM_6 // 48 for the other side of Chain DualKey
Chain M5Chain;
chain_status_t chain_status;
device_list_t *device_list = NULL;
uint16_t device_count = 0;
uint8_t opr_status = 0;
uint8_t rgb_test[] = { 255, 255, 255 };
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("======================");
Serial.println("M5Stack Chain Bus Test");
M5Chain.begin(&Serial2, 115200, RXD_PIN, TXD_PIN);
if (M5Chain.isDeviceConnected()) {
Serial.println("Device is connected");
} else {
Serial.println("Device is NOT connected");
return;
}
chain_status = M5Chain.getDeviceNum(&device_count);
if (chain_status == CHAIN_OK) {
device_list = (device_list_t *)malloc(sizeof(device_list_t));
device_list->count = device_count;
device_list->devices = (device_info_t *)malloc(sizeof(device_info_t) * device_count);
Serial.println("Get device count succeeded");
} else {
Serial.printf("Get device count failed, chain status: %d\r\n", chain_status);
return;
}
if (M5Chain.getDeviceList(device_list)) {
Serial.println("Get device list succeeded\r\n");
} else {
Serial.println("Get device list failed");
return;
}
if (device_list == NULL) {
Serial.println("Device list is NULL");
return;
}
}
void loop() {
Serial.print("Device count: ");
Serial.println(device_list->count);
for (int i = 0; i < device_list->count; i++) {
Serial.print("- Device ID: ");
Serial.print(device_list->devices[i].id);
Serial.print(", type: ");
switch (device_list->devices[i].device_type) {
case CHAIN_UNKNOWN_TYPE_CODE:
Serial.println("Unknown");
break;
case CHAIN_ENCODER_TYPE_CODE:
Serial.println("Chain Encoder");
break;
case CHAIN_ANGLE_TYPE_CODE:
Serial.println("Chain Angle");
break;
case CHAIN_KEY_TYPE_CODE:
Serial.println("Chain Key");
break;
case CHAIN_JOYSTICK_TYPE_CODE:
Serial.println("Chain Joystick");
break;
case CHAIN_TOF_TYPE_CODE:
Serial.println("Chain ToF");
break;
case CHAIN_UART_TYPE_CODE:
Serial.println("Chain UART");
break;
case CHAIN_SWITCH_TYPE_CODE:
Serial.println("Chain Switch");
break;
case CHAIN_PEDAL_TYPE_CODE:
Serial.println("Chain Pedal");
break;
case CHAIN_PIR_TYPE_CODE:
Serial.println("Chain PIR");
break;
case CHAIN_MIC_TYPE_CODE:
Serial.println("Chain Mic");
break;
case CHAIN_BUZZER_TYPE_CODE:
Serial.println("Chain Buzzer");
break;
}
chain_status = M5Chain.setRGBLight(device_list->devices[i].id, 100, &opr_status);
// Device ID, LED brightness (0-100), operation status pointer
if (chain_status == CHAIN_OK && opr_status) {
Serial.println("Set RGB light succeeded");
} else {
Serial.printf("Set RGB light failed, chain status: %d, operation status: %d\r\n", chain_status, opr_status);
}
rgb_test[0] = random(0, 256); // [0, 255]
rgb_test[1] = random(0, 256); // [0, 255]
rgb_test[2] = random(0, 256); // [0, 255]
chain_status = M5Chain.setRGBValue(device_list->devices[i].id, 0, 1, rgb_test, 3, &opr_status);
// Device ID, LED start index, LED count, RGB color, size of RGB color, operation status pointer
if (chain_status == CHAIN_OK && opr_status) {
Serial.println("Set RGB color succeeded");
} else {
Serial.printf("Set RGB color failed, chain status: %d, operation status: %d\r\n", chain_status, opr_status);
}
}
Serial.println();
delay(2000);
}Use the Chain Bridge connector to connect the main controller Chain DualKey and various Chain series input devices. Pay attention to the direction when connecting — the triangle arrow should point outward from the main controller Chain DualKey, as shown below:
Compile and upload the program to the device. The program will detect every 2 seconds the devices connected to the Chain Bus on the G5 and G6 pins of the Chain DualKey, print the results to the serial monitor, and randomly change the color of the RGB LED. You can hot-plug multiple Chain series devices during program execution.