summaryrefslogtreecommitdiffstats
path: root/keyboards/keychron/bluetooth/bat_level_animation.c
diff options
context:
space:
mode:
authorlokher <lokher@gmail.com>2022-08-03 16:53:42 +0800
committerlokher <lokher@gmail.com>2022-08-05 14:45:45 +0800
commit968655c74cc13a1a63320ef795fe436449708429 (patch)
treeb37716e10d16dcf760ba608dc66051cdb47eb31b /keyboards/keychron/bluetooth/bat_level_animation.c
parent068bfc49de127c1f1305ee7680f51425ca9cd1f1 (diff)
Add K8 pro
Diffstat (limited to 'keyboards/keychron/bluetooth/bat_level_animation.c')
-rw-r--r--keyboards/keychron/bluetooth/bat_level_animation.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/keyboards/keychron/bluetooth/bat_level_animation.c b/keyboards/keychron/bluetooth/bat_level_animation.c
new file mode 100644
index 0000000000..94a21f11e1
--- /dev/null
+++ b/keyboards/keychron/bluetooth/bat_level_animation.c
@@ -0,0 +1,128 @@
+
+#include "quantum.h"
+#include "bluetooth.h"
+#include "indicator.h"
+#include "lpm.h"
+#if defined(PROTOCOL_CHIBIOS)
+# include <usb_main.h>
+#elif if defined(PROTOCOL_LUFA)
+# include "lufa.h"
+#endif
+
+#ifndef BAT_LEVEL_GROWING_INTERVAL
+# define BAT_LEVEL_GROWING_INTERVAL 150
+#endif
+
+#ifndef BAT_LEVEL_ON_INTERVAL
+# define BAT_LEVEL_ON_INTERVAL 3000
+#endif
+
+enum {
+ BAT_LVL_ANI_NONE,
+ BAT_LVL_ANI_GROWING,
+ BAT_LVL_ANI_BLINK_OFF,
+ BAT_LVL_ANI_BLINK_ON,
+};
+
+static uint8_t animation_state;
+static uint32_t bat_lvl_ani_timer_buffer;
+static uint8_t bat_percentage;
+static uint8_t cur_percentage;
+static uint8_t bat_lvl_led_list[10] = BAT_LEVEL_LED_LIST;
+static uint32_t time_interval;
+#ifdef RGB_MATRIX_ENABLE
+static uint8_t r, g, b;
+#endif
+
+extern indicator_config_t indicator_config;
+extern backlight_state_t original_backlight_state;
+
+void bat_level_animiation_start(uint8_t percentage) {
+ /* Turn on backlight mode for indicator */
+ indicator_enable();
+
+ animation_state = BAT_LVL_ANI_GROWING;
+ bat_percentage = percentage;
+ bat_lvl_ani_timer_buffer = sync_timer_read32();
+ cur_percentage = 0;
+ time_interval = BAT_LEVEL_GROWING_INTERVAL;
+#ifdef RGB_MATRIX_ENABLE
+ r = g = b = 255;
+#endif
+}
+
+void bat_level_animiation_stop(void) {
+ animation_state = BAT_LVL_ANI_NONE;
+}
+
+bool bat_level_animiation_actived(void) {
+ return animation_state;
+}
+
+void bat_level_animiation_indicate(void) {
+#ifdef LED_MATRIX_ENABLE
+ for (uint8_t i = 0; i <= DRIVER_LED_TOTAL; i++) {
+ led_matrix_set_value(i, 0);
+ }
+
+ if (animation_state == BAT_LVL_ANI_GROWING || animation_state == BAT_LVL_ANI_BLINK_ON)
+ for (uint8_t i = 0; i < cur_percentage / 10; i++)
+ led_matrix_set_value(bat_lvl_led_list[i], 255);
+#endif
+
+#ifdef RGB_MATRIX_ENABLE
+ for (uint8_t i = 0; i <= DRIVER_LED_TOTAL; i++) {
+ rgb_matrix_set_color(i, 0, 0, 0);
+ }
+
+ if (animation_state == BAT_LVL_ANI_GROWING || animation_state == BAT_LVL_ANI_BLINK_ON)
+ for (uint8_t i = 0; i < cur_percentage / 10; i++) {
+ rgb_matrix_set_color(bat_lvl_led_list[i], r, g, b);
+ }
+#endif
+}
+
+void bat_level_animiation_update(void) {
+ switch (animation_state) {
+ case BAT_LVL_ANI_GROWING:
+ if (cur_percentage < bat_percentage)
+ cur_percentage += 10;
+ else {
+ if (cur_percentage == 0) cur_percentage = 10;
+ animation_state = BAT_LVL_ANI_BLINK_OFF;
+ }
+ break;
+
+ case BAT_LVL_ANI_BLINK_OFF:
+#ifdef RGB_MATRIX_ENABLE
+ if (bat_percentage < 30) {
+ r = 255;
+ b = g = 0;
+ } else {
+ r = b = 0;
+ g = 255;
+ }
+#endif
+ time_interval = BAT_LEVEL_ON_INTERVAL;
+ animation_state = BAT_LVL_ANI_BLINK_ON;
+ break;
+
+ case BAT_LVL_ANI_BLINK_ON:
+ animation_state = BAT_LVL_ANI_NONE;
+ if (indicator_config.value == 0 && indicator_is_backlit_enabled_eeprom()) {
+ indicator_disable();
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ bat_lvl_ani_timer_buffer = sync_timer_read32();
+}
+
+void bat_level_animiation_task(void) {
+ if (animation_state && sync_timer_elapsed32(bat_lvl_ani_timer_buffer) > time_interval) {
+ bat_level_animiation_update();
+ }
+}