#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 switchId = 0x46;
UnitByte dev;
void setup()
{
M5.begin();
Serial.begin(115200);
M5.Display.setFont(&fonts::lgfxJapanMinchoP_20);
while (!dev.begin(&Wire, switchId, 2, 1, 400000)) {
M5.Display.drawString("Unit ByteSwitch 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 switchStatus8Bytes = dev.getSwitchStatus(i);
M5.Display.printf("CH[%d]: %d\r\n", i, switchStatus8Bytes);
}
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 switch corresponds to a specific RGB LED color in different states, achieving synchronized 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();
Unit ByteSwitch supports changing the I2C address, making it easy to connect multiple devices simultaneously. Refer to the API below; after initializing the I2C bus, modify the device address as needed.
dev.begin(&Wire, 2, 1, 400000);
dev.setI2CAddress(new_addr);
Real-time display of the switch status and control of RGB LED color switching.