Skip to content

Commit

Permalink
Merge branch 'fix/keyboard_bug_fix' into 'master'
Browse files Browse the repository at this point in the history
feat(keyboard): upgrade version to v0.2.0

Closes AEG-1823

See merge request ae_group/esp-iot-solution!1063
  • Loading branch information
lijunru-hub committed Aug 13, 2024
2 parents 641cc22 + 8b0b0a4 commit c09eabf
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 57 deletions.
1 change: 1 addition & 0 deletions docs/en/usb/usb_overview/usb_device_solutions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Links:

* `USB HID Keyboard and Mouse Example <https://github.com/espressif/esp-iot-solution/tree/master/examples/usb/device/usb_hid_device>`_
* `USB HID Surface Dial Example <https://github.com/espressif/esp-iot-solution/tree/master/examples/usb/device/usb_surface_dial>`_
* `USB Custom Keyboard Example <https://github.com/espressif/esp-iot-solution/tree/master/examples/keyboard>`_

USB Drag-and-Drop OTA Upgrade
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions docs/zh_CN/usb/usb_overview/usb_device_solutions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ USB HID 设备方案基于 HID(Human Interface Device)协议标准,可作

* `USB HID 键盘和鼠标示例 <https://github.com/espressif/esp-iot-solution/tree/master/examples/usb/device/usb_hid_device>`_
* `USB HID Surface Dial 示例 <https://github.com/espressif/esp-iot-solution/tree/master/examples/usb/device/usb_surface_dial>`_
* `USB 客制化键盘示例 <https://github.com/espressif/esp-iot-solution/tree/master/examples/keyboard>`_

U 盘拖拽升级
^^^^^^^^^^^^^^^
Expand Down
8 changes: 8 additions & 0 deletions examples/keyboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ How to modify key mappings:
#### license

Note: keyboard_rgb_martix comes from the **QMK** project. Due to the use of the GPL license, if you have product plans based on this example, it is recommended to replace this component.

### Change LOG

* v0.2.0 - 2024-8-12

* Added support for lighting effects in BLE mode, with the keyboard backlight turning off by default after 60 seconds.
* Resolved the issue with adjusting the speed of lighting effects.
* Increased the number of default lighting effects to 30.
8 changes: 8 additions & 0 deletions examples/keyboard/README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ Win11 灯效
#### license

Note: keyboard_rgb_martix 来自 **QMK** 工程,由于使用 GPL 协议,如果您有基于本示例的产品计划,建议替换该组件。

#### Change LOG

* v0.2.0 - 2024-8-12

* BLE 模式下支持灯效,默认 60s 关闭键盘灯光
* 解决灯效速度调节问题
* 默认支持灯效增加到 30 种
33 changes: 28 additions & 5 deletions examples/keyboard/components/esp32_s3_kbd_kit/esp32_s3_kbd_kit.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ esp_err_t bsp_keyboard_init(keyboard_btn_handle_t *kbd_handle, keyboard_btn_conf
}

static led_strip_handle_t s_led_strip = NULL;
static bool s_led_enable = false;

esp_err_t bsp_ws2812_init(led_strip_handle_t *led_strip)
{
Expand Down Expand Up @@ -67,12 +68,16 @@ esp_err_t bsp_ws2812_init(led_strip_handle_t *led_strip)
.flags.invert_out = false, // whether to invert the output signal (useful when your hardware has a level inverter)
};

led_strip_rmt_config_t rmt_config = {
.clk_src = RMT_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption
.resolution_hz = 20 * 1000 * 1000, // 10MHz
.flags.with_dma = false, // whether to enable the DMA feature
// LED strip backend configuration: SPI
led_strip_spi_config_t spi_config = {
.clk_src = SPI_CLK_SRC_XTAL, // different clock source can lead to different power consumption
.flags.with_dma = true, // Using DMA can improve performance and help drive more LEDs
.spi_bus = SPI2_HOST, // SPI bus ID
};
led_strip_new_rmt_device(&strip_config, &rmt_config, &s_led_strip);

// LED Strip object handle
ESP_ERROR_CHECK(led_strip_new_spi_device(&strip_config, &spi_config, &s_led_strip));

if (led_strip) {
*led_strip = s_led_strip;
}
Expand All @@ -81,10 +86,28 @@ esp_err_t bsp_ws2812_init(led_strip_handle_t *led_strip)

esp_err_t bsp_ws2812_enable(bool enable)
{
if (!enable) {
gpio_hold_dis(KBD_WS2812_POWER_IO);
}
gpio_set_level(KBD_WS2812_POWER_IO, !enable);
/*!< Make output stable in light sleep */
if (enable) {
gpio_hold_en(KBD_WS2812_POWER_IO);
}
s_led_enable = enable;
return ESP_OK;
}

esp_err_t bsp_ws2812_clear(void)
{
return led_strip_clear(s_led_strip);
}

bool bsp_ws2812_is_enable(void)
{
return s_led_enable;
}

esp_err_t bsp_lamp_array_init(uint32_t bind)
{
if (!s_led_strip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ dependencies:
led_strip:
version: "^2.5.3"

lijunru-hub/keyboard_rgb_matrix: "^0.1.0"
lijunru-hub/keyboard_rgb_matrix: "^0.1.1"
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ esp_err_t bsp_ws2812_init(led_strip_handle_t *led_strip);

esp_err_t bsp_ws2812_enable(bool enable);

esp_err_t bsp_ws2812_clear(void);

bool bsp_ws2812_is_enable(void);

esp_err_t bsp_lamp_array_init(uint32_t bind);

esp_err_t bsp_rgb_matrix_init(void);
Expand Down
34 changes: 22 additions & 12 deletions examples/keyboard/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
menu "ESP KeyBoard Example"

config TUSB_VID
hex "USB Device VID"
default 0x303A
config TUSB_PID
hex "USB Device PID"
default 0x8000
config TUSB_MANUFACTURER
string "USB Device Manufacture"
default "Espressif"
config TUSB_PRODUCT
string "Product Name"
default "HID Demo"
menu "USB Configuration"
config TUSB_VID
hex "USB Device VID"
default 0x303A
config TUSB_PID
hex "USB Device PID"
default 0x8000
config TUSB_MANUFACTURER
string "USB Device Manufacture"
default "Espressif"
config TUSB_PRODUCT
string "Product Name"
default "HID Demo"
endmenu

menu "Wireless Configuration"
config LIGHT_SLEEP_TIMEOUT_MS
int "Light Sleep Timeout"
default 60000
help
"Time to enter light sleep mode and close all light when no key is pressed"
endmenu

endmenu
34 changes: 26 additions & 8 deletions examples/keyboard/main/btn_progress.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "settings.h"
#include "esp_system.h"
#include "esp_pm.h"
#include "bsp/esp-bsp.h"

static btn_report_type_t report_type = TINYUSB_HID_REPORT;
static light_type_t light_type = RGB_MATRIX;
Expand Down Expand Up @@ -114,9 +115,15 @@ void btn_progress(keyboard_btn_report_t kbd_report)

/*!< Change to win11 light or local light */
case KC_NUM_LOCK:
light_type = (light_type == RGB_MATRIX) ? LAMP_ARRAY_MATRIX : RGB_MATRIX;
sys_param->light_type = light_type;
settings_write_parameter_to_nvs();
if (report_type == TINYUSB_HID_REPORT) {
light_type = (light_type == RGB_MATRIX) ? LAMP_ARRAY_MATRIX : RGB_MATRIX;
sys_param->light_type = light_type;
settings_write_parameter_to_nvs();
} else if (report_type == BLE_HID_REPORT) {
light_type = RGB_MATRIX;
sys_param->light_type = light_type;
settings_write_parameter_to_nvs();
}
break;

case KC_KB_MUTE:
Expand Down Expand Up @@ -150,15 +157,26 @@ void btn_progress(keyboard_btn_report_t kbd_report)

case QK_BACKLIGHT_TOGGLE:
rgb_matrix_toggle();
if (!rgb_matrix_is_enabled()) {
bsp_ws2812_clear();
}
bsp_ws2812_enable(rgb_matrix_is_enabled());
break;

case RGB_MODE_FORWARD:
rgb_matrix_mode(rgb_matrix_get_mode() + 1);
case RGB_MODE_FORWARD: {
uint16_t index = (rgb_matrix_get_mode() + 1) % RGB_MATRIX_EFFECT_MAX;
rgb_matrix_mode(index);
break;
}

case RGB_MODE_REVERSE:
rgb_matrix_mode(rgb_matrix_get_mode() - 1);
case RGB_MODE_REVERSE: {
uint16_t index = rgb_matrix_get_mode() - 1;
if (index < 1) {
index = RGB_MATRIX_EFFECT_MAX - 1;
}
rgb_matrix_mode(index);
break;
}

case RGB_TOG:
rgb_matrix_sethsv(hsv_color[hsv_index][0], hsv_color[hsv_index][1], hsv_color[hsv_index][2]);
Expand All @@ -174,7 +192,7 @@ void btn_progress(keyboard_btn_report_t kbd_report)
}

/*!< Find if consumer key release */
for (int i = 0; i <= kbd_report.key_release_num; i++) {
for (int i = 0; i < kbd_report.key_release_num; i++) {
uint8_t row = kbd_report.key_release_data[i].output_index;
uint8_t col = kbd_report.key_release_data[i].input_index;
uint16_t kc = keymaps[mo_action_layer][row][col];
Expand Down
1 change: 1 addition & 0 deletions examples/keyboard/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 0.2.0
dependencies:
idf: ">=5.2"
espressif/tinyusb:
Expand Down
48 changes: 38 additions & 10 deletions examples/keyboard/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,26 @@
#include "rgb_matrix.h"
#include "settings.h"
#include "tinyusb_hid.h"
#include "esp_timer.h"

static keyboard_btn_handle_t kbd_handle = NULL;
static TaskHandle_t light_progress_task_handle = NULL;
uint64_t last_time = 0;
sys_param_t* sys_param;

static void keyboard_cb(keyboard_btn_handle_t kbd_handle, keyboard_btn_report_t kbd_report, void *user_data)
{
if (sys_param->report_type == BLE_HID_REPORT) {
if (eTaskGetState(light_progress_task_handle) == eSuspended) {
if (rgb_matrix_is_enabled()) {
bsp_ws2812_enable(true);
rgb_matrix_set_suspend_state(false);
vTaskResume(light_progress_task_handle);
}
}
last_time = esp_timer_get_time();
}

btn_progress(kbd_report);

/*!< Lighting with key pressed */
Expand All @@ -33,6 +48,24 @@ static void keyboard_cb(keyboard_btn_handle_t kbd_handle, keyboard_btn_report_t
}
}

static void light_progress_task(void *pvParameters)
{
while (1) {
if (bsp_ws2812_is_enable()) {
light_progress();
}
vTaskDelay(10 / portTICK_PERIOD_MS);
if (sys_param->report_type == BLE_HID_REPORT) {
if (esp_timer_get_time() - last_time > CONFIG_LIGHT_SLEEP_TIMEOUT_MS * 1000) {
last_time = esp_timer_get_time();
rgb_matrix_set_suspend_state(true);
bsp_ws2812_enable(false);
vTaskSuspend(NULL);
}
}
}
}

void app_main(void)
{
esp_err_t ret;
Expand All @@ -55,15 +88,16 @@ void app_main(void)
bsp_lamp_array_init(KC_NUM_LOCK);
bsp_rgb_matrix_init();
settings_read_parameter_from_nvs();
sys_param_t* sys_param = settings_get_parameter();
sys_param = settings_get_parameter();
btn_progress_set_report_type(sys_param->report_type);
btn_progress_set_light_type(sys_param->light_type);
bsp_ws2812_enable(true);
if (sys_param->report_type == TINYUSB_HID_REPORT) {
tinyusb_hid_init();
bsp_ws2812_enable(1);
} else if (sys_param->report_type == BLE_HID_REPORT) {
/*!< BLE temporarily does not support lighting effects on Windows 11 and is forced to be set to RGB Matrix */
btn_progress_set_light_type(RGB_MATRIX);
ble_hid_init();
bsp_ws2812_enable(0);
esp_pm_config_t pm_config = {
.max_freq_mhz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ,
.min_freq_mhz = 160,
Expand All @@ -80,11 +114,5 @@ void app_main(void)
};
keyboard_button_register_cb(kbd_handle, cb_cfg, NULL);

/*!< Lighting progress */
if (sys_param->report_type == TINYUSB_HID_REPORT) {
while (1) {
light_progress();
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
xTaskCreate(light_progress_task, "light_progress_task", 4096, NULL, 5, &light_progress_task_handle);
}
55 changes: 34 additions & 21 deletions examples/keyboard/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
# This file was generated using idf.py save-defconfig. It can be edited manually.
# Espressif IoT Development Framework (ESP-IDF) 5.2.1 Project Minimal Configuration
# Espressif IoT Development Framework (ESP-IDF) 5.2.2 Project Minimal Configuration
#
CONFIG_IDF_TARGET="esp32s3"
CONFIG_BT_ENABLED=y
CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y
CONFIG_BT_CTRL_MODEM_SLEEP=y
CONFIG_BT_CTRL_MAIN_XTAL_PU_DURING_LIGHT_SLEEP=y
CONFIG_ESP_PHY_MAC_BB_PD=y
CONFIG_PM_ENABLE=y
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
CONFIG_FREERTOS_HZ=1000
CONFIG_FREERTOS_USE_TICKLESS_IDLE=y
CONFIG_MATRIX_ROWS=6
CONFIG_MATRIX_COLS=15
CONFIG_MATRIX_LED_COUNT=82
CONFIG_ENABLE_RGB_MATRIX_TYPING_HEATMAP=y
CONFIG_ENABLE_RGB_MATRIX_DIGITAL_RAIN=y
CONFIG_ENABLE_RGB_MATRIX_SPLASH=y
CONFIG_ENABLE_RGB_MATRIX_MULTISPLASH=y
CONFIG_ENABLE_RGB_MATRIX_SOLID_SPLASH=y
CONFIG_ENABLE_RGB_MATRIX_SOLID_MULTISPLASH=y
CONFIG_ENABLE_RGB_MATRIX_BREATHING=y
CONFIG_ENABLE_RGB_MATRIX_BAND_PINWHEEL_SAT=y
CONFIG_ENABLE_RGB_MATRIX_BAND_PINWHEEL_VAL=y
CONFIG_ENABLE_RGB_MATRIX_BAND_SAT=y
CONFIG_ENABLE_RGB_MATRIX_BAND_SPIRAL_SAT=y
CONFIG_ENABLE_RGB_MATRIX_BAND_SPIRAL_VAL=y
CONFIG_ENABLE_RGB_MATRIX_CYCLE_OUT_IN=y
CONFIG_ENABLE_RGB_MATRIX_HUE_BREATHING=y

CONFIG_BT_ENABLED=y
CONFIG_BT_BLUEDROID_ENABLED=y
CONFIG_BT_BLE_ENABLED=y
CONFIG_BT_HID_ENABLED=y
CONFIG_BT_HID_DEVICE_ENABLED=y
CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y

# Enable support for power management
CONFIG_PM_ENABLE=y
# Enable tickless idle mode
CONFIG_FREERTOS_USE_TICKLESS_IDLE=y
# Minimum number of ticks to enter sleep mode for
CONFIG_FREERTOS_IDLE_TIME_BEFORE_SLEEP=3
CONFIG_BT_CTRL_MODEM_SLEEP=y
CONFIG_BT_CTRL_MODEM_SLEEP_MODE_1=y
CONFIG_BT_CTRL_LPCLK_SEL_MAIN_XTAL=y
CONFIG_BT_CTRL_MAIN_XTAL_PU_DURING_LIGHT_SLEEP=y
CONFIG_ESP_PHY_MAC_BB_PD=y
CONFIG_ENABLE_RGB_MATRIX_CYCLE_OUT_IN_DUAL=y
CONFIG_ENABLE_RGB_MATRIX_CYCLE_PINWHEEL=y
CONFIG_ENABLE_RGB_MATRIX_CYCLE_SPIRAL=y
CONFIG_ENABLE_RGB_MATRIX_CYCLE_UP_DOWN=y
CONFIG_ENABLE_RGB_MATRIX_DUAL_BEACON=y
CONFIG_ENABLE_RGB_MATRIX_FLOWER_BLOOMING=y
CONFIG_ENABLE_RGB_MATRIX_GRADIENT_LEFT_RIGHT=y
CONFIG_ENABLE_RGB_MATRIX_GRADIENT_UP_DOWN=y
CONFIG_ENABLE_RGB_MATRIX_HUE_WAVE=y
CONFIG_ENABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS=y
CONFIG_ENABLE_RGB_MATRIX_PIXEL_RAIN=y
CONFIG_ENABLE_RGB_MATRIX_RAINBOW_BEACON=y
CONFIG_ENABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON=y
CONFIG_ENABLE_RGB_MATRIX_RAINBOW_PINWHEELS=y
CONFIG_ENABLE_RGB_MATRIX_RIVERFLOW=y
CONFIG_ENABLE_RGB_MATRIX_STARLIGHT_DUAL_HUE=y
CONFIG_ENABLE_RGB_MATRIX_STARLIGHT_DUAL_SAT=y

0 comments on commit c09eabf

Please sign in to comment.