diff options
author | lokher <lokher@gmail.com> | 2023-01-12 15:50:37 +0800 |
---|---|---|
committer | lokher <lokher@gmail.com> | 2023-01-12 15:50:37 +0800 |
commit | a0d162e03ae1301c64a04b04264b53d0787c1358 (patch) | |
tree | adb31f18f533845f83011de41423b1c01a511c1c | |
parent | bbe437487d4da158376a9f55c589acebc3396ff0 (diff) |
change battery voltage meause and battery low indication; update from latest chibios hal
35 files changed, 414 insertions, 177 deletions
diff --git a/keyboards/keychron/bluetooth/battery.c b/keyboards/keychron/bluetooth/battery.c index 6dd453357e..bc463f9336 100644 --- a/keyboards/keychron/bluetooth/battery.c +++ b/keyboards/keychron/bluetooth/battery.c @@ -20,6 +20,8 @@ #include "transport.h" #include "ckbt51.h" #include "lpm.h" +#include "indicator.h" +#include "rtc_timer.h" #define BATTERY_EMPTY_COUNT 10 #define CRITICAL_LOW_COUNT 20 @@ -29,12 +31,14 @@ static uint16_t voltage = FULL_VOLTAGE_VALUE; static uint8_t bat_empty = 0; static uint8_t critical_low = 0; static uint8_t bat_state; +static uint8_t power_on_sample = 0; void battery_init(void) { - bat_monitor_timer_buffer = 0; bat_state = BAT_NOT_CHARGING; } -__attribute__((weak)) void battery_measure(void) {} +__attribute__((weak)) void battery_measure(void) { + ckbt51_read_state_reg(0x05, 0x02); +} /* Calculate the voltage */ __attribute__((weak)) void battery_calculate_voltage(uint16_t value) {} @@ -71,10 +75,16 @@ bool battery_is_critical_low(void) { void battery_check_empty(void) { if (voltage < EMPTY_VOLTAGE_VALUE) { if (bat_empty <= BATTERY_EMPTY_COUNT) { - if (++bat_empty > BATTERY_EMPTY_COUNT) indicator_battery_low_enable(true); + 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; + } } - } else if (bat_empty <= BATTERY_EMPTY_COUNT) { - bat_empty = BATTERY_EMPTY_COUNT; } } @@ -88,20 +98,43 @@ void battery_check_critical_low(void) { } } +bool battery_power_on_sample(void) { + return power_on_sample < VOLTAGE_POWER_ON_MEASURE_COUNT; +} + void battery_task(void) { + uint32_t t = rtc_timer_elapsed_ms(bat_monitor_timer_buffer); if (get_transport() == TRANSPORT_BLUETOOTH && bluetooth_get_state() == BLUETOOTH_CONNECTED) { - if (sync_timer_elapsed32(bat_monitor_timer_buffer) > VOLTAGE_MEASURE_INTERVAL) { + if ((battery_power_on_sample() +#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) + && !indicator_is_enabled() +#endif + && t > BACKLIGHT_OFF_VOLTAGE_MEASURE_INTERVAL) || + t > VOLTAGE_MEASURE_INTERVAL) { + battery_check_empty(); battery_check_critical_low(); - bat_monitor_timer_buffer = sync_timer_read32(); + bat_monitor_timer_buffer = rtc_timer_read_ms(); + if (bat_monitor_timer_buffer > RTC_MAX_TIME) { + bat_monitor_timer_buffer = 0; + rtc_timer_clear(); + } + battery_measure(); + power_on_sample++; + if (power_on_sample > VOLTAGE_POWER_ON_MEASURE_COUNT) power_on_sample = VOLTAGE_POWER_ON_MEASURE_COUNT; } } 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 } } diff --git a/keyboards/keychron/bluetooth/battery.h b/keyboards/keychron/bluetooth/battery.h index 5d00a529c6..45de2bc23a 100644 --- a/keyboards/keychron/bluetooth/battery.h +++ b/keyboards/keychron/bluetooth/battery.h @@ -38,6 +38,14 @@ enum { # define VOLTAGE_MEASURE_INTERVAL 3000 #endif +#ifndef VOLTAGE_POWER_ON_MEASURE_COUNT +# define VOLTAGE_POWER_ON_MEASURE_COUNT 15 +#endif + +#ifndef BACKLIGHT_OFF_VOLTAGE_MEASURE_INTERVAL +# define BACKLIGHT_OFF_VOLTAGE_MEASURE_INTERVAL 200 +#endif + void battery_init(void); void battery_measure(void); void battery_calculte_voltage(uint16_t value); @@ -47,5 +55,6 @@ 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); void battery_task(void); diff --git a/keyboards/keychron/bluetooth/bluetooth.c b/keyboards/keychron/bluetooth/bluetooth.c index 690ddde716..1580138ab0 100644 --- a/keyboards/keychron/bluetooth/bluetooth.c +++ b/keyboards/keychron/bluetooth/bluetooth.c @@ -21,6 +21,7 @@ #include "battery.h" #include "indicator.h" #include "transport.h" +#include "rtc_timer.h" extern uint8_t pairing_indication; extern host_driver_t chibios_driver; @@ -102,6 +103,7 @@ void bluetooth_init(void) { #endif lpm_init(); + rtc_timer_init(); } /* @@ -213,8 +215,11 @@ static void bluetooth_enter_connected(uint8_t host_idx) { #endif bluetooth_enter_connected_kb(host_idx); - - if (battery_is_empty()) indicator_battery_low_enable(true); +#ifdef BAT_LOW_LED_PIN + if (battery_is_empty()) { + indicator_battery_low_enable(true); + } +#endif } /* Enters disconnected state. Upon entering this state we perform the following actions: @@ -236,10 +241,14 @@ static void bluetooth_enter_disconnected(uint8_t host_idx) { #endif retry = 0; bluetooth_enter_disconnected_kb(host_idx); +#ifdef BAT_LOW_LED_PIN indicator_battery_low_enable(false); +#endif +#if defined(LOW_BAT_IND_INDEX) + indicator_battery_low_backlit_enable(false); +#endif } - /* Enter pin code entry state. */ static void bluetooth_enter_pin_code_entry(void) { #if defined(NKRO_ENABLE) @@ -380,7 +389,12 @@ void bluetooth_send_extra(report_extra_t *report) { } void bluetooth_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 bluetooth_disconnect(); } @@ -446,6 +460,17 @@ __attribute__((weak)) bool process_record_kb_bt(uint16_t keycode, keyrecord_t *r bool process_record_kb(uint16_t keycode, keyrecord_t *record) { if (get_transport() == TRANSPORT_BLUETOOTH) { lpm_timer_reset(); + +#if defined(BAT_LOW_LED_PIN) || defined(LOW_BAT_IND_INDEX) + if (battery_is_empty() && bluetooth_get_state() == BLUETOOTH_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 } process_record_kb_bt(keycode, record); return process_record_user(keycode, record); diff --git a/keyboards/keychron/bluetooth/bluetooth.mk b/keyboards/keychron/bluetooth/bluetooth.mk index 22a22eef6e..5d874fc11a 100644 --- a/keyboards/keychron/bluetooth/bluetooth.mk +++ b/keyboards/keychron/bluetooth/bluetooth.mk @@ -10,5 +10,7 @@ SRC += \ $(BLUETOOTH_DIR)/lpm_stm32l432.c \ $(BLUETOOTH_DIR)/battery.c \ $(BLUETOOTH_DIR)/factory_test.c \ - $(BLUETOOTH_DIR)/bat_level_animation.c + $(BLUETOOTH_DIR)/bat_level_animation.c \ + $(BLUETOOTH_DIR)/rtc_timer.c + VPATH += $(TOP_DIR)/keyboards/keychron/$(BLUETOOTH_DIR) diff --git a/keyboards/keychron/bluetooth/ckbt51.c b/keyboards/keychron/bluetooth/ckbt51.c index f60101d8bf..2be217c091 100644 --- a/keyboards/keychron/bluetooth/ckbt51.c +++ b/keyboards/keychron/bluetooth/ckbt51.c @@ -39,8 +39,8 @@ enum { CKBT51_CMD_SEND_KB_NKRO = 0x12, CKBT51_CMD_SEND_CONSUMER = 0x13, CKBT51_CMD_SEND_SYSTEM = 0x14, - CKBT51_CMD_SEND_FN = 0x15, // Not used currently - CKBT51_CMD_SEND_MOUSE = 0x16, // Not used currently + CKBT51_CMD_SEND_FN = 0x15, // Not used currently + CKBT51_CMD_SEND_MOUSE = 0x16, // Not used currently CKBT51_CMD_SEND_BOOT_KB = 0x17, /* Bluetooth connections */ CKBT51_CMD_PAIRING = 0x21, @@ -51,14 +51,14 @@ enum { /* Battery */ CKBT51_CMD_BATTERY_MANAGE = 0x31, CKBT51_CMD_UPDATE_BAT_LVL = 0x32, - /* Set/get parameters */ - CKBT51_CMD_GET_MODULE_INFO = 0x40, - CKBT51_CMD_SET_CONFIG = 0x41, - CKBT51_CMD_GET_CONFIG = 0x42, - CKBT51_CMD_SET_BDA = 0x43, - CKBT51_CMD_GET_BDA = 0x44, - CKBT51_CMD_SET_NAME = 0x45, - CKBT51_CMD_GET_NAME = 0x46, + /* Set/get parameters */ + CKBT51_CMD_GET_MODULE_INFO = 0x40, + CKBT51_CMD_SET_CONFIG = 0x41, + CKBT51_CMD_GET_CONFIG = 0x42, + CKBT51_CMD_SET_BDA = 0x43, + CKBT51_CMD_GET_BDA = 0x44, + CKBT51_CMD_SET_NAME = 0x45, + CKBT51_CMD_GET_NAME = 0x46, /* DFU */ CKBT51_CMD_GET_DFU_VER = 0x60, CKBT51_CMD_HAND_SHAKE_TOKEN = 0x61, @@ -69,10 +69,11 @@ enum { /* Factory test */ CKBT51_CMD_FACTORY_RESET = 0x71, CKBT51_CMD_INT_PIN_TEST = 0x72, + CKBT51_CMD_RADIO_TEST = 0x73, /* Event */ CKBT51_EVT_CKBT51_CMD_RECEIVED = 0xA1, - CKBT51_EVT_OTA_RSP = 0xA3, - CKBT51_CONNECTION_EVT_ACK = 0xA4, + CKBT51_EVT_OTA_RSP = 0xA3, + CKBT51_CONNECTION_EVT_ACK = 0xA4, }; enum { @@ -139,11 +140,14 @@ void ckbt51_init(bool wakeup_from_low_power_mode) { } void ckbt51_send_cmd(uint8_t* payload, uint8_t len, bool ack_enable, bool retry) { - static uint8_t sn = 1; + static uint8_t sn = 0; uint8_t i; uint8_t pkt[PACKET_MAX_LEN] = {0}; memset(pkt, 0, PACKET_MAX_LEN); + if (!retry) ++sn; + if (sn == 0) ++sn; + systime_t start = 0; for (i=0; i< 3; i++) { @@ -161,15 +165,13 @@ void ckbt51_send_cmd(uint8_t* payload, uint8_t len, bool ack_enable, bool retry) pkt[i++] = ack_enable ? 0x56 : 0x55; pkt[i++] = len + 2; pkt[i++] = ~(len + 2) & 0xFF; - pkt[i++] = sn++; + pkt[i++] = sn; memcpy(pkt + i, payload, len); i += len; pkt[i++] = checksum & 0xFF; pkt[i++] = (checksum >> 8) & 0xFF; sdWrite(&BT_DRIVER, pkt, i); - - if (sn == 0) sn = 1; } void ckbt51_send_keyboard(uint8_t* report) { @@ -352,7 +354,7 @@ void ckbt51_set_local_name(const char* name) { ckbt51_send_cmd(payload, i, false, false); } -void ckbt51_get_local_name(char* name) { +void ckbt51_get_local_name(void) { uint8_t i = 0; memset(payload, 0, PACKET_MAX_LEN); @@ -379,6 +381,16 @@ void ckbt51_int_pin_test(bool enable) { ckbt51_send_cmd(payload, i, false, false); } +void ckbt51_radio_test(uint8_t channel) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + payload[i++] = CKBT51_CMD_RADIO_TEST; + payload[i++] = channel; + payload[i++] = 0; + + ckbt51_send_cmd(payload, i, false, false); +} + void ckbt51_dfu_tx(uint8_t rsp, uint8_t* data, uint8_t len, uint8_t sn) { uint16_t checksum = 0; uint8_t buf[RAW_EPSIZE] = {0}; diff --git a/keyboards/keychron/bluetooth/ckbt51.h b/keyboards/keychron/bluetooth/ckbt51.h index ff6da33a07..f0bc08a98f 100644 --- a/keyboards/keychron/bluetooth/ckbt51.h +++ b/keyboards/keychron/bluetooth/ckbt51.h @@ -146,11 +146,12 @@ void ckbt51_get_info(module_info_t* info); void ckbt51_set_param(module_param_t* param); void ckbt51_get_param(module_param_t* param); void ckbt51_set_local_name(const char* name); -void ckbt51_get_local_name(char* name); +void ckbt51_get_local_name(void); void ckbt51_factory_reset(void); void ckbt51_int_pin_test(bool enable); void ckbt51_dfu_rx(uint8_t* data, uint8_t length); +void ckbt51_radio_test(uint8_t channel); void ckbt51_task(void); diff --git a/keyboards/keychron/bluetooth/factory_test.c b/keyboards/keychron/bluetooth/factory_test.c index 3b8f404499..b42d2bde36 100644 --- a/keyboards/keychron/bluetooth/factory_test.c +++ b/keyboards/keychron/bluetooth/factory_test.c @@ -53,6 +53,7 @@ enum { FACTORY_TEST_CMD_INT_PIN, FACTORY_TEST_CMD_GET_TRANSPORT, FACTORY_TEST_CMD_CHARGING_ADC, + FACTORY_TEST_CMD_RADIO_CARRIER, }; enum { @@ -193,6 +194,7 @@ bool led_matrix_indicators_user(void) { #ifdef RGB_MATRIX_ENABLE bool rgb_matrix_indicators_user(void) { if (factory_reset_ind_state) { + backlight_test_mode = BACKLIGHT_TEST_OFF; rgb_matrix_set_color_all(factory_reset_ind_state % 2 ? 0 : 255, 0, 0); } else if (backlight_test_mode) { switch (backlight_test_mode) { @@ -246,10 +248,12 @@ void factory_test_rx(uint8_t *data, uint8_t length) { } /* Verify checksum */ if ((checksum & 0xFF) != data[RAW_EPSIZE - 2] || checksum >> 8 != data[RAW_EPSIZE - 1]) return; + #ifdef KC_BLUETOOTH_ENABLE uint8_t payload[32]; uint8_t len = 0; #endif + switch (data[1]) { case FACTORY_TEST_CMD_BACKLIGHT: backlight_test_mode = data[2]; @@ -302,12 +306,18 @@ void factory_test_rx(uint8_t *data, uint8_t length) { factory_test_send(payload, len); break; #endif + case FACTORY_TEST_CMD_RADIO_CARRIER: + if (data[2] < 79) ckbt51_radio_test(data[2]); + break; } } } bool dip_switch_update_user(uint8_t index, bool active) { if (report_os_sw_state) { +#ifdef INVERT_OS_SWITCH_STATE + active = !active; +#endif uint8_t payload[3] = {FACTORY_TEST_CMD_OS_SWITCH, OS_SWITCH, active}; factory_test_send(payload, 3); } diff --git a/keyboards/keychron/bluetooth/indicator.c b/keyboards/keychron/bluetooth/indicator.c index 58dca9f7a3..e4e4f2eccf 100644 --- a/keyboards/keychron/bluetooth/indicator.c +++ b/keyboards/keychron/bluetooth/indicator.c @@ -21,6 +21,8 @@ #include "eeconfig.h" #include "bluetooth_config.h" #include "config.h" +#include "rtc_timer.h" + #if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) # ifdef LED_MATRIX_ENABLE # include "led_matrix.h" @@ -58,7 +60,17 @@ static bluetooth_state_t indicator_state; static uint16_t next_period; static indicator_type_t type; static uint32_t indicator_timer_buffer = 0; -static uint32_t battery_low_indicator = 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) +static uint32_t bat_low_backlit_indicator = 0; +static uint8_t bat_low_ind_state = 0; +static uint32_t rtc_time = 0; +#endif #if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) backlight_state_t original_backlight_state; @@ -78,6 +90,7 @@ static pin_t host_led_pin_list[HOST_DEVICES_COUNT] = HOST_LED_PIN_LIST; # define SET_LED_OFF(idx) led_matrix_set_value(idx, 0) # define SET_LED_ON(idx) led_matrix_set_value(idx, 255) # define SET_LED_BT(idx) led_matrix_set_value(idx, 255) +# define SET_LED_LOW_BAT(idx) led_matrix_set_value(idx, 255) # define LED_DRIVER_IS_ENABLED led_matrix_is_enabled # define LED_DRIVER_EECONFIG_RELOAD() \ eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); \ @@ -99,6 +112,7 @@ static pin_t host_led_pin_list[HOST_DEVICES_COUNT] = HOST_LED_PIN_LIST; # define SET_LED_OFF(idx) rgb_matrix_set_color(idx, 0, 0, 0) # define SET_LED_ON(idx) rgb_matrix_set_color(idx, 255, 255, 255) # define SET_LED_BT(idx) rgb_matrix_set_color(idx, 0, 0, 255) +# define SET_LED_LOW_BAT(idx) rgb_matrix_set_color(idx, 255, 0, 0) # define LED_DRIVER_IS_ENABLED rgb_matrix_is_enabled # define LED_DRIVER_EECONFIG_RELOAD() \ eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); \ @@ -113,7 +127,7 @@ static pin_t host_led_pin_list[HOST_DEVICES_COUNT] = HOST_LED_PIN_LIST; #endif void indicator_init(void) { memset(&indicator_config, 0, sizeof(indicator_config)); - + #ifdef HOST_LED_PIN_LIST for (uint8_t i = 0; i < HOST_DEVICES_COUNT; i++) { setPinOutput(host_led_pin_list[i]); @@ -157,7 +171,14 @@ void indicator_eeconfig_reload(void) { #endif bool indicator_is_running(void) { - return !!indicator_config.value; + return +#if defined(BAT_LOW_LED_PIN) + bat_low_blink_duration || +#endif +#if defined(LOW_BAT_IND_INDEX) + bat_low_ind_state || +#endif + !!indicator_config.value; } static void indicator_timer_cb(void *arg) { @@ -283,6 +304,9 @@ void indicator_set(bluetooth_state_t state, uint8_t host_index) { switch (state) { case BLUETOOTH_DISCONNECTED: +#ifdef HOST_LED_PIN_LIST + writePin(host_led_pin_list[host_index - 1], !HOST_LED_PIN_ON_STATE); +#endif INDICATOR_SET(disconnected); indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index; indicator_timer_cb((void *)&indicator_config.type); @@ -343,10 +367,82 @@ void indicator_stop(void) { } } +#ifdef BAT_LOW_LED_PIN void indicator_battery_low_enable(bool enable) { - battery_low_indicator = enable ? (sync_timer_read32() | 1) : 0; + if (enable) { + if (bat_low_blink_duration == 0) { + bat_low_blink_duration = bat_low_pin_indicator = sync_timer_read32() | 1; + } else + bat_low_blink_duration = sync_timer_read32() | 1; + } 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 (enable) { + uint32_t t = rtc_timer_read_ms(); + /* Check overflow */ + if (rtc_time > t) { + if (bat_low_ind_state == 0) + rtc_time = t; // Update rtc_time if indicating is not running + else { + 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() | 1) : 0; + rtc_time = rtc_timer_read_ms(); + bat_low_ind_state = 1; + + indicator_enable(); + } + } else { + rtc_time = 0; + bat_low_ind_state = 0; + + indicator_eeconfig_reload(); + if (!LED_DRIVER_IS_ENABLED()) indicator_disable(); + } +} +#endif + +void indicator_battery_low(void) { #ifdef BAT_LOW_LED_PIN - if (!enable) writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE); + if (bat_low_pin_indicator && sync_timer_elapsed32(bat_low_pin_indicator) > (LOW_BAT_LED_BLINK_PERIOD)) { + togglePin(BAT_LOW_LED_PIN); + bat_low_pin_indicator = sync_timer_read32() | 1; + // Turn off low battery indication if we reach the duration + if (sync_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 (bat_low_ind_state) { + if ((bat_low_ind_state & 0x0F) <= (LOW_BAT_LED_BLINK_TIMES) && sync_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++; + } else { + bat_low_ind_state |= 0x80; + } + + bat_low_backlit_indicator = sync_timer_read32() | 1; + + /* 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()) +# endif + indicator_disable(); + } + } else if ((bat_low_ind_state & 0x0F) > (LOW_BAT_LED_BLINK_TIMES)) { + bat_low_ind_state = 0; + } + } #endif } @@ -358,12 +454,7 @@ void indicator_task(void) { indicator_timer_buffer = sync_timer_read32(); } - if (battery_low_indicator && sync_timer_elapsed32(battery_low_indicator) > LOW_BAT_ON_OFF_DURATION) { -#ifdef BAT_LOW_LED_PIN - togglePin(BAT_LOW_LED_PIN); -#endif - battery_low_indicator = sync_timer_read32() | 1; - } + indicator_battery_low(); } #if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) @@ -407,6 +498,15 @@ bool LED_INDICATORS_KB(void) { return false; } +# if (defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)) && defined(LOW_BAT_IND_INDEX) + if (battery_is_empty()) SET_ALL_LED_OFF(); + if (bat_low_ind_state && (bat_low_ind_state & 0x0F) <= LOW_BAT_LED_BLINK_TIMES) { + if (bat_low_ind_state & 0x80) + SET_LED_LOW_BAT(LOW_BAT_IND_INDEX); + else + SET_LED_OFF(LOW_BAT_IND_INDEX); + } +# endif if (bat_level_animiation_actived()) { bat_level_animiation_indicate(); } @@ -432,19 +532,19 @@ bool LED_INDICATORS_KB(void) { } else os_state_indicate(); - + return false; } bool led_update_user(led_t led_state) { if (!LED_DRIVER_IS_ENABLED()) { -# ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE +# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE) LED_DRIVER.exit_shutdown(); # endif SET_ALL_LED_OFF(); os_state_indicate(); LED_DRIVER.flush(); -# ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE +# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE) if (LED_DRIVER_ALLOW_SHUTDOWN()) LED_DRIVER.shutdown(); # endif } @@ -455,7 +555,7 @@ void LED_NONE_INDICATORS_KB(void) { os_state_indicate(); } -# ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE +# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE) bool LED_DRIVER_ALLOW_SHUTDOWN(void) { # if defined(NUM_LOCK_INDEX) if (host_keyboard_led_state().num_lock) return false; diff --git a/keyboards/keychron/bluetooth/indicator.h b/keyboards/keychron/bluetooth/indicator.h index 17bda1e65a..bf408a0c1c 100644 --- a/keyboards/keychron/bluetooth/indicator.h +++ b/keyboards/keychron/bluetooth/indicator.h @@ -49,8 +49,30 @@ # define CONNECTED_BACKLIGHT_OFF_DELAY_TIME 600 #endif -#ifndef LOW_BAT_ON_OFF_DURATION -# define LOW_BAT_ON_OFF_DURATION 1000 +#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 +#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 3 +# endif + +# ifndef LOW_BAT_LED_TRIG_INTERVAL +# define LOW_BAT_LED_TRIG_INTERVAL 30000 +# endif #endif #if BT_HOST_MAX_COUNT > 6 @@ -82,8 +104,14 @@ void indicator_enable(void); void indicator_disable(void); 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); diff --git a/keyboards/keychron/bluetooth/lpm.c b/keyboards/keychron/bluetooth/lpm.c index 62e78d03b1..187c6d75c0 100644 --- a/keyboards/keychron/bluetooth/lpm.c +++ b/keyboards/keychron/bluetooth/lpm.c @@ -86,7 +86,7 @@ void lpm_task(void) { #ifdef RGB_MATRIX_ENABLE && rgb_matrix_is_driver_shutdown() #endif - && !lpm_any_matrix_action()) + && !lpm_any_matrix_action() && !battery_power_on_sample()) enter_power_mode(LOW_POWER_MODE); } diff --git a/keyboards/keychron/bluetooth/lpm_stm32l432.c b/keyboards/keychron/bluetooth/lpm_stm32l432.c index f19bf55d68..8e9f2fa77f 100644 --- a/keyboards/keychron/bluetooth/lpm_stm32l432.c +++ b/keyboards/keychron/bluetooth/lpm_stm32l432.c @@ -30,6 +30,7 @@ #include "transport.h" #include "battery.h" #include "report_buffer.h" +#include "stm32_bd.inc" extern pin_t row_pins[MATRIX_ROWS]; extern void select_all_cols(void); @@ -139,15 +140,6 @@ bool lpm_set(pm_t mode) { } static inline void enter_low_power_mode_prepare(void) { -#if HAL_USE_RTC - nvicEnableVector(STM32_EXTI20_NUMBER, 13); - - PWR->CR3 |= PWR_CR3_EIWF; - - RTCWakeup wakeupspec; - wakeupspec.wutr = 0x040014; - rtcSTM32SetPeriodicWakeup(&RTCD1, &wakeupspec); -#endif #if defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE) /* Usb unit is actived and running, stop and disconnect first */ @@ -183,10 +175,9 @@ static inline void enter_low_power_mode_prepare(void) { } static inline void lpm_wakeup(void) { - if (usb_power_connected()) - stm32_clock_init(); - else - stm32_clock_fast_init(); + chSysLock(); + stm32_clock_fast_init(); + chSysUnlock(); if (bluetooth_transport.init) bluetooth_transport.init(true); @@ -215,6 +206,7 @@ static inline void lpm_wakeup(void) { #if defined(KEEP_USB_CONNECTION_IN |