From 94e88996b44eb4dd5a3a58bdba34cf5451e11847 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Wed, 25 Dec 2024 09:24:52 +0800 Subject: [PATCH] Update T-Deck keyboard example --- .../Keyboard_T_Deck_Master.ino | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/examples/Keyboard_T_Deck_Master/Keyboard_T_Deck_Master.ino b/examples/Keyboard_T_Deck_Master/Keyboard_T_Deck_Master.ino index 55717f9..967e101 100644 --- a/examples/Keyboard_T_Deck_Master/Keyboard_T_Deck_Master.ino +++ b/examples/Keyboard_T_Deck_Master/Keyboard_T_Deck_Master.ino @@ -9,13 +9,40 @@ #include #include -#define LILYGO_KB_SLAVE_ADDRESS 0x55 - +#define LILYGO_KB_SLAVE_ADDRESS 0x55 +#define LILYGO_KB_BRIGHTNESS_CMD 0x01 +#define LILYGO_KB_ALT_B_BRIGHTNESS_CMD 0x02 #define BOARD_POWERON 10 #define BOARD_I2C_SDA 18 #define BOARD_I2C_SCL 8 +/* +* Dynamically modify backlight brightness at runtime +* Brightness Range: 0 ~ 255 +* */ +void setKeyboardBrightness(uint8_t value) +{ + Wire.beginTransmission(LILYGO_KB_SLAVE_ADDRESS); + Wire.write(LILYGO_KB_BRIGHTNESS_CMD); + Wire.write(value); + Wire.endTransmission(); +} + +/* +* Set the default backlight brightness level. If the user sets the backlight to 0 +* via setKeyboardBrightness, the default brightness is used when pressing ALT+B, +* rather than the backlight brightness level set by the user. This ensures that +* pressing ALT+B can respond to the backlight being turned on and off normally. +* Brightness Range: 30 ~ 255 +* */ +void setKeyboardDefaultBrightness(uint8_t value) +{ + Wire.beginTransmission(LILYGO_KB_SLAVE_ADDRESS); + Wire.write(LILYGO_KB_ALT_B_BRIGHTNESS_CMD); + Wire.write(value); + Wire.endTransmission(); +} void setup() { @@ -36,12 +63,16 @@ void setup() Wire.requestFrom(LILYGO_KB_SLAVE_ADDRESS, 1); if (Wire.read() == -1) { while (1) { - Serial.println("LILYGO Keyboad not online ."); + Serial.println("LILYGO Keyboard not online ."); delay(1000); } } + // Set the default backlight brightness level. + setKeyboardDefaultBrightness(127); + } +bool test_bl_done = false; void loop() { // Read key value from esp32c3 @@ -54,6 +85,22 @@ void loop() Serial.println(keyValue); } } + + // Test brightness + static uint32_t interval = 0; + static uint8_t brightness = 0; + if (millis() > interval && !test_bl_done) { + // Dynamically modify backlight brightness at runtime + setKeyboardBrightness(brightness); + brightness++; + brightness %= 255; + if (brightness == 0) { + Serial.println("Test brightness done..., Press ALT+B test default brightness level"); + setKeyboardBrightness(brightness); + test_bl_done = true; + } + interval = millis() + 50; + } delay(5); }