Skip to content

Commit

Permalink
Add Keychron K8 Pro data and personal layout
Browse files Browse the repository at this point in the history
  • Loading branch information
eldios committed Oct 5, 2022
1 parent 0127e62 commit 2c611bd
Show file tree
Hide file tree
Showing 60 changed files with 6,035 additions and 0 deletions.
132 changes: 132 additions & 0 deletions keyboards/keychron/bluetooth/bat_level_animation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@

#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 = 0;
static uint32_t bat_lvl_ani_timer_buffer = 0;
static uint8_t bat_percentage;
static uint8_t cur_percentage;
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
uint8_t bat_lvl_led_list[10] = BAT_LEVEL_LED_LIST;

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
uint8_t bat_lvl_led_list[10] = BAT_LEVEL_LED_LIST;

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();
}
}
23 changes: 23 additions & 0 deletions keyboards/keychron/bluetooth/bat_level_animation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright 2022 @ lokher (https://www.keychron.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

void bat_level_animiation_start(uint8_t percentage);
void bat_level_animiation_stop(void);
bool bat_level_animiation_actived(void);
void bat_level_animiation_indicate(void);
void bat_level_animiation_task(void);
107 changes: 107 additions & 0 deletions keyboards/keychron/bluetooth/battery.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* Copyright 2022 @ lokher (https://www.keychron.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "quantum.h"
#include "bluetooth.h"
#include "battery.h"
#include "transport.h"
#include "ckbt51.h"
#include "lpm.h"

#define BATTERY_EMPTY_COUNT 10
#define CRITICAL_LOW_COUNT 20

static uint32_t bat_monitor_timer_buffer = 0;
static uint16_t voltage = FULL_VOLTAGE_VALUE;
static uint8_t bat_empty = 0;
static uint8_t critical_low = 0;
static uint8_t bat_state;

void battery_init(void) {
bat_monitor_timer_buffer = 0;
bat_state = BAT_NOT_CHARGING;
}
__attribute__((weak)) void battery_measure(void) {}

/* Calculate the voltage */
__attribute__((weak)) void battery_calculate_voltage(uint16_t value) {}

void battery_set_voltage(uint16_t value) {
voltage = value;
}

uint16_t battery_get_voltage(void) {
return voltage;
}

uint8_t battery_get_percentage(void) {
if (voltage > FULL_VOLTAGE_VALUE) return 100;

if (voltage > EMPTY_VOLTAGE_VALUE) {
return ((uint32_t)voltage - EMPTY_VOLTAGE_VALUE) * 80 / (FULL_VOLTAGE_VALUE - EMPTY_VOLTAGE_VALUE) + 20;
}

if (voltage > SHUTDOWN_VOLTAGE_VALUE) {
return ((uint32_t)voltage - SHUTDOWN_VOLTAGE_VALUE) * 20 / (EMPTY_VOLTAGE_VALUE - SHUTDOWN_VOLTAGE_VALUE);
} else
return 0;
}

bool battery_is_empty(void) {
return bat_empty > BATTERY_EMPTY_COUNT;
}

bool battery_is_critical_low(void) {
return critical_low > CRITICAL_LOW_COUNT;
}

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);
}
} else if (bat_empty <= BATTERY_EMPTY_COUNT) {
bat_empty = BATTERY_EMPTY_COUNT;
}
}

void battery_check_critical_low(void) {
if (voltage < SHUTDOWN_VOLTAGE_VALUE) {
if (critical_low <= CRITICAL_LOW_COUNT) {
if (++critical_low > CRITICAL_LOW_COUNT) bluetooth_low_battery_shutdown();
}
} else if (critical_low <= CRITICAL_LOW_COUNT) {
critical_low = 0;
}
}

void battery_task(void) {
if (get_transport() == TRANSPORT_BLUETOOTH && bluetooth_get_state() == BLUETOOTH_CONNECTED) {
if (sync_timer_elapsed32(bat_monitor_timer_buffer) > VOLTAGE_MEASURE_INTERVAL) {
battery_check_empty();
battery_check_critical_low();

bat_monitor_timer_buffer = sync_timer_read32();
battery_measure();
}
}

if ((bat_empty || critical_low) && usb_power_connected()) {
bat_empty = false;
critical_low = false;
indicator_battery_low_enable(false);
}
}
51 changes: 51 additions & 0 deletions keyboards/keychron/bluetooth/battery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright 2022 @ lokher (https://www.keychron.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

enum {
BAT_NOT_CHARGING = 0,
BAT_CHARGING,
BAT_CHARGING_FINISHED,
};

#ifndef FULL_VOLTAGE_VALUE
# define FULL_VOLTAGE_VALUE 4100
#endif

#ifndef EMPTY_VOLTAGE_VALUE
# define EMPTY_VOLTAGE_VALUE 3500
#endif

#ifndef SHUTDOWN_VOLTAGE_VALUE
# define SHUTDOWN_VOLTAGE_VALUE 3300
#endif

#ifndef VOLTAGE_MEASURE_INTERVAL
# define VOLTAGE_MEASURE_INTERVAL 3000
#endif

void battery_init(void);
void battery_measure(void);
void battery_calculte_voltage(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);

void battery_task(void);
Loading

0 comments on commit 2c611bd

Please sign in to comment.