Read the input statuses and configure the RGB LEDs to user-defined mode, updating the LED colors via commands.
#include <M5Unified.h>#include "unit_byte.hpp" uint32_t color_list[] = {0xff0000, 0x00ff00, 0x0000ff};uint8_t color_index = 0;time_t last_update_time = 0;uint8_t buttonId = 0x47;UnitByte dev; void setup(){ M5.begin(); Serial.begin(115200); M5.Display.setFont(&fonts::lgfxJapanMinchoP_20); while (!dev.begin(&Wire, buttonId, 2, 1, 400000)) { M5.Display.drawString("Unit ByteButton init Fail!", 0, 0); delay(1000); } dev.setLEDShowMode(BYTE_LED_USER_DEFINED); for (uint8_t i = 0; i <= 8; i++) { dev.setLEDBrightness(i, 250); }} void loop(){ // M5.Display.clear(); M5.Display.setCursor(0, 0); for (uint8_t i = 0; i < 8; i++) { uint8_t buttonStatus8Bytes = dev.getSwitchStatus(i); M5.Display.printf(" CH[%d]: %d\r\n", i, buttonStatus8Bytes); } if (millis() - last_update_time > 1000) { color_index = (color_index + 1) % 3; for (int i = 0; i <= 8; i++) { dev.setRGB888(i, color_list[color_index]); } last_update_time = millis(); }}
Configure the RGB LEDs so that each button has a designated color for its different states, achieving synchronized on/off lighting.
const uint32_t colors[] = { 0xFF0000, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF, 0xFFA500, 0x808080, 0x00FF00,};dev.setLEDShowMode(BYTE_LED_MODE_DEFAULT);Serial.println("Set LED show sys define.");delay(1000);dev.setRGB233(8, colors[1]);delay(1000);dev.setFlashWriteBack();delay(1000);for (int i = 0; i < 8; i++) { dev.setSwitchOffRGB888(i, colors[i]); // Output the hexadecimal value of the current color Serial.printf("Set Switch Off RGB to %06X\n", (unsigned int)colors[i]);}delay(1000);for (int i = 0; i < 8; i++) { dev.setSwitchOnRGB888(i, colors[9 - i]); // Output the hexadecimal value of the current color Serial.printf("Set Switch On RGB to %06X\n", (unsigned int)colors[i]);}delay(1000);dev.setFlashWriteBack();
The Unit ByteButton supports modifying its I2C address, which is convenient when connecting multiple devices simultaneously. Refer to the API below; after initializing the I2C bus, change the device address as needed.
dev.begin(&Wire, 2, 1, 400000);dev.setI2CAddress(new_addr);
Real-time display of the button status and control of RGB LED color switching.