Unified low battery indication of LED/RGB matrix and individual LED; Fixed bluetooth name may not be set correctly after factory reset

This commit is contained in:
lokher 2024-11-09 15:45:12 +08:00
parent b5de40f5c1
commit 27a218a3d1
11 changed files with 98 additions and 147 deletions

View File

@ -167,12 +167,7 @@ void battery_check_empty(void) {
if (voltage < EMPTY_VOLTAGE_VALUE) {
if (bat_empty <= BATTERY_EMPTY_COUNT) {
if (++bat_empty > BATTERY_EMPTY_COUNT) {
#ifdef BAT_LOW_LED_PIN
indicator_battery_low_enable(true);
#endif
#if defined(LOW_BAT_IND_INDEX)
indicator_battery_low_backlit_enable(true);
#endif
power_on_sample = VOLTAGE_POWER_ON_MEASURE_COUNT;
}
}
@ -229,11 +224,6 @@ void battery_task(void) {
if ((bat_empty || critical_low) && usb_power_connected()) {
bat_empty = false;
critical_low = false;
#ifdef BAT_LOW_LED_PIN
indicator_battery_low_enable(false);
#endif
#if defined(LOW_BAT_IND_INDEX)
indicator_battery_low_backlit_enable(false);
#endif
}
}

View File

@ -54,7 +54,6 @@ void battery_calculate_voltage(bool vol_src_bt, uint16_t value);
void battery_set_voltage(uint16_t value);
uint16_t battery_get_voltage(void);
uint8_t battery_get_percentage(void);
void indicator_battery_low_enable(bool enable);
bool battery_is_empty(void);
bool battery_is_critical_low(void);
bool battery_power_on_sample(void);

View File

@ -28,7 +28,6 @@
# include "factory_test.h"
#endif
#include "lpm.h"
#include "keychron_task.h"
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
# ifdef LED_MATRIX_ENABLE
@ -71,12 +70,7 @@ static uint16_t next_period;
static indicator_type_t type;
static uint32_t indicator_timer_buffer = 0;
#if defined(BAT_LOW_LED_PIN)
static uint32_t bat_low_pin_indicator = 0;
static uint32_t bat_low_blink_duration = 0;
#endif
#if defined(LOW_BAT_IND_INDEX)
#if defined(BAT_LOW_LED_PIN) || defined(SPACE_KEY_LOW_BAT_IND)
static uint32_t bat_low_backlit_indicator = 0;
static uint8_t bat_low_ind_state = 0;
static uint32_t rtc_time = 0;
@ -177,8 +171,13 @@ void indicator_init(void) {
#endif
#ifdef BAT_LOW_LED_PIN
setPinOutput(BAT_LOW_LED_PIN);
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
#ifdef POWER_ON_LED_DURATION
if (timer_read32() > POWER_ON_LED_DURATION)
#endif
{
setPinOutput(BAT_LOW_LED_PIN);
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
}
#endif
}
@ -213,10 +212,7 @@ void indicator_eeconfig_reload(void) {
bool indicator_is_running(void) {
return
#if defined(BAT_LOW_LED_PIN)
bat_low_blink_duration ||
#endif
#if defined(LOW_BAT_IND_INDEX)
#if defined(BAT_LOW_LED_PIN) || defined(SPACE_KEY_LOW_BAT_IND)
bat_low_ind_state ||
#endif
!!indicator_config.value;
@ -342,6 +338,7 @@ static void indicator_timer_cb(void *arg) {
void indicator_set(wt_state_t state, uint8_t host_index) {
if (get_transport() == TRANSPORT_USB) return;
static uint8_t pre_state = 0;
static uint8_t current_state = 0;
static uint8_t current_host = 0;
bool host_index_changed = false;
@ -354,6 +351,10 @@ void indicator_set(wt_state_t state, uint8_t host_index) {
}
if (current_state != state || host_index_changed || state == WT_RECONNECTING) {
// Some BT chips need to reset to enter sleep mode, ignore it.
if (current_state == WT_SUSPEND && state == WT_DISCONNECTED) return;
pre_state = current_state;
current_state = state;
} else {
return;
@ -385,6 +386,9 @@ void indicator_set(wt_state_t state, uint8_t host_index) {
indicator_set_backlit_timeout(1000);
} else {
if (pre_state == WT_CONNECTED)
indicator_set_backlit_timeout(1000);
else
/* Set timer so that user has chance to turn on the backlight when is off */
indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
}
@ -465,22 +469,11 @@ void indicator_stop(void) {
#endif
}
#ifdef BAT_LOW_LED_PIN
void indicator_battery_low_enable(bool enable) {
if (enable) {
if (bat_low_blink_duration == 0) {
bat_low_blink_duration = bat_low_pin_indicator = timer_read32();
} else
bat_low_blink_duration = timer_read32();
} else
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
}
#endif
#if defined(LOW_BAT_IND_INDEX)
void indicator_battery_low_backlit_enable(bool enable) {
#if defined(BAT_LOW_LED_PIN) || defined(SPACE_KEY_LOW_BAT_IND)
if (enable) {
uint32_t t = rtc_timer_read_ms();
/* Check overflow */
if (rtc_time > t) {
if (bat_low_ind_state == 0)
@ -489,55 +482,62 @@ void indicator_battery_low_backlit_enable(bool enable) {
rtc_time += t;
}
}
/* Indicating at first time or after the interval */
if ((rtc_time == 0 || t - rtc_time > LOW_BAT_LED_TRIG_INTERVAL) && bat_low_ind_state == 0) {
bat_low_backlit_indicator = enable ? timer_read32() : 0;
rtc_time = rtc_timer_read_ms();
bat_low_ind_state = 1;
# if defined(SPACE_KEY_LOW_BAT_IND)
indicator_enable();
# endif
}
} else {
rtc_time = 0;
bat_low_ind_state = 0;
# if defined(SPACE_KEY_LOW_BAT_IND)
indicator_eeconfig_reload();
if (!LED_DRIVER_IS_ENABLED()) indicator_disable();
# endif
}
}
#endif
}
void indicator_battery_low(void) {
#ifdef BAT_LOW_LED_PIN
if (bat_low_pin_indicator && timer_elapsed32(bat_low_pin_indicator) > (LOW_BAT_LED_BLINK_PERIOD)) {
togglePin(BAT_LOW_LED_PIN);
bat_low_pin_indicator = timer_read32();
// Turn off low battery indication if we reach the duration
if (timer_elapsed32(bat_low_blink_duration) > LOW_BAT_LED_BLINK_DURATION && palReadLine(BAT_LOW_LED_PIN) != BAT_LOW_LED_PIN_ON_STATE) {
bat_low_blink_duration = bat_low_pin_indicator = 0;
}
}
#endif
#if defined(LOW_BAT_IND_INDEX)
#if defined(BAT_LOW_LED_PIN) || defined(SPACE_KEY_LOW_BAT_IND)
if (bat_low_ind_state) {
if ((bat_low_ind_state & 0x0F) <= (LOW_BAT_LED_BLINK_TIMES) && timer_elapsed32(bat_low_backlit_indicator) > (LOW_BAT_LED_BLINK_PERIOD)) {
if (bat_low_ind_state & 0x80) {
bat_low_ind_state &= 0x7F;
bat_low_ind_state++;
# if defined(BAT_LOW_LED_PIN)
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
# endif
} else {
bat_low_ind_state |= 0x80;
# if defined(BAT_LOW_LED_PIN)
writePin(BAT_LOW_LED_PIN, BAT_LOW_LED_PIN_ON_STATE);
# endif
}
bat_low_backlit_indicator = timer_read32();
/* Restore backligth state */
if ((bat_low_ind_state & 0x0F) > (LOW_BAT_LED_BLINK_TIMES)) {
# if defined(NUM_LOCK_INDEX) || defined(CAPS_LOCK_INDEX) || defined(SCROLL_LOCK_INDEX) || defined(COMPOSE_LOCK_INDEX) || defined(KANA_LOCK_INDEX)
if (LED_DRIVER_ALLOW_SHUTDOWN())
# if defined(BAT_LOW_LED_PIN)
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
# endif
# if defined(SPACE_KEY_LOW_BAT_IND)
# if defined(NUM_LOCK_INDEX) || defined(CAPS_LOCK_INDEX) || defined(SCROLL_LOCK_INDEX) || defined(COMPOSE_LOCK_INDEX) || defined(KANA_LOCK_INDEX)
if (LED_DRIVER_ALLOW_SHUTDOWN())
# endif
indicator_disable();
# endif
}
} else if ((bat_low_ind_state & 0x0F) > (LOW_BAT_LED_BLINK_TIMES)) {
# if defined(BAT_LOW_LED_PIN)
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
# endif
bat_low_ind_state = 0;
lpm_timer_reset();
}
@ -546,9 +546,7 @@ void indicator_battery_low(void) {
}
void indicator_task(void) {
#if (defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)) && defined(BAT_LEVEL_LED_LIST)
bat_level_animiation_task();
#endif
if (indicator_config.value && timer_elapsed32(indicator_timer_buffer) >= next_period) {
indicator_timer_cb((void *)&type);
indicator_timer_buffer = timer_read32();
@ -606,7 +604,6 @@ bool LED_INDICATORS_KB(void) {
return true;
}
# if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
if (battery_is_empty()) SET_ALL_LED_OFF();
# if defined(LOW_BAT_IND_INDEX)
if (bat_low_ind_state && (bat_low_ind_state & 0x0F) <= LOW_BAT_LED_BLINK_TIMES) {
@ -619,8 +616,8 @@ bool LED_INDICATORS_KB(void) {
}
}
}
# endif
# endif
# if (defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)) && defined(BAT_LEVEL_LED_LIST)
if (bat_level_animiation_actived()) {
bat_level_animiation_indicate();
@ -690,10 +687,7 @@ bool led_update_kb(led_t led_state) {
}
void LED_NONE_INDICATORS_KB(void) {
# if defined(RGB_DISABLE_WHEN_USB_SUSPENDED)
if (get_transport() == TRANSPORT_USB && USB_DRIVER.state == USB_SUSPENDED) return;
# endif
# if defined(LED_DISABLE_WHEN_USB_SUSPENDED)
# if defined(RGB_DISABLE_WHEN_USB_SUSPENDED) || defined(LED_DISABLE_WHEN_USB_SUSPENDED)
if (get_transport() == TRANSPORT_USB && USB_DRIVER.state == USB_SUSPENDED) return;
# endif

View File

@ -49,37 +49,35 @@
# define CONNECTED_BACKLIGHT_OFF_DELAY_TIME 600
#endif
#ifdef BAT_LOW_LED_PIN
/* Uint: ms */
# ifndef LOW_BAT_LED_BLINK_PERIOD
# define LOW_BAT_LED_BLINK_PERIOD 1000
# endif
# ifndef LOW_BAT_LED_BLINK_DURATION
# define LOW_BAT_LED_BLINK_DURATION 10000
# endif
#ifndef LOW_BAT_LED_BLINK_PERIOD
# define LOW_BAT_LED_BLINK_PERIOD 1000
#endif
#ifdef LOW_BAT_IND_INDEX
/* Uint: ms */
# ifndef LOW_BAT_LED_BLINK_PERIOD
# define LOW_BAT_LED_BLINK_PERIOD 500
# endif
#ifndef LOW_BAT_LED_BLINK_TIMES
# define LOW_BAT_LED_BLINK_TIMES 5
#endif
# ifndef LOW_BAT_LED_BLINK_TIMES
# define LOW_BAT_LED_BLINK_TIMES 3
# endif
#ifndef LOW_BAT_LED_TRIG_INTERVAL
# define LOW_BAT_LED_TRIG_INTERVAL 30000
#endif
# ifndef LOW_BAT_LED_TRIG_INTERVAL
# define LOW_BAT_LED_TRIG_INTERVAL 30000
# endif
#if ((defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)) && defined(LOW_BAT_IND_INDEX))
# define SPACE_KEY_LOW_BAT_IND
#endif
#if BT_HOST_MAX_COUNT > 6
# pragma error("HOST_COUNT max value is 6")
#endif
typedef enum { INDICATOR_NONE, INDICATOR_OFF, INDICATOR_ON, INDICATOR_ON_OFF, INDICATOR_BLINK, INDICATOR_LAST } indicator_type_t;
typedef enum {
INDICATOR_NONE,
INDICATOR_OFF,
INDICATOR_ON,
INDICATOR_ON_OFF,
INDICATOR_BLINK,
INDICATOR_LAST,
} indicator_type_t;
typedef struct {
indicator_type_t type;
@ -98,6 +96,7 @@ typedef struct {
void indicator_init(void);
void indicator_set(wt_state_t state, uint8_t host_index);
void indicator_set_backlit_timeout(uint32_t time);
void indicator_backlight_timer_reset(bool enable);
bool indicator_hook_key(uint16_t keycode);
void indicator_enable(void);
@ -106,12 +105,6 @@ void indicator_stop(void);
void indicator_eeconfig_reload(void);
bool indicator_is_enabled(void);
bool indicator_is_running(void);
#ifdef BAT_LOW_LED_PIN
void indicator_battery_low_enable(bool enable);
#endif
#if defined(LOW_BAT_IND_INDEX)
void indicator_battery_low_backlit_enable(bool enable);
#endif
void indicator_task(void);

View File

@ -100,7 +100,7 @@ void wireless_enter_reset_kb(uint8_t reason) {
lkbt51_param_init();
}
void wireless_enter_disconnected_kb(uint8_t host_idx) {
void wireless_enter_disconnected_kb(uint8_t host_idx, uint8_t reason) {
/* CKBT51 bluetooth module boot time is slower, it enters disconnected after boot,
so we place initialization here. */
if (firstDisconnect && timer_read32() < 1000) {

View File

@ -25,9 +25,9 @@
extern void factory_test_send(uint8_t* payload, uint8_t length);
# ifndef RAW_EPSIZE
# define RAW_EPSIZE 32
# endif
#ifndef RAW_EPSIZE
# define RAW_EPSIZE 32
#endif
#ifndef SPI_SCK_PIN
# define SPI_SCK_PIN A5
@ -147,6 +147,7 @@ static uint8_t reg_offset = 0xFF;
static uint8_t expect_len = 22;
static uint16_t connection_interval = 1;
static uint32_t wake_time;
static uint32_t factory_reset = 0;
// clang-format off
wt_func_t wireless_transport = {
@ -434,6 +435,8 @@ void lkbt51_disconnect(void) {
payload[i++] = LKBT51_CMD_DISCONNECT;
payload[i++] = 0; // Sleep mode
if (WT_DRIVER.state != SPI_READY)
spiStart(&WT_DRIVER, &spicfg);
spiSelect(&SPID1);
wait_ms(30);
// spiUnselect(&SPID1);
@ -539,6 +542,7 @@ void lkbt51_factory_reset(uint8_t p2p4g_clr_msk) {
lkbt51_wake();
lkbt51_send_cmd(payload, i, false, false);
factory_reset = timer_read32();
}
void lkbt51_int_pin_test(bool enable) {
@ -791,6 +795,10 @@ void lkbt51_task(void) {
break;
case LKBT51_DISCONNECTED:
event.evt_type = EVT_DISCONNECTED;
if (factory_reset && timer_elapsed32(factory_reset) < 3000) {
factory_reset = 0;
event.data = 1;
}
break;
case LKBT51_PINCODE_ENTRY:
event.evt_type = EVT_BT_PINCODE_ENTRY;

View File

@ -140,12 +140,6 @@ void set_transport(transport_t new_transport) {
p24g_transport_enable(false);
wireless_disconnect();
lpm_timer_stop();
#ifdef NKRO_ENABLE
# if defined(WIRELESS_NKRO_ENABLE)
nkro.bluetooth = keymap_config.nkro;
# endif
keymap_config.nkro = nkro.usb;
#endif
break;
case TRANSPORT_BLUETOOTH:
@ -154,14 +148,6 @@ void set_transport(transport_t new_transport) {
bt_transport_enable(true);
usb_transport_enable(false);
lpm_timer_reset();
#if defined(NKRO_ENABLE)
nkro.usb = keymap_config.nkro;
# if defined(WIRELESS_NKRO_ENABLE)
keymap_config.nkro = nkro.bluetooth;
# else
keymap_config.nkro = FALSE;
# endif
#endif
break;
case TRANSPORT_P2P4:
@ -170,14 +156,6 @@ void set_transport(transport_t new_transport) {
p24g_transport_enable(true);
usb_transport_enable(false);
lpm_timer_reset();
#if defined(NKRO_ENABLE)
nkro.usb = keymap_config.nkro;
# if defined(WIRELESS_NKRO_ENABLE)
keymap_config.nkro = nkro.bluetooth;
# else
keymap_config.nkro = FALSE;
# endif
#endif
break;
default:
@ -244,7 +222,11 @@ void usb_remote_wakeup(void) {
/* Do this in the suspended state */
suspend_power_down(); // on AVR this deep sleeps for 15ms
/* Remote wakeup */
if (suspend_wakeup_condition()) {
if (suspend_wakeup_condition()
#ifdef ENCODER_ENABLE
|| encoder_read()
#endif
) {
usbWakeupHost(&USB_DRIVER);
wait_ms(300);
#ifdef MOUSEKEY_ENABLE

View File

@ -227,20 +227,19 @@ static void wireless_enter_connected(uint8_t host_idx) {
#endif
wireless_enter_connected_kb(host_idx);
#ifdef BAT_LOW_LED_PIN
if (battery_is_empty()) {
indicator_battery_low_enable(true);
}
#endif
if (wireless_transport.update_bat_level) wireless_transport.update_bat_level(battery_get_percentage());
lpm_timer_reset();
}
/* Enters disconnected state. Upon entering this state we perform the following actions:
* - change state to DISCONNECTED
* - set disconnected indication
*/
static void wireless_enter_disconnected(uint8_t host_idx) {
kc_printf("wireless_disconnected %d\n\r", host_idx);
static void wireless_enter_disconnected(uint8_t host_idx, uint8_t reason) {
kc_printf("wireless_disconnected %d, %d\n\r", host_idx, reason);
uint8_t previous_state = wireless_state;
led_state = 0;
@ -252,20 +251,19 @@ static void wireless_enter_disconnected(uint8_t host_idx) {
if (previous_state == WT_CONNECTED) {
lpm_timer_reset();
indicator_set(WT_SUSPEND, host_idx);
} else
} else {
indicator_set(wireless_state, host_idx);
if (reason && (get_transport() & TRANSPORT_WIRELESS))
indicator_set_backlit_timeout(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT*1000);
}
#ifndef DISABLE_REPORT_BUFFER
report_buffer_init();
#endif
retry = 0;
wireless_enter_disconnected_kb(host_idx);
#ifdef BAT_LOW_LED_PIN
wireless_enter_disconnected_kb(host_idx, reason);
indicator_battery_low_enable(false);
#endif
#if defined(LOW_BAT_IND_INDEX)
indicator_battery_low_backlit_enable(false);
#endif
}
/* Enter pin code entry state. */
@ -294,18 +292,15 @@ static void wireless_enter_sleep(void) {
kc_printf("wireless_enter_sleep %d\n\r", wireless_state);
led_state = 0;
if (wireless_state == WT_PARING) {
if (wireless_state == WT_CONNECTED || wireless_state == WT_PARING) {
wireless_state = WT_SUSPEND;
kc_printf("WT_SUSPEND\n\r");
lpm_timer_reset();
wireless_enter_sleep_kb();
indicator_set(wireless_state, 0);
#ifdef BAT_LOW_LED_PIN
indicator_battery_low_enable(false);
#endif
#if defined(LOW_BAT_IND_INDEX)
indicator_battery_low_backlit_enable(false);
#endif
}
}
@ -313,7 +308,7 @@ __attribute__((weak)) void wireless_enter_reset_kb(uint8_t reason) {}
__attribute__((weak)) void wireless_enter_discoverable_kb(uint8_t host_idx) {}
__attribute__((weak)) void wireless_enter_reconnecting_kb(uint8_t host_idx) {}
__attribute__((weak)) void wireless_enter_connected_kb(uint8_t host_idx) {}
__attribute__((weak)) void wireless_enter_disconnected_kb(uint8_t host_idx) {}
__attribute__((weak)) void wireless_enter_disconnected_kb(uint8_t host_idx, uint8_t reason) {}
__attribute__((weak)) void wireless_enter_bluetooth_pin_code_entry_kb(void) {}
__attribute__((weak)) void wireless_exit_bluetooth_pin_code_entry_kb(void) {}
__attribute__((weak)) void wireless_enter_sleep_kb(void) {}
@ -434,12 +429,8 @@ void wireless_send_extra(report_extra_t *report) {
}
void wireless_low_battery_shutdown(void) {
#ifdef BAT_LOW_LED_PIN
indicator_battery_low_enable(false);
#endif
#if defined(LOW_BAT_IND_INDEX)
indicator_battery_low_backlit_enable(false);
#endif
report_buffer_init();
clear_keyboard(); //
@ -483,7 +474,7 @@ void wireless_event_task(void) {
wireless_enter_reconnecting(event.params.hostIndex);
break;
case EVT_DISCONNECTED:
wireless_enter_disconnected(event.params.hostIndex);
wireless_enter_disconnected(event.params.hostIndex, event.data);
break;
case EVT_BT_PINCODE_ENTRY:
wireless_enter_bluetooth_pin_code_entry();
@ -537,16 +528,9 @@ bool process_record_wireless(uint16_t keycode, keyrecord_t *record) {
if (get_transport() & TRANSPORT_WIRELESS) {
lpm_timer_reset();
#if defined(BAT_LOW_LED_PIN) || defined(LOW_BAT_IND_INDEX)
if (battery_is_empty() && wireless_get_state() == WT_CONNECTED && record->event.pressed) {
# if defined(BAT_LOW_LED_PIN)
indicator_battery_low_enable(true);
# endif
# if defined(LOW_BAT_IND_INDEX)
indicator_battery_low_backlit_enable(true);
# endif
}
#endif
}
if (!process_record_keychron_wireless(keycode, record)) return false;

View File

@ -84,7 +84,7 @@ void wireless_enter_reset_kb(uint8_t reason);
void wireless_enter_discoverable_kb(uint8_t host_idx);
void wireless_enter_reconnecting_kb(uint8_t host_idx);
void wireless_enter_connected_kb(uint8_t host_idx);
void wireless_enter_disconnected_kb(uint8_t host_idx);
void wireless_enter_disconnected_kb(uint8_t host_idx, uint8_t reason);
void wireless_enter_bluetooth_pin_code_entry_kb(void);
void wireless_exit_bluetooth_pin_code_entry_kb(void);
void wireless_enter_sleep_kb(void);

View File

@ -41,4 +41,5 @@ typedef struct {
uint8_t protocol; /* Parameters to EVT_HID_SET_PROTOCOL event */
uint8_t interval; /* Parameters to EVT_CONECTION_INTERVAL event */
} params;
uint8_t data;
} wireless_event_t;

View File

@ -1,7 +1,7 @@
{
"usb": {
"pid": "0x0810",
"device_version": "1.0.0"
"device_version": "1.0.2"
},
"layouts": {
"LAYOUT_ansi_82": {