diff options
Diffstat (limited to 'keyboards/keychron')
-rw-r--r-- | keyboards/keychron/bluetooth/bat_level_animation.c | 12 | ||||
-rw-r--r-- | keyboards/keychron/bluetooth/bluetooth.c | 4 | ||||
-rw-r--r-- | keyboards/keychron/bluetooth/bluetooth.h | 3 | ||||
-rw-r--r-- | keyboards/keychron/bluetooth/indicator.c | 268 | ||||
-rw-r--r-- | keyboards/keychron/bluetooth/indicator.h | 2 | ||||
-rw-r--r-- | keyboards/keychron/bluetooth/lpm.c | 4 | ||||
-rw-r--r-- | keyboards/keychron/bluetooth/lpm_stm32l432.c | 20 | ||||
-rw-r--r-- | keyboards/keychron/k8_pro/k8_pro.c | 13 |
8 files changed, 199 insertions, 127 deletions
diff --git a/keyboards/keychron/bluetooth/bat_level_animation.c b/keyboards/keychron/bluetooth/bat_level_animation.c index 83a4f91cae..2e726e17b8 100644 --- a/keyboards/keychron/bluetooth/bat_level_animation.c +++ b/keyboards/keychron/bluetooth/bat_level_animation.c @@ -8,6 +8,7 @@ #elif if defined(PROTOCOL_LUFA) # include "lufa.h" #endif +#include "eeprom.h" #ifndef BAT_LEVEL_GROWING_INTERVAL # define BAT_LEVEL_GROWING_INTERVAL 150 @@ -17,6 +18,14 @@ # define BAT_LEVEL_ON_INTERVAL 3000 #endif +#ifdef LED_MATRIX_ENABLE +# define LED_DRIVER_IS_ENABLED led_matrix_is_enabled +#endif + +#ifdef RGB_MATRIX_ENABLE +# define LED_DRIVER_IS_ENABLED rgb_matrix_is_enabled +#endif + enum { BAT_LVL_ANI_NONE, BAT_LVL_ANI_GROWING, @@ -113,7 +122,8 @@ void bat_level_animiation_update(void) { case BAT_LVL_ANI_BLINK_ON: animation_state = BAT_LVL_ANI_NONE; - if (indicator_config.value == 0 && !indicator_is_backlit_enabled_eeprom()) { + indicator_eeconfig_reload(); + if (indicator_config.value == 0 && !LED_DRIVER_IS_ENABLED()) { indicator_disable(); } break; diff --git a/keyboards/keychron/bluetooth/bluetooth.c b/keyboards/keychron/bluetooth/bluetooth.c index 10c79257a9..e93fd8d4ac 100644 --- a/keyboards/keychron/bluetooth/bluetooth.c +++ b/keyboards/keychron/bluetooth/bluetooth.c @@ -432,10 +432,12 @@ bluetooth_state_t bluetooth_get_state(void) { return bt_state; }; +__attribute__((weak)) bool process_record_kb_bt(uint16_t keycode, keyrecord_t *record) { return true;}; + bool process_record_kb(uint16_t keycode, keyrecord_t *record) { if (get_transport() == TRANSPORT_BLUETOOTH) { lpm_timer_reset(); } - + process_record_kb_bt(keycode, record); return process_record_user(keycode, record); } diff --git a/keyboards/keychron/bluetooth/bluetooth.h b/keyboards/keychron/bluetooth/bluetooth.h index b24df48845..bfbc838000 100644 --- a/keyboards/keychron/bluetooth/bluetooth.h +++ b/keyboards/keychron/bluetooth/bluetooth.h @@ -83,3 +83,6 @@ bluetooth_state_t bluetooth_get_state(void); void bluetooth_low_battery_shutdown(void); +bool process_record_kb_bt(uint16_t keycode, keyrecord_t *record); + + diff --git a/keyboards/keychron/bluetooth/indicator.c b/keyboards/keychron/bluetooth/indicator.c index fa9c9fc6e4..de7023e2fb 100644 --- a/keyboards/keychron/bluetooth/indicator.c +++ b/keyboards/keychron/bluetooth/indicator.c @@ -30,6 +30,7 @@ # endif # include "i2c_master.h" # include "bat_level_animation.h" +# include "eeprom.h" #endif #ifdef LED_MATRIX_ENABLE @@ -40,19 +41,7 @@ #endif #define LED_ON 0x80 - -#define INDICATOR_SET(s) \ - indicator_config.type = s##_config.type; \ - indicator_config.on_time = s##_config.on_time; \ - indicator_config.off_time = s##_config.off_time; \ - indicator_config.duration = s##_config.duration; \ - indicator_config.highlight = s##_config.highlight; \ - indicator_config.elapsed = 0; - -indicator_config_t pairing_config = INDICATOR_CONFIG_PARING; -indicator_config_t connected_config = INDICATOR_CONFIG_CONNECTD; -indicator_config_t reconnecting_config = INDICATOR_CONFIG_RECONNECTING; -indicator_config_t disconnected_config = INDICATOR_CONFIG_DISCONNECTED; +#define INDICATOR_SET(s) memcpy(&indicator_config, &s##_config, sizeof(indicator_config_t)); enum { BACKLIGHT_OFF = 0x00, @@ -60,8 +49,11 @@ enum { BACKLIGHT_ON_UNCONNECTED = 0x02, }; +static indicator_config_t pairing_config = INDICATOR_CONFIG_PARING; +static indicator_config_t connected_config = INDICATOR_CONFIG_CONNECTD; +static indicator_config_t reconnecting_config = INDICATOR_CONFIG_RECONNECTING; +static indicator_config_t disconnected_config = INDICATOR_CONFIG_DISCONNECTED; indicator_config_t indicator_config; - static bluetooth_state_t indicator_state; static uint16_t next_period; static indicator_type_t type; @@ -78,6 +70,51 @@ static uint8_t host_led_matrix_list[HOST_DEVICES_COUNT] = HOST_LED_MATRIX_LIST; static pin_t host_led_pin_list[HOST_DEVICES_COUNT] = HOST_LED_PIN_LIST; #endif +#ifndef BACKLIGHT_TURN_OFF_VALUE +# define BACKLIGHT_TURN_OFF_VALUE 32 +#endif + +#ifdef LED_MATRIX_ENABLE +# define LED_DRIVER led_matrix_driver +# define LED_INDICATORS_KB led_matrix_indicators_kb +# define LED_NONE_INDICATORS_KB led_matrix_none_indicators_kb +# define SET_ALL_LED_OFF() led_matrix_set_value_all(0) +# 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 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)); \ + if (!led_matrix_eeconfig.mode) { \ + eeconfig_update_led_matrix_default(); \ + } +# define LED_DRIVER_ALLOW_SHUTDOWN led_matrix_driver_allow_shutdown +# define LED_DRIVER_ENABLE_NOEEPROM led_matrix_enable_noeeprom +# define LED_DRIVER_DISABLE_NOEEPROM led_matrix_disable_noeeprom +# define LED_DRIVER_DISABLE_TIMEOUT_SET led_matrix_disable_timeout_set +# define LED_DRIVER_DISABLE_TIME_RESET led_matrix_disable_time_reset +#endif + +#ifdef RGB_MATRIX_ENABLE +# define LED_DRIVER rgb_matrix_driver +# define LED_INDICATORS_KB rgb_matrix_indicators_kb +# define LED_NONE_INDICATORS_KB rgb_matrix_none_indicators_kb +# define SET_ALL_LED_OFF() rgb_matrix_set_color_all(0, 0, 0) +# 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 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)); \ + if (!rgb_matrix_config.mode) { \ + eeconfig_update_rgb_matrix_default(); \ + } +# define LED_DRIVER_ALLOW_SHUTDOWN rgb_matrix_driver_allow_shutdown +# define LED_DRIVER_ENABLE_NOEEPROM rgb_matrix_enable_noeeprom +# define LED_DRIVER_DISABLE_NOEEPROM rgb_matrix_disable_noeeprom +# define LED_DRIVER_DISABLE_TIMEOUT_SET rgb_matrix_disable_timeout_set +# define LED_DRIVER_DISABLE_TIME_RESET rgb_matrix_disable_time_reset +#endif void indicator_init(void) { memset(&indicator_config, 0, sizeof(indicator_config)); @@ -96,69 +133,39 @@ void indicator_init(void) { #if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) void indicator_enable(void) { -# ifdef LED_MATRIX_ENABLE - if (!led_matrix_is_enabled()) { - led_matrix_enable_noeeprom(); - } -# endif -# ifdef RGB_MATRIX_ENABLE - if (!rgb_matrix_is_enabled()) { - rgb_matrix_enable_noeeprom(); + if (!LED_DRIVER_IS_ENABLED()) { + LED_DRIVER_ENABLE_NOEEPROM(); } -# endif } -void indicator_disable(void) { -# ifdef LED_MATRIX_ENABLE - led_matrix_disable_noeeprom(); -# endif -# ifdef RGB_MATRIX_ENABLE - rgb_matrix_disable_noeeprom(); -# endif +inline void indicator_disable(void) { + LED_DRIVER_DISABLE_NOEEPROM(); } void indicator_set_backlit_timeout(uint32_t time) { -# ifdef LED_MATRIX_ENABLE - led_matrix_disable_timeout_set(time); -# endif -# ifdef RGB_MATRIX_ENABLE - rgb_matrix_disable_timeout_set(time); -# endif + LED_DRIVER_DISABLE_TIMEOUT_SET(time); } static inline void indicator_reset_backlit_time(void) { -# ifdef LED_MATRIX_ENABLE - led_matrix_disable_time_reset(); -# endif -# ifdef RGB_MATRIX_ENABLE - rgb_matrix_disable_time_reset(); -# endif + LED_DRIVER_DISABLE_TIME_RESET(); } bool indicator_is_enabled(void) { -# ifdef LED_MATRIX_ENABLE - return led_matrix_is_enabled(); -# endif -# ifdef RGB_MATRIX_ENABLE - return rgb_matrix_is_enabled(); -# endif + return LED_DRIVER_IS_ENABLED(); } -bool indicator_is_backlit_enabled_eeprom(void) { -# ifdef LED_MATRIX_ENABLE - return led_matrix_is_enabled_eeprom(); -# endif -# ifdef RGB_MATRIX_ENABLE - return rgb_matrix_is_enabled_eeprom(); -# endif - return false; +void indicator_eeconfig_reload(void) { + LED_DRIVER_EECONFIG_RELOAD(); } #endif -bool indicator_is_running(void) { return !!indicator_config.value; } +bool indicator_is_running(void) { + return !!indicator_config.value; +} static void indicator_timer_cb(void *arg) { +uprintf("indicator_timer_cb: %02x\n", (uint8_t)(*(indicator_type_t *)arg)); if (*(indicator_type_t *)arg != INDICATOR_LAST) type = *(indicator_type_t *)arg; bool time_up = false; @@ -226,8 +233,7 @@ static void indicator_timer_cb(void *arg) { } #ifdef HOST_LED_PIN_LIST - if (indicator_config.value) - { + if (indicator_config.value) { uint8_t idx = (indicator_config.value & 0x0F) - 1; if (idx < HOST_DEVICES_COUNT) { @@ -243,17 +249,15 @@ static void indicator_timer_cb(void *arg) { if (time_up) { /* Set indicator to off on timeup, avoid keeping light up until next update in raindrop effect */ indicator_config.value = indicator_config.value & 0x0F; -#ifdef LED_MATRIX_ENABLE - led_matrix_indicators_kb(); -#endif -#ifdef RGB_MATRIX_ENABLE - rgb_matrix_indicators_kb(); +#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) + LED_INDICATORS_KB(); #endif indicator_config.value = 0; } - if (indicator_config.value == 0 && !indicator_is_backlit_enabled_eeprom()) { - indicator_disable(); + if (indicator_config.value == 0) { + indicator_eeconfig_reload(); + if (!LED_DRIVER_IS_ENABLED()) indicator_disable(); } } @@ -335,8 +339,9 @@ void indicator_set(bluetooth_state_t state, uint8_t host_index) { void indicator_stop(void) { indicator_config.value = 0; + indicator_eeconfig_reload(); - if (indicator_is_backlit_enabled_eeprom()) { + if (indicator_is_enabled()) { indicator_enable(); } else { indicator_disable(); @@ -366,50 +371,44 @@ void indicator_task(void) { } } -#ifdef LED_MATRIX_ENABLE -void led_matrix_indicators_kb(void) { - if (get_transport() == TRANSPORT_BLUETOOTH) { - if (battery_is_critical_low()) { - /* Prevent backlight flash caused by key activities */ - led_matrix_set_value_all(0); - return; - } - - if (bat_level_animiation_actived()) { - bat_level_animiation_indicate(); - } - - static uint8_t last_host_index = 0xFF; - if (indicator_config.value) { - uint8_t host_index = indicator_config.value & 0x0F; - - if (indicator_config.highlight) { - led_matrix_set_value_all(0); - } else if (last_host_index != host_index) { - led_matrix_set_value(host_led_matrix_list[last_host_index - 1], 0); - last_host_index = host_index; - } - - if (indicator_config.value & 0x80) - led_matrix_set_value(host_led_matrix_list[host_index - 1], 255); - else - led_matrix_set_value(host_led_matrix_list[host_index - 1], 0); - } -# if defined(DIM_CAPS_LOCK) && defined(CAPS_LOCK_INDEX) - else if (host_keyboard_led_state().caps_lock) { - led_matrix_set_value(CAPS_LOCK_INDEX, 0); - } +#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) +static void os_state_indicate(void) { +# if defined(NUM_LOCK_INDEX) + if (host_keyboard_led_state().num_lock) { + SET_LED_ON(NUM_LOCK_INDEX); + } # endif +# if defined(CAPS_LOCK_INDEX) + if (host_keyboard_led_state().caps_lock) { +# if defined(DIM_CAPS_LOCK) + SET_LED_OFF(CAPS_LOCK_INDEX); +# else + SET_LED_ON(CAPS_LOCK_INDEX); +# endif } +# endif +# if defined(SCROLL_LOCK_INDEX) + if (host_keyboard_led_state().scroll_lock) { + SET_LED_ON(SCROLL_LOCK_INDEX); + } +# endif +# if defined(COMPOSE_LOCK_INDEX) + if (host_keyboard_led_state().compose) { + SET_LED_ON(COMPOSE_LOCK_INDEX); + } +# endif +# if defined(KANA_LOCK_INDEX) + if (host_keyboard_led_state().kana) { + SET_LED_ON(KANA_LOCK_INDEX); + } +# endif } -#endif -#ifdef RGB_MATRIX_ENABLE -void rgb_matrix_indicators_kb(void) { - { +void LED_INDICATORS_KB(void) { + if (get_transport() == TRANSPORT_BLUETOOTH) { + /* Prevent backlight flash caused by key activities */ if (battery_is_critical_low()) { - /* Prevent backlight flash caused by key activities */ - rgb_matrix_set_color_all(0, 0, 0); + SET_ALL_LED_OFF(); return; } @@ -422,23 +421,62 @@ void rgb_matrix_indicators_kb(void) { uint8_t host_index = indicator_config.value & 0x0F; if (indicator_config.highlight) { - rgb_matrix_set_color_all(0, 0, 0); + SET_ALL_LED_OFF(); } else if (last_host_index != host_index) { - rgb_matrix_set_color(host_led_matrix_list[last_host_index - 1], 0, 0, 0); + SET_LED_OFF(host_led_matrix_list[last_host_index - 1]); last_host_index = host_index; } if (indicator_config.value & 0x80) { - rgb_matrix_set_color(host_led_matrix_list[host_index - 1], 0, 0, 255); + SET_LED_BT(host_led_matrix_list[host_index - 1]); } else { - rgb_matrix_set_color(host_led_matrix_list[host_index - 1], 0, 0, 0); + SET_LED_OFF(host_led_matrix_list[host_index - 1]); } - } -# if defined(DIM_CAPS_LOCK) && defined(CAPS_LOCK_INDEX) - else if (host_keyboard_led_state().caps_lock) { - rgb_matrix_set_color(CAPS_LOCK_INDEX, 0, 0, 0); - } + } else + os_state_indicate(); + + } else + os_state_indicate(); +} + +bool led_update_user(led_t led_state) { + if (!LED_DRIVER_IS_ENABLED()) { +# ifdef 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 (LED_DRIVER_ALLOW_SHUTDOWN()) LED_DRIVER.shutdown(); # endif } + return true; } + +void LED_NONE_INDICATORS_KB(void) { + os_state_indicate(); +} + +# ifdef 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; +# endif +# if defined(CAPS_LOCK_INDEX) && !defined(DIM_CAPS_LOCK) + if (host_keyboard_led_state().caps_lock) return false; +# endif +# if defined(SCROLL_LOCK_INDEX) + if (host_keyboard_led_state().scroll_lock) return false; +# endif +# if defined(COMPOSE_LOCK_INDEX) + if (host_keyboard_led_state().compose) return false; +# endif +# if defined(KANA_LOCK_INDEX) + if (host_keyboard_led_state().kana) return false; +# endif + return true; +} +# endif + #endif diff --git a/keyboards/keychron/bluetooth/indicator.h b/keyboards/keychron/bluetooth/indicator.h index d09cf3c7cf..17bda1e65a 100644 --- a/keyboards/keychron/bluetooth/indicator.h +++ b/keyboards/keychron/bluetooth/indicator.h @@ -81,7 +81,7 @@ bool indicator_hook_key(uint16_t keycode); void indicator_enable(void); void indicator_disable(void); void indicator_stop(void); -bool indicator_is_backlit_enabled_eeprom(void); +void indicator_eeconfig_reload(void); bool indicator_is_running(void); void indicator_battery_low_enable(bool enable); diff --git a/keyboards/keychron/bluetooth/lpm.c b/keyboards/keychron/bluetooth/lpm.c index 84f629a672..62e78d03b1 100644 --- a/keyboards/keychron/bluetooth/lpm.c +++ b/keyboards/keychron/bluetooth/lpm.c @@ -41,7 +41,11 @@ static matrix_row_t empty_matrix[MATRIX_ROWS] = {0}; void lpm_init(void) { #ifdef USB_POWER_SENSE_PIN +# if (USB_POWER_CONNECTED_LEVEL == 0) setPinInputHigh(USB_POWER_SENSE_PIN); +# else + setPinInputLow(USB_POWER_SENSE_PIN); +# endif #endif lpm_timer_reset(); } diff --git a/keyboards/keychron/bluetooth/lpm_stm32l432.c b/keyboards/keychron/bluetooth/lpm_stm32l432.c index a871a1b53c..10fe9f5ceb 100644 --- a/keyboards/keychron/bluetooth/lpm_stm32l432.c +++ b/keyboards/keychron/bluetooth/lpm_stm32l432.c @@ -192,12 +192,6 @@ static inline void lpm_wakeup(void) { PWR->SCR |= PWR_SCR_CWUF; PWR->SCR |= PWR_SCR_CSBF; -#if defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE) - /* Remove USB isolation.*/ - PWR->CR2 |= PWR_CR2_USV; /* PWR_CR2_USV is available on STM32L4x2xx and STM32L4x3xx devices only. */ - usb_start(&USBD1); -#endif - /* TIMx is disable during stop/standby/sleep mode, init after wakeup */ stInit(); timer_init(); @@ -211,8 +205,21 @@ static inline void lpm_wakeup(void) { } } palDisableLineEvent(BLUETOOTH_INT_INPUT_PIN); + +#ifdef USB_POWER_SENSE_PIN palDisableLineEvent(USB_POWER_SENSE_PIN); +#if defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE) + if (usb_power_connected()) { + /* Remove USB isolation.*/ + //PWR->CR2 |= PWR_CR2_USV; /* PWR_CR2_USV is available on STM32L4x2xx and STM32L4x3xx devices only. */ + usb_power_connect(); + usb_start(&USBD1); + } +#endif + +#endif + #if defined(DIP_SWITCH_PINS) dip_switch_init(); dip_switch_read(true); @@ -222,6 +229,7 @@ static inline void lpm_wakeup(void) { rtcSTM32SetPeriodicWakeup(&RTCD1, NULL); nvicDisableVector(STM32_EXTI20_NUMBER); #endif + } /* diff --git a/keyboards/keychron/k8_pro/k8_pro.c b/keyboards/keychron/k8_pro/k8_pro.c index 58b1e70779..19852b0c06 100644 --- a/keyboards/keychron/k8_pro/k8_pro.c +++ b/keyboards/keychron/k8_pro/k8_pro.c @@ -67,7 +67,11 @@ bool dip_switch_update_kb(uint8_t index, bool active) { return true; } -bool process_record_user(uint16_t keycode, keyrecord_t *record) { +#ifdef BLUETOOTH_ENABLE +bool process_record_kb_bt(uint16_t keycode, keyrecord_t *record) { +#else +bool process_record_kb(uint16_t keycode, keyrecord_t *record) { +#endif static uint8_t host_idx = 0; switch (keycode) { @@ -136,7 +140,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { return true; } -void keyboard_post_init_user(void) { +void keyboard_post_init_kb(void) { dip_switch_read(true); #ifdef BLUETOOTH_ENABLE @@ -158,9 +162,11 @@ void keyboard_post_init_user(void) { #ifdef BLUETOOTH_ENABLE writePin(H3, HOST_LED_PIN_ON_STATE); # endif + + keyboard_post_init_user(); } -void matrix_scan_user(void) { +void matrix_scan_kb(void) { if (power_on_indicator_timer_buffer) { if (sync_timer_elapsed32(power_on_indicator_timer_buffer) > POWER_ON_LED_DURATION) { power_on_indicator_timer_buffer = 0; @@ -184,6 +190,7 @@ void matrix_scan_user(void) { #ifdef FACTORY_RESET_TASK FACTORY_RESET_TASK(); # endif + matrix_scan_user(); } #ifdef BLUETOOTH_ENABLE |