Skip to content

Commit d0f2088

Browse files
committed
set wifi power to fix macos bug;
display switch value; save chain rgb value; battery process manager; change switch value 3000->2000; web cache change to 60min.
1 parent 2495e56 commit d0f2088

File tree

13 files changed

+265
-99
lines changed

13 files changed

+265
-99
lines changed

components/esp_duo/include/bsp/keymap.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,39 @@ static uint16_t keymaps[][1][KBD_COL_NUM] = {
3636
{LCMD(HID_KEY_Z), LSG(HID_KEY_Z)},
3737
},
3838
[5] = {
39-
{RCS(KC_TAB), LCTL(KC_TAB)},
39+
{LCTL(KC_TAB), LCS(KC_TAB)},
4040
},
4141
[6] = {
42-
{LOPT(KC_TAB), LCMD(KC_TAB)},
42+
{QK_LALT, KC_TAB},
4343
},
4444
[7] = {
45-
{LCTL(KC_KP_MINUS), LCTL(KC_KP_PLUS)},
45+
{QK_LGUI, KC_TAB},
4646
},
4747
[8] = {
48-
{LCMD(KC_KP_MINUS), LCMD(KC_KP_PLUS)},
48+
{LCTL(KC_KP_MINUS), LCTL(KC_KP_PLUS)},
4949
},
5050
[9] = {
51-
{KC_PAGE_UP, KC_PAGE_DOWN},
51+
{LCMD(KC_KP_MINUS), LCMD(KC_KP_PLUS)},
5252
},
5353
[10] = {
54-
{KC_KB_VOLUME_UP, KC_KB_VOLUME_DOWN},
54+
{KC_PAGE_UP, KC_PAGE_DOWN},
5555
},
5656
[11] = {
57-
{KC_MEDIA_PREV_TRACK, KC_MEDIA_NEXT_TRACK},
57+
{KC_KB_VOLUME_UP, KC_KB_VOLUME_DOWN},
5858
},
5959
[12] = {
60-
{KC_MEDIA_PLAY_PAUSE, KC_MEDIA_STOP},
60+
{KC_MEDIA_PREV_TRACK, KC_MEDIA_NEXT_TRACK},
6161
},
6262
[13] = {
63-
{KC_HOME, KC_END},
63+
{KC_MEDIA_PLAY_PAUSE, KC_MEDIA_STOP},
6464
},
6565
[14] = {
66-
{KC_UP, KC_DOWN},
66+
{KC_HOME, KC_END},
6767
},
6868
[15] = {
69+
{KC_UP, KC_DOWN},
70+
},
71+
[16] = {
6972
{KC_LEFT, KC_RIGHT},
7073
},
7174
};

components/esp_duo/include/bsp/quantum_keycodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#define LSA(kc) (QK_LSFT | QK_LALT | (kc))
6363
#define RSA(kc) (QK_RSFT | QK_RALT | (kc))
6464
#define RCS(kc) (QK_RCTL | QK_RSFT | (kc))
65+
#define LCS(kc) (QK_LCTL | QK_LSFT | (kc))
6566
#define SAGR(kc) RSA(kc)
6667

6768
// Modified keycode aliases

main/adc_detect.c

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static adc_oneshot_unit_handle_t adc_handle = NULL;
7171
float g_battery_voltage = 3.7f;
7272
int g_charging_status = 0;
7373
int g_battery_percentage = 75;
74+
float g_usb_voltage = 0.0f;
7475
bool g_usb_connected = false;
7576

7677
// 安全的LED操作函数
@@ -319,29 +320,76 @@ void test_adc_detection(led_strip_handle_t led_strip, adc_oneshot_unit_handle_t
319320
update_power_status(led_strip);
320321
}
321322

323+
static int calculate_smooth_percentage(float voltage, bool is_charging)
324+
{
325+
// 定义查找表结构
326+
typedef struct { float v; int p; } point_t;
327+
328+
// 充电曲线表 (电压从小到大)
329+
const point_t chrg_table[] = {
330+
{3.40f, 0}, {3.61f, 25}, {3.88f, 50}, {4.12f, 75}, {4.20f, 100}
331+
};
332+
// 放电曲线表 (电压从小到大)
333+
const point_t dischrg_table[] = {
334+
{3.33f, 0}, {3.55f, 25}, {3.81f, 50}, {4.07f, 75}, {4.20f, 100}
335+
};
336+
337+
const point_t *table = is_charging ? chrg_table : dischrg_table;
338+
int count = 5;
339+
340+
if (voltage <= table[0].v) return 0;
341+
if (voltage >= table[count-1].v) return 100;
342+
343+
// 线性插值
344+
for (int i = 0; i < count - 1; i++) {
345+
if (voltage >= table[i].v && voltage < table[i+1].v) {
346+
float range_v = table[i+1].v - table[i].v;
347+
float range_p = table[i+1].p - table[i].p;
348+
float offset = voltage - table[i].v;
349+
return (int)(table[i].p + (offset / range_v) * range_p);
350+
}
351+
}
352+
return 100;
353+
}
354+
322355
void update_power_status(led_strip_handle_t led_strip)
323356
{
324357
adc_result_t result = perform_adc_detection();
325-
358+
326359
// 更新全局变量
327360
g_battery_voltage = result.battery_voltage;
328-
g_charging_status = (strcmp(result.charge_status, "Charging") == 0) ? 1
361+
362+
// 判定充电状态: 0=未充电, 1=充电中, 2=充满
363+
g_charging_status = (strcmp(result.charge_status, "Charging") == 0) ? 1
329364
: (strcmp(result.charge_status, "Fully Charged") == 0) ? 2
330-
: 0;
365+
: 0;
366+
g_usb_voltage = result.vbus_voltage;
367+
368+
g_usb_connected = result.usb_connected;
369+
370+
// --- 优化后的电量计算逻辑开始 ---
331371
if (g_charging_status == 2) {
372+
// 硬件指示已充满,强制100%
332373
g_battery_percentage = 100;
333374
} else {
334-
g_battery_percentage = (int)((g_battery_voltage - 3.0f) / 1.2f * 100);
375+
// 根据充电/放电状态选择不同的曲线
376+
bool is_charging_curve = (g_charging_status == 1);
377+
g_battery_percentage = calculate_smooth_percentage(g_battery_voltage, is_charging_curve);
335378
}
336-
g_usb_connected = result.usb_connected;
379+
// --- 优化后的电量计算逻辑结束 ---
380+
381+
// 边界保护
337382
if (g_battery_percentage > 100) g_battery_percentage = 100;
338383
if (g_battery_percentage < 0) g_battery_percentage = 0;
339384

340385
// 更新LED状态
341386
if (led_strip != NULL) {
342-
// 打印检测结果
387+
// 打印检测结果,增加百分比显示
343388
ESP_LOGI(TAG, "=== ADC Detection Results ===");
344-
ESP_LOGI(TAG, "Battery Voltage: %.2fV %s", result.battery_voltage, result.battery_low ? "(LOW!)" : "(OK)");
389+
ESP_LOGI(TAG, "Battery: %.2fV [%d%%] %s",
390+
result.battery_voltage,
391+
g_battery_percentage,
392+
result.battery_low ? "(LOW!)" : "(OK)");
345393
ESP_LOGI(TAG, "USB Voltage: %.2fV %s", result.vbus_voltage,
346394
result.usb_connected ? "(Connected)" : "(Disconnected)");
347395
ESP_LOGI(TAG, "Charge Status: %s (%.2fV)", result.charge_status, result.chrg_voltage);

main/ble/ble_hid.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "esp_hidd.h"
1717
#include "ble_hid.h"
1818
#include "esp_mac.h"
19+
#include "freertos/task.h"
1920

2021
extern uint8_t g_connect_status;
2122

@@ -77,6 +78,14 @@ static void ble_hidd_event_callback(void *handler_args, esp_event_base_t base, i
7778
g_connect_status = 2; // 连接建立
7879

7980
ESP_LOGI(TAG, "CONNECT");
81+
82+
// 连接建立后,等待50ms让蓝牙协议栈稳定,然后发送初始电量
83+
extern uint8_t g_battery_percentage;
84+
vTaskDelay(pdMS_TO_TICKS(50));
85+
if (s_ble_hid_param.hid_dev != NULL) {
86+
esp_hidd_dev_battery_set(s_ble_hid_param.hid_dev, g_battery_percentage);
87+
ESP_LOGI(TAG, "Initial battery level sent: %d%%", g_battery_percentage);
88+
}
8089
break;
8190
}
8291
case ESP_HIDD_PROTOCOL_MODE_EVENT: {
@@ -162,7 +171,7 @@ esp_err_t ble_hid_deinit(void)
162171
void ble_hid_keyboard_report(hid_report_t report)
163172
{
164173
static bool use_full_key = false;
165-
if (s_ble_hid_param.is_connected == false) {
174+
if (s_ble_hid_param.is_connected == false || s_ble_hid_param.hid_dev == NULL) {
166175
return;
167176
}
168177

@@ -200,11 +209,10 @@ void ble_hid_keyboard_report(hid_report_t report)
200209

201210
esp_err_t ble_hid_set_battery(uint8_t level)
202211
{
203-
if (s_ble_hid_param.hid_dev == NULL) {
212+
if (s_ble_hid_param.hid_dev == NULL || !s_ble_hid_param.is_connected) {
204213
return ESP_FAIL;
205214
}
206215

207-
// 调用ESP-IDF的电量设置API
208216
return esp_hidd_dev_battery_set(s_ble_hid_param.hid_dev, level);
209217
}
210218

main/ble/esp_hid_gap.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ static void ble_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_p
172172
ESP_LOGV(TAG, "BLE GAP ADV_START_COMPLETE");
173173
if (param->adv_start_cmpl.status == ESP_BT_STATUS_SUCCESS) {
174174
ESP_LOGI(TAG, "BLE advertising started successfully");
175-
// 广播开始时,连接状态重置为未连接
176-
g_connect_status = 0;
175+
// 仅在未连接状态下重置连接状态
176+
if (g_connect_status != 2) {
177+
g_connect_status = 0;
178+
}
177179
} else {
178180
ESP_LOGE(TAG, "BLE advertising start failed: %d", param->adv_start_cmpl.status);
179181
}
@@ -245,12 +247,10 @@ static void ble_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_p
245247
param->phy_update.rx_phy);
246248
break;
247249
case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
248-
ESP_LOGI(TAG, "BLE GAP UPDATE_CONN_PARAMS min:%d max: %d", param->update_conn_params.min_int,
249-
param->update_conn_params.max_int);
250-
// 连接参数更新通常表示连接已建立
251-
if (g_connect_status == 0) {
252-
g_connect_status = 1; // 连接中
253-
}
250+
ESP_LOGI(TAG, "BLE GAP UPDATE_CONN_PARAMS min:%d max: %d status:%d",
251+
param->update_conn_params.min_int,
252+
param->update_conn_params.max_int,
253+
param->update_conn_params.status);
254254
break;
255255
default:
256256
ESP_LOGI(TAG, "BLE GAP EVENT %d", event);

main/btn_progress.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ extern bool g_ble_mapping_enabled;
2424

2525
static btn_report_type_t report_type = USB_CDC_REPORT;
2626

27-
// 当前按键映射索引,默认为9(翻页模式)
28-
static int current_key_mapping_index = 9;
27+
// 当前按键映射索引,默认为10(翻页模式)
28+
static int current_key_mapping_index = 10;
2929

3030
#define HSV_MAX 18
3131
static uint8_t hsv_index = 0;
@@ -288,7 +288,7 @@ void btn_progress_set_report_type(btn_report_type_t type)
288288

289289
void btn_progress_set_key_mapping(int mapping_index)
290290
{
291-
if (mapping_index >= 0 && mapping_index < 16) {
291+
if (mapping_index >= 0 && mapping_index < 17) {
292292
current_key_mapping_index = mapping_index;
293293
ESP_LOGI("btn_progress", "按键映射已设置为: %d", mapping_index);
294294
} else {

main/chain_bus/chain_bus.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,14 @@ void chain_bus_scan_single(int bus_idx)
197197

198198
// 如果UID有效,尝试加载保存的配置
199199
if (status->device_status[i].uid_valid) {
200+
uint32_t loaded_rgb_color = 0;
200201
esp_err_t load_ret = chain_bus_config_load(status->device_status[i].uid, status->device_status[i].type,
201-
&status->device_status[i].hid_config);
202+
&status->device_status[i].hid_config, &loaded_rgb_color);
202203
if (load_ret == ESP_OK) {
203204
config_loaded = true;
204205
status->device_status[i].hid_config_initialized = true;
205206
ESP_LOGI(TAG, "[%s] 设备 %d 已加载保存的配置", bus_names[bus_idx], status->device_status[i].id);
207+
chain_bus_set_device_rgb(bus_idx, status->device_status[i].id, loaded_rgb_color);
206208
}
207209
}
208210

@@ -1030,6 +1032,8 @@ esp_err_t chain_bus_set_device_rgb(int bus_idx, uint8_t device_id, uint32_t rgb_
10301032
ret = ESP_FAIL;
10311033
}
10321034

1035+
chain_bus_save_device_config(bus_idx, device_id);
1036+
10331037
// 重置RGB设置状态
10341038
dev_status->rgb_setting = false;
10351039

@@ -1178,7 +1182,7 @@ esp_err_t chain_bus_save_device_config(int bus_idx, uint8_t device_id)
11781182
return ESP_ERR_INVALID_STATE;
11791183
}
11801184

1181-
ret = chain_bus_config_save(dev_status->uid, dev_status->type, &dev_status->hid_config);
1185+
ret = chain_bus_config_save(dev_status->uid, dev_status->type, &dev_status->hid_config, dev_status->rgb_color);
11821186
if (ret == ESP_OK) {
11831187
char uid_str[25];
11841188
chain_bus_uid_to_string(dev_status->uid, uid_str);

main/chain_bus/chain_bus_config.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static uint32_t calculate_config_crc(const chain_device_saved_config_t* config)
5656

5757
// 保存设备配置到NVS
5858
esp_err_t chain_bus_config_save(const uint8_t* uid, chain_device_type_t device_type,
59-
const chain_device_hid_config_t* hid_config)
59+
const chain_device_hid_config_t* hid_config, uint32_t rgb_color)
6060
{
6161
if (!config_initialized) {
6262
ESP_LOGE(TAG, "配置系统未初始化");
@@ -72,6 +72,7 @@ esp_err_t chain_bus_config_save(const uint8_t* uid, chain_device_type_t device_t
7272
memcpy(saved_config.uid, uid, CHAIN_UID_SIZE);
7373
saved_config.device_type = device_type;
7474
memcpy(&saved_config.hid_config, hid_config, sizeof(chain_device_hid_config_t));
75+
saved_config.rgb_color = rgb_color;
7576
saved_config.crc32 = calculate_config_crc(&saved_config);
7677

7778
char nvs_key[16];
@@ -98,14 +99,14 @@ esp_err_t chain_bus_config_save(const uint8_t* uid, chain_device_type_t device_t
9899

99100
// 从NVS加载设备配置
100101
esp_err_t chain_bus_config_load(const uint8_t* uid, chain_device_type_t device_type,
101-
chain_device_hid_config_t* hid_config)
102+
chain_device_hid_config_t* hid_config, uint32_t* rgb_color)
102103
{
103104
if (!config_initialized) {
104105
ESP_LOGE(TAG, "配置系统未初始化");
105106
return ESP_ERR_INVALID_STATE;
106107
}
107108

108-
if (!uid || !hid_config) {
109+
if (!uid || !hid_config || !rgb_color) {
109110
return ESP_ERR_INVALID_ARG;
110111
}
111112

@@ -144,6 +145,7 @@ esp_err_t chain_bus_config_load(const uint8_t* uid, chain_device_type_t device_t
144145
}
145146

146147
memcpy(hid_config, &saved_config.hid_config, sizeof(chain_device_hid_config_t));
148+
*rgb_color = saved_config.rgb_color;
147149

148150
char uid_str[25];
149151
chain_bus_uid_to_string(uid, uid_str);

main/chain_bus/chain_bus_config.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef struct {
1717
uint8_t uid[CHAIN_UID_SIZE]; // 设备UID
1818
chain_device_type_t device_type; // 设备类型
1919
chain_device_hid_config_t hid_config; // HID配置
20+
uint32_t rgb_color; // RGB颜色
2021
uint32_t crc32; // CRC32校验
2122
} chain_device_saved_config_t;
2223

@@ -31,20 +32,22 @@ esp_err_t chain_bus_config_init(void);
3132
* @param uid 设备UID
3233
* @param device_type 设备类型
3334
* @param hid_config HID配置
35+
* @param rgb_color RGB颜色
3436
* @return esp_err_t ESP_OK 成功
3537
*/
3638
esp_err_t chain_bus_config_save(const uint8_t* uid, chain_device_type_t device_type,
37-
const chain_device_hid_config_t* hid_config);
39+
const chain_device_hid_config_t* hid_config, uint32_t rgb_color);
3840

3941
/**
4042
* @brief 从NVS加载设备配置
4143
* @param uid 设备UID
4244
* @param device_type 设备类型(用于验证)
4345
* @param hid_config 返回的HID配置
46+
* @param rgb_color 返回的RGB颜色
4447
* @return esp_err_t ESP_OK 成功,ESP_ERR_NOT_FOUND 未找到配置
4548
*/
4649
esp_err_t chain_bus_config_load(const uint8_t* uid, chain_device_type_t device_type,
47-
chain_device_hid_config_t* hid_config);
50+
chain_device_hid_config_t* hid_config, uint32_t* rgb_color);
4851

4952
/**
5053
* @brief 删除设备配置

main/include/adc_detect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void update_power_status(led_strip_handle_t led_strip);
3434
extern float g_battery_voltage;
3535
extern int g_charging_status;
3636
extern int g_battery_percentage;
37+
extern float g_usb_voltage;
3738
extern bool g_usb_connected;
3839

3940
#ifdef __cplusplus

0 commit comments

Comments
 (0)