Accidentally deleted too much, restoring...
Some checks failed
Automatic Approve / automatic_approve (push) Has been cancelled
Some checks failed
Automatic Approve / automatic_approve (push) Has been cancelled
This commit is contained in:
parent
d6581b5a38
commit
f513c31058
142
keyboards/keychron/bluetooth/bat_level_animation.c
Normal file
142
keyboards/keychron/bluetooth/bat_level_animation.c
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
|
||||||
|
#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
|
||||||
|
#include "eeprom.h"
|
||||||
|
|
||||||
|
#ifndef BAT_LEVEL_GROWING_INTERVAL
|
||||||
|
# define BAT_LEVEL_GROWING_INTERVAL 150
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BAT_LEVEL_ON_INTERVAL
|
||||||
|
# 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,
|
||||||
|
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 <= LED_MATRIX_LED_COUNT; 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 <= RGB_MATRIX_LED_COUNT; 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;
|
||||||
|
indicator_eeconfig_reload();
|
||||||
|
if (indicator_config.value == 0 && !LED_DRIVER_IS_ENABLED()) {
|
||||||
|
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
keyboards/keychron/bluetooth/bat_level_animation.h
Normal file
23
keyboards/keychron/bluetooth/bat_level_animation.h
Normal file
@ -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);
|
||||||
140
keyboards/keychron/bluetooth/battery.c
Normal file
140
keyboards/keychron/bluetooth/battery.c
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
/* 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"
|
||||||
|
#include "indicator.h"
|
||||||
|
#include "rtc_timer.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;
|
||||||
|
static uint8_t power_on_sample = 0;
|
||||||
|
|
||||||
|
void battery_init(void) {
|
||||||
|
bat_state = BAT_NOT_CHARGING;
|
||||||
|
}
|
||||||
|
__attribute__((weak)) void battery_measure(void) {
|
||||||
|
ckbt51_read_state_reg(0x05, 0x02);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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) {
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 ((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 = 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;
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
indicator_battery_low_enable(false);
|
||||||
|
#endif
|
||||||
|
#if defined(LOW_BAT_IND_INDEX)
|
||||||
|
indicator_battery_low_backlit_enable(false);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
60
keyboards/keychron/bluetooth/battery.h
Normal file
60
keyboards/keychron/bluetooth/battery.h
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
#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);
|
||||||
|
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);
|
||||||
|
|
||||||
|
void battery_task(void);
|
||||||
493
keyboards/keychron/bluetooth/bluetooth.c
Normal file
493
keyboards/keychron/bluetooth/bluetooth.c
Normal file
@ -0,0 +1,493 @@
|
|||||||
|
/* 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 "action.h"
|
||||||
|
#include "quantum.h"
|
||||||
|
#include "bluetooth.h"
|
||||||
|
#include "report_buffer.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#include "battery.h"
|
||||||
|
#include "indicator.h"
|
||||||
|
#include "transport.h"
|
||||||
|
#include "rtc_timer.h"
|
||||||
|
|
||||||
|
extern uint8_t pairing_indication;
|
||||||
|
extern host_driver_t chibios_driver;
|
||||||
|
extern report_buffer_t kb_rpt;
|
||||||
|
extern uint32_t retry_time_buffer;
|
||||||
|
extern uint8_t retry;
|
||||||
|
|
||||||
|
#ifdef NKRO_ENABLE
|
||||||
|
extern nkro_t nkro;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static uint8_t host_index = 0;
|
||||||
|
static uint8_t led_state = 0;
|
||||||
|
|
||||||
|
extern bluetooth_transport_t bluetooth_transport;
|
||||||
|
static bluetooth_state_t bt_state = BLUETOOTH_RESET;
|
||||||
|
static bool pincodeEntry = false;
|
||||||
|
uint8_t bluetooth_report_protocol = true;
|
||||||
|
|
||||||
|
/* declarations */
|
||||||
|
uint8_t bluetooth_keyboard_leds(void);
|
||||||
|
void bluetooth_send_keyboard(report_keyboard_t *report);
|
||||||
|
void bluetooth_send_nkro(report_nkro_t *report);
|
||||||
|
void bluetooth_send_mouse(report_mouse_t *report);
|
||||||
|
void bluetooth_send_extra(report_extra_t *report);
|
||||||
|
|
||||||
|
/* host struct */
|
||||||
|
host_driver_t bluetooth_driver = {bluetooth_keyboard_leds, bluetooth_send_keyboard, bluetooth_send_nkro, bluetooth_send_mouse, bluetooth_send_extra};
|
||||||
|
|
||||||
|
#define BLUETOOTH_EVENT_QUEUE_SIZE 16
|
||||||
|
bluetooth_event_t bt_event_queue[BLUETOOTH_EVENT_QUEUE_SIZE];
|
||||||
|
uint8_t bt_event_queue_head;
|
||||||
|
uint8_t bt_event_queue_tail;
|
||||||
|
|
||||||
|
void bluetooth_bt_event_queue_init(void) {
|
||||||
|
// Initialise the event queue
|
||||||
|
memset(&bt_event_queue, 0, sizeof(bt_event_queue));
|
||||||
|
bt_event_queue_head = 0;
|
||||||
|
bt_event_queue_tail = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bluetooth_event_queue_enqueue(bluetooth_event_t event) {
|
||||||
|
uint8_t next = (bt_event_queue_head + 1) % BLUETOOTH_EVENT_QUEUE_SIZE;
|
||||||
|
if (next == bt_event_queue_tail) {
|
||||||
|
/* Override the first report */
|
||||||
|
bt_event_queue_tail = (bt_event_queue_tail + 1) % BLUETOOTH_EVENT_QUEUE_SIZE;
|
||||||
|
}
|
||||||
|
bt_event_queue[bt_event_queue_head] = event;
|
||||||
|
bt_event_queue_head = next;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool bluetooth_event_queue_dequeue(bluetooth_event_t *event) {
|
||||||
|
if (bt_event_queue_head == bt_event_queue_tail) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*event = bt_event_queue[bt_event_queue_tail];
|
||||||
|
bt_event_queue_tail = (bt_event_queue_tail + 1) % BLUETOOTH_EVENT_QUEUE_SIZE;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Bluetooth init.
|
||||||
|
*/
|
||||||
|
void bluetooth_init(void) {
|
||||||
|
bt_state = BLUETOOTH_INITIALIZED;
|
||||||
|
|
||||||
|
bluetooth_bt_event_queue_init();
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
report_buffer_init();
|
||||||
|
#endif
|
||||||
|
indicator_init();
|
||||||
|
#ifdef BLUETOOTH_INT_INPUT_PIN
|
||||||
|
setPinInputHigh(BLUETOOTH_INT_INPUT_PIN);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
lpm_init();
|
||||||
|
rtc_timer_init();
|
||||||
|
|
||||||
|
#ifdef BLUETOOTH_NKRO_ENABLE
|
||||||
|
keymap_config.raw = eeconfig_read_keymap();
|
||||||
|
nkro.bluetooth = keymap_config.nkro;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Bluetooth trasponrt init. Bluetooth module driver shall use this function to register a callback
|
||||||
|
* to its implementation.
|
||||||
|
*/
|
||||||
|
void bluetooth_set_transport(bluetooth_transport_t *transport) {
|
||||||
|
if (transport) memcpy(&bluetooth_transport, transport, sizeof(bluetooth_transport_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enter pairing with current host index
|
||||||
|
*/
|
||||||
|
void bluetooth_pairing(void) {
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
bluetooth_pairing_ex(0, NULL);
|
||||||
|
bt_state = BLUETOOTH_PARING;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enter pairing with specified host index and param
|
||||||
|
*/
|
||||||
|
void bluetooth_pairing_ex(uint8_t host_idx, void *param) {
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
if (bluetooth_transport.pairing_ex) bluetooth_transport.pairing_ex(host_idx, param);
|
||||||
|
bt_state = BLUETOOTH_PARING;
|
||||||
|
|
||||||
|
host_index = host_idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initiate connection request to paired host
|
||||||
|
*/
|
||||||
|
void bluetooth_connect(void) {
|
||||||
|
/* Work around empty report after wakeup, which leads to reconneect/disconnected loop */
|
||||||
|
if (battery_is_critical_low() || sync_timer_read32() == 0) return;
|
||||||
|
|
||||||
|
bluetooth_transport.connect_ex(0, 0);
|
||||||
|
bt_state = BLUETOOTH_RECONNECTING;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initiate connection request to paired host with argument
|
||||||
|
*/
|
||||||
|
void bluetooth_connect_ex(uint8_t host_idx, uint16_t timeout) {
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
if (host_idx != 0) {
|
||||||
|
if (host_index == host_idx && bt_state == BLUETOOTH_CONNECTED) return;
|
||||||
|
host_index = host_idx;
|
||||||
|
led_state = 0;
|
||||||
|
}
|
||||||
|
bluetooth_transport.connect_ex(host_idx, timeout);
|
||||||
|
bt_state = BLUETOOTH_RECONNECTING;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initiate a disconnection */
|
||||||
|
void bluetooth_disconnect(void) {
|
||||||
|
if (bluetooth_transport.disconnect) bluetooth_transport.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Called when the BT device is reset. */
|
||||||
|
static void bluetooth_enter_reset(uint8_t reason) {
|
||||||
|
bt_state = BLUETOOTH_RESET;
|
||||||
|
bluetooth_enter_reset_kb(reason);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Enters discoverable state. Upon entering this state we perform the following actions:
|
||||||
|
* - change state to BLUETOOTH_PARING
|
||||||
|
* - set pairing indication
|
||||||
|
*/
|
||||||
|
static void bluetooth_enter_discoverable(uint8_t host_idx) {
|
||||||
|
bt_state = BLUETOOTH_PARING;
|
||||||
|
indicator_set(bt_state, host_idx);
|
||||||
|
bluetooth_enter_discoverable_kb(host_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enters reconnecting state. Upon entering this state we perform the following actions:
|
||||||
|
* - change state to RECONNECTING
|
||||||
|
* - set reconnect indication
|
||||||
|
*/
|
||||||
|
static void bluetooth_enter_reconnecting(uint8_t host_idx) {
|
||||||
|
bt_state = BLUETOOTH_RECONNECTING;
|
||||||
|
indicator_set(bt_state, host_idx);
|
||||||
|
bluetooth_enter_reconnecting_kb(host_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Enters connected state. Upon entering this state we perform the following actions:
|
||||||
|
* - change state to CONNECTED
|
||||||
|
* - set connected indication
|
||||||
|
* - enable bluetooth NKRO is support
|
||||||
|
*/
|
||||||
|
static void bluetooth_enter_connected(uint8_t host_idx) {
|
||||||
|
bt_state = BLUETOOTH_CONNECTED;
|
||||||
|
indicator_set(bt_state, host_idx);
|
||||||
|
host_index = host_idx;
|
||||||
|
|
||||||
|
clear_keyboard();
|
||||||
|
|
||||||
|
/* Enable NKRO since it may be disabled in pin code entry */
|
||||||
|
#if defined(NKRO_ENABLE) && defined(BLUETOOTH_NKRO_ENABLE)
|
||||||
|
keymap_config.nkro = nkro.bluetooth;
|
||||||
|
#else
|
||||||
|
keymap_config.nkro = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bluetooth_enter_connected_kb(host_idx);
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
if (battery_is_empty()) {
|
||||||
|
indicator_battery_low_enable(true);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Enters disconnected state. Upon entering this state we perform the following actions:
|
||||||
|
* - change state to DISCONNECTED
|
||||||
|
* - set disconnected indication
|
||||||
|
*/
|
||||||
|
static void bluetooth_enter_disconnected(uint8_t host_idx) {
|
||||||
|
uint8_t previous_state = bt_state;
|
||||||
|
bt_state = BLUETOOTH_DISCONNECTED;
|
||||||
|
|
||||||
|
if (previous_state == BLUETOOTH_CONNECTED) {
|
||||||
|
lpm_timer_reset();
|
||||||
|
indicator_set(BLUETOOTH_SUSPEND, host_idx);
|
||||||
|
} else
|
||||||
|
indicator_set(bt_state, host_idx);
|
||||||
|
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
report_buffer_init();
|
||||||
|
#endif
|
||||||
|
retry = 0;
|
||||||
|
bluetooth_enter_disconnected_kb(host_idx);
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
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)
|
||||||
|
keymap_config.nkro = FALSE;
|
||||||
|
#endif
|
||||||
|
pincodeEntry = true;
|
||||||
|
bluetooth_enter_pin_code_entry_kb();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Exit pin code entry state. */
|
||||||
|
static void bluetooth_exit_pin_code_entry(void) {
|
||||||
|
#if defined(NKRO_ENABLE)
|
||||||
|
keymap_config.nkro = true;
|
||||||
|
#endif
|
||||||
|
pincodeEntry = false;
|
||||||
|
bluetooth_exit_pin_code_entry_kb();
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((weak)) void bluetooth_enter_reset_kb(uint8_t reason){};
|
||||||
|
__attribute__((weak)) void bluetooth_enter_discoverable_kb(uint8_t host_idx){};
|
||||||
|
__attribute__((weak)) void bluetooth_enter_reconnecting_kb(uint8_t host_idx){};
|
||||||
|
__attribute__((weak)) void bluetooth_enter_connected_kb(uint8_t host_idx){};
|
||||||
|
__attribute__((weak)) void bluetooth_enter_disconnected_kb(uint8_t host_idx){};
|
||||||
|
__attribute__((weak)) void bluetooth_enter_pin_code_entry_kb(void) {}
|
||||||
|
__attribute__((weak)) void bluetooth_exit_pin_code_entry_kb(void){};
|
||||||
|
|
||||||
|
/* */
|
||||||
|
static void bluetooth_hid_set_protocol(bool report_protocol) {
|
||||||
|
bluetooth_report_protocol = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t bluetooth_keyboard_leds(void) {
|
||||||
|
if (bt_state == BLUETOOTH_CONNECTED) {
|
||||||
|
return led_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern keymap_config_t keymap_config;
|
||||||
|
|
||||||
|
void bluetooth_send_keyboard(report_keyboard_t *report) {
|
||||||
|
if (bt_state == BLUETOOTH_PARING && !pincodeEntry) return;
|
||||||
|
|
||||||
|
if (bt_state == BLUETOOTH_CONNECTED || (bt_state == BLUETOOTH_PARING && pincodeEntry)) {
|
||||||
|
if (bluetooth_transport.send_keyboard) {
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
bool firstBuffer = false;
|
||||||
|
if (report_buffer_is_empty() && report_buffer_next_inverval() && report_buffer_get_retry() == 0) {
|
||||||
|
firstBuffer = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
report_buffer_t report_buffer;
|
||||||
|
report_buffer.type = REPORT_TYPE_KB;
|
||||||
|
memcpy(&report_buffer.keyboard, report, sizeof(report_keyboard_t));
|
||||||
|
report_buffer_enqueue(&report_buffer);
|
||||||
|
|
||||||
|
if (firstBuffer) {
|
||||||
|
report_buffer_set_retry(0);
|
||||||
|
report_buffer_task();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
bluetooth_transport.send_keyboard(&report->nkro.mods);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} else if (bt_state != BLUETOOTH_RESET) {
|
||||||
|
bluetooth_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void bluetooth_send_nkro(report_nkro_t *report) {
|
||||||
|
if (bt_state == BLUETOOTH_PARING && !pincodeEntry) return;
|
||||||
|
|
||||||
|
if (bt_state == BLUETOOTH_CONNECTED || (bt_state == BLUETOOTH_PARING && pincodeEntry)) {
|
||||||
|
if (bluetooth_transport.send_keyboard) {
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
if (report_buffer_is_empty() && report_buffer_next_inverval()) {
|
||||||
|
bluetooth_transport.send_keyboard(&report->mods);
|
||||||
|
report_buffer_update_timer();
|
||||||
|
} else {
|
||||||
|
report_buffer_t report_buffer;
|
||||||
|
report_buffer.type = REPORT_TYPE_NKRO;
|
||||||
|
memcpy(&report_buffer.nkro, report, sizeof(report_nkro_t));
|
||||||
|
report_buffer_enqueue(&report_buffer);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
bluetooth_transport.send_nkro(&report->mods);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} else if (bt_state != BLUETOOTH_RESET) {
|
||||||
|
bluetooth_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bluetooth_send_mouse(report_mouse_t *report) {
|
||||||
|
if (bt_state == BLUETOOTH_CONNECTED) {
|
||||||
|
if (bluetooth_transport.send_mouse) bluetooth_transport.send_mouse((uint8_t *)report);
|
||||||
|
} else if (bt_state != BLUETOOTH_RESET) {
|
||||||
|
bluetooth_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bluetooth_send_system(uint16_t data) {
|
||||||
|
if (bt_state == BLUETOOTH_CONNECTED) {
|
||||||
|
if (bluetooth_transport.send_system) bluetooth_transport.send_system(data);
|
||||||
|
} else if (bt_state != BLUETOOTH_RESET) {
|
||||||
|
bluetooth_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bluetooth_send_consumer(uint16_t data) {
|
||||||
|
if (bt_state == BLUETOOTH_CONNECTED) {
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
if (report_buffer_is_empty() && report_buffer_next_inverval()) {
|
||||||
|
if (bluetooth_transport.send_consumer) bluetooth_transport.send_consumer(data);
|
||||||
|
report_buffer_update_timer();
|
||||||
|
} else {
|
||||||
|
report_buffer_t report_buffer;
|
||||||
|
report_buffer.type = REPORT_TYPE_CONSUMER;
|
||||||
|
report_buffer.consumer = data;
|
||||||
|
report_buffer_enqueue(&report_buffer);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if (bluetooth_transport.send_consumer) bluetooth_transport.send_consumer(data);
|
||||||
|
#endif
|
||||||
|
} else if (bt_state != BLUETOOTH_RESET) {
|
||||||
|
bluetooth_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bluetooth_send_extra(report_extra_t *report) {
|
||||||
|
if (report->report_id == REPORT_ID_SYSTEM) {
|
||||||
|
bluetooth_send_system(report->usage);
|
||||||
|
} else if (report->report_id == REPORT_ID_CONSUMER) {
|
||||||
|
bluetooth_send_consumer(report->usage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bluetooth_low_battery_shutdown(void) {
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
indicator_battery_low_enable(false);
|
||||||
|
#endif
|
||||||
|
#if defined(LOW_BAT_IND_INDEX)
|
||||||
|
indicator_battery_low_backlit_enable(false);
|
||||||
|
#endif
|
||||||
|
clear_keyboard();
|
||||||
|
send_keyboard_report();
|
||||||
|
wait_ms(50);
|
||||||
|
bluetooth_disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
void bluetooth_event_queue_task(void) {
|
||||||
|
bluetooth_event_t event;
|
||||||
|
while (bluetooth_event_queue_dequeue(&event)) {
|
||||||
|
switch (event.evt_type) {
|
||||||
|
case EVT_RESET:
|
||||||
|
bluetooth_enter_reset(event.params.reason);
|
||||||
|
break;
|
||||||
|
case EVT_CONNECTED:
|
||||||
|
bluetooth_enter_connected(event.params.hostIndex);
|
||||||
|
break;
|
||||||
|
case EVT_DISCOVERABLE:
|
||||||
|
bluetooth_enter_discoverable(event.params.hostIndex);
|
||||||
|
break;
|
||||||
|
case EVT_RECONNECTING:
|
||||||
|
bluetooth_enter_reconnecting(event.params.hostIndex);
|
||||||
|
break;
|
||||||
|
case EVT_DISCONNECTED:
|
||||||
|
led_state = 0;
|
||||||
|
bluetooth_enter_disconnected(event.params.hostIndex);
|
||||||
|
break;
|
||||||
|
case EVT_BT_PINCODE_ENTRY:
|
||||||
|
bluetooth_enter_pin_code_entry();
|
||||||
|
break;
|
||||||
|
case EVT_EXIT_BT_PINCODE_ENTRY:
|
||||||
|
bluetooth_exit_pin_code_entry();
|
||||||
|
break;
|
||||||
|
case EVT_HID_INDICATOR:
|
||||||
|
led_state = event.params.led;
|
||||||
|
break;
|
||||||
|
case EVT_HID_SET_PROTOCOL:
|
||||||
|
bluetooth_hid_set_protocol(event.params.protocol);
|
||||||
|
break;
|
||||||
|
case EVT_CONECTION_INTERVAL:
|
||||||
|
report_buffer_set_inverval(event.params.interval);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bluetooth_task(void) {
|
||||||
|
bluetooth_transport.task();
|
||||||
|
bluetooth_event_queue_task();
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
report_buffer_task();
|
||||||
|
#endif
|
||||||
|
indicator_task();
|
||||||
|
battery_task();
|
||||||
|
lpm_task();
|
||||||
|
}
|
||||||
|
|
||||||
|
void send_string_task(void) {
|
||||||
|
if (get_transport() == TRANSPORT_BLUETOOTH && bluetooth_get_state()== BLUETOOTH_CONNECTED) {
|
||||||
|
bluetooth_transport.task();
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
report_buffer_task();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (!process_record_user(keycode, record)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
indicator_battery_low_enable(true);
|
||||||
|
# endif
|
||||||
|
# if defined(LOW_BAT_IND_INDEX)
|
||||||
|
indicator_battery_low_backlit_enable(true);
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return process_record_kb_bt(keycode, record);
|
||||||
|
// return process_record_user(keycode, record);
|
||||||
|
}
|
||||||
89
keyboards/keychron/bluetooth/bluetooth.h
Normal file
89
keyboards/keychron/bluetooth/bluetooth.h
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
#include "bluetooth_event_type.h"
|
||||||
|
#include "action.h"
|
||||||
|
|
||||||
|
/* Low power mode */
|
||||||
|
#ifndef LOW_POWER_MODE
|
||||||
|
# define LOW_POWER_MODE PM_STOP1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Wake pin used for blueooth module/controller to wake up MCU in low power mode*/
|
||||||
|
#ifndef BLUETOOTH_INT_INPUT_PIN
|
||||||
|
# define WAKE_PIN A5
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Type of an enumeration of the possible BT state.*/
|
||||||
|
typedef enum {
|
||||||
|
BLUETOOTH_RESET,
|
||||||
|
BLUETOOTH_INITIALIZED, // 1
|
||||||
|
BLUETOOTH_DISCONNECTED, // 2
|
||||||
|
BLUETOOTH_CONNECTED, // 3
|
||||||
|
BLUETOOTH_PARING, // 4
|
||||||
|
BLUETOOTH_RECONNECTING, // 5
|
||||||
|
BLUETOOTH_SUSPEND
|
||||||
|
} bluetooth_state_t;
|
||||||
|
|
||||||
|
extern event_listener_t bt_driver;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void (*init)(bool);
|
||||||
|
void (*connect_ex)(uint8_t, uint16_t);
|
||||||
|
void (*pairing_ex)(uint8_t, void *);
|
||||||
|
void (*disconnect)(void);
|
||||||
|
void (*send_keyboard)(uint8_t *);
|
||||||
|
void (*send_nkro)(uint8_t *);
|
||||||
|
void (*send_consumer)(uint16_t);
|
||||||
|
void (*send_system)(uint16_t);
|
||||||
|
void (*send_mouse)(uint8_t *);
|
||||||
|
void (*task)(void);
|
||||||
|
} bluetooth_transport_t;
|
||||||
|
|
||||||
|
void bluetooth_init(void);
|
||||||
|
void bluetooth_set_transport(bluetooth_transport_t *transport);
|
||||||
|
void bluetooth_task(void);
|
||||||
|
|
||||||
|
bool bluetooth_event_queue_enqueue(bluetooth_event_t event);
|
||||||
|
|
||||||
|
void bluetooth_connect(void);
|
||||||
|
void bluetooth_connect_ex(uint8_t host_idx, uint16_t timeout);
|
||||||
|
void bluetooth_disconnect(void);
|
||||||
|
|
||||||
|
void bluetooth_pairing(void);
|
||||||
|
void bluetooth_pairing_ex(uint8_t host_idx, void *param);
|
||||||
|
bool bluetooth_is_activated(void);
|
||||||
|
|
||||||
|
void bluetooth_enter_reset_kb(uint8_t reason);
|
||||||
|
void bluetooth_enter_discoverable_kb(uint8_t host_idx);
|
||||||
|
void bluetooth_enter_reconnecting_kb(uint8_t host_idx);
|
||||||
|
void bluetooth_enter_connected_kb(uint8_t host_idx);
|
||||||
|
void bluetooth_enter_disconnected_kb(uint8_t host_idx);
|
||||||
|
void bluetooth_enter_pin_code_entry_kb(void);
|
||||||
|
void bluetooth_exit_pin_code_entry_kb(void);
|
||||||
|
|
||||||
|
void bluetooth_task(void);
|
||||||
|
void bluetooth_pre_task(void);
|
||||||
|
void bluetooth_post_task(void);
|
||||||
|
void send_string_task(void);
|
||||||
|
|
||||||
|
bluetooth_state_t bluetooth_get_state(void);
|
||||||
|
|
||||||
|
void bluetooth_low_battery_shutdown(void);
|
||||||
|
|
||||||
|
bool process_record_kb_bt(uint16_t keycode, keyrecord_t *record);
|
||||||
23
keyboards/keychron/bluetooth/bluetooth.mk
Normal file
23
keyboards/keychron/bluetooth/bluetooth.mk
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
OPT_DEFS += -DKC_BLUETOOTH_ENABLE
|
||||||
|
|
||||||
|
BLUETOOTH_DIR = bluetooth
|
||||||
|
SRC += \
|
||||||
|
$(BLUETOOTH_DIR)/bluetooth.c \
|
||||||
|
$(BLUETOOTH_DIR)/report_buffer.c \
|
||||||
|
$(BLUETOOTH_DIR)/ckbt51.c \
|
||||||
|
$(BLUETOOTH_DIR)/indicator.c \
|
||||||
|
$(BLUETOOTH_DIR)/bluetooth_main.c \
|
||||||
|
$(BLUETOOTH_DIR)/transport.c \
|
||||||
|
$(BLUETOOTH_DIR)/lpm.c \
|
||||||
|
$(BLUETOOTH_DIR)/lpm_stm32l432.c \
|
||||||
|
$(BLUETOOTH_DIR)/battery.c \
|
||||||
|
$(BLUETOOTH_DIR)/factory_test.c \
|
||||||
|
$(BLUETOOTH_DIR)/bat_level_animation.c \
|
||||||
|
$(BLUETOOTH_DIR)/rtc_timer.c
|
||||||
|
|
||||||
|
VPATH += $(TOP_DIR)/keyboards/keychron/$(BLUETOOTH_DIR)
|
||||||
|
|
||||||
|
# Work around RTC clock issue without touching chibios, refer to the link for this bug
|
||||||
|
# https://forum.chibios.org/viewtopic.php?f=35&t=6197
|
||||||
|
OPT_DEFS += -DRCC_APBENR1_RTCAPBEN
|
||||||
38
keyboards/keychron/bluetooth/bluetooth_config.h
Normal file
38
keyboards/keychron/bluetooth/bluetooth_config.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BLUETOOTH_CONFIG_H
|
||||||
|
#define BLUETOOTH_CONFIG_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
//
|
||||||
|
#ifndef HOST_DEVICES_COUNT
|
||||||
|
# define HOST_DEVICES_COUNT 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Uint: Second
|
||||||
|
#ifndef DISCONNECTED_BACKLIGHT_OFF_DELAY_TIME
|
||||||
|
# define DISCONNECTED_BACKLIGHT_OFF_DELAY_TIME 40
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Uint: Second, the timer restarts on key activities.
|
||||||
|
#ifndef CONNECTED_BACKLIGHT_OFF_DELAY_TIME
|
||||||
|
# define CONNECTED_BACKLIGHT_OFF_DELAY_TIME 600
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
44
keyboards/keychron/bluetooth/bluetooth_event_type.h
Normal file
44
keyboards/keychron/bluetooth/bluetooth_event_type.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
/* Type of an enumeration of the possible BT events.*/
|
||||||
|
typedef enum {
|
||||||
|
EVT_NONE = 0,
|
||||||
|
EVT_RESET,
|
||||||
|
EVT_DISCOVERABLE,
|
||||||
|
EVT_RECONNECTING,
|
||||||
|
EVT_CONNECTED,
|
||||||
|
EVT_DISCONNECTED,
|
||||||
|
EVT_BT_PINCODE_ENTRY,
|
||||||
|
EVT_EXIT_BT_PINCODE_ENTRY,
|
||||||
|
EVT_HID_SET_PROTOCOL,
|
||||||
|
EVT_HID_INDICATOR,
|
||||||
|
EVT_CONECTION_INTERVAL,
|
||||||
|
} event_type_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
event_type_t evt_type; /*The type of the event. */
|
||||||
|
union {
|
||||||
|
uint8_t reason; /* Parameters to BLUETOOTH_RESET event */
|
||||||
|
uint8_t hostIndex; /* Parameters to connection event from EVT_DISCOVERABLE to EVT_DISCONECTED */
|
||||||
|
uint8_t led; /* Parameters to EVT_HID_INDICATOR event */
|
||||||
|
uint8_t protocol; /* Parameters to EVT_HID_SET_PROTOCOL event */
|
||||||
|
uint8_t interval; /* Parameters to EVT_CONECTION_INTERVAL event */
|
||||||
|
} params;
|
||||||
|
} bluetooth_event_t;
|
||||||
|
|
||||||
37
keyboards/keychron/bluetooth/bluetooth_main.c
Normal file
37
keyboards/keychron/bluetooth/bluetooth_main.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/* 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 "transport.h"
|
||||||
|
|
||||||
|
__attribute__((weak)) void bluetooth_pre_task(void) {}
|
||||||
|
__attribute__((weak)) void bluetooth_post_task(void) {}
|
||||||
|
|
||||||
|
void bluetooth_tasks(void) {
|
||||||
|
bluetooth_pre_task();
|
||||||
|
bluetooth_task();
|
||||||
|
bluetooth_post_task();
|
||||||
|
|
||||||
|
/* usb_remote_wakeup() should be invoked last so that we have chance
|
||||||
|
* to switch to bluetooth after start-up when usb is not connected
|
||||||
|
*/
|
||||||
|
if (get_transport() == TRANSPORT_USB) usb_remote_wakeup();
|
||||||
|
}
|
||||||
|
|
||||||
|
void housekeeping_task_kb(void) {
|
||||||
|
bluetooth_tasks();
|
||||||
|
}
|
||||||
606
keyboards/keychron/bluetooth/ckbt51.c
Normal file
606
keyboards/keychron/bluetooth/ckbt51.c
Normal file
@ -0,0 +1,606 @@
|
|||||||
|
/* Copyright 2021 @ Keychron (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 <stdbool.h>
|
||||||
|
#include "quantum.h"
|
||||||
|
#include "ckbt51.h"
|
||||||
|
#include "bluetooth.h"
|
||||||
|
#include "battery.h"
|
||||||
|
#include "raw_hid.h"
|
||||||
|
#include "report_buffer.h"
|
||||||
|
|
||||||
|
#ifndef RAW_EPSIZE
|
||||||
|
# define RAW_EPSIZE 32
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef CKBT51_INT_INPUT_PIN
|
||||||
|
# error "CKBT51_INT_INPUT_PIN is not defined"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef CKBT51_TX_RETRY_COUNT
|
||||||
|
# define CKBT51_TX_RETRY_COUNT 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* CKBT51 disable its uart peripheral to save power if uart inactivity for 3s, need to
|
||||||
|
* assert this pin and wait some time for its uart getting ready before sending data*/
|
||||||
|
#define CKBT51_WAKE_WAIT_TIME 3000 // us
|
||||||
|
|
||||||
|
enum {
|
||||||
|
/* HID Report */
|
||||||
|
CKBT51_CMD_SEND_KB = 0x11,
|
||||||
|
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_BOOT_KB = 0x17,
|
||||||
|
/* Bluetooth connections */
|
||||||
|
CKBT51_CMD_PAIRING = 0x21,
|
||||||
|
CKBT51_CMD_CONNECT = 0x22,
|
||||||
|
CKBT51_CMD_DISCONNECT = 0x23,
|
||||||
|
CKBT51_CMD_SWITCH_HOST = 0x24,
|
||||||
|
CKBT51_CMD_READ_STATE_REG = 0x25,
|
||||||
|
/* 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,
|
||||||
|
/* DFU */
|
||||||
|
CKBT51_CMD_GET_DFU_VER = 0x60,
|
||||||
|
CKBT51_CMD_HAND_SHAKE_TOKEN = 0x61,
|
||||||
|
CKBT51_CMD_START_DFU = 0x62,
|
||||||
|
CKBT51_CMD_SEND_FW_DATA = 0x63,
|
||||||
|
CKBT51_CMD_VERIFY_CRC32 = 0x64,
|
||||||
|
CKBT51_CMD_SWITCH_FW = 0x65,
|
||||||
|
/* 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,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
CKBT51_EVT_ACK = 0xA1,
|
||||||
|
CKBT51_EVT_QUERY_RSP = 0xA2,
|
||||||
|
CKBT51_EVT_RESET = 0xB0,
|
||||||
|
CKBT51_EVT_LE_CONNECTION = 0xB1,
|
||||||
|
CKBT51_EVT_HOST_TYPE = 0xB2,
|
||||||
|
CKBT51_EVT_CONNECTION = 0xB3,
|
||||||
|
CKBT51_EVT_HID_EVENT = 0xB4,
|
||||||
|
CKBT51_EVT_BATTERY = 0xB5,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum { CKBT51_CONNECTED = 0x20, CKBT51_DISCOVERABLE = 0x21, CKBT51_RECONNECTING = 0x22, CKBT51_DISCONNECTED = 0x23, CKBT51_PINCODE_ENTRY = 0x24, CKBT51_EXIT_PINCODE_ENTRY = 0x25 };
|
||||||
|
|
||||||
|
enum {
|
||||||
|
ACK_SUCCESS = 0x00,
|
||||||
|
ACK_CHECKSUM_ERROR,
|
||||||
|
ACK_FIFO_HALF_WARNING,
|
||||||
|
ACK_FIFO_FULL_ERROR,
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint8_t payload[PACKET_MAX_LEN];
|
||||||
|
static uint8_t reg_offset = 0xFF;
|
||||||
|
|
||||||
|
bluetooth_transport_t bluetooth_transport = {ckbt51_init, ckbt51_connect, ckbt51_become_discoverable, ckbt51_disconnect, ckbt51_send_keyboard, ckbt51_send_nkro, ckbt51_send_consumer, ckbt51_send_system, ckbt51_send_mouse, ckbt51_task};
|
||||||
|
|
||||||
|
void ckbt51_init(bool wakeup_from_low_power_mode) {
|
||||||
|
#if (HAL_USE_SERIAL == TRUE)
|
||||||
|
SerialConfig config = {460800, 0, USART_CR2_STOP1_BITS, 0};
|
||||||
|
|
||||||
|
if (wakeup_from_low_power_mode) {
|
||||||
|
sdInit();
|
||||||
|
sdStart(&WT_DRIVER, &config);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sdStart(&WT_DRIVER, &config);
|
||||||
|
palSetPadMode(WT_DRIVER_UART_TX_BANK, WT_DRIVER_UART_TX, PAL_MODE_ALTERNATE(WT_DRIVER_UART_TX_PAL_MODE));
|
||||||
|
palSetPadMode(WT_DRIVER_UART_RX_BANK, WT_DRIVER_UART_RX, PAL_MODE_ALTERNATE(WT_DRIVER_UART_RX_PAL_MODE));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
setPinOutput(CKBT51_INT_INPUT_PIN);
|
||||||
|
writePinHigh(CKBT51_INT_INPUT_PIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_send_cmd(uint8_t* payload, uint8_t len, bool ack_enable, bool retry) {
|
||||||
|
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++) {
|
||||||
|
writePin(CKBT51_INT_INPUT_PIN, i % 2);
|
||||||
|
start = chVTGetSystemTime();
|
||||||
|
while (chTimeI2US(chVTTimeElapsedSinceX(start)) < CKBT51_WAKE_WAIT_TIME / 3) {
|
||||||
|
};
|
||||||
|
}
|
||||||
|
writePinHigh(CKBT51_INT_INPUT_PIN);
|
||||||
|
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
checksum += payload[i];
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
pkt[i++] = 0xAA;
|
||||||
|
pkt[i++] = ack_enable ? 0x56 : 0x55;
|
||||||
|
pkt[i++] = len + 2;
|
||||||
|
pkt[i++] = ~(len + 2) & 0xFF;
|
||||||
|
pkt[i++] = sn;
|
||||||
|
memcpy(pkt + i, payload, len);
|
||||||
|
i += len;
|
||||||
|
pkt[i++] = checksum & 0xFF;
|
||||||
|
pkt[i++] = (checksum >> 8) & 0xFF;
|
||||||
|
|
||||||
|
sdWrite(&WT_DRIVER, pkt, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_send_keyboard(uint8_t* report) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_SEND_KB;
|
||||||
|
memcpy(payload + i, report, 8);
|
||||||
|
i += 8;
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_send_nkro(uint8_t* report) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_SEND_KB_NKRO;
|
||||||
|
memcpy(payload + i, report, 20); // NKRO report lenght is limited to 20 bytes
|
||||||
|
i += 20;
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_send_consumer(uint16_t report) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_SEND_CONSUMER;
|
||||||
|
payload[i++] = report & 0xFF;
|
||||||
|
payload[i++] = ((report) >> 8) & 0xFF;
|
||||||
|
i += 4; // QMK doesn't send multiple consumer reports, just skip 2nd and 3rd consumer reports
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_send_system(uint16_t report) {
|
||||||
|
/* CKBT51 supports only System Sleep */
|
||||||
|
if ((report & 0xFF) != 0x82) return;
|
||||||
|
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_SEND_SYSTEM;
|
||||||
|
payload[i++] = 0x01 << ((report & 0xFF) - 0x82);
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_send_mouse(uint8_t* report) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_SEND_MOUSE; // Cmd type
|
||||||
|
payload[i++] = report[1]; // Button
|
||||||
|
payload[i++] = report[2]; // X
|
||||||
|
payload[i++] = (report[2] & 0x80) ? 0xff : 0x00; // ckbt51 use 16bit report, set high byte
|
||||||
|
payload[i++] = report[3]; // Y
|
||||||
|
payload[i++] = (report[3] & 0x80) ? 0xff : 0x00; // ckbt51 use 16bit report, set high byte
|
||||||
|
payload[i++] = report[4]; // V wheel
|
||||||
|
payload[i++] = report[5]; // H wheel
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Send ack to connection event, bluetooth module will retry 2 times if no ack received */
|
||||||
|
void ckbt51_send_conn_evt_ack(void) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CONNECTION_EVT_ACK;
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_become_discoverable(uint8_t host_idx, void* param) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
pairing_param_t default_pairing_param = {0, 0, PAIRING_MODE_LESC_OR_SSP, BT_MODE_CLASSIC, 0, NULL};
|
||||||
|
|
||||||
|
if (param == NULL) {
|
||||||
|
param = &default_pairing_param;
|
||||||
|
}
|
||||||
|
pairing_param_t* p = (pairing_param_t*)param;
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_PAIRING; // Cmd type
|
||||||
|
payload[i++] = host_idx; // Host Index
|
||||||
|
payload[i++] = p->timeout & 0xFF; // Timeout
|
||||||
|
payload[i++] = (p->timeout >> 8) & 0xFF;
|
||||||
|
payload[i++] = p->pairingMode;
|
||||||
|
payload[i++] = p->BRorLE; // BR/LE
|
||||||
|
payload[i++] = p->txPower; // LE TX POWER
|
||||||
|
if (p->leName) {
|
||||||
|
memcpy(&payload[i], p->leName, strlen(p->leName));
|
||||||
|
i += strlen(p->leName);
|
||||||
|
}
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Timeout : 2 ~ 255 seconds */
|
||||||
|
void ckbt51_connect(uint8_t hostIndex, uint16_t timeout) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_CONNECT;
|
||||||
|
payload[i++] = hostIndex; // Host index
|
||||||
|
payload[i++] = timeout & 0xFF; // Timeout
|
||||||
|
payload[i++] = (timeout >> 8) & 0xFF;
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_disconnect(void) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_DISCONNECT;
|
||||||
|
payload[i++] = 0; // Sleep mode
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_switch_host(uint8_t hostIndex) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_SWITCH_HOST;
|
||||||
|
payload[i++] = hostIndex;
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_read_state_reg(uint8_t reg, uint8_t len) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_READ_STATE_REG;
|
||||||
|
payload[i++] = reg_offset = reg;
|
||||||
|
payload[i++] = len;
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
ckbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_get_info(module_info_t* info) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_GET_MODULE_INFO;
|
||||||
|
ckbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_set_param(module_param_t* param) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_SET_CONFIG;
|
||||||
|
memcpy(payload + i, param, sizeof(module_param_t));
|
||||||
|
i += sizeof(module_param_t);
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_get_param(module_param_t* param) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_GET_CONFIG;
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_set_local_name(const char* name) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
uint8_t len = strlen(name);
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_SET_NAME;
|
||||||
|
memcpy(payload + i, name, len);
|
||||||
|
i += len;
|
||||||
|
ckbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_get_local_name(void) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_GET_NAME;
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_factory_reset(void) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = CKBT51_CMD_FACTORY_RESET;
|
||||||
|
|
||||||
|
ckbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_int_pin_test(bool enable) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
payload[i++] = CKBT51_CMD_INT_PIN_TEST;
|
||||||
|
payload[i++] = 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};
|
||||||
|
uint8_t i = 0;
|
||||||
|
|
||||||
|
buf[i++] = 0x03;
|
||||||
|
buf[i++] = 0xAA;
|
||||||
|
buf[i++] = 0x57;
|
||||||
|
buf[i++] = len;
|
||||||
|
buf[i++] = ~len;
|
||||||
|
buf[i++] = sn;
|
||||||
|
buf[i++] = rsp;
|
||||||
|
memcpy(&buf[i], data, len);
|
||||||
|
i += len;
|
||||||
|
|
||||||
|
for (uint8_t k = 0; k < i; k++)
|
||||||
|
checksum += buf[i];
|
||||||
|
|
||||||
|
raw_hid_send(buf, RAW_EPSIZE);
|
||||||
|
|
||||||
|
if (len > 25) {
|
||||||
|
i = 0;
|
||||||
|
memset(buf, 0, RAW_EPSIZE);
|
||||||
|
buf[i++] = 0x03;
|
||||||
|
memcpy(&buf[i], data + 25, len - 25);
|
||||||
|
i = i + len - 25;
|
||||||
|
raw_hid_send(buf, RAW_EPSIZE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_dfu_rx(uint8_t* data, uint8_t length) {
|
||||||
|
if (data[0] == 0xAA && (data[1] == 0x55 || data[1] == 0x56) && data[2] == (~data[3] & 0xFF)) {
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
uint8_t payload_len = data[2];
|
||||||
|
|
||||||
|
/* Check payload_len validity */
|
||||||
|
if (payload_len > RAW_EPSIZE - PACKECT_HEADER_LEN) return;
|
||||||
|
|
||||||
|
uint8_t* payload = &data[PACKECT_HEADER_LEN];
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < payload_len - 2; i++) {
|
||||||
|
checksum += payload[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Verify checksum */
|
||||||
|
if ((checksum & 0xFF) != payload[payload_len - 2] || checksum >> 8 != payload[payload_len - 1]) return;
|
||||||
|
static uint8_t sn = 0;
|
||||||
|
|
||||||
|
bool retry = true;
|
||||||
|
if (sn != data[4]) {
|
||||||
|
sn = data[4];
|
||||||
|
retry = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((payload[0] & 0xF0) == 0x60) {
|
||||||
|
ckbt51_send_cmd(payload, payload_len - 2, data[1] == 0x56, retry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((weak)) void ckbt51_default_ack_handler(uint8_t* data, uint8_t len){};
|
||||||
|
|
||||||
|
static void ack_handler(uint8_t* data, uint8_t len) {
|
||||||
|
switch (data[1]) {
|
||||||
|
case CKBT51_CMD_SEND_KB:
|
||||||
|
case CKBT51_CMD_SEND_KB_NKRO:
|
||||||
|
case CKBT51_CMD_SEND_CONSUMER:
|
||||||
|
case CKBT51_CMD_SEND_SYSTEM:
|
||||||
|
case CKBT51_CMD_SEND_MOUSE:
|
||||||
|
switch (data[2]) {
|
||||||
|
case ACK_SUCCESS:
|
||||||
|
report_buffer_set_retry(0);
|
||||||
|
report_buffer_set_inverval(DEFAULT_REPORT_INVERVAL_MS);
|
||||||
|
break;
|
||||||
|
case ACK_FIFO_HALF_WARNING:
|
||||||
|
report_buffer_set_retry(0);
|
||||||
|
report_buffer_set_inverval(DEFAULT_REPORT_INVERVAL_MS + 5);
|
||||||
|
break;
|
||||||
|
case ACK_FIFO_FULL_ERROR:
|
||||||
|
report_buffer_set_retry(10);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ckbt51_default_ack_handler(data, len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void query_rsp_handler(uint8_t* data, uint8_t len) {
|
||||||
|
if (data[2]) return;
|
||||||
|
|
||||||
|
switch (data[1]) {
|
||||||
|
case CKBT51_CMD_READ_STATE_REG:
|
||||||
|
switch (reg_offset) {
|
||||||
|
case 0x05:
|
||||||
|
battery_calculte_voltage(data[3] | (data[4] << 8));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
reg_offset = 0xFF;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ckbt51_event_handler(uint8_t evt_type, uint8_t* data, uint8_t len, uint8_t sn) {
|
||||||
|
bluetooth_event_t event = {0};
|
||||||
|
|
||||||
|
switch (evt_type) {
|
||||||
|
case CKBT51_EVT_ACK:
|
||||||
|
ack_handler(data, len);
|
||||||
|
break;
|
||||||
|
case CKBT51_EVT_RESET:
|
||||||
|
dprintf("CKBT51_EVT_RESET\n");
|
||||||
|
event.evt_type = EVT_RESET;
|
||||||
|
event.params.reason = data[0];
|
||||||
|
break;
|
||||||
|
case CKBT51_EVT_LE_CONNECTION:
|
||||||
|
dprintf("CKBT51_EVT_LE_CONNECTION\n");
|
||||||
|
break;
|
||||||
|
case CKBT51_EVT_HOST_TYPE:
|
||||||
|
dprintf("CKBT51_EVT_HOST_TYPE\n");
|
||||||
|
break;
|
||||||
|
case CKBT51_EVT_CONNECTION:
|
||||||
|
dprintf("CKBT51_EVT_CONNECTION %d\n", data[0]);
|
||||||
|
/* Only connection status change message will retry 2 times if no ack */
|
||||||
|
ckbt51_send_conn_evt_ack();
|
||||||
|
switch (data[0]) {
|
||||||
|
case CKBT51_CONNECTED:
|
||||||
|
event.evt_type = EVT_CONNECTED;
|
||||||
|
break;
|
||||||
|
case CKBT51_DISCOVERABLE:
|
||||||
|
event.evt_type = EVT_DISCOVERABLE;
|
||||||
|
break;
|
||||||
|
case CKBT51_RECONNECTING:
|
||||||
|
event.evt_type = EVT_RECONNECTING;
|
||||||
|
break;
|
||||||
|
case CKBT51_DISCONNECTED:
|
||||||
|
event.evt_type = EVT_DISCONNECTED;
|
||||||
|
break;
|
||||||
|
case CKBT51_PINCODE_ENTRY:
|
||||||
|
event.evt_type = EVT_BT_PINCODE_ENTRY;
|
||||||
|
break;
|
||||||
|
case CKBT51_EXIT_PINCODE_ENTRY:
|
||||||
|
event.evt_type = EVT_EXIT_BT_PINCODE_ENTRY;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
event.params.hostIndex = data[2];
|
||||||
|
break;
|
||||||
|
case CKBT51_EVT_HID_EVENT:
|
||||||
|
dprintf("CKBT51_EVT_HID_EVENT\n");
|
||||||
|
event.evt_type = EVT_HID_INDICATOR;
|
||||||
|
event.params.led = data[0];
|
||||||
|
break;
|
||||||
|
case CKBT51_EVT_QUERY_RSP:
|
||||||
|
dprintf("CKBT51_EVT_QUERY_RSP\n");
|
||||||
|
query_rsp_handler(data, len);
|
||||||
|
break;
|
||||||
|
case CKBT51_EVT_OTA_RSP:
|
||||||
|
dprintf("CKBT51_EVT_OTA_RSP\n");
|
||||||
|
ckbt51_dfu_tx(CKBT51_EVT_OTA_RSP, data, len, sn);
|
||||||
|
break;
|
||||||
|
case CKBT51_EVT_BATTERY:
|
||||||
|
if (data[0] == 0x01) {
|
||||||
|
dprintf("CKBT51_EVT_BATTERY\n");
|
||||||
|
battery_calculte_voltage(data[1] | (data[2] << 8));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
dprintf("Unknown event!!!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.evt_type) bluetooth_event_queue_enqueue(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ckbt51_task(void) {
|
||||||
|
static bool wait_for_new_pkt = true;
|
||||||
|
static uint8_t len = 0xff;
|
||||||
|
static uint8_t sn = 0;
|
||||||
|
|
||||||
|
if (wait_for_new_pkt && WT_DRIVER.iqueue.q_counter >= PACKECT_HEADER_LEN) {
|
||||||
|
uint8_t buf[32] = {0};
|
||||||
|
|
||||||
|
if (wait_for_new_pkt) {
|
||||||
|
if (sdGet(&WT_DRIVER) == 0xAA && sdGet(&WT_DRIVER) == 0x57) {
|
||||||
|
for (uint8_t i = 0; i < 3; i++) {
|
||||||
|
buf[i] = sdGet(&WT_DRIVER);
|
||||||
|
}
|
||||||
|
// Check wheather len is valid
|
||||||
|
if ((~buf[0] & 0xFF) == buf[1]) {
|
||||||
|
len = buf[0];
|
||||||
|
sn = buf[2];
|
||||||
|
|
||||||
|
wait_for_new_pkt = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!wait_for_new_pkt && WT_DRIVER.iqueue.q_counter >= len) {
|
||||||
|
uint8_t buf[32] = {0};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < len; i++) {
|
||||||
|
buf[i] = sdGetTimeout(&WT_DRIVER, TIME_IMMEDIATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
wait_for_new_pkt = true;
|
||||||
|
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
for (int i = 0; i < len - 2; i++)
|
||||||
|
checksum += buf[i];
|
||||||
|
|
||||||
|
if ((checksum & 0xff) == buf[len - 2] && ((checksum >> 8) & 0xff) == buf[len - 1]) {
|
||||||
|
ckbt51_event_handler(buf[0], buf + 1, len - 3, sn);
|
||||||
|
} else {
|
||||||
|
// TODO: Error handle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
157
keyboards/keychron/bluetooth/ckbt51.h
Normal file
157
keyboards/keychron/bluetooth/ckbt51.h
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
/* Copyright 2021 @ Keychron (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
|
||||||
|
|
||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
|
#ifdef WT_DRIVER_UART_BANK
|
||||||
|
# define WT_DRIVER_UART_TX_BANK WT_DRIVER_UART_BANK
|
||||||
|
# define WT_DRIVER_UART_RX_BANK WT_DRIVER_UART_BANK
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WT_DRIVER_UART_TX_BANK
|
||||||
|
# define WT_DRIVER_UART_TX_BANK GPIOA
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WT_DRIVER_UART_RX_BANK
|
||||||
|
# define WT_DRIVER_UART_RX_BANK GPIOA
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WT_DRIVER_UART_TX
|
||||||
|
# define WT_DRIVER_UART_TX 2
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WT_DRIVER_UART_RX
|
||||||
|
# define WT_DRIVER_UART_RX 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WT_DRIVER
|
||||||
|
# define WT_DRIVER SD2
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_GPIOV1
|
||||||
|
# ifndef WT_DRIVER_UART_TX_PAL_MODE
|
||||||
|
# define WT_DRIVER_UART_TX_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL
|
||||||
|
# endif
|
||||||
|
# ifndef WT_DRIVER_UART_RX_PAL_MODE
|
||||||
|
# define WT_DRIVER_UART_RX_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL
|
||||||
|
# endif
|
||||||
|
#else
|
||||||
|
// The default PAL alternate modes are used to signal that the pins are used for I2C
|
||||||
|
# ifndef WT_DRIVER_UART_TX_PAL_MODE
|
||||||
|
# define WT_DRIVER_UART_TX_PAL_MODE 7
|
||||||
|
# endif
|
||||||
|
# ifndef WT_DRIVER_UART_RX_PAL_MODE
|
||||||
|
# define WT_DRIVER_UART_RX_PAL_MODE 7
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Error checking
|
||||||
|
#if !STM32_SERIAL_USE_USART1 && !STM32_SERIAL_USE_USART2 && !STM32_SERIAL_USE_USART3 && !STM32_SERIAL_USE_UART4 && !STM32_SERIAL_USE_UART5 && !STM32_SERIAL_USE_USART6 && !STM32_SERIAL_USE_UART7 && !STM32_SERIAL_USE_UART8 && !STM32_SERIAL_USE_LPUART1
|
||||||
|
# error "BT driver activated but no USART/UART peripheral assigned"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define PACKECT_HEADER_LEN 5
|
||||||
|
#define BDA_LEN 6
|
||||||
|
#define PACKET_MAX_LEN 64
|
||||||
|
|
||||||
|
enum {
|
||||||
|
PAIRING_MODE_DEFAULT = 0x00,
|
||||||
|
PAIRING_MODE_JUST_WORK,
|
||||||
|
PAIRING_MODE_PASSKEY_ENTRY,
|
||||||
|
PAIRING_MODE_LESC_OR_SSP,
|
||||||
|
PAIRING_MODE_INVALID
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BT_MODE_DEFAUL,
|
||||||
|
BT_MODE_CLASSIC,
|
||||||
|
BT_MODE_LE, // Note: CKBT51 doesn't support BLE
|
||||||
|
BT_MODE_INVALID,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t hostIndex;
|
||||||
|
uint16_t timeout; /* Pairing timeout, valid value range from 30 to 3600 seconds, 0 for default */
|
||||||
|
uint8_t pairingMode; /* 0: default, 1: Just Works, 2: Passkey Entry */
|
||||||
|
uint8_t BRorLE; /* Only available for dual mode module. Keep 0 for single mode module */
|
||||||
|
uint8_t txPower; /* Only available for BLE module */
|
||||||
|
const char* leName; /* Only available for BLE module */
|
||||||
|
} pairing_param_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t type;
|
||||||
|
uint16_t full_votage;
|
||||||
|
uint16_t empty_voltage;
|
||||||
|
uint16_t shutdown_voltage;
|
||||||
|
} battery_param_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t model_name[11];
|
||||||
|
uint8_t mode;
|
||||||
|
uint8_t bluetooth_version;
|
||||||
|
uint8_t firmware_version[11];
|
||||||
|
uint8_t hardware_version[11];
|
||||||
|
uint16_t cmd_set_verson;
|
||||||
|
} __attribute__((packed)) module_info_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t event_mode; /* Must be 0x02 */
|
||||||
|
uint16_t connected_idle_timeout;
|
||||||
|
uint16_t pairing_timeout; /* Range: 30 ~ 3600 second, 0 for default */
|
||||||
|
uint8_t pairing_mode; /* 0: default, 1: Just Works, 2: Passkey Entry */
|
||||||
|
uint16_t reconnect_timeout; /* 0: default, 0xFF: Unlimited time, 2 ~ 254 seconds */
|
||||||
|
uint8_t report_rate; /* 90 or 133 */
|
||||||
|
uint8_t rsvd1;
|
||||||
|
uint8_t rsvd2;
|
||||||
|
uint8_t vendor_id_source; /* 0: From Bluetooth SIG, 1: From USB-IF */
|
||||||
|
uint16_t verndor_id; /* No effect, the vendor ID is 0x3434 */
|
||||||
|
uint16_t product_id;
|
||||||
|
/* Below parametes is only available for BLE module */
|
||||||
|
uint16_t le_connection_interval_min;
|
||||||
|
uint16_t le_connection_interval_max;
|
||||||
|
uint16_t le_connection_interval_timeout;
|
||||||
|
} __attribute__((packed)) module_param_t;
|
||||||
|
|
||||||
|
void ckbt51_init(bool wakeup_from_low_power_mode);
|
||||||
|
void ckbt51_send_cmd(uint8_t* payload, uint8_t len, bool ack_enable, bool retry);
|
||||||
|
|
||||||
|
void ckbt51_send_keyboard(uint8_t* report);
|
||||||
|
void ckbt51_send_nkro(uint8_t* report);
|
||||||
|
void ckbt51_send_consumer(uint16_t report);
|
||||||
|
void ckbt51_send_system(uint16_t report);
|
||||||
|
void ckbt51_send_mouse(uint8_t* report);
|
||||||
|
|
||||||
|
void ckbt51_become_discoverable(uint8_t host_idx, void* param);
|
||||||
|
void ckbt51_connect(uint8_t hostIndex, uint16_t timeout);
|
||||||
|
void ckbt51_disconnect(void);
|
||||||
|
void ckbt51_switch_host(uint8_t hostIndex);
|
||||||
|
void ckbt51_read_state_reg(uint8_t reg, uint8_t len);
|
||||||
|
|
||||||
|
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(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);
|
||||||
|
|
||||||
343
keyboards/keychron/bluetooth/factory_test.c
Normal file
343
keyboards/keychron/bluetooth/factory_test.c
Normal file
@ -0,0 +1,343 @@
|
|||||||
|
/* Copyright 2021 @ Keychron (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 "raw_hid.h"
|
||||||
|
#ifdef KC_BLUETOOTH_ENABLE
|
||||||
|
# include "transport.h"
|
||||||
|
# include "ckbt51.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef RAW_EPSIZE
|
||||||
|
# define RAW_EPSIZE 32
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BL_TEST_KEY1
|
||||||
|
# define BL_TEST_KEY1 KC_RIGHT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BL_TEST_KEY2
|
||||||
|
# define BL_TEST_KEY2 KC_HOME
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern bool bt_factory_reset;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BACKLIGHT_TEST_OFF = 0,
|
||||||
|
BACKLIGHT_TEST_WHITE,
|
||||||
|
BACKLIGHT_TEST_RED,
|
||||||
|
BACKLIGHT_TEST_GREEN,
|
||||||
|
BACKLIGHT_TEST_BLUE,
|
||||||
|
BACKLIGHT_TEST_MAX,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
KEY_PRESS_FN = 0x01 << 0,
|
||||||
|
KEY_PRESS_J = 0x01 << 1,
|
||||||
|
KEY_PRESS_Z = 0x01 << 2,
|
||||||
|
KEY_PRESS_BL_KEY1 = 0x01 << 3,
|
||||||
|
KEY_PRESS_BL_KEY2 = 0x01 << 4,
|
||||||
|
KEY_PRESS_FACTORY_RESET = KEY_PRESS_FN | KEY_PRESS_J | KEY_PRESS_Z,
|
||||||
|
KEY_PRESS_BACKLIGTH_TEST = KEY_PRESS_FN | KEY_PRESS_BL_KEY1 | KEY_PRESS_BL_KEY2,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
FACTORY_TEST_CMD_BACKLIGHT = 0x01,
|
||||||
|
FACTORY_TEST_CMD_OS_SWITCH,
|
||||||
|
FACTORY_TEST_CMD_JUMP_TO_BL,
|
||||||
|
FACTORY_TEST_CMD_INT_PIN,
|
||||||
|
FACTORY_TEST_CMD_GET_TRANSPORT,
|
||||||
|
FACTORY_TEST_CMD_CHARGING_ADC,
|
||||||
|
FACTORY_TEST_CMD_RADIO_CARRIER,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
OS_SWITCH = 0x01,
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t factory_reset_timer = 0;
|
||||||
|
static uint8_t factory_reset_state = 0;
|
||||||
|
static uint8_t backlight_test_mode = BACKLIGHT_TEST_OFF;
|
||||||
|
|
||||||
|
static uint32_t factory_reset_ind_timer = 0;
|
||||||
|
static uint8_t factory_reset_ind_state = 0;
|
||||||
|
static bool report_os_sw_state = false;
|
||||||
|
|
||||||
|
void factory_timer_start(void) {
|
||||||
|
factory_reset_timer = timer_read32() == 0 ? 1 : timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void factory_timer_check(void) {
|
||||||
|
if (sync_timer_elapsed32(factory_reset_timer) > 3000) {
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
|
||||||
|
if (factory_reset_state == KEY_PRESS_FACTORY_RESET) {
|
||||||
|
factory_reset_ind_timer = timer_read32() == 0 ? 1 : timer_read32();
|
||||||
|
factory_reset_ind_state++;
|
||||||
|
|
||||||
|
layer_state_t default_layer_tmp = default_layer_state;
|
||||||
|
eeconfig_init();
|
||||||
|
default_layer_set(default_layer_tmp);
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
if (!led_matrix_is_enabled()) led_matrix_enable();
|
||||||
|
led_matrix_init();
|
||||||
|
#endif
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
if (!rgb_matrix_is_enabled()) rgb_matrix_enable();
|
||||||
|
rgb_matrix_init();
|
||||||
|
#endif
|
||||||
|
#ifdef KC_BLUETOOTH_ENABLE
|
||||||
|
ckbt51_factory_reset();
|
||||||
|
bt_factory_reset = true;
|
||||||
|
#endif
|
||||||
|
} else if (factory_reset_state == KEY_PRESS_BACKLIGTH_TEST) {
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
if (!led_matrix_is_enabled()) led_matrix_enable();
|
||||||
|
#endif
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
if (!rgb_matrix_is_enabled()) rgb_matrix_enable();
|
||||||
|
#endif
|
||||||
|
backlight_test_mode = BACKLIGHT_TEST_WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
factory_reset_state = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void factory_reset_ind_timer_check(void) {
|
||||||
|
if (factory_reset_ind_timer && timer_elapsed32(factory_reset_ind_timer) > 250) {
|
||||||
|
if (factory_reset_ind_state++ > 6) {
|
||||||
|
factory_reset_ind_timer = factory_reset_ind_state = 0;
|
||||||
|
} else {
|
||||||
|
factory_reset_ind_timer = timer_read32() == 0 ? 1 : timer_read32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void process_record_factory_reset(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
switch (keycode) {
|
||||||
|
#if defined(FN_KEY1) || defined(FN_KEY2)
|
||||||
|
# ifdef FN_KEY1
|
||||||
|
case FN_KEY1: /* fall through */
|
||||||
|
# endif
|
||||||
|
# ifdef FN_KEY2
|
||||||
|
case FN_KEY2:
|
||||||
|
# endif
|
||||||
|
if (record->event.pressed) {
|
||||||
|
factory_reset_state |= KEY_PRESS_FN;
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_FN;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
case KC_J:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
factory_reset_state |= KEY_PRESS_J;
|
||||||
|
if (factory_reset_state == 0x07) factory_timer_start();
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_J;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case KC_Z:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
factory_reset_state |= KEY_PRESS_Z;
|
||||||
|
if (factory_reset_state == 0x07) factory_timer_start();
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_Z;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#ifdef BL_TEST_KEY1
|
||||||
|
case BL_TEST_KEY1:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
if (backlight_test_mode) {
|
||||||
|
if (++backlight_test_mode >= BACKLIGHT_TEST_MAX) {
|
||||||
|
backlight_test_mode = BACKLIGHT_TEST_WHITE;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
factory_reset_state |= KEY_PRESS_BL_KEY1;
|
||||||
|
if (factory_reset_state == 0x19) factory_timer_start();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_BL_KEY1;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#ifdef BL_TEST_KEY2
|
||||||
|
case BL_TEST_KEY2:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
if (backlight_test_mode) {
|
||||||
|
backlight_test_mode = BACKLIGHT_TEST_OFF;
|
||||||
|
} else {
|
||||||
|
factory_reset_state |= KEY_PRESS_BL_KEY2;
|
||||||
|
if (factory_reset_state == 0x19) factory_timer_start();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_BL_KEY2;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
bool led_matrix_indicators_user(void) {
|
||||||
|
if (factory_reset_ind_state) {
|
||||||
|
led_matrix_set_value_all(factory_reset_ind_state % 2 ? 0 : 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#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) {
|
||||||
|
case BACKLIGHT_TEST_WHITE:
|
||||||
|
rgb_matrix_set_color_all(255, 255, 255);
|
||||||
|
break;
|
||||||
|
case BACKLIGHT_TEST_RED:
|
||||||
|
rgb_matrix_set_color_all(255, 0, 0);
|
||||||
|
break;
|
||||||
|
case BACKLIGHT_TEST_GREEN:
|
||||||
|
rgb_matrix_set_color_all(0, 255, 0);
|
||||||
|
break;
|
||||||
|
case BACKLIGHT_TEST_BLUE:
|
||||||
|
rgb_matrix_set_color_all(0, 0, 255);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void factory_reset_task(void) {
|
||||||
|
if (factory_reset_timer) factory_timer_check();
|
||||||
|
if (factory_reset_ind_timer) factory_reset_ind_timer_check();
|
||||||
|
}
|
||||||
|
|
||||||
|
void factory_test_send(uint8_t *payload, uint8_t length) {
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
uint8_t data[RAW_EPSIZE] = {0};
|
||||||
|
|
||||||
|
uint8_t i = 0;
|
||||||
|
data[i++] = 0xAB;
|
||||||
|
|
||||||
|
memcpy(&data[i], payload, length);
|
||||||
|
i += length;
|
||||||
|
|
||||||
|
for (uint8_t i = 1; i < RAW_EPSIZE - 3; i++)
|
||||||
|
checksum += data[i];
|
||||||
|
data[RAW_EPSIZE - 2] = checksum & 0xFF;
|
||||||
|
data[RAW_EPSIZE - 1] = (checksum >> 8) & 0xFF;
|
||||||
|
|
||||||
|
raw_hid_send(data, RAW_EPSIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void factory_test_rx(uint8_t *data, uint8_t length) {
|
||||||
|
if (data[0] == 0xAB) {
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
|
||||||
|
for (uint8_t i = 1; i < RAW_EPSIZE - 3; i++) {
|
||||||
|
checksum += data[i];
|
||||||
|
}
|
||||||
|
/* 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];
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
break;
|
||||||
|
case FACTORY_TEST_CMD_OS_SWITCH:
|
||||||
|
report_os_sw_state = data[2];
|
||||||
|
if (report_os_sw_state) {
|
||||||
|
dip_switch_read(true);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case FACTORY_TEST_CMD_JUMP_TO_BL:
|
||||||
|
// if (memcmp(&data[2], "JumpToBootloader", strlen("JumpToBootloader")) == 0) bootloader_jump();
|
||||||
|
break;
|
||||||
|
#ifdef KC_BLUETOOTH_ENABLE
|
||||||
|
case FACTORY_TEST_CMD_INT_PIN:
|
||||||
|
switch (data[2]) {
|
||||||
|
/* Enalbe/disable test */
|
||||||
|
case 0xA1:
|
||||||
|
ckbt51_int_pin_test(data[3]);
|
||||||
|
break;
|
||||||
|
/* Set INT state */
|
||||||
|
case 0xA2:
|
||||||
|
writePin(CKBT51_INT_INPUT_PIN, data[3]);
|
||||||
|
break;
|
||||||
|
/* Report INT state */
|
||||||
|
case 0xA3:
|
||||||
|
payload[len++] = FACTORY_TEST_CMD_INT_PIN;
|
||||||
|
payload[len++] = 0xA3;
|
||||||
|
payload[len++] = readPin(BLUETOOTH_INT_INPUT_PIN);
|
||||||
|
factory_test_send(payload, len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case FACTORY_TEST_CMD_GET_TRANSPORT:
|
||||||
|
payload[len++] = FACTORY_TEST_CMD_GET_TRANSPORT;
|
||||||
|
payload[len++] = get_transport();
|
||||||
|
payload[len++] = readPin(USB_POWER_SENSE_PIN);
|
||||||
|
factory_test_send(payload, len);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#ifdef BATTERY_CHARGE_DONE_DETECT_ADC
|
||||||
|
case FACTORY_TEST_CMD_CHARGING_ADC:
|
||||||
|
case 0xA1:
|
||||||
|
battery_charging_monitor(data[3]);
|
||||||
|
break;
|
||||||
|
case 0xA2:
|
||||||
|
payload[len++] = FACTORY_TEST_CMD_CHARGING_ADC;
|
||||||
|
payload[len++] = battery_adc_read_charging_pin();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
24
keyboards/keychron/bluetooth/factory_test.h
Normal file
24
keyboards/keychron/bluetooth/factory_test.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
#define FACTORY_RESET_CHECK process_record_factory_reset
|
||||||
|
#define FACTORY_RESET_TASK factory_reset_task
|
||||||
|
|
||||||
|
void process_record_factory_reset(uint16_t keycode, keyrecord_t *record);
|
||||||
|
void factory_reset_task(void);
|
||||||
|
void factory_test_rx(uint8_t *data, uint8_t length);
|
||||||
607
keyboards/keychron/bluetooth/indicator.c
Normal file
607
keyboards/keychron/bluetooth/indicator.c
Normal file
@ -0,0 +1,607 @@
|
|||||||
|
/* Copyright 2021 @ 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 "indicator.h"
|
||||||
|
#include "transport.h"
|
||||||
|
#include "battery.h"
|
||||||
|
#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"
|
||||||
|
# endif
|
||||||
|
# ifdef RGB_MATRIX_ENABLE
|
||||||
|
# include "rgb_matrix.h"
|
||||||
|
# endif
|
||||||
|
# include "i2c_master.h"
|
||||||
|
# include "bat_level_animation.h"
|
||||||
|
# include "eeprom.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
# define DECIDE_TIME(t, duration) (duration == 0 ? LED_MATRIX_TIMEOUT_INFINITE : ((t > duration) ? t : duration))
|
||||||
|
#endif
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
# define DECIDE_TIME(t, duration) (duration == 0 ? RGB_MATRIX_TIMEOUT_INFINITE : ((t > duration) ? t : duration))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define LED_ON 0x80
|
||||||
|
#define INDICATOR_SET(s) memcpy(&indicator_config, &s##_config, sizeof(indicator_config_t));
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BACKLIGHT_OFF = 0x00,
|
||||||
|
BACKLIGHT_ON_CONNECTED = 0x01,
|
||||||
|
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;
|
||||||
|
static uint32_t indicator_timer_buffer = 0;
|
||||||
|
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
static uint32_t bat_low_pin_indicator = 0;
|
||||||
|
static uint32_t bat_low_blink_duration = 0;
|
||||||
|
# ifdef BAT_LOW_LED_PIN_STATE
|
||||||
|
bool bat_low_led_pin_state = false;
|
||||||
|
# endif
|
||||||
|
#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;
|
||||||
|
|
||||||
|
static uint8_t host_led_matrix_list[HOST_DEVICES_COUNT] = HOST_LED_MATRIX_LIST;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HOST_LED_PIN_LIST
|
||||||
|
static pin_t host_led_pin_list[HOST_DEVICES_COUNT] = HOST_LED_PIN_LIST;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
# define LED_DRIVER led_matrix_driver
|
||||||
|
# define LED_INDICATORS_KB led_matrix_indicators_kb
|
||||||
|
# define LED_INDICATORS_USER led_matrix_indicators_user
|
||||||
|
# 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 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)); \
|
||||||
|
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_INDICATORS_USER rgb_matrix_indicators_user
|
||||||
|
# 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 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)); \
|
||||||
|
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));
|
||||||
|
|
||||||
|
#ifdef HOST_LED_PIN_LIST
|
||||||
|
for (uint8_t i = 0; i < HOST_DEVICES_COUNT; i++) {
|
||||||
|
setPinOutput(host_led_pin_list[i]);
|
||||||
|
writePin(host_led_pin_list[i], !HOST_LED_PIN_ON_STATE);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef BAT_LOW_LED_PIN
|
||||||
|
setPinOutput(BAT_LOW_LED_PIN);
|
||||||
|
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
void indicator_enable(void) {
|
||||||
|
if (!LED_DRIVER_IS_ENABLED()) {
|
||||||
|
LED_DRIVER_ENABLE_NOEEPROM();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void indicator_disable(void) {
|
||||||
|
LED_DRIVER_DISABLE_NOEEPROM();
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_set_backlit_timeout(uint32_t time) {
|
||||||
|
LED_DRIVER_DISABLE_TIMEOUT_SET(time);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void indicator_reset_backlit_time(void) {
|
||||||
|
LED_DRIVER_DISABLE_TIME_RESET();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool indicator_is_enabled(void) {
|
||||||
|
return LED_DRIVER_IS_ENABLED();
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_eeconfig_reload(void) {
|
||||||
|
LED_DRIVER_EECONFIG_RELOAD();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool indicator_is_running(void) {
|
||||||
|
return
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
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) {
|
||||||
|
if (*(indicator_type_t *)arg != INDICATOR_LAST) type = *(indicator_type_t *)arg;
|
||||||
|
|
||||||
|
bool time_up = false;
|
||||||
|
switch (type) {
|
||||||
|
case INDICATOR_NONE:
|
||||||
|
break;
|
||||||
|
case INDICATOR_OFF:
|
||||||
|
next_period = 0;
|
||||||
|
time_up = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case INDICATOR_ON:
|
||||||
|
if (indicator_config.value) {
|
||||||
|
if (indicator_config.elapsed == 0) {
|
||||||
|
indicator_config.value |= LED_ON;
|
||||||
|
|
||||||
|
if (indicator_config.duration) {
|
||||||
|
indicator_config.elapsed += indicator_config.duration;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
time_up = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case INDICATOR_ON_OFF:
|
||||||
|
if (indicator_config.value) {
|
||||||
|
if (indicator_config.elapsed == 0) {
|
||||||
|
indicator_config.value |= LED_ON;
|
||||||
|
next_period = indicator_config.on_time;
|
||||||
|
} else {
|
||||||
|
indicator_config.value = indicator_config.value & 0x0F;
|
||||||
|
next_period = indicator_config.duration - indicator_config.on_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((indicator_config.duration == 0 || indicator_config.elapsed <= indicator_config.duration) && next_period != 0) {
|
||||||
|
indicator_config.elapsed += next_period;
|
||||||
|
} else {
|
||||||
|
time_up = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case INDICATOR_BLINK:
|
||||||
|
if (indicator_config.value) {
|
||||||
|
if (indicator_config.value & LED_ON) {
|
||||||
|
indicator_config.value = indicator_config.value & 0x0F;
|
||||||
|
next_period = indicator_config.off_time;
|
||||||
|
} else {
|
||||||
|
indicator_config.value |= LED_ON;
|
||||||
|
next_period = indicator_config.on_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((indicator_config.duration == 0 || indicator_config.elapsed <= indicator_config.duration) && next_period != 0) {
|
||||||
|
indicator_config.elapsed += next_period;
|
||||||
|
} else {
|
||||||
|
time_up = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
time_up = true;
|
||||||
|
|
||||||
|
next_period = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef HOST_LED_PIN_LIST
|
||||||
|
if (indicator_config.value) {
|
||||||
|
uint8_t idx = (indicator_config.value & 0x0F) - 1;
|
||||||
|
|
||||||
|
if (idx < HOST_DEVICES_COUNT) {
|
||||||
|
if ((indicator_config.value & 0x80) && !time_up) {
|
||||||
|
writePin(host_led_pin_list[idx], HOST_LED_PIN_ON_STATE);
|
||||||
|
} else {
|
||||||
|
writePin(host_led_pin_list[idx], !HOST_LED_PIN_ON_STATE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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;
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
LED_INDICATORS_KB();
|
||||||
|
#endif
|
||||||
|
indicator_config.value = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (indicator_config.value == 0) {
|
||||||
|
indicator_eeconfig_reload();
|
||||||
|
if (!LED_DRIVER_IS_ENABLED()) indicator_disable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_set(bluetooth_state_t state, uint8_t host_index) {
|
||||||
|
if (get_transport() != TRANSPORT_BLUETOOTH) return;
|
||||||
|
dprintf("indicator set: %d, %d\n", state, host_index);
|
||||||
|
|
||||||
|
static uint8_t current_state = 0;
|
||||||
|
static uint8_t current_host = 0;
|
||||||
|
|
||||||
|
bool host_index_changed = false;
|
||||||
|
if (current_host != host_index && state != BLUETOOTH_DISCONNECTED) {
|
||||||
|
host_index_changed = true;
|
||||||
|
current_host = host_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current_state != state || host_index_changed) {
|
||||||
|
current_state = state;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
indicator_timer_buffer = sync_timer_read32();
|
||||||
|
|
||||||
|
/* Turn on backlight mode for indicator */
|
||||||
|
indicator_enable();
|
||||||
|
indicator_reset_backlit_time();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (battery_is_critical_low()) {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BLUETOOTH_CONNECTED:
|
||||||
|
if (indicator_state != BLUETOOTH_CONNECTED) {
|
||||||
|
INDICATOR_SET(connected);
|
||||||
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index;
|
||||||
|
indicator_timer_cb((void *)&indicator_config.type);
|
||||||
|
}
|
||||||
|
indicator_set_backlit_timeout(DECIDE_TIME(CONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BLUETOOTH_PARING:
|
||||||
|
INDICATOR_SET(pairing);
|
||||||
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : LED_ON | host_index;
|
||||||
|
indicator_timer_cb((void *)&indicator_config.type);
|
||||||
|
indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BLUETOOTH_RECONNECTING:
|
||||||
|
INDICATOR_SET(reconnecting);
|
||||||
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : LED_ON | host_index;
|
||||||
|
indicator_timer_cb((void *)&indicator_config.type);
|
||||||
|
indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BLUETOOTH_SUSPEND:
|
||||||
|
INDICATOR_SET(disconnected);
|
||||||
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index;
|
||||||
|
indicator_timer_cb((void *)&indicator_config.type);
|
||||||
|
indicator_set_backlit_timeout(100);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
indicator_state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_stop(void) {
|
||||||
|
indicator_config.value = 0;
|
||||||
|
indicator_eeconfig_reload();
|
||||||
|
|
||||||
|
if (indicator_is_enabled()) {
|
||||||
|
indicator_enable();
|
||||||
|
} else {
|
||||||
|
indicator_disable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
void indicator_battery_low_enable(bool enable) {
|
||||||
|
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 {
|
||||||
|
# if defined(BAT_LOW_LED_PIN)
|
||||||
|
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
|
||||||
|
# else
|
||||||
|
bat_low_led_pin_state = false;
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#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() == 0 ? 1 : timer_read32()) : 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) {
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
if (bat_low_pin_indicator && sync_timer_elapsed32(bat_low_pin_indicator) > (LOW_BAT_LED_BLINK_PERIOD)) {
|
||||||
|
# if defined(BAT_LOW_LED_PIN)
|
||||||
|
togglePin(BAT_LOW_LED_PIN);
|
||||||
|
# else
|
||||||
|
bat_low_led_pin_state = !bat_low_led_pin_state;
|
||||||
|
# endif
|
||||||
|
bat_low_pin_indicator = sync_timer_read32() | 1;
|
||||||
|
// Turn off low battery indication if we reach the duration
|
||||||
|
# if defined(BAT_LOW_LED_PIN)
|
||||||
|
if (sync_timer_elapsed32(bat_low_blink_duration) > LOW_BAT_LED_BLINK_DURATION && palReadLine(BAT_LOW_LED_PIN) != BAT_LOW_LED_PIN_ON_STATE) {
|
||||||
|
# elif defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
if (sync_timer_elapsed32(bat_low_blink_duration) > LOW_BAT_LED_BLINK_DURATION) {
|
||||||
|
# endif
|
||||||
|
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() == 0 ? 1 : sync_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())
|
||||||
|
# endif
|
||||||
|
indicator_disable();
|
||||||
|
}
|
||||||
|
} else if ((bat_low_ind_state & 0x0F) > (LOW_BAT_LED_BLINK_TIMES)) {
|
||||||
|
bat_low_ind_state = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_task(void) {
|
||||||
|
bat_level_animiation_task();
|
||||||
|
|
||||||
|
if (indicator_config.value && sync_timer_elapsed32(indicator_timer_buffer) >= next_period) {
|
||||||
|
indicator_timer_cb((void *)&type);
|
||||||
|
indicator_timer_buffer = sync_timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
indicator_battery_low();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
__attribute__((weak)) 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
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LED_INDICATORS_KB(void) {
|
||||||
|
if (!LED_INDICATORS_USER()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (get_transport() == TRANSPORT_BLUETOOTH) {
|
||||||
|
/* Prevent backlight flash caused by key activities */
|
||||||
|
if (battery_is_critical_low()) {
|
||||||
|
SET_ALL_LED_OFF();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
static uint8_t last_host_index = 0xFF;
|
||||||
|
|
||||||
|
if (indicator_config.value) {
|
||||||
|
uint8_t host_index = indicator_config.value & 0x0F;
|
||||||
|
|
||||||
|
if (indicator_config.highlight) {
|
||||||
|
SET_ALL_LED_OFF();
|
||||||
|
} else if (last_host_index != host_index) {
|
||||||
|
SET_LED_OFF(host_led_matrix_list[last_host_index - 1]);
|
||||||
|
last_host_index = host_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (indicator_config.value & 0x80) {
|
||||||
|
SET_LED_BT(host_led_matrix_list[host_index - 1]);
|
||||||
|
} else {
|
||||||
|
SET_LED_OFF(host_led_matrix_list[host_index - 1]);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
os_state_indicate();
|
||||||
|
|
||||||
|
} else
|
||||||
|
os_state_indicate();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool led_update_kb(led_t led_state) {
|
||||||
|
bool res = led_update_user(led_state);
|
||||||
|
if (res) {
|
||||||
|
led_update_ports(led_state);
|
||||||
|
|
||||||
|
if (!LED_DRIVER_IS_ENABLED()) {
|
||||||
|
# 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();
|
||||||
|
# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
|
||||||
|
if (LED_DRIVER_ALLOW_SHUTDOWN()) LED_DRIVER.shutdown();
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LED_NONE_INDICATORS_KB(void) {
|
||||||
|
os_state_indicate();
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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;
|
||||||
|
# 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
|
||||||
118
keyboards/keychron/bluetooth/indicator.h
Normal file
118
keyboards/keychron/bluetooth/indicator.h
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "bluetooth.h"
|
||||||
|
|
||||||
|
/* Indication of pairing */
|
||||||
|
#ifndef INDICATOR_CONFIG_PARING
|
||||||
|
# define INDICATOR_CONFIG_PARING {INDICATOR_BLINK, 1000, 1000, 0, true, 0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Indication on Connected */
|
||||||
|
#ifndef INDICATOR_CONFIG_CONNECTD
|
||||||
|
# define INDICATOR_CONFIG_CONNECTD {INDICATOR_ON_OFF, 2000, 250, 2000, true, 0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Reconnecting indication */
|
||||||
|
#ifndef INDICATOR_CONFIG_RECONNECTING
|
||||||
|
# define INDICATOR_CONFIG_RECONNECTING {INDICATOR_BLINK, 100, 100, 600, true, 0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Disconnected indication */
|
||||||
|
#ifndef INDICATOR_CONFIG_DISCONNECTED
|
||||||
|
# define INDICATOR_CONFIG_DISCONNECTED {INDICATOR_NONE, 100, 100, 600, false, 0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Uint: Second */
|
||||||
|
#ifndef DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT
|
||||||
|
# define DISCONNECTED_BACKLIGHT_OFF_DELAY_TIME 40
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Uint: Second, the timer restarts on key activities. */
|
||||||
|
#ifndef CONNECTED_BACKLIGHT_DISABLE_TIMEOUT
|
||||||
|
# define CONNECTED_BACKLIGHT_OFF_DELAY_TIME 600
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||||
|
/* 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
|
||||||
|
# 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 struct PACKED {
|
||||||
|
indicator_type_t type;
|
||||||
|
uint32_t on_time;
|
||||||
|
uint32_t off_time;
|
||||||
|
uint32_t duration;
|
||||||
|
bool highlight;
|
||||||
|
uint8_t value;
|
||||||
|
uint32_t elapsed;
|
||||||
|
} indicator_config_t;
|
||||||
|
|
||||||
|
typedef struct PACKED {
|
||||||
|
uint8_t value;
|
||||||
|
bool saved;
|
||||||
|
} backlight_state_t;
|
||||||
|
|
||||||
|
void indicator_init(void);
|
||||||
|
void indicator_set(bluetooth_state_t state, uint8_t host_index);
|
||||||
|
void indicator_backlight_timer_reset(bool enable);
|
||||||
|
bool indicator_hook_key(uint16_t keycode);
|
||||||
|
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);
|
||||||
|
void os_state_indicate(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);
|
||||||
92
keyboards/keychron/bluetooth/lpm.c
Normal file
92
keyboards/keychron/bluetooth/lpm.c
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
/* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
*
|
||||||
|
* Filename: lpm.c
|
||||||
|
*
|
||||||
|
* Description: Contains low power mode implementation
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include "quantum.h"
|
||||||
|
#if defined(PROTOCOL_CHIBIOS)
|
||||||
|
# include <usb_main.h>
|
||||||
|
#endif
|
||||||
|
#include "bluetooth.h"
|
||||||
|
#include "indicator.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#include "transport.h"
|
||||||
|
#include "battery.h"
|
||||||
|
|
||||||
|
extern matrix_row_t matrix[MATRIX_ROWS];
|
||||||
|
extern bluetooth_transport_t bluetooth_transport;
|
||||||
|
|
||||||
|
static uint32_t lpm_timer_buffer;
|
||||||
|
static bool lpm_time_up = false;
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void lpm_timer_reset(void) {
|
||||||
|
lpm_time_up = false;
|
||||||
|
lpm_timer_buffer = sync_timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
void lpm_timer_stop(void) {
|
||||||
|
lpm_time_up = false;
|
||||||
|
lpm_timer_buffer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool lpm_any_matrix_action(void) { return memcmp(matrix, empty_matrix, sizeof(empty_matrix)); }
|
||||||
|
|
||||||
|
/* Implement of entering low power mode and wakeup varies per mcu or platform */
|
||||||
|
__attribute__((weak)) void enter_power_mode(pm_t mode) {}
|
||||||
|
|
||||||
|
__attribute__((weak)) bool usb_power_connected(void) {
|
||||||
|
#ifdef USB_POWER_SENSE_PIN
|
||||||
|
return readPin(USB_POWER_SENSE_PIN) == USB_POWER_CONNECTED_LEVEL;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void lpm_task(void) {
|
||||||
|
if (!lpm_time_up && sync_timer_elapsed32(lpm_timer_buffer) > RUN_MODE_PROCESS_TIME) {
|
||||||
|
lpm_time_up = true;
|
||||||
|
lpm_timer_buffer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (get_transport() == TRANSPORT_BLUETOOTH && lpm_time_up && !indicator_is_running()
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
&& led_matrix_is_driver_shutdown()
|
||||||
|
#endif
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
&& rgb_matrix_is_driver_shutdown()
|
||||||
|
#endif
|
||||||
|
&& !lpm_any_matrix_action() && !battery_power_on_sample())
|
||||||
|
|
||||||
|
enter_power_mode(LOW_POWER_MODE);
|
||||||
|
}
|
||||||
30
keyboards/keychron/bluetooth/lpm.h
Normal file
30
keyboards/keychron/bluetooth/lpm.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
#ifndef RUN_MODE_PROCESS_TIME
|
||||||
|
# define RUN_MODE_PROCESS_TIME 1000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum { PM_RUN, PM_LOW_POWER_RUN, PM_SLEEP, PM_LOW_POWER_SLEEP, PM_STOP0, PM_STOP1, PM_STOP2, PM_STANDBY_WITH_RAM, PM_STANDBY, PM_SHUTDOWN } pm_t;
|
||||||
|
|
||||||
|
void lpm_init(void);
|
||||||
|
void lpm_timer_reset(void);
|
||||||
|
void lpm_timer_stop(void);
|
||||||
|
bool usb_power_connected(void);
|
||||||
|
void enter_power_mode(pm_t mode);
|
||||||
|
void lpm_task(void);
|
||||||
330
keyboards/keychron/bluetooth/lpm_stm32l432.c
Normal file
330
keyboards/keychron/bluetooth/lpm_stm32l432.c
Normal file
@ -0,0 +1,330 @@
|
|||||||
|
/* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
*
|
||||||
|
* Filename: lpm_stm32l432.c
|
||||||
|
*
|
||||||
|
* Description: Contains low power mode implementation
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include "quantum.h"
|
||||||
|
#include <usb_main.h>
|
||||||
|
#include "bluetooth.h"
|
||||||
|
#include "indicator.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#include "transport.h"
|
||||||
|
#include "battery.h"
|
||||||
|
#include "report_buffer.h"
|
||||||
|
#include "stm32_bd.inc"
|
||||||
|
#include "debounce.h"
|
||||||
|
|
||||||
|
extern pin_t row_pins[MATRIX_ROWS];
|
||||||
|
extern void select_all_cols(void);
|
||||||
|
extern bluetooth_transport_t bluetooth_transport;
|
||||||
|
|
||||||
|
static pm_t power_mode = PM_RUN;
|
||||||
|
|
||||||
|
static inline void stm32_clock_fast_init(void);
|
||||||
|
|
||||||
|
bool lpm_set(pm_t mode) {
|
||||||
|
switch (mode) {
|
||||||
|
#ifdef LOW_POWER_RUN_MODE_ENABLE
|
||||||
|
case PM_RUN:
|
||||||
|
if (power_mode != PM_LOW_POWER_RUN)) return;
|
||||||
|
/* Set main regulator */
|
||||||
|
PWR->CR1 &= ~PWR_CR1_LPR;
|
||||||
|
while (PWR->SR2 & PWR_SR2_REGLPF)
|
||||||
|
;
|
||||||
|
// TODO: restore sysclk
|
||||||
|
return true;
|
||||||
|
// break;
|
||||||
|
|
||||||
|
case PM_LOW_POWER_RUN:
|
||||||
|
if (power_mode != PM_RUN) return;
|
||||||
|
|
||||||
|
// FLASH->ACR |= FLASH_ACR_RUN_PD; // Optional
|
||||||
|
// TODO: Decrease sysclk below 2 MHz
|
||||||
|
PWR->CR1 |= PWR_CR1_LPR;
|
||||||
|
return true;
|
||||||
|
// break;
|
||||||
|
#endif
|
||||||
|
case PM_SLEEP:
|
||||||
|
/* Wake source: Any interrupt or event */
|
||||||
|
if (power_mode != PM_RUN) return false;
|
||||||
|
|
||||||
|
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
break;
|
||||||
|
|
||||||
|
#ifdef LOW_POWER_RUN_MODE_ENABLE
|
||||||
|
case PM_LOW_POWER_SLEEP:
|
||||||
|
/* Wake source: Any interrupt or event */
|
||||||
|
if (power_mode != PM_LOW_POWER_RUN) return; /* Can only transit from PM_LOW_POWER_RUN */
|
||||||
|
|
||||||
|
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
__WFI();
|
||||||
|
exit_low_power_mode();
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
case PM_STOP0:
|
||||||
|
/* Wake source: Reset pin, all I/Os, BOR, PVD, PVM, RTC, LCD, IWDG,
|
||||||
|
COMPx, USARTx, LPUART1, I2Cx, LPTIMx, USB, SWPMI */
|
||||||
|
if (power_mode != PM_RUN) return false;
|
||||||
|
|
||||||
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
PWR->CR1 |= PWR_CR1_LPMS_STOP0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PM_STOP1:
|
||||||
|
/* Wake source: Reset pin, all I/Os, BOR, PVD, PVM, RTC, LCD, IWDG,
|
||||||
|
COMPx, USARTx, LPUART1, I2Cx, LPTIMx, USB, SWPMI */
|
||||||
|
if (power_mode != PM_RUN && power_mode != PM_LOW_POWER_RUN) return false;
|
||||||
|
|
||||||
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
PWR->CR1 |= PWR_CR1_LPMS_STOP1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PM_STOP2:
|
||||||
|
/* Wake source: Reset pin, all I/Os, BOR, PVD, PVM, RTC, LCD, IWDG,
|
||||||
|
COMPx (x=1, 2), I2C3, LPUART1, LPTIM1, LPTIM2 */
|
||||||
|
if (power_mode != PM_RUN) return false;
|
||||||
|
|
||||||
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
PWR->CR1 |= PWR_CR1_LPMS_STOP2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PM_STANDBY_WITH_RAM:
|
||||||
|
/* Wake source: Reset, 5 I/O(PA0, PC13, PE6, PA2, PC5), BOR, RTC, IWDG */
|
||||||
|
if (power_mode != PM_RUN && power_mode != PM_LOW_POWER_RUN) return false;
|
||||||
|
|
||||||
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
PWR->CR1 |= PWR_CR1_LPMS_STANDBY;
|
||||||
|
PWR->CR3 |= PWR_CR3_RRS;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PM_STANDBY:
|
||||||
|
/* Wake source: Reset, 2 I/O(PA0, PA2) in STM32L432Kx,, BOR, RTC, IWDG */
|
||||||
|
if (power_mode != PM_RUN && power_mode != PM_LOW_POWER_RUN) return false;
|
||||||
|
|
||||||
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
PWR->CR1 |= PWR_CR1_LPMS_STANDBY;
|
||||||
|
PWR->CR3 &= ~PWR_CR3_RRS;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PM_SHUTDOWN:
|
||||||
|
/* Wake source: Reset, 2 I/O(PA0, PA2) in STM32L432Kx, RTC */
|
||||||
|
if (power_mode != PM_RUN && power_mode != PM_LOW_POWER_RUN) return false;
|
||||||
|
|
||||||
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
PWR->CR1 |= PWR_CR1_LPMS_SHUTDOWN;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void enter_low_power_mode_prepare(void) {
|
||||||
|
#if defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE)
|
||||||
|
/* Usb unit is actived and running, stop and disconnect first */
|
||||||
|
usbStop(&USBD1);
|
||||||
|
usbDisconnectBus(&USBD1);
|
||||||
|
|
||||||
|
/* Isolate USB to save power.*/
|
||||||
|
PWR->CR2 &= ~PWR_CR2_USV; /*PWR_CR2_USV is available on STM32L4x2xx and STM32L4x3xx devices only. */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
palEnableLineEvent(BLUETOOTH_INT_INPUT_PIN, PAL_EVENT_MODE_FALLING_EDGE);
|
||||||
|
palEnableLineEvent(USB_POWER_SENSE_PIN, PAL_EVENT_MODE_BOTH_EDGES);
|
||||||
|
|
||||||
|
/* Enable key matrix wake up */
|
||||||
|
pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
|
||||||
|
|
||||||
|
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||||
|
if (row_pins[x] != NO_PIN) {
|
||||||
|
palEnableLineEvent(row_pins[x], PAL_EVENT_MODE_BOTH_EDGES);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
select_all_cols();
|
||||||
|
|
||||||
|
#if defined(DIP_SWITCH_PINS)
|
||||||
|
# define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(pin_t))
|
||||||
|
static pin_t dip_switch_pad[] = DIP_SWITCH_PINS;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
|
||||||
|
setPinInputLow(dip_switch_pad[i]);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void lpm_wakeup(void) {
|
||||||
|
chSysLock();
|
||||||
|
stm32_clock_fast_init();
|
||||||
|
chSysUnlock();
|
||||||
|
|
||||||
|
if (bluetooth_transport.init) bluetooth_transport.init(true);
|
||||||
|
|
||||||
|
chSysLock();
|
||||||
|
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
|
||||||
|
PWR->SCR |= PWR_SCR_CWUF;
|
||||||
|
PWR->SCR |= PWR_SCR_CSBF;
|
||||||
|
|
||||||
|
/* TIMx is disable during stop/standby/sleep mode, init after wakeup */
|
||||||
|
stInit();
|
||||||
|
timer_init();
|
||||||
|
chSysUnlock();
|
||||||
|
battery_init();
|
||||||
|
|
||||||
|
/* Disable all wake up pins */
|
||||||
|
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||||
|
if (row_pins[x] != NO_PIN) {
|
||||||
|
palDisableLineEvent(row_pins[x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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()) {
|
||||||
|
hsi48_init();
|
||||||
|
/* 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);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* NOTE:
|
||||||
|
* 1. Shall not use PM_LOW_POWER_RUN, PM_LOW_POWER_SLEEP, due to PM_LOW_POWER_RUN
|
||||||
|
* need to decrease system clock below 2 MHz. Dynamic clock is not yet supported
|
||||||
|
* for STM32L432xx in latest ChibiOS 21.6.0 so far.
|
||||||
|
* 2. Care must be taken to use PM_STANDBY_WITH_RAM, PM_STANDBY, PM_SHUTDOWN due to
|
||||||
|
* limited wake source, thus can't be waken via keyscan. PM_SHUTDOWN need LSE.
|
||||||
|
* 3. Reference from AN4621: STM32L4 and STM32L4+ ultra-low-power features overview
|
||||||
|
* for detail wake source
|
||||||
|
*/
|
||||||
|
|
||||||
|
void enter_power_mode(pm_t mode) {
|
||||||
|
#if defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE)
|
||||||
|
/* Don't enter low power mode if attached to the host */
|
||||||
|
if (mode > PM_SLEEP && usb_power_connected()) return;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!lpm_set(mode)) return;
|
||||||
|
enter_low_power_mode_prepare();
|
||||||
|
|
||||||
|
// __DSB();
|
||||||
|
__WFI();
|
||||||
|
// __ISB();
|
||||||
|
|
||||||
|
lpm_wakeup();
|
||||||
|
lpm_timer_reset();
|
||||||
|
report_buffer_init();
|
||||||
|
|
||||||
|
/* Call debounce_free() to avoid memory leak as debounce_init() invoked in matrix_init() allocates
|
||||||
|
* new memory when using per row/key debounce
|
||||||
|
*/
|
||||||
|
debounce_free();
|
||||||
|
matrix_init();
|
||||||
|
power_mode = PM_RUN;
|
||||||
|
}
|
||||||
|
|
||||||
|
void usb_power_connect(void) {
|
||||||
|
PWR->CR2 |= PWR_CR2_USV;
|
||||||
|
}
|
||||||
|
|
||||||
|
void usb_power_disconnect(void) {
|
||||||
|
PWR->CR2 &= ~PWR_CR2_USV;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is a simplified version of stm32_clock_init() by removing unnecessary clock initlization
|
||||||
|
* code snippet. The original stm32_clock_init() take about 2ms, but ckbt51 sends data via uart
|
||||||
|
* about 200us after wakeup pin is assert, it means that we must get everything ready before data
|
||||||
|
* coming when wakeup pin interrupt of MCU is triggerred.
|
||||||
|
* Here we reduce clock init time to less than 100us.
|
||||||
|
*/
|
||||||
|
void stm32_clock_fast_init(void) {
|
||||||
|
#if !STM32_NO_INIT
|
||||||
|
/* Clocks setup.*/
|
||||||
|
msi_init(); // 6.x us
|
||||||
|
hsi16_init(); // 4.x us
|
||||||
|
|
||||||
|
/* PLLs activation, if required.*/
|
||||||
|
pll_init();
|
||||||
|
pllsai1_init();
|
||||||
|
pllsai2_init();
|
||||||
|
/* clang-format off */
|
||||||
|
/* Other clock-related settings (dividers, MCO etc).*/
|
||||||
|
RCC->CFGR = STM32_MCOPRE | STM32_MCOSEL | STM32_STOPWUCK |
|
||||||
|
STM32_PPRE2 | STM32_PPRE1 | STM32_HPRE;
|
||||||
|
/* CCIPR register initialization, note, must take care of the _OFF
|
||||||
|
pseudo settings.*/
|
||||||
|
{
|
||||||
|
uint32_t ccipr = STM32_DFSDMSEL | STM32_SWPMI1SEL | STM32_ADCSEL |
|
||||||
|
STM32_CLK48SEL | STM32_LPTIM2SEL | STM32_LPTIM1SEL |
|
||||||
|
STM32_I2C3SEL | STM32_I2C2SEL | STM32_I2C1SEL |
|
||||||
|
STM32_UART5SEL | STM32_UART4SEL | STM32_USART3SEL |
|
||||||
|
STM32_USART2SEL | STM32_USART1SEL | STM32_LPUART1SEL;
|
||||||
|
/* clang-format on */
|
||||||
|
# if STM32_SAI2SEL != STM32_SAI2SEL_OFF
|
||||||
|
ccipr |= STM32_SAI2SEL;
|
||||||
|
# endif
|
||||||
|
# if STM32_SAI1SEL != STM32_SAI1SEL_OFF
|
||||||
|
ccipr |= STM32_SAI1SEL;
|
||||||
|
# endif
|
||||||
|
RCC->CCIPR = ccipr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set flash WS's for SYSCLK source */
|
||||||
|
if (STM32_FLASHBITS > STM32_MSI_FLASHBITS) {
|
||||||
|
FLASH->ACR = (FLASH->ACR & ~FLASH_ACR_LATENCY_Msk) | STM32_FLASHBITS;
|
||||||
|
while ((FLASH->ACR & FLASH_ACR_LATENCY_Msk) != (STM32_FLASHBITS & FLASH_ACR_LATENCY_Msk)) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Switching to the configured SYSCLK source if it is different from MSI.*/
|
||||||
|
# if (STM32_SW != STM32_SW_MSI)
|
||||||
|
RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */
|
||||||
|
/* Wait until SYSCLK is stable.*/
|
||||||
|
while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
|
||||||
|
;
|
||||||
|
# endif
|
||||||
|
|
||||||
|
/* Reduce the flash WS's for SYSCLK source if they are less than MSI WSs */
|
||||||
|
if (STM32_FLASHBITS < STM32_MSI_FLASHBITS) {
|
||||||
|
FLASH->ACR = (FLASH->ACR & ~FLASH_ACR_LATENCY_Msk) | STM32_FLASHBITS;
|
||||||
|
while ((FLASH->ACR & FLASH_ACR_LATENCY_Msk) != (STM32_FLASHBITS & FLASH_ACR_LATENCY_Msk)) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* STM32_NO_INIT */
|
||||||
|
}
|
||||||
19
keyboards/keychron/bluetooth/lpm_stm32l432.h
Normal file
19
keyboards/keychron/bluetooth/lpm_stm32l432.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
typedef enum { PM_RUN, PM_LOW_POWER_RUN, PM_SLEEP, PM_LOW_POWER_SLEEP, PM_STOP0, PM_STOP1, PM_STOP2, PM_STANDBY_WITH_RAM, PM_STANDBY, PM_SHUTDOWN } pm_t;
|
||||||
141
keyboards/keychron/bluetooth/report_buffer.c
Normal file
141
keyboards/keychron/bluetooth/report_buffer.c
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/* 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 "report_buffer.h"
|
||||||
|
#include "bluetooth.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
|
||||||
|
/* The report buffer is mainly used to fix key press lost issue of macro
|
||||||
|
* when bluetooth module fifo isn't large enough. The maximun macro
|
||||||
|
* string length is determined by this queue size, and should be
|
||||||
|
* REPORT_BUFFER_QUEUE_SIZE devided by 2 since each character is implemented
|
||||||
|
* by sending a key pressing then a key releasing report.
|
||||||
|
* Please note that it cosume sizeof(report_buffer_t) * REPORT_BUFFER_QUEUE_SIZE
|
||||||
|
* bytes RAM, with default setting, used RAM size is
|
||||||
|
* sizeof(report_buffer_t) * 256 = 34* 256 = 8704 bytes
|
||||||
|
*/
|
||||||
|
#ifndef REPORT_BUFFER_QUEUE_SIZE
|
||||||
|
# define REPORT_BUFFER_QUEUE_SIZE 512
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern bluetooth_transport_t bluetooth_transport;
|
||||||
|
|
||||||
|
/* report_interval value should be less than bluetooth connection interval because
|
||||||
|
* it takes some time for communicating between mcu and bluetooth module. Carefully
|
||||||
|
* set this value to feed the bt module so that we don't lost the key report nor lost
|
||||||
|
* the anchor point of bluetooth interval. The bluetooth connection interval varies
|
||||||
|
* if BLE is used, invoke report_buffer_set_inverval() to update the value
|
||||||
|
*/
|
||||||
|
uint8_t report_interval = DEFAULT_REPORT_INVERVAL_MS;
|
||||||
|
|
||||||
|
static uint32_t report_timer_buffer = 0;
|
||||||
|
uint32_t retry_time_buffer = 0;
|
||||||
|
report_buffer_t report_buffer_queue[REPORT_BUFFER_QUEUE_SIZE];
|
||||||
|
uint16_t report_buffer_queue_head;
|
||||||
|
uint16_t report_buffer_queue_tail;
|
||||||
|
report_buffer_t kb_rpt;
|
||||||
|
uint8_t retry = 0;
|
||||||
|
|
||||||
|
void report_buffer_init(void) {
|
||||||
|
// Initialise the report queue
|
||||||
|
memset(&report_buffer_queue, 0, sizeof(report_buffer_queue));
|
||||||
|
report_buffer_queue_head = 0;
|
||||||
|
report_buffer_queue_tail = 0;
|
||||||
|
retry = 0;
|
||||||
|
report_timer_buffer = sync_timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool report_buffer_enqueue(report_buffer_t *report) {
|
||||||
|
uint16_t next = (report_buffer_queue_head + 1) % REPORT_BUFFER_QUEUE_SIZE;
|
||||||
|
if (next == report_buffer_queue_tail) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
report_buffer_queue[report_buffer_queue_head] = *report;
|
||||||
|
report_buffer_queue_head = next;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool report_buffer_dequeue(report_buffer_t *report) {
|
||||||
|
if (report_buffer_queue_head == report_buffer_queue_tail) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
*report = report_buffer_queue[report_buffer_queue_tail];
|
||||||
|
report_buffer_queue_tail = (report_buffer_queue_tail + 1) % REPORT_BUFFER_QUEUE_SIZE;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool report_buffer_is_empty() {
|
||||||
|
return report_buffer_queue_head == report_buffer_queue_tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
void report_buffer_update_timer(void) {
|
||||||
|
report_timer_buffer = sync_timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool report_buffer_next_inverval(void) {
|
||||||
|
return sync_timer_elapsed32(report_timer_buffer) > report_interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
void report_buffer_set_inverval(uint8_t interval) {
|
||||||
|
report_interval = interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t report_buffer_get_retry(void) {
|
||||||
|
return retry;
|
||||||
|
}
|
||||||
|
|
||||||
|
void report_buffer_set_retry(uint8_t times) {
|
||||||
|
retry = times;
|
||||||
|
}
|
||||||
|
|
||||||
|
void report_buffer_task(void) {
|
||||||
|
if (bluetooth_get_state() == BLUETOOTH_CONNECTED && (!report_buffer_is_empty() || retry) && report_buffer_next_inverval()) {
|
||||||
|
bool pending_data = false;
|
||||||
|
|
||||||
|
if (!retry) {
|
||||||
|
if (report_buffer_dequeue(&kb_rpt) && kb_rpt.type != REPORT_TYPE_NONE) {
|
||||||
|
if (sync_timer_read32() > 2) {
|
||||||
|
pending_data = true;
|
||||||
|
retry = RETPORT_RETRY_COUNT;
|
||||||
|
retry_time_buffer = sync_timer_read32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (sync_timer_elapsed32(retry_time_buffer) > 7) {
|
||||||
|
pending_data = true;
|
||||||
|
--retry;
|
||||||
|
retry_time_buffer = sync_timer_read32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pending_data) {
|
||||||
|
#if defined(NKRO_ENABLE) && defined(BLUETOOTH_NKRO_ENABLE)
|
||||||
|
if (kb_rpt.type == REPORT_TYPE_NKRO && bluetooth_transport.send_nkro) {
|
||||||
|
bluetooth_transport.send_nkro(&kb_rpt.nkro.mods);
|
||||||
|
} else if (kb_rpt.type == REPORT_TYPE_KB && bluetooth_transport.send_keyboard)
|
||||||
|
bluetooth_transport.send_keyboard(&kb_rpt.keyboard.mods);
|
||||||
|
#else
|
||||||
|
if (kb_rpt.type == REPORT_TYPE_KB && bluetooth_transport.send_keyboard) bluetooth_transport.send_keyboard(&kb_rpt.keyboard.mods);
|
||||||
|
#endif
|
||||||
|
if (kb_rpt.type == REPORT_TYPE_CONSUMER && bluetooth_transport.send_consumer) bluetooth_transport.send_consumer(kb_rpt.consumer);
|
||||||
|
report_timer_buffer = sync_timer_read32();
|
||||||
|
lpm_timer_reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
56
keyboards/keychron/bluetooth/report_buffer.h
Normal file
56
keyboards/keychron/bluetooth/report_buffer.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
#include "report.h"
|
||||||
|
|
||||||
|
/* Default report interval value */
|
||||||
|
#ifndef DEFAULT_REPORT_INVERVAL_MS
|
||||||
|
# define DEFAULT_REPORT_INVERVAL_MS 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Default report interval value */
|
||||||
|
#ifndef RETPORT_RETRY_COUNT
|
||||||
|
# define RETPORT_RETRY_COUNT 30
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum {
|
||||||
|
REPORT_TYPE_NONE,
|
||||||
|
REPORT_TYPE_KB,
|
||||||
|
REPORT_TYPE_NKRO,
|
||||||
|
REPORT_TYPE_CONSUMER,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t type;
|
||||||
|
union {
|
||||||
|
report_keyboard_t keyboard;
|
||||||
|
report_nkro_t nkro;
|
||||||
|
uint16_t consumer;
|
||||||
|
};
|
||||||
|
} report_buffer_t;
|
||||||
|
|
||||||
|
void report_buffer_init(void);
|
||||||
|
bool report_buffer_enqueue(report_buffer_t *report);
|
||||||
|
bool report_buffer_dequeue(report_buffer_t *report);
|
||||||
|
bool report_buffer_is_empty(void);
|
||||||
|
void report_buffer_update_timer(void);
|
||||||
|
bool report_buffer_next_inverval(void);
|
||||||
|
void report_buffer_set_inverval(uint8_t interval);
|
||||||
|
uint8_t report_buffer_get_retry(void);
|
||||||
|
void report_buffer_set_retry(uint8_t times);
|
||||||
|
void report_buffer_task(void);
|
||||||
43
keyboards/keychron/bluetooth/rtc_timer.c
Normal file
43
keyboards/keychron/bluetooth/rtc_timer.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* Copyright 2023 @ 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 "hal.h"
|
||||||
|
|
||||||
|
#if (HAL_USE_RTC)
|
||||||
|
|
||||||
|
# include "rtc_timer.h"
|
||||||
|
|
||||||
|
void rtc_timer_init(void) {
|
||||||
|
rtc_timer_clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void rtc_timer_clear(void) {
|
||||||
|
RTCDateTime tm = {0, 0, 0, 0, 0, 0};
|
||||||
|
rtcSetTime(&RTCD1, &tm);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t rtc_timer_read_ms(void) {
|
||||||
|
RTCDateTime tm;
|
||||||
|
rtcGetTime(&RTCD1, &tm);
|
||||||
|
|
||||||
|
return tm.millisecond;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t rtc_timer_elapsed_ms(uint32_t last) {
|
||||||
|
return TIMER_DIFF_32(rtc_timer_read_ms(), last);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
43
keyboards/keychron/bluetooth/rtc_timer.h
Normal file
43
keyboards/keychron/bluetooth/rtc_timer.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* Copyright 2023 @ 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
|
||||||
|
|
||||||
|
#include "timer.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define RTC_MAX_TIME (24 * 3600 * 1000) // Set to 1 day
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
# define TIMER_DIFF(a, b, max) ((max == UINT8_MAX) ? ((uint8_t)((a) - (b))) : ((max == UINT16_MAX) ? ((uint16_t)((a) - (b))) : ((max == UINT32_MAX) ? ((uint32_t)((a) - (b))) : ((a) >= (b) ? (a) - (b) : (max) + 1 - (b) + (a)))))
|
||||||
|
# define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX)
|
||||||
|
# define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX)
|
||||||
|
# define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX)
|
||||||
|
# define TIMER_DIFF_RAW(a, b) TIMER_DIFF_8(a, b)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void rtc_timer_init(void);
|
||||||
|
void rtc_timer_clear(void);
|
||||||
|
uint32_t rtc_timer_read_ms(void);
|
||||||
|
uint32_t rtc_timer_elapsed_ms(uint32_t last);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
190
keyboards/keychron/bluetooth/transport.c
Normal file
190
keyboards/keychron/bluetooth/transport.c
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
/* 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 "indicator.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#if defined(PROTOCOL_CHIBIOS)
|
||||||
|
# include <usb_main.h>
|
||||||
|
#endif
|
||||||
|
#include "transport.h"
|
||||||
|
|
||||||
|
#ifndef REINIT_LED_DRIVER
|
||||||
|
# define REINIT_LED_DRIVER 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(PROTOCOL_CHIBIOS)
|
||||||
|
extern host_driver_t chibios_driver;
|
||||||
|
#endif
|
||||||
|
extern host_driver_t bluetooth_driver;
|
||||||
|
extern keymap_config_t keymap_config;
|
||||||
|
|
||||||
|
static transport_t transport = TRANSPORT_USB;
|
||||||
|
|
||||||
|
#ifdef NKRO_ENABLE
|
||||||
|
nkro_t nkro = {false, false};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void transport_changed(transport_t new_transport);
|
||||||
|
|
||||||
|
__attribute__((weak)) void bt_transport_enable(bool enable) {
|
||||||
|
if (enable) {
|
||||||
|
if (host_get_driver() != &bluetooth_driver) {
|
||||||
|
host_set_driver(&bluetooth_driver);
|
||||||
|
|
||||||
|
/* Disconnect and reconnect to sync the bluetooth state
|
||||||
|
* TODO: query bluetooth state to sync
|
||||||
|
*/
|
||||||
|
bluetooth_disconnect();
|
||||||
|
bluetooth_connect();
|
||||||
|
// TODO: Clear USB report
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
indicator_stop();
|
||||||
|
|
||||||
|
if (bluetooth_get_state() == BLUETOOTH_CONNECTED) {
|
||||||
|
report_keyboard_t empty_report = {0};
|
||||||
|
bluetooth_driver.send_keyboard(&empty_report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* There is no dedicated pin for USB power on chip such as STM32L432, but USB power
|
||||||
|
* can be connected and disconnected via registers.
|
||||||
|
* Overwrite these two functions if such chip is used. */
|
||||||
|
__attribute__((weak)) void usb_power_connect(void) {}
|
||||||
|
__attribute__((weak)) void usb_power_disconnect(void) {}
|
||||||
|
|
||||||
|
__attribute__((weak)) void usb_transport_enable(bool enable) {
|
||||||
|
if (enable) {
|
||||||
|
if (host_get_driver() != &chibios_driver) {
|
||||||
|
#if !defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE)
|
||||||
|
usb_power_connect();
|
||||||
|
usb_start(&USBD1);
|
||||||
|
#endif
|
||||||
|
host_set_driver(&chibios_driver);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (USB_DRIVER.state == USB_ACTIVE) {
|
||||||
|
report_keyboard_t empty_report = {0};
|
||||||
|
chibios_driver.send_keyboard(&empty_report);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE)
|
||||||
|
usbStop(&USBD1);
|
||||||
|
usbDisconnectBus(&USBD1);
|
||||||
|
usb_power_disconnect();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_transport(transport_t new_transport) {
|
||||||
|
if (transport != new_transport) {
|
||||||
|
transport = new_transport;
|
||||||
|
|
||||||
|
clear_keyboard();
|
||||||
|
|
||||||
|
switch (transport) {
|
||||||
|
case TRANSPORT_USB:
|
||||||
|
usb_transport_enable(true);
|
||||||
|
bt_transport_enable(false);
|
||||||
|
lpm_timer_stop();
|
||||||
|
#ifdef NKRO_ENABLE
|
||||||
|
# if defined(BLUETOOTH_NKRO_ENABLE)
|
||||||
|
nkro.bluetooth = keymap_config.nkro;
|
||||||
|
# endif
|
||||||
|
keymap_config.nkro = nkro.usb;
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TRANSPORT_BLUETOOTH:
|
||||||
|
bt_transport_enable(true);
|
||||||
|
usb_transport_enable(false);
|
||||||
|
lpm_timer_reset();
|
||||||
|
#if defined(NKRO_ENABLE)
|
||||||
|
nkro.usb = keymap_config.nkro;
|
||||||
|
# if defined(BLUETOOTH_NKRO_ENABLE)
|
||||||
|
keymap_config.nkro = nkro.bluetooth;
|
||||||
|
# else
|
||||||
|
keymap_config.nkro = FALSE;
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
transport_changed(transport);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transport_t get_transport(void) {
|
||||||
|
return transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Changing transport may cause bronw-out reset of led driver
|
||||||
|
* withoug MCU reset, which lead backlight to not work,
|
||||||
|
* reinit the led driver workgound this issue */
|
||||||
|
static void reinit_led_drvier(void) {
|
||||||
|
/* Wait circuit to discharge for a while */
|
||||||
|
systime_t start = chVTGetSystemTime();
|
||||||
|
while (chTimeI2MS(chVTTimeElapsedSinceX(start)) < 100) {
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
led_matrix_init();
|
||||||
|
#endif
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
rgb_matrix_init();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void transport_changed(transport_t new_transport) {
|
||||||
|
#if (REINIT_LED_DRIVER)
|
||||||
|
reinit_led_drvier();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_TIMEOUT)
|
||||||
|
# if (RGB_MATRIX_TIMEOUT > 0)
|
||||||
|
rgb_matrix_disable_timeout_set(RGB_MATRIX_TIMEOUT_INFINITE);
|
||||||
|
rgb_matrix_disable_time_reset();
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_TIMEOUT)
|
||||||
|
# if (LED_MATRIX_TIMEOUT > 0)
|
||||||
|
led_matrix_disable_timeout_set(LED_MATRIX_TIMEOUT_INFINITE);
|
||||||
|
led_matrix_disable_time_reset();
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void usb_remote_wakeup(void) {
|
||||||
|
if (USB_DRIVER.state == USB_SUSPENDED) {
|
||||||
|
while (USB_DRIVER.state == USB_SUSPENDED) {
|
||||||
|
/* Do this in the suspended state */
|
||||||
|
suspend_power_down(); // on AVR this deep sleeps for 15ms
|
||||||
|
/* Remote wakeup */
|
||||||
|
if (suspend_wakeup_condition()) {
|
||||||
|
usbWakeupHost(&USB_DRIVER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wait_ms(500);
|
||||||
|
/* Woken up */
|
||||||
|
// variables has been already cleared by the wakeup hook
|
||||||
|
send_keyboard_report();
|
||||||
|
}
|
||||||
|
}
|
||||||
39
keyboards/keychron/bluetooth/transport.h
Normal file
39
keyboards/keychron/bluetooth/transport.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
TRANSPORT_NONE,
|
||||||
|
TRANSPORT_USB,
|
||||||
|
TRANSPORT_BLUETOOTH,
|
||||||
|
} transport_t;
|
||||||
|
|
||||||
|
#ifdef NKRO_ENABLE
|
||||||
|
typedef struct {
|
||||||
|
bool usb : 1;
|
||||||
|
bool bluetooth : 1;
|
||||||
|
} nkro_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void set_transport(transport_t new_transport);
|
||||||
|
transport_t get_transport(void);
|
||||||
|
|
||||||
|
void bt_transport_enable(bool enable);
|
||||||
|
void usb_power_connect(void);
|
||||||
|
void usb_power_disconnect(void);
|
||||||
|
void usb_transport_enable(bool enable);
|
||||||
|
void usb_remote_wakeup(void);
|
||||||
6
keyboards/keychron/common/common.mk
Normal file
6
keyboards/keychron/common/common.mk
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
COMMON_DIR = common
|
||||||
|
SRC += $(COMMON_DIR)/matrix.c
|
||||||
|
|
||||||
|
VPATH += $(TOP_DIR)/keyboards/keychron/$(COMMON_DIR)
|
||||||
|
|
||||||
|
include $(TOP_DIR)/keyboards/keychron/$(COMMON_DIR)/debounce/debounce.mk
|
||||||
170
keyboards/keychron/common/debounce/asym_eager_defer_pk.c
Normal file
170
keyboards/keychron/common/debounce/asym_eager_defer_pk.c
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017 Alex Ong <the.onga@gmail.com>
|
||||||
|
* Copyright 2020 Andrei Purdea <andrei@purdea.ro>
|
||||||
|
* Copyright 2021 Simon Arlott
|
||||||
|
* Copyright 2024 @ keychron (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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Basic symmetric per-key algorithm. Uses an 8-bit counter per key.
|
||||||
|
When no state changes have occured for DEBOUNCE milliseconds, we push the state.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "debounce.h"
|
||||||
|
#include "timer.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifdef PROTOCOL_CHIBIOS
|
||||||
|
# if CH_CFG_USE_MEMCORE == FALSE
|
||||||
|
# error ChibiOS is configured without a memory allocator. Your keyboard may have set `#define CH_CFG_USE_MEMCORE FALSE`, which is incompatible with this debounce algorithm.
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define ROW_SHIFTER ((matrix_row_t)1)
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool pressed : 1;
|
||||||
|
uint8_t time : 7;
|
||||||
|
} debounce_counter_t;
|
||||||
|
|
||||||
|
extern uint8_t debounce_time;
|
||||||
|
|
||||||
|
static debounce_counter_t *debounce_counters = NULL;
|
||||||
|
static fast_timer_t last_time;
|
||||||
|
static bool counters_need_update;
|
||||||
|
static bool matrix_need_update;
|
||||||
|
static bool cooked_changed;
|
||||||
|
|
||||||
|
# define DEBOUNCE_ELAPSED 0
|
||||||
|
|
||||||
|
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time);
|
||||||
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
|
||||||
|
|
||||||
|
// we use num_rows rather than MATRIX_ROWS to support split keyboards
|
||||||
|
void asym_eager_defer_pk_debounce_init(uint8_t num_rows) {
|
||||||
|
debounce_counters = malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (uint8_t r = 0; r < num_rows; r++) {
|
||||||
|
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
|
||||||
|
debounce_counters[i++].time = DEBOUNCE_ELAPSED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void asym_eager_defer_pk_debounce_free(void) {
|
||||||
|
if (debounce_counters != NULL) {
|
||||||
|
free(debounce_counters);
|
||||||
|
debounce_counters = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool asym_eager_defer_pk_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
|
|
||||||
|
bool updated_last = false;
|
||||||
|
cooked_changed = false;
|
||||||
|
|
||||||
|
if (counters_need_update) {
|
||||||
|
fast_timer_t now = timer_read_fast();
|
||||||
|
fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
|
||||||
|
|
||||||
|
last_time = now;
|
||||||
|
updated_last = true;
|
||||||
|
if (elapsed_time > UINT8_MAX) {
|
||||||
|
elapsed_time = UINT8_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elapsed_time > 0) {
|
||||||
|
update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, elapsed_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed || matrix_need_update) {
|
||||||
|
if (!updated_last) {
|
||||||
|
last_time = timer_read_fast();
|
||||||
|
}
|
||||||
|
|
||||||
|
transfer_matrix_values(raw, cooked, num_rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cooked_changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time) {
|
||||||
|
debounce_counter_t *debounce_pointer = debounce_counters;
|
||||||
|
|
||||||
|
counters_need_update = false;
|
||||||
|
matrix_need_update = false;
|
||||||
|
|
||||||
|
for (uint8_t row = 0; row < num_rows; row++) {
|
||||||
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
|
matrix_row_t col_mask = (ROW_SHIFTER << col);
|
||||||
|
|
||||||
|
if (debounce_pointer->time != DEBOUNCE_ELAPSED) {
|
||||||
|
if (debounce_pointer->time <= elapsed_time) {
|
||||||
|
debounce_pointer->time = DEBOUNCE_ELAPSED;
|
||||||
|
|
||||||
|
if (debounce_pointer->pressed) {
|
||||||
|
// key-down: eager
|
||||||
|
matrix_need_update = true;
|
||||||
|
} else {
|
||||||
|
// key-up: defer
|
||||||
|
matrix_row_t cooked_next = (cooked[row] & ~col_mask) | (raw[row] & col_mask);
|
||||||
|
cooked_changed |= cooked_next ^ cooked[row];
|
||||||
|
cooked[row] = cooked_next;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
debounce_pointer->time -= elapsed_time;
|
||||||
|
counters_need_update = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debounce_pointer++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
|
||||||
|
debounce_counter_t *debounce_pointer = debounce_counters;
|
||||||
|
|
||||||
|
matrix_need_update = false;
|
||||||
|
|
||||||
|
for (uint8_t row = 0; row < num_rows; row++) {
|
||||||
|
matrix_row_t delta = raw[row] ^ cooked[row];
|
||||||
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
|
matrix_row_t col_mask = (ROW_SHIFTER << col);
|
||||||
|
|
||||||
|
if (delta & col_mask) {
|
||||||
|
if (debounce_pointer->time == DEBOUNCE_ELAPSED) {
|
||||||
|
debounce_pointer->pressed = (raw[row] & col_mask);
|
||||||
|
debounce_pointer->time = debounce_time;;
|
||||||
|
counters_need_update = true;
|
||||||
|
|
||||||
|
if (debounce_pointer->pressed) {
|
||||||
|
// key-down: eager
|
||||||
|
cooked[row] ^= col_mask;
|
||||||
|
cooked_changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (debounce_pointer->time != DEBOUNCE_ELAPSED) {
|
||||||
|
if (!debounce_pointer->pressed) {
|
||||||
|
// key-up: defer
|
||||||
|
debounce_pointer->time = DEBOUNCE_ELAPSED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debounce_pointer++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
keyboards/keychron/common/debounce/debounce.mk
Normal file
14
keyboards/keychron/common/debounce/debounce.mk
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
DEBOUNCE_DIR = common/debounce
|
||||||
|
SRC += \
|
||||||
|
$(DEBOUNCE_DIR)/sym_defer_g.c \
|
||||||
|
$(DEBOUNCE_DIR)/sym_defer_pr.c \
|
||||||
|
$(DEBOUNCE_DIR)/sym_defer_pk.c \
|
||||||
|
$(DEBOUNCE_DIR)/sym_eager_pr.c \
|
||||||
|
$(DEBOUNCE_DIR)/sym_eager_pk.c \
|
||||||
|
$(DEBOUNCE_DIR)/asym_eager_defer_pk.c \
|
||||||
|
$(DEBOUNCE_DIR)/none.c \
|
||||||
|
$(DEBOUNCE_DIR)/keychron_debounce.c
|
||||||
|
|
||||||
|
VPATH += $(TOP_DIR)/keyboards/keychron/$(DEBOUNCE_DIR)
|
||||||
|
|
||||||
|
OPT_DEFS += -DDYNAMIC_DEBOUNCE_ENABLE
|
||||||
20
keyboards/keychron/common/debounce/eeconfig_debounce.h
Normal file
20
keyboards/keychron/common/debounce/eeconfig_debounce.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
#define EECONFIG_SIZE_DEBOUNCE 2
|
||||||
|
|
||||||
214
keyboards/keychron/common/debounce/keychron_debounce.c
Normal file
214
keyboards/keychron/common/debounce/keychron_debounce.c
Normal file
@ -0,0 +1,214 @@
|
|||||||
|
|
||||||
|
/* Copyright 2024 @ Keychron (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 "keychron_debounce.h"
|
||||||
|
#include "raw_hid.h"
|
||||||
|
#include "quantum.h"
|
||||||
|
#include "eeconfig.h"
|
||||||
|
#include "eeconfig_kb.h"
|
||||||
|
#include "keychron_raw_hid.h"
|
||||||
|
|
||||||
|
#ifdef SPLIT_KEYBOARD
|
||||||
|
# pragma(error "Split keyboard is not supported")
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef DEBOUNCE
|
||||||
|
# define DEBOUNCE 5
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Maximum debounce: 255ms
|
||||||
|
#if DEBOUNCE > UINT8_MAX
|
||||||
|
# undef DEBOUNCE
|
||||||
|
# define DEBOUNCE UINT8_MAX
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef DEFAULT_DEBOUNCE_TYPE
|
||||||
|
#define DEFAULT_DEBOUNCE_TYPE DEBOUNCE_SYM_EAGER_PER_KEY
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define DEBOUNCE_SET_QMK 0
|
||||||
|
#define OFFSET_DEBOUNCE ((uint8_t *)(EECONFIG_BASE_DYNAMIC_DEBOUNCE))
|
||||||
|
|
||||||
|
static uint8_t debounce_type = 0;
|
||||||
|
uint8_t debounce_time = 0;
|
||||||
|
static debounce_t debounce_func = {NULL, NULL, NULL};
|
||||||
|
|
||||||
|
extern void sym_defer_g_debounce_init(uint8_t num_rows);
|
||||||
|
extern bool sym_defer_g_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||||
|
extern void sym_defer_g_debounce_free(void);
|
||||||
|
|
||||||
|
extern void sym_defer_pr_debounce_init(uint8_t num_rows);
|
||||||
|
extern bool sym_defer_pr_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||||
|
extern void sym_defer_pr_debounce_free(void);
|
||||||
|
|
||||||
|
extern void sym_defer_pk_debounce_init(uint8_t num_rows);
|
||||||
|
extern bool sym_defer_pk_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||||
|
extern void sym_defer_pk_debounce_free(void);
|
||||||
|
|
||||||
|
extern void sym_eager_pr_debounce_init(uint8_t num_rows);
|
||||||
|
extern bool sym_eager_pr_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||||
|
extern void sym_eager_pr_debounce_free(void);
|
||||||
|
|
||||||
|
extern void sym_eager_pk_debounce_init(uint8_t num_rows);
|
||||||
|
extern bool sym_eager_pk_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||||
|
extern void sym_eager_pk_debounce_free(void);
|
||||||
|
|
||||||
|
extern void asym_eager_defer_pk_debounce_init(uint8_t num_rows);
|
||||||
|
extern bool asym_eager_defer_pk_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||||
|
extern void asym_eager_defer_pk_debounce_free(void);
|
||||||
|
|
||||||
|
extern void none_debounce_init(uint8_t num_rows);
|
||||||
|
extern bool none_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||||
|
extern void none_debounce_free(void);
|
||||||
|
|
||||||
|
void debounce_set(uint8_t new_debounce_type, uint8_t time, bool force);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Debounce raw matrix events according to the choosen debounce algorithm.
|
||||||
|
*
|
||||||
|
* @param raw The current key state
|
||||||
|
* @param cooked The debounced key state
|
||||||
|
* @param num_rows Number of rows to debounce
|
||||||
|
* @param changed True if raw has changed since the last call
|
||||||
|
* @return true Cooked has new keychanges after debouncing
|
||||||
|
* @return false Cooked is the same as before
|
||||||
|
*/
|
||||||
|
|
||||||
|
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
|
if (debounce_func.debounce) debounce_func.debounce(raw, cooked, num_rows, changed);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void debounce_init(uint8_t num_rows) {
|
||||||
|
debounce_type = 0;
|
||||||
|
|
||||||
|
// debounce_set(DEBOUNCE_SYM_EAGER_PER_KEY, DEBOUNCE);
|
||||||
|
if (!eeconfig_is_enabled()) {
|
||||||
|
eeconfig_init();
|
||||||
|
}
|
||||||
|
uint8_t type = eeprom_read_byte(OFFSET_DEBOUNCE);
|
||||||
|
uint8_t time = eeprom_read_byte(OFFSET_DEBOUNCE + 1);
|
||||||
|
|
||||||
|
if (type >= DEBOUNCE_MAX) type = DEFAULT_DEBOUNCE_TYPE;
|
||||||
|
|
||||||
|
debounce_set(type, time, debounce_type == type);
|
||||||
|
}
|
||||||
|
|
||||||
|
void debounce_free(void) {
|
||||||
|
if (debounce_func.debounce_free) debounce_func.debounce_free();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool debounce_save(void) {
|
||||||
|
eeprom_update_byte(OFFSET_DEBOUNCE, debounce_type);
|
||||||
|
eeprom_update_byte(OFFSET_DEBOUNCE + 1, debounce_time);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void debounce_config_reset(void) {
|
||||||
|
debounce_set(DEFAULT_DEBOUNCE_TYPE, DEBOUNCE, true);
|
||||||
|
debounce_save();
|
||||||
|
}
|
||||||
|
|
||||||
|
void debounce_set(uint8_t new_debounce_type, uint8_t time, bool force) {
|
||||||
|
if (new_debounce_type == debounce_type && time == debounce_time && !force) return;
|
||||||
|
|
||||||
|
debounce_free();
|
||||||
|
|
||||||
|
debounce_type = new_debounce_type;
|
||||||
|
debounce_time = time;
|
||||||
|
|
||||||
|
if (debounce_time == 0) new_debounce_type = DEBOUNCE_NONE;
|
||||||
|
|
||||||
|
switch (new_debounce_type) {
|
||||||
|
case DEBOUNCE_SYM_DEFER_GLOBAL:
|
||||||
|
debounce_func.debounce_init = sym_defer_g_debounce_init;
|
||||||
|
debounce_func.debounce = sym_defer_g_debounce;
|
||||||
|
debounce_func.debounce_free = sym_defer_g_debounce_free;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DEBOUNCE_SYM_DEFER_PER_ROW:
|
||||||
|
debounce_func.debounce_init = sym_defer_pr_debounce_init;
|
||||||
|
debounce_func.debounce = sym_defer_pr_debounce;
|
||||||
|
debounce_func.debounce_free = sym_defer_pr_debounce_free;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DEBOUNCE_SYM_DEFER_PER_KEY:
|
||||||
|
debounce_func.debounce_init = sym_defer_pk_debounce_init;
|
||||||
|
debounce_func.debounce = sym_defer_pk_debounce;
|
||||||
|
debounce_func.debounce_free = sym_defer_pk_debounce_free;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DEBOUNCE_SYM_EAGER_PER_ROW:
|
||||||
|
debounce_func.debounce_init = sym_eager_pr_debounce_init;
|
||||||
|
debounce_func.debounce = sym_eager_pr_debounce;
|
||||||
|
debounce_func.debounce_free = sym_eager_pr_debounce_free;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DEBOUNCE_SYM_EAGER_PER_KEY:
|
||||||
|
debounce_func.debounce_init = sym_eager_pk_debounce_init;
|
||||||
|
debounce_func.debounce = sym_eager_pk_debounce;
|
||||||
|
debounce_func.debounce_free = sym_eager_pk_debounce_free;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DEBOUNCE_ASYM_EAGER_DEFER_PER_KEY:
|
||||||
|
debounce_func.debounce_init = asym_eager_defer_pk_debounce_init;
|
||||||
|
debounce_func.debounce = asym_eager_defer_pk_debounce;
|
||||||
|
debounce_func.debounce_free = asym_eager_defer_pk_debounce_free;
|
||||||
|
if (debounce_time > 127) debounce_time = 127;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DEBOUNCE_NONE:
|
||||||
|
debounce_func.debounce_init = none_debounce_init;
|
||||||
|
debounce_func.debounce = none_debounce;
|
||||||
|
debounce_func.debounce_free = none_debounce_free;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (debounce_func.debounce_init) debounce_func.debounce_init(MATRIX_ROWS);
|
||||||
|
}
|
||||||
|
|
||||||
|
void debounce_time_set(uint8_t time) {
|
||||||
|
debounce_time = time;
|
||||||
|
}
|
||||||
|
|
||||||
|
void debounce_rx(uint8_t *data, uint8_t length) {
|
||||||
|
uint8_t cmd = data[1];
|
||||||
|
switch (cmd) {
|
||||||
|
case DEBOUNCE_GET:
|
||||||
|
data[2] = 0;
|
||||||
|
data[3] = DEBOUNCE_SET_QMK;
|
||||||
|
data[4] = debounce_type;
|
||||||
|
data[5] = debounce_time;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DEBOUNCE_SET: {
|
||||||
|
uint8_t type = data[2];
|
||||||
|
uint8_t time = data[3];
|
||||||
|
if (type < DEBOUNCE_MAX) {
|
||||||
|
data[2] = 0;
|
||||||
|
debounce_set(type, time, false);
|
||||||
|
debounce_save();
|
||||||
|
} else
|
||||||
|
data[2] = 1;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
data[0] = 0xFF;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
56
keyboards/keychron/common/debounce/keychron_debounce.h
Normal file
56
keyboards/keychron/common/debounce/keychron_debounce.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "matrix.h"
|
||||||
|
|
||||||
|
enum {
|
||||||
|
DEBOUNCE_SYM_DEFER_GLOBAL,
|
||||||
|
DEBOUNCE_SYM_DEFER_PER_ROW,
|
||||||
|
DEBOUNCE_SYM_DEFER_PER_KEY,
|
||||||
|
DEBOUNCE_SYM_EAGER_PER_ROW,
|
||||||
|
DEBOUNCE_SYM_EAGER_PER_KEY,
|
||||||
|
DEBOUNCE_ASYM_EAGER_DEFER_PER_KEY,
|
||||||
|
DEBOUNCE_NONE,
|
||||||
|
DEBOUNCE_MAX,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void (*debounce_init)(uint8_t);
|
||||||
|
bool (*debounce)(matrix_row_t [], matrix_row_t [], uint8_t, bool);
|
||||||
|
void (*debounce_free)(void);
|
||||||
|
} debounce_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Debounce raw matrix events according to the choosen debounce algorithm.
|
||||||
|
*
|
||||||
|
* @param raw The current key state
|
||||||
|
* @param cooked The debounced key state
|
||||||
|
* @param num_rows Number of rows to debounce
|
||||||
|
* @param changed True if raw has changed since the last call
|
||||||
|
* @return true Cooked has new keychanges after debouncing
|
||||||
|
* @return false Cooked is the same as before
|
||||||
|
*/
|
||||||
|
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||||
|
|
||||||
|
void debounce_init(uint8_t num_rows);
|
||||||
|
void debounce_config_reset(void);
|
||||||
|
|
||||||
|
void debounce_free(void);
|
||||||
|
void debounce_rx(uint8_t *data, uint8_t length);
|
||||||
36
keyboards/keychron/common/debounce/none.c
Normal file
36
keyboards/keychron/common/debounce/none.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* Copyright 2021 Simon Arlott
|
||||||
|
*
|
||||||
|
* 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 "debounce.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void none_debounce_init(uint8_t num_rows) {}
|
||||||
|
|
||||||
|
bool none_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
|
bool cooked_changed = false;
|
||||||
|
|
||||||
|
if (changed) {
|
||||||
|
size_t matrix_size = num_rows * sizeof(matrix_row_t);
|
||||||
|
if (memcmp(cooked, raw, matrix_size) != 0) {
|
||||||
|
memcpy(cooked, raw, matrix_size);
|
||||||
|
cooked_changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cooked_changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void none_debounce_free(void) {}
|
||||||
36
keyboards/keychron/common/debounce/none.h
Normal file
36
keyboards/keychron/common/debounce/none.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* Copyright 2021 Simon Arlott
|
||||||
|
*
|
||||||
|
* 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 "debounce.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void none_debounce_init(uint8_t num_rows) {}
|
||||||
|
|
||||||
|
bool none_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
|
bool cooked_changed = false;
|
||||||
|
|
||||||
|
if (changed) {
|
||||||
|
size_t matrix_size = num_rows * sizeof(matrix_row_t);
|
||||||
|
if (memcmp(cooked, raw, matrix_size) != 0) {
|
||||||
|
memcpy(cooked, raw, matrix_size);
|
||||||
|
cooked_changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cooked_changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void none_debounce_free(void) {}
|
||||||
51
keyboards/keychron/common/debounce/sym_defer_g.c
Normal file
51
keyboards/keychron/common/debounce/sym_defer_g.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 Alex Ong<the.onga@gmail.com>
|
||||||
|
Copyright 2021 Simon Arlott
|
||||||
|
Copyright 2024 @ keychron (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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Basic global debounce algorithm. Used in 99% of keyboards at time of implementation
|
||||||
|
When no state changes have occured for DEBOUNCE milliseconds, we push the state.
|
||||||
|
*/
|
||||||
|
#include "debounce.h"
|
||||||
|
#include "timer.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
extern uint8_t debounce_time;
|
||||||
|
static bool debouncing = false;
|
||||||
|
static fast_timer_t debouncing_time;
|
||||||
|
|
||||||
|
void sym_defer_g_debounce_init(uint8_t num_rows) {}
|
||||||
|
|
||||||
|
bool sym_defer_g_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
|
bool cooked_changed = false;
|
||||||
|
|
||||||
|
if (changed) {
|
||||||
|
debouncing = true;
|
||||||
|
debouncing_time = timer_read_fast();
|
||||||
|
} else if (debouncing && timer_elapsed_fast(debouncing_time) >= debounce_time) {
|
||||||
|
size_t matrix_size = num_rows * sizeof(matrix_row_t);
|
||||||
|
if (memcmp(cooked, raw, matrix_size) != 0) {
|
||||||
|
memcpy(cooked, raw, matrix_size);
|
||||||
|
cooked_changed = true;
|
||||||
|
}
|
||||||
|
debouncing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cooked_changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_defer_g_debounce_free(void) {}
|
||||||
|
|
||||||
137
keyboards/keychron/common/debounce/sym_defer_pk.c
Normal file
137
keyboards/keychron/common/debounce/sym_defer_pk.c
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 Alex Ong<the.onga@gmail.com>
|
||||||
|
Copyright 2020 Andrei Purdea<andrei@purdea.ro>
|
||||||
|
Copyright 2021 Simon Arlott
|
||||||
|
Copyright 2024 @ keychron (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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Basic symmetric per-key algorithm. Uses an 8-bit counter per key.
|
||||||
|
When no state changes have occured for DEBOUNCE milliseconds, we push the state.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "debounce.h"
|
||||||
|
#include "timer.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifdef PROTOCOL_CHIBIOS
|
||||||
|
# if CH_CFG_USE_MEMCORE == FALSE
|
||||||
|
# error ChibiOS is configured without a memory allocator. Your keyboard may have set `#define CH_CFG_USE_MEMCORE FALSE`, which is incompatible with this debounce algorithm.
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#define ROW_SHIFTER ((matrix_row_t)1)
|
||||||
|
|
||||||
|
typedef uint8_t debounce_counter_t;
|
||||||
|
|
||||||
|
extern uint8_t debounce_time;
|
||||||
|
|
||||||
|
static debounce_counter_t *debounce_counters = NULL;
|
||||||
|
static fast_timer_t last_time;
|
||||||
|
static bool counters_need_update;
|
||||||
|
static bool cooked_changed;
|
||||||
|
|
||||||
|
# define DEBOUNCE_ELAPSED 0
|
||||||
|
|
||||||
|
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time);
|
||||||
|
static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
|
||||||
|
|
||||||
|
// we use num_rows rather than MATRIX_ROWS to support split keyboards
|
||||||
|
void sym_defer_pk_debounce_init(uint8_t num_rows) {
|
||||||
|
debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (uint8_t r = 0; r < num_rows; r++) {
|
||||||
|
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
|
||||||
|
debounce_counters[i++] = DEBOUNCE_ELAPSED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_defer_pk_debounce_free(void) {
|
||||||
|
if (debounce_counters != NULL) {
|
||||||
|
free(debounce_counters);
|
||||||
|
debounce_counters = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool sym_defer_pk_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
|
bool updated_last = false;
|
||||||
|
cooked_changed = false;
|
||||||
|
|
||||||
|
if (counters_need_update) {
|
||||||
|
fast_timer_t now = timer_read_fast();
|
||||||
|
fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
|
||||||
|
|
||||||
|
last_time = now;
|
||||||
|
updated_last = true;
|
||||||
|
if (elapsed_time > UINT8_MAX) {
|
||||||
|
elapsed_time = UINT8_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elapsed_time > 0) {
|
||||||
|
update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, elapsed_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed) {
|
||||||
|
if (!updated_last) {
|
||||||
|
last_time = timer_read_fast();
|
||||||
|
}
|
||||||
|
|
||||||
|
start_debounce_counters(raw, cooked, num_rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cooked_changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time) {
|
||||||
|
counters_need_update = false;
|
||||||
|
debounce_counter_t *debounce_pointer = debounce_counters;
|
||||||
|
for (uint8_t row = 0; row < num_rows; row++) {
|
||||||
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
|
if (*debounce_pointer != DEBOUNCE_ELAPSED) {
|
||||||
|
if (*debounce_pointer <= elapsed_time) {
|
||||||
|
*debounce_pointer = DEBOUNCE_ELAPSED;
|
||||||
|
matrix_row_t cooked_next = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col));
|
||||||
|
cooked_changed |= cooked[row] ^ cooked_next;
|
||||||
|
cooked[row] = cooked_next;
|
||||||
|
} else {
|
||||||
|
*debounce_pointer -= elapsed_time;
|
||||||
|
counters_need_update = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debounce_pointer++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
|
||||||
|
debounce_counter_t *debounce_pointer = debounce_counters;
|
||||||
|
for (uint8_t row = 0; row < num_rows; row++) {
|
||||||
|
matrix_row_t delta = raw[row] ^ cooked[row];
|
||||||
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
|
if (delta & (ROW_SHIFTER << col)) {
|
||||||
|
if (*debounce_pointer == DEBOUNCE_ELAPSED) {
|
||||||
|
*debounce_pointer = debounce_time;;
|
||||||
|
counters_need_update = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*debounce_pointer = DEBOUNCE_ELAPSED;
|
||||||
|
}
|
||||||
|
debounce_pointer++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
80
keyboards/keychron/common/debounce/sym_defer_pr.c
Normal file
80
keyboards/keychron/common/debounce/sym_defer_pr.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2021 Chad Austin <chad@chadaustin.me>
|
||||||
|
Copyright 2024 @ keychron (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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Symmetric per-row debounce algorithm. Changes only apply when
|
||||||
|
DEBOUNCE milliseconds have elapsed since the last change.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "debounce.h"
|
||||||
|
#include "timer.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
extern uint8_t debounce_time;
|
||||||
|
|
||||||
|
static uint16_t last_time;
|
||||||
|
// [row] milliseconds until key's state is considered debounced.
|
||||||
|
static uint8_t* countdowns = NULL;
|
||||||
|
// [row]
|
||||||
|
static matrix_row_t* last_raw = NULL;
|
||||||
|
|
||||||
|
void sym_defer_pr_debounce_init(uint8_t num_rows) {
|
||||||
|
countdowns = (uint8_t*)calloc(num_rows, sizeof(uint8_t));
|
||||||
|
last_raw = (matrix_row_t*)calloc(num_rows, sizeof(matrix_row_t));
|
||||||
|
last_time = timer_read();
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_defer_pr_debounce_free(void) {
|
||||||
|
if (countdowns != NULL) {
|
||||||
|
free(countdowns);
|
||||||
|
countdowns = NULL;
|
||||||
|
}
|
||||||
|
if (last_raw != NULL) {
|
||||||
|
free(last_raw);
|
||||||
|
last_raw = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool sym_defer_pr_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
|
uint16_t now = timer_read();
|
||||||
|
uint16_t elapsed16 = TIMER_DIFF_16(now, last_time);
|
||||||
|
last_time = now;
|
||||||
|
uint8_t elapsed = (elapsed16 > 255) ? 255 : elapsed16;
|
||||||
|
bool cooked_changed = false;
|
||||||
|
|
||||||
|
uint8_t* countdown = countdowns;
|
||||||
|
|
||||||
|
for (uint8_t row = 0; row < num_rows; ++row, ++countdown) {
|
||||||
|
matrix_row_t raw_row = raw[row];
|
||||||
|
|
||||||
|
if (raw_row != last_raw[row]) {
|
||||||
|
*countdown = debounce_time;
|
||||||
|
last_raw[row] = raw_row;
|
||||||
|
} else if (*countdown > elapsed) {
|
||||||
|
*countdown -= elapsed;
|
||||||
|
} else if (*countdown) {
|
||||||
|
cooked_changed |= cooked[row] ^ raw_row;
|
||||||
|
cooked[row] = raw_row;
|
||||||
|
*countdown = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cooked_changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool debounce_active(void) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
142
keyboards/keychron/common/debounce/sym_eager_pk.c
Normal file
142
keyboards/keychron/common/debounce/sym_eager_pk.c
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 Alex Ong<the.onga@gmail.com>
|
||||||
|
Copyright 2021 Simon Arlott
|
||||||
|
Copyright 2024 @ keychron (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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Basic per-key algorithm. Uses an 8-bit counter per key.
|
||||||
|
After pressing a key, it immediately changes state, and sets a counter.
|
||||||
|
No further inputs are accepted until DEBOUNCE milliseconds have occurred.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "debounce.h"
|
||||||
|
#include "timer.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifdef PROTOCOL_CHIBIOS
|
||||||
|
# if CH_CFG_USE_MEMCORE == FALSE
|
||||||
|
# error ChibiOS is configured without a memory allocator. Your keyboard may have set `#define CH_CFG_USE_MEMCORE FALSE`, which is incompatible with this debounce algorithm.
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern uint8_t debounce_time;
|
||||||
|
|
||||||
|
#define ROW_SHIFTER ((matrix_row_t)1)
|
||||||
|
|
||||||
|
typedef uint8_t debounce_counter_t;
|
||||||
|
|
||||||
|
|
||||||
|
static debounce_counter_t *debounce_counters = NULL;
|
||||||
|
static fast_timer_t last_time;
|
||||||
|
static bool counters_need_update;
|
||||||
|
static bool matrix_need_update;
|
||||||
|
static bool cooked_changed;
|
||||||
|
|
||||||
|
# define DEBOUNCE_ELAPSED 0
|
||||||
|
|
||||||
|
static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time);
|
||||||
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
|
||||||
|
|
||||||
|
// we use num_rows rather than MATRIX_ROWS to support split keyboards
|
||||||
|
void sym_eager_pk_debounce_init(uint8_t num_rows) {
|
||||||
|
debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
|
||||||
|
int i = 0;
|
||||||
|
for (uint8_t r = 0; r < num_rows; r++) {
|
||||||
|
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
|
||||||
|
debounce_counters[i++] = DEBOUNCE_ELAPSED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_eager_pk_debounce_free(void) {
|
||||||
|
if (debounce_counters != NULL) {
|
||||||
|
free(debounce_counters);
|
||||||
|
debounce_counters = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool sym_eager_pk_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
|
bool updated_last = false;
|
||||||
|
cooked_changed = false;
|
||||||
|
|
||||||
|
if (counters_need_update) {
|
||||||
|
fast_timer_t now = timer_read_fast();
|
||||||
|
fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
|
||||||
|
|
||||||
|
last_time = now;
|
||||||
|
updated_last = true;
|
||||||
|
if (elapsed_time > UINT8_MAX) {
|
||||||
|
elapsed_time = UINT8_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elapsed_time > 0) {
|
||||||
|
update_debounce_counters(num_rows, elapsed_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed || matrix_need_update) {
|
||||||
|
if (!updated_last) {
|
||||||
|
last_time = timer_read_fast();
|
||||||
|
}
|
||||||
|
|
||||||
|
transfer_matrix_values(raw, cooked, num_rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cooked_changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the current time is > debounce counter, set the counter to enable input.
|
||||||
|
static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) {
|
||||||
|
counters_need_update = false;
|
||||||
|
matrix_need_update = false;
|
||||||
|
debounce_counter_t *debounce_pointer = debounce_counters;
|
||||||
|
for (uint8_t row = 0; row < num_rows; row++) {
|
||||||
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
|
if (*debounce_pointer != DEBOUNCE_ELAPSED) {
|
||||||
|
if (*debounce_pointer <= elapsed_time) {
|
||||||
|
*debounce_pointer = DEBOUNCE_ELAPSED;
|
||||||
|
matrix_need_update = true;
|
||||||
|
} else {
|
||||||
|
*debounce_pointer -= elapsed_time;
|
||||||
|
counters_need_update = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debounce_pointer++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// upload from raw_matrix to final matrix;
|
||||||
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
|
||||||
|
matrix_need_update = false;
|
||||||
|
debounce_counter_t *debounce_pointer = debounce_counters;
|
||||||
|
for (uint8_t row = 0; row < num_rows; row++) {
|
||||||
|
matrix_row_t delta = raw[row] ^ cooked[row];
|
||||||
|
matrix_row_t existing_row = cooked[row];
|
||||||
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
|
matrix_row_t col_mask = (ROW_SHIFTER << col);
|
||||||
|
if (delta & col_mask) {
|
||||||
|
if (*debounce_pointer == DEBOUNCE_ELAPSED) {
|
||||||
|
*debounce_pointer = debounce_time;
|
||||||
|
counters_need_update = true;
|
||||||
|
existing_row ^= col_mask; // flip the bit.
|
||||||
|
cooked_changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debounce_pointer++;
|
||||||
|
}
|
||||||
|
cooked[row] = existing_row;
|
||||||
|
}
|
||||||
|
}
|
||||||
134
keyboards/keychron/common/debounce/sym_eager_pr.c
Normal file
134
keyboards/keychron/common/debounce/sym_eager_pr.c
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2019 Alex Ong<the.onga@gmail.com>
|
||||||
|
Copyright 2021 Simon Arlott
|
||||||
|
Copyright 2024 @ keychron (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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Basic per-row algorithm. Uses an 8-bit counter per row.
|
||||||
|
After pressing a key, it immediately changes state, and sets a counter.
|
||||||
|
No further inputs are accepted until DEBOUNCE milliseconds have occurred.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "debounce.h"
|
||||||
|
#include "timer.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifdef PROTOCOL_CHIBIOS
|
||||||
|
# if CH_CFG_USE_MEMCORE == FALSE
|
||||||
|
# error ChibiOS is configured without a memory allocator. Your keyboard may have set `#define CH_CFG_USE_MEMCORE FALSE`, which is incompatible with this debounce algorithm.
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
typedef uint8_t debounce_counter_t;
|
||||||
|
|
||||||
|
extern uint8_t debounce_time;
|
||||||
|
|
||||||
|
static bool matrix_need_update;
|
||||||
|
|
||||||
|
static debounce_counter_t *debounce_counters = NULL;
|
||||||
|
static fast_timer_t last_time;
|
||||||
|
static bool counters_need_update;
|
||||||
|
static bool cooked_changed;
|
||||||
|
|
||||||
|
# define DEBOUNCE_ELAPSED 0
|
||||||
|
|
||||||
|
static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time);
|
||||||
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
|
||||||
|
|
||||||
|
// we use num_rows rather than MATRIX_ROWS to support split keyboards
|
||||||
|
void sym_eager_pr_debounce_init(uint8_t num_rows) {
|
||||||
|
debounce_counters = (debounce_counter_t *)malloc(num_rows * sizeof(debounce_counter_t));
|
||||||
|
for (uint8_t r = 0; r < num_rows; r++) {
|
||||||
|
debounce_counters[r] = DEBOUNCE_ELAPSED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sym_eager_pr_debounce_free(void) {
|
||||||
|
if (debounce_counters != NULL) {
|
||||||
|
free(debounce_counters);
|
||||||
|
debounce_counters = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool sym_eager_pr_debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
|
bool updated_last = false;
|
||||||
|
cooked_changed = false;
|
||||||
|
|
||||||
|
if (counters_need_update) {
|
||||||
|
fast_timer_t now = timer_read_fast();
|
||||||
|
fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
|
||||||
|
|
||||||
|
last_time = now;
|
||||||
|
updated_last = true;
|
||||||
|
if (elapsed_time > UINT8_MAX) {
|
||||||
|
elapsed_time = UINT8_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elapsed_time > 0) {
|
||||||
|
update_debounce_counters(num_rows, elapsed_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed || matrix_need_update) {
|
||||||
|
if (!updated_last) {
|
||||||
|
last_time = timer_read_fast();
|
||||||
|
}
|
||||||
|
|
||||||
|
transfer_matrix_values(raw, cooked, num_rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cooked_changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the current time is > debounce counter, set the counter to enable input.
|
||||||
|
static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) {
|
||||||
|
counters_need_update = false;
|
||||||
|
matrix_need_update = false;
|
||||||
|
debounce_counter_t *debounce_pointer = debounce_counters;
|
||||||
|
for (uint8_t row = 0; row < num_rows; row++) {
|
||||||
|
if (*debounce_pointer != DEBOUNCE_ELAPSED) {
|
||||||
|
if (*debounce_pointer <= elapsed_time) {
|
||||||
|
*debounce_pointer = DEBOUNCE_ELAPSED;
|
||||||
|
matrix_need_update = true;
|
||||||
|
} else {
|
||||||
|
*debounce_pointer -= elapsed_time;
|
||||||
|
counters_need_update = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debounce_pointer++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// upload from raw_matrix to final matrix;
|
||||||
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
|
||||||
|
matrix_need_update = false;
|
||||||
|
debounce_counter_t *debounce_pointer = debounce_counters;
|
||||||
|
for (uint8_t row = 0; row < num_rows; row++) {
|
||||||
|
matrix_row_t existing_row = cooked[row];
|
||||||
|
matrix_row_t raw_row = raw[row];
|
||||||
|
|
||||||
|
// determine new value basd on debounce pointer + raw value
|
||||||
|
if (existing_row != raw_row) {
|
||||||
|
if (*debounce_pointer == DEBOUNCE_ELAPSED) {
|
||||||
|
*debounce_pointer = debounce_time;
|
||||||
|
cooked_changed |= cooked[row] ^ raw_row;
|
||||||
|
cooked[row] = raw_row;
|
||||||
|
counters_need_update = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debounce_pointer++;
|
||||||
|
}
|
||||||
|
}
|
||||||
49
keyboards/keychron/common/dfu_info.c
Normal file
49
keyboards/keychron/common/dfu_info.c
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 <string.h>
|
||||||
|
#include "quantum.h"
|
||||||
|
|
||||||
|
enum {
|
||||||
|
DFU_INFO_CHIP = 1,
|
||||||
|
DFU_INFO_TYPE,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BL_TYPE_STM32 = 1,
|
||||||
|
BL_TYPE_WB32,
|
||||||
|
};
|
||||||
|
|
||||||
|
void dfu_info_rx(uint8_t *data, uint8_t length) {
|
||||||
|
uint8_t i = 2;
|
||||||
|
|
||||||
|
data[i++] = 0; // success
|
||||||
|
data[i++] = DFU_INFO_CHIP,
|
||||||
|
data[i++] = strlen(STR(QMK_MCU));
|
||||||
|
memcpy(&data[i], STR(QMK_MCU), strlen(STR(QMK_MCU)));
|
||||||
|
i += strlen(STR(QMK_MCU));
|
||||||
|
data[i++] = DFU_INFO_TYPE;
|
||||||
|
data[i++] = 1;
|
||||||
|
data[i++] =
|
||||||
|
#if defined(BOOTLOADER_STM32_DFU)
|
||||||
|
BL_TYPE_STM32
|
||||||
|
#elif defined(BOOTLOADER_WB32_DFU)
|
||||||
|
BL_TYPE_WB32
|
||||||
|
#else
|
||||||
|
0
|
||||||
|
#endif
|
||||||
|
;
|
||||||
|
}
|
||||||
35
keyboards/keychron/common/eeconfig_kb.c
Normal file
35
keyboards/keychron/common/eeconfig_kb.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 "eeconfig_kb.h"
|
||||||
|
#ifdef DYNAMIC_DEBOUNCE_ENABLE
|
||||||
|
# include "keychron_debounce.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void eeconfig_init_kb_datablock(void) {
|
||||||
|
#ifdef DYNAMIC_DEBOUNCE_ENABLE
|
||||||
|
extern void debounce_config_reset(void);
|
||||||
|
debounce_config_reset();
|
||||||
|
#endif
|
||||||
|
#if defined(SNAP_CLICK_ENABLE)
|
||||||
|
extern void snap_click_config_reset(void);
|
||||||
|
snap_click_config_reset();
|
||||||
|
#endif
|
||||||
|
#if defined(KEYCHRON_RGB_ENABLE) && defined(RGB_MATRIX_ENABLE)
|
||||||
|
extern void eeconfig_reset_custom_rgb(void);
|
||||||
|
eeconfig_reset_custom_rgb();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
61
keyboards/keychron/common/eeconfig_kb.h
Normal file
61
keyboards/keychron/common/eeconfig_kb.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
#include "eeconfig_language.h"
|
||||||
|
|
||||||
|
#define EECONFIG_BASE_LANGUAGE 37
|
||||||
|
#define EECONFIG_END_LANGUAGE (EECONFIG_BASE_LANGUAGE + EECONFIG_SIZE_LANGUAGE)
|
||||||
|
|
||||||
|
#ifdef DYNAMIC_DEBOUNCE_ENABLE
|
||||||
|
# include "eeconfig_debounce.h"
|
||||||
|
# define __EECONFIG_SIZE_DEBOUNCE EECONFIG_SIZE_DEBOUNCE
|
||||||
|
#else
|
||||||
|
# define __EECONFIG_SIZE_DEBOUNCE 0
|
||||||
|
#endif
|
||||||
|
#define EECONFIG_BASE_DYNAMIC_DEBOUNCE EECONFIG_END_LANGUAGE
|
||||||
|
#define EECONFIG_END_DYNAMIC_DEBOUNCE (EECONFIG_BASE_DYNAMIC_DEBOUNCE + __EECONFIG_SIZE_DEBOUNCE)
|
||||||
|
|
||||||
|
#ifdef SNAP_CLICK_ENABLE
|
||||||
|
# include "eeconfig_snap_click.h"
|
||||||
|
# define __EECONFIG_SIZE_SNAP_CLICK EECONFIG_SIZE_SNAP_CLICK
|
||||||
|
#else
|
||||||
|
# define __EECONFIG_SIZE_SNAP_CLICK 0
|
||||||
|
#endif
|
||||||
|
#define EECONFIG_BASE_SNAP_CLICK (EECONFIG_END_DYNAMIC_DEBOUNCE)
|
||||||
|
#define EECONFIG_END_SNAP_CLICK (EECONFIG_BASE_SNAP_CLICK + __EECONFIG_SIZE_SNAP_CLICK)
|
||||||
|
|
||||||
|
#if defined(KEYCHRON_RGB_ENABLE) && defined(RGB_MATRIX_ENABLE)
|
||||||
|
# include "eeconfig_custom_rgb.h"
|
||||||
|
# define __EECONFIG_SIZE_CUSTOM_RGB EECONFIG_SIZE_CUSTOM_RGB
|
||||||
|
#else
|
||||||
|
# define __EECONFIG_SIZE_CUSTOM_RGB 0
|
||||||
|
#endif
|
||||||
|
#define EECONFIG_BASE_CUSTOM_RGB EECONFIG_END_SNAP_CLICK
|
||||||
|
#define EECONFIG_END_CUSTOM_RGB (EECONFIG_BASE_CUSTOM_RGB + __EECONFIG_SIZE_CUSTOM_RGB)
|
||||||
|
|
||||||
|
#if defined(WIRELESS_CONFIG_ENABLE)
|
||||||
|
# include "eeconfig_wireless.h"
|
||||||
|
# define __EECONFIG_SIZE_WIRELESS_CONFIG EECONFIG_SIZE_WIRELESS_CONFIG
|
||||||
|
#else
|
||||||
|
# define __EECONFIG_SIZE_WIRELESS_CONFIG 0
|
||||||
|
#endif
|
||||||
|
#define EECONFIG_BASE_WIRELESS_CONFIG EECONFIG_END_CUSTOM_RGB
|
||||||
|
#define EECONFIG_END_WIRELESS_CONFIG (EECONFIG_BASE_WIRELESS_CONFIG + __EECONFIG_SIZE_WIRELESS_CONFIG)
|
||||||
|
|
||||||
|
#define EECONFIG_KB_DATA_SIZE (EECONFIG_END_WIRELESS_CONFIG - EECONFIG_BASE_LANGUAGE)
|
||||||
|
|
||||||
465
keyboards/keychron/common/factory_test.c
Normal file
465
keyboards/keychron/common/factory_test.c
Normal file
@ -0,0 +1,465 @@
|
|||||||
|
/* Copyright 2021~2025 @ Keychron (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 "raw_hid.h"
|
||||||
|
#include "via.h"
|
||||||
|
|
||||||
|
#include "keychron_task.h"
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
# include "transport.h"
|
||||||
|
# include "battery.h"
|
||||||
|
# include "lpm.h"
|
||||||
|
# include "lkbt51.h"
|
||||||
|
# include "indicator.h"
|
||||||
|
#endif
|
||||||
|
#ifdef DYNAMIC_DEBOUNCE_ENABLE
|
||||||
|
# include "keychron_debounce.h"
|
||||||
|
#endif
|
||||||
|
#ifdef SNAP_CLICK_ENABLE
|
||||||
|
# include "snap_click.h"
|
||||||
|
#endif
|
||||||
|
#include "config.h"
|
||||||
|
#include "version.h"
|
||||||
|
|
||||||
|
#ifndef RAW_EPSIZE
|
||||||
|
# define RAW_EPSIZE 32
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BL_CYCLE_KEY
|
||||||
|
# define BL_CYCLE_KEY KC_RIGHT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BL_TRIG_KEY
|
||||||
|
# define BL_TRIG_KEY KC_HOME
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef P2P4G_CELAR_MASK
|
||||||
|
# define P2P4G_CELAR_MASK P2P4G_CLEAR_PAIRING_TYPE_C
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BACKLIGHT_TEST_OFF = 0,
|
||||||
|
BACKLIGHT_TEST_WHITE,
|
||||||
|
BACKLIGHT_TEST_RED,
|
||||||
|
BACKLIGHT_TEST_GREEN,
|
||||||
|
BACKLIGHT_TEST_BLUE,
|
||||||
|
BACKLIGHT_TEST_MAX,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
KEY_PRESS_FN = 0x01 << 0,
|
||||||
|
KEY_PRESS_J = 0x01 << 1,
|
||||||
|
KEY_PRESS_Z = 0x01 << 2,
|
||||||
|
KEY_PRESS_BL_KEY1 = 0x01 << 3,
|
||||||
|
KEY_PRESS_BL_KEY2 = 0x01 << 4,
|
||||||
|
KEY_PRESS_FACTORY_RESET = KEY_PRESS_FN | KEY_PRESS_J | KEY_PRESS_Z,
|
||||||
|
KEY_PRESS_BACKLIGTH_TEST = KEY_PRESS_FN | KEY_PRESS_BL_KEY1 | KEY_PRESS_BL_KEY2,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum { FACTORY_TEST_CMD_BACKLIGHT = 0x01, FACTORY_TEST_CMD_OS_SWITCH, FACTORY_TEST_CMD_JUMP_TO_BL, FACTORY_TEST_CMD_INT_PIN, FACTORY_TEST_CMD_GET_TRANSPORT, FACTORY_TEST_CMD_CHARGING_ADC, FACTORY_TEST_CMD_RADIO_CARRIER, FACTORY_TEST_CMD_GET_BUILD_TIME, FACTORY_TEST_CMD_GET_DEVICE_ID };
|
||||||
|
|
||||||
|
enum {
|
||||||
|
P2P4G_CLEAR_PAIRING_TYPE_A = 0x01 << 0,
|
||||||
|
P2P4G_CLEAR_PAIRING_TYPE_C = 0x01 << 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
OS_SWITCH = 0x01,
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t factory_reset_timer = 0;
|
||||||
|
static uint8_t factory_reset_state = 0;
|
||||||
|
static uint8_t backlight_test_mode = BACKLIGHT_TEST_OFF;
|
||||||
|
|
||||||
|
static uint32_t factory_reset_ind_timer = 0;
|
||||||
|
static uint8_t factory_reset_ind_state = 0;
|
||||||
|
static bool report_os_sw_state = false;
|
||||||
|
static uint8_t keys_released = 0;
|
||||||
|
|
||||||
|
extern void eeconfig_reset_custom_rgb(void);
|
||||||
|
|
||||||
|
void factory_timer_start(void) {
|
||||||
|
factory_reset_timer = timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void factory_timer_check(void) {
|
||||||
|
if (timer_elapsed32(factory_reset_timer) > 3000) {
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
|
||||||
|
if (factory_reset_state == KEY_PRESS_FACTORY_RESET) {
|
||||||
|
factory_reset_ind_timer = timer_read32();
|
||||||
|
factory_reset_ind_state++;
|
||||||
|
keys_released = false;
|
||||||
|
|
||||||
|
clear_keyboard(); // Avoid key being pressed after NKRO state changed
|
||||||
|
layer_state_t default_layer_tmp = default_layer_state;
|
||||||
|
eeconfig_init();
|
||||||
|
keymap_config.raw = eeconfig_read_keymap();
|
||||||
|
default_layer_set(default_layer_tmp);
|
||||||
|
#ifdef DYNAMIC_DEBOUNCE_ENABLE
|
||||||
|
debounce_config_reset();
|
||||||
|
#endif
|
||||||
|
#ifdef SNAP_CLICK_ENABLE
|
||||||
|
snap_click_config_reset();
|
||||||
|
#endif
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
if (!led_matrix_is_enabled()) led_matrix_enable();
|
||||||
|
led_matrix_init();
|
||||||
|
#endif
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
if (!rgb_matrix_is_enabled()) rgb_matrix_enable();
|
||||||
|
rgb_matrix_init();
|
||||||
|
# if defined(KEYCHRON_RGB_ENABLE) && defined(EECONFIG_SIZE_CUSTOM_RGB)
|
||||||
|
eeconfig_reset_custom_rgb();
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
# ifdef EECONFIG_SIZE_WIRELESS_CONFIG
|
||||||
|
wireless_config_reset();
|
||||||
|
# endif
|
||||||
|
wait_ms(50);
|
||||||
|
lkbt51_factory_reset(P2P4G_CELAR_MASK);
|
||||||
|
#endif
|
||||||
|
} else if (factory_reset_state == KEY_PRESS_BACKLIGTH_TEST) {
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
if (!led_matrix_is_enabled()) led_matrix_enable();
|
||||||
|
#endif
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
if (!rgb_matrix_is_enabled()) rgb_matrix_enable();
|
||||||
|
#endif
|
||||||
|
backlight_test_mode = BACKLIGHT_TEST_WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
factory_reset_state = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void factory_reset_ind_timer_check(void) {
|
||||||
|
if (factory_reset_ind_timer && timer_elapsed32(factory_reset_ind_timer) > 250) {
|
||||||
|
if (factory_reset_ind_state++ > 6) {
|
||||||
|
factory_reset_ind_timer = factory_reset_ind_state = 0;
|
||||||
|
} else {
|
||||||
|
factory_reset_ind_timer = timer_read32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool process_record_factory_test(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
switch (keycode) {
|
||||||
|
#if defined(FN_KEY_1) || defined(FN_KEY_2)
|
||||||
|
# if defined(FN_KEY_1)
|
||||||
|
case FN_KEY_1: /* fall through */
|
||||||
|
# endif
|
||||||
|
# if defined(FN_KEY_2)
|
||||||
|
case FN_KEY_2:
|
||||||
|
# endif
|
||||||
|
# if defined(FN_KEY_3)
|
||||||
|
case FN_KEY_3:
|
||||||
|
# endif
|
||||||
|
if (record->event.pressed) {
|
||||||
|
factory_reset_state |= KEY_PRESS_FN;
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_FN;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
case KC_J:
|
||||||
|
#if defined(FN_J_KEY)
|
||||||
|
case FN_J_KEY:
|
||||||
|
#endif
|
||||||
|
if (record->event.pressed) {
|
||||||
|
factory_reset_state |= KEY_PRESS_J;
|
||||||
|
if (factory_reset_state == 0x07) factory_timer_start();
|
||||||
|
if ((factory_reset_state & KEY_PRESS_FN) && keycode == KC_J) return false;
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_J;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
/* Avoid changing backlight effect on key released if FN_Z_KEY is mode*/
|
||||||
|
if (keys_released & KEY_PRESS_J) {
|
||||||
|
keys_released &= ~KEY_PRESS_J;
|
||||||
|
if (keycode >= QK_BACKLIGHT_ON && keycode <= RGB_MODE_TWINKLE) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case KC_Z:
|
||||||
|
#if defined(FN_Z_KEY)
|
||||||
|
case FN_Z_KEY:
|
||||||
|
#endif
|
||||||
|
if (record->event.pressed) {
|
||||||
|
factory_reset_state |= KEY_PRESS_Z;
|
||||||
|
if (factory_reset_state == 0x07) factory_timer_start();
|
||||||
|
if ((factory_reset_state & KEY_PRESS_FN) && keycode == KC_Z) return false;
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_Z;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
/* Avoid changing backlight effect on key released if FN_Z_KEY is mode*/
|
||||||
|
if (keys_released & KEY_PRESS_Z) {
|
||||||
|
keys_released &= ~KEY_PRESS_Z;
|
||||||
|
if (keycode >= QK_BACKLIGHT_ON && keycode <= RGB_MODE_TWINKLE) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#if defined(BL_CYCLE_KEY) || defined(BL_CYCLE_KEY_2)
|
||||||
|
# if defined(BL_CYCLE_KEY)
|
||||||
|
case BL_CYCLE_KEY:
|
||||||
|
# endif
|
||||||
|
# if defined(FN_BL_CYCLE_KEY)
|
||||||
|
case FN_BL_CYCLE_KEY:
|
||||||
|
# endif
|
||||||
|
if (record->event.pressed) {
|
||||||
|
if (backlight_test_mode) {
|
||||||
|
if (++backlight_test_mode >= BACKLIGHT_TEST_MAX) {
|
||||||
|
backlight_test_mode = BACKLIGHT_TEST_WHITE;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
factory_reset_state |= KEY_PRESS_BL_KEY1;
|
||||||
|
if (factory_reset_state == 0x19) {
|
||||||
|
factory_timer_start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_BL_KEY1;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#if defined(BL_TRIG_KEY) || defined(BL_TRIG_KEY_2)
|
||||||
|
# if defined(BL_TRIG_KEY)
|
||||||
|
case BL_TRIG_KEY:
|
||||||
|
# endif
|
||||||
|
# if defined(FN_BL_TRIG_KEY)
|
||||||
|
case FN_BL_TRIG_KEY:
|
||||||
|
# endif
|
||||||
|
if (record->event.pressed) {
|
||||||
|
if (backlight_test_mode) {
|
||||||
|
backlight_test_mode = BACKLIGHT_TEST_OFF;
|
||||||
|
} else {
|
||||||
|
factory_reset_state |= KEY_PRESS_BL_KEY2;
|
||||||
|
if (factory_reset_state == 0x19) {
|
||||||
|
factory_timer_start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
factory_reset_state &= ~KEY_PRESS_BL_KEY2;
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
bool factory_test_indicator(void) {
|
||||||
|
if (factory_reset_ind_state) {
|
||||||
|
led_matrix_set_value_all(factory_reset_ind_state % 2 ? 0 : 255);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
bool factory_test_indicator(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);
|
||||||
|
return false;
|
||||||
|
} else if (backlight_test_mode) {
|
||||||
|
switch (backlight_test_mode) {
|
||||||
|
case BACKLIGHT_TEST_WHITE:
|
||||||
|
rgb_matrix_set_color_all(255, 255, 255);
|
||||||
|
break;
|
||||||
|
case BACKLIGHT_TEST_RED:
|
||||||
|
rgb_matrix_set_color_all(255, 0, 0);
|
||||||
|
break;
|
||||||
|
case BACKLIGHT_TEST_GREEN:
|
||||||
|
rgb_matrix_set_color_all(0, 255, 0);
|
||||||
|
break;
|
||||||
|
case BACKLIGHT_TEST_BLUE:
|
||||||
|
rgb_matrix_set_color_all(0, 0, 255);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool factory_reset_indicating(void) {
|
||||||
|
return factory_reset_ind_timer;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool factory_test_task(void) {
|
||||||
|
if (factory_reset_timer) factory_timer_check();
|
||||||
|
if (factory_reset_ind_timer) factory_reset_ind_timer_check();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void factory_test_send(uint8_t *payload, uint8_t length) {
|
||||||
|
#ifdef RAW_ENABLE
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
uint8_t data[RAW_EPSIZE] = {0};
|
||||||
|
|
||||||
|
uint8_t i = 0;
|
||||||
|
data[i++] = 0xAB;
|
||||||
|
|
||||||
|
memcpy(&data[i], payload, length);
|
||||||
|
i += length;
|
||||||
|
|
||||||
|
for (uint8_t i = 1; i < RAW_EPSIZE - 3; i++)
|
||||||
|
checksum += data[i];
|
||||||
|
data[RAW_EPSIZE - 2] = checksum & 0xFF;
|
||||||
|
data[RAW_EPSIZE - 1] = (checksum >> 8) & 0xFF;
|
||||||
|
|
||||||
|
raw_hid_send(data, RAW_EPSIZE);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void factory_test_rx(uint8_t *data, uint8_t length) {
|
||||||
|
if (data[0] == 0xAB) {
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
|
||||||
|
for (uint8_t i = 1; i < RAW_EPSIZE - 3; i++) {
|
||||||
|
checksum += data[i];
|
||||||
|
}
|
||||||
|
/* Verify checksum */
|
||||||
|
if ((checksum & 0xFF) != data[RAW_EPSIZE - 2] || checksum >> 8 != data[RAW_EPSIZE - 1]) return;
|
||||||
|
|
||||||
|
uint8_t payload[32];
|
||||||
|
uint8_t len = 0;
|
||||||
|
|
||||||
|
switch (data[1]) {
|
||||||
|
case FACTORY_TEST_CMD_BACKLIGHT:
|
||||||
|
backlight_test_mode = data[2];
|
||||||
|
factory_reset_timer = 0;
|
||||||
|
break;
|
||||||
|
case FACTORY_TEST_CMD_OS_SWITCH:
|
||||||
|
report_os_sw_state = data[2];
|
||||||
|
if (report_os_sw_state) {
|
||||||
|
// dip_switch_read(true);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case FACTORY_TEST_CMD_JUMP_TO_BL:
|
||||||
|
// if (memcmp(&data[2], "JumpToBootloader", strlen("JumpToBootloader")) == 0) bootloader_jump();
|
||||||
|
break;
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
case FACTORY_TEST_CMD_INT_PIN:
|
||||||
|
switch (data[2]) {
|
||||||
|
/* Enalbe/disable test */
|
||||||
|
case 0xA1:
|
||||||
|
lkbt51_int_pin_test(data[3]);
|
||||||
|
break;
|
||||||
|
/* Set INT state */
|
||||||
|
case 0xA2:
|
||||||
|
kc_printf("pin %d\n\r", data[3]);
|
||||||
|
writePin(BLUETOOTH_INT_OUTPUT_PIN, data[3]);
|
||||||
|
break;
|
||||||
|
/* Report INT state */
|
||||||
|
// case 0xA3:
|
||||||
|
// payload[len++] = FACTORY_TEST_CMD_INT_PIN;
|
||||||
|
// payload[len++] = 0xA3;
|
||||||
|
// payload[len++] = readPin(LKBT51_INT_INPUT_PIN);
|
||||||
|
// factory_test_send(payload, len);
|
||||||
|
// break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case FACTORY_TEST_CMD_GET_TRANSPORT:
|
||||||
|
payload[len++] = FACTORY_TEST_CMD_GET_TRANSPORT;
|
||||||
|
payload[len++] = get_transport();
|
||||||
|
payload[len++] = readPin(USB_POWER_SENSE_PIN);
|
||||||
|
factory_test_send(payload, len);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#ifdef BATTERY_CHARGE_DONE_DETECT_ADC
|
||||||
|
case FACTORY_TEST_CMD_CHARGING_ADC:
|
||||||
|
case 0xA1:
|
||||||
|
battery_charging_monitor(data[3]);
|
||||||
|
break;
|
||||||
|
case 0xA2:
|
||||||
|
payload[len++] = FACTORY_TEST_CMD_CHARGING_ADC;
|
||||||
|
payload[len++] = battery_adc_read_charging_pin();
|
||||||
|
factory_test_send(payload, len);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
case FACTORY_TEST_CMD_RADIO_CARRIER:
|
||||||
|
if (data[2] < 79) lkbt51_radio_test(data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
# ifdef WERELESS_PRESSURE_TEST
|
||||||
|
case 0x70:
|
||||||
|
switch (data[2]) {
|
||||||
|
/* Enalbe/disable test */
|
||||||
|
case 0xB1:
|
||||||
|
SEND_STRING("abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890\n");
|
||||||
|
break;
|
||||||
|
case 0xB2:
|
||||||
|
payload[len++] = 0x70;
|
||||||
|
payload[len++] = 0xB2;
|
||||||
|
payload[len++] = wireless_get_state();
|
||||||
|
factory_test_send(payload, len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
case FACTORY_TEST_CMD_GET_BUILD_TIME: {
|
||||||
|
payload[len++] = FACTORY_TEST_CMD_GET_BUILD_TIME;
|
||||||
|
payload[len++] = 'v';
|
||||||
|
if ((DEVICE_VER & 0xF000) != 0) itoa((DEVICE_VER >> 12), (char *)&payload[len++], 16);
|
||||||
|
itoa((DEVICE_VER >> 8) & 0xF, (char *)&payload[len++], 16);
|
||||||
|
payload[len++] = '.';
|
||||||
|
itoa((DEVICE_VER >> 4) & 0xF, (char *)&payload[len++], 16);
|
||||||
|
payload[len++] = '.';
|
||||||
|
itoa((DEVICE_VER >> 4) & 0xF, (char *)&payload[len++], 16);
|
||||||
|
payload[len++] = ' ';
|
||||||
|
memcpy(&payload[len], QMK_BUILDDATE, sizeof(QMK_BUILDDATE));
|
||||||
|
len += sizeof(QMK_BUILDDATE);
|
||||||
|
factory_test_send(payload, len);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case FACTORY_TEST_CMD_GET_DEVICE_ID:
|
||||||
|
payload[len++] = FACTORY_TEST_CMD_GET_DEVICE_ID;
|
||||||
|
payload[len++] = 12; // UUID length
|
||||||
|
memcpy(&payload[len], (uint32_t *)UID_BASE, 4);
|
||||||
|
memcpy(&payload[len + 4], (uint32_t *)UID_BASE + 4, 4);
|
||||||
|
memcpy(&payload[len + 8], (uint32_t *)UID_BASE + 8, 4);
|
||||||
|
|
||||||
|
len += 12;
|
||||||
|
factory_test_send(payload, len);
|
||||||
|
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); */
|
||||||
|
/* } */
|
||||||
|
|
||||||
|
/* return true; */
|
||||||
|
/* } */
|
||||||
34
keyboards/keychron/common/factory_test.h
Normal file
34
keyboards/keychron/common/factory_test.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
#define FACTORY_RESET_CHECK process_record_factory_test
|
||||||
|
#define FACTORY_RESET_TASK factory_test_task
|
||||||
|
|
||||||
|
void factory_test_init(void);
|
||||||
|
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
bool factory_test_indicator(void);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//void process_record_factory_test(uint16_t keycode, keyrecord_t *record);
|
||||||
|
bool factory_reset_indicating(void);
|
||||||
|
void factory_test_task(void);
|
||||||
|
void factory_test_rx(uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
|
bool process_record_factory_test(uint16_t keycode, keyrecord_t *record);
|
||||||
|
|
||||||
184
keyboards/keychron/common/keychron_common.c
Normal file
184
keyboards/keychron/common/keychron_common.c
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
/* Copyright 2023 @ Keychron (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 QMK_KEYBOARD_H
|
||||||
|
#include "keychron_common.h"
|
||||||
|
#ifdef FACTORY_TEST_ENABLE
|
||||||
|
# include "factory_test.h"
|
||||||
|
#endif
|
||||||
|
#ifdef RETAIL_DEMO_ENABLE
|
||||||
|
# include "retail_demo.h"
|
||||||
|
#endif
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
# include "lkbt51.h"
|
||||||
|
# include "wireless.h"
|
||||||
|
#endif
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
# include "led_matrix.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool is_siri_active = false;
|
||||||
|
uint32_t siri_timer = 0;
|
||||||
|
|
||||||
|
static uint8_t mac_keycode[4] = {
|
||||||
|
KC_LOPT,
|
||||||
|
KC_ROPT,
|
||||||
|
KC_LCMD,
|
||||||
|
KC_RCMD,
|
||||||
|
};
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
static key_combination_t key_comb_list[] = {
|
||||||
|
{2, {KC_LWIN, KC_TAB}},
|
||||||
|
{2, {KC_LWIN, KC_E}},
|
||||||
|
{3, {KC_LSFT, KC_LCMD, KC_4}},
|
||||||
|
{2, {KC_LWIN, KC_C}},
|
||||||
|
#ifdef WIN_LOCK_SCREEN_ENABLE
|
||||||
|
{2, {KC_LWIN, KC_L}},
|
||||||
|
#endif
|
||||||
|
#ifdef MAC_LOCK_SCREEN_ENABLE
|
||||||
|
{3, {KC_LCTL, KC_LCMD, KC_Q}},
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
void keychron_common_init(void) {
|
||||||
|
#ifdef SNAP_CLICK_ENABLE
|
||||||
|
extern void snap_click_init(void);
|
||||||
|
snap_click_init();
|
||||||
|
#endif
|
||||||
|
#if defined(RGB_MATRIX_ENABLE) && defined(KEYCHRON_RGB_ENABLE)
|
||||||
|
extern void eeconfig_init_custom_rgb(void);
|
||||||
|
eeconfig_init_custom_rgb();
|
||||||
|
#endif
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
# ifdef P2P4_MODE_SELECT_PIN
|
||||||
|
palSetLineMode(P2P4_MODE_SELECT_PIN, PAL_MODE_INPUT);
|
||||||
|
# endif
|
||||||
|
# ifdef BT_MODE_SELECT_PIN
|
||||||
|
palSetLineMode(BT_MODE_SELECT_PIN, PAL_MODE_INPUT);
|
||||||
|
# endif
|
||||||
|
# ifdef BAT_LOW_LED_PIN
|
||||||
|
writePin(BAT_LOW_LED_PIN, BAT_LOW_LED_PIN_ON_STATE);
|
||||||
|
# endif
|
||||||
|
|
||||||
|
lkbt51_init(false);
|
||||||
|
wireless_init();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ENCODER_ENABLE
|
||||||
|
encoder_cb_init();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool process_record_keychron_common(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
switch (keycode) {
|
||||||
|
case KC_MCTRL:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
register_code(KC_MISSION_CONTROL);
|
||||||
|
} else {
|
||||||
|
unregister_code(KC_MISSION_CONTROL);
|
||||||
|
}
|
||||||
|
return false; // Skip all further processing of this key
|
||||||
|
case KC_LNPAD:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
register_code(KC_LAUNCHPAD);
|
||||||
|
} else {
|
||||||
|
unregister_code(KC_LAUNCHPAD);
|
||||||
|
}
|
||||||
|
return false; // Skip all further processing of this key
|
||||||
|
case KC_LOPTN:
|
||||||
|
case KC_ROPTN:
|
||||||
|
case KC_LCMMD:
|
||||||
|
case KC_RCMMD:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
register_code(mac_keycode[keycode - KC_LOPTN]);
|
||||||
|
} else {
|
||||||
|
unregister_code(mac_keycode[keycode - KC_LOPTN]);
|
||||||
|
}
|
||||||
|
return false; // Skip all further processing of this key
|
||||||
|
case KC_SIRI:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
if (!is_siri_active) {
|
||||||
|
is_siri_active = true;
|
||||||
|
register_code(KC_LCMD);
|
||||||
|
register_code(KC_SPACE);
|
||||||
|
}
|
||||||
|
siri_timer = timer_read32();
|
||||||
|
} else {
|
||||||
|
// Do something else when release
|
||||||
|
}
|
||||||
|
return false; // Skip all further processing of this key
|
||||||
|
case KC_TASK:
|
||||||
|
case KC_FILE:
|
||||||
|
case KC_SNAP:
|
||||||
|
case KC_CTANA:
|
||||||
|
#ifdef WIN_LOCK_SCREEN_ENABLE
|
||||||
|
case KC_WLCK:
|
||||||
|
#endif
|
||||||
|
#ifdef MAC_LOCK_SCREEN_ENABLE
|
||||||
|
case KC_MLCK:
|
||||||
|
#endif
|
||||||
|
if (record->event.pressed) {
|
||||||
|
for (uint8_t i = 0; i < key_comb_list[keycode - KC_TASK].len; i++) {
|
||||||
|
register_code(key_comb_list[keycode - KC_TASK].keycode[i]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (uint8_t i = 0; i < key_comb_list[keycode - KC_TASK].len; i++) {
|
||||||
|
unregister_code(key_comb_list[keycode - KC_TASK].keycode[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false; // Skip all further processing of this key
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
case BL_SPI:
|
||||||
|
led_matrix_increase_speed();
|
||||||
|
break;
|
||||||
|
case BL_SPD:
|
||||||
|
led_matrix_decrease_speed();
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
return true; // Process all other keycodes normally
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void keychron_common_task(void) {
|
||||||
|
if (is_siri_active && timer_elapsed32(siri_timer) > 500) {
|
||||||
|
unregister_code(KC_LCMD);
|
||||||
|
unregister_code(KC_SPACE);
|
||||||
|
is_siri_active = false;
|
||||||
|
siri_timer = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef ENCODER_ENABLE
|
||||||
|
static void encoder_pad_cb(void *param) {
|
||||||
|
uint8_t index = (uint32_t)param;
|
||||||
|
encoder_inerrupt_read(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void encoder_cb_init(void) {
|
||||||
|
pin_t encoders_pad_a[] = ENCODERS_PAD_A;
|
||||||
|
pin_t encoders_pad_b[] = ENCODERS_PAD_B;
|
||||||
|
for (uint32_t i = 0; i < NUM_ENCODERS; i++) {
|
||||||
|
palEnableLineEvent(encoders_pad_a[i], PAL_EVENT_MODE_BOTH_EDGES);
|
||||||
|
palEnableLineEvent(encoders_pad_b[i], PAL_EVENT_MODE_BOTH_EDGES);
|
||||||
|
palSetLineCallback(encoders_pad_a[i], encoder_pad_cb, (void *)i);
|
||||||
|
palSetLineCallback(encoders_pad_b[i], encoder_pad_cb, (void *)i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
96
keyboards/keychron/common/keychron_common.h
Normal file
96
keyboards/keychron/common/keychron_common.h
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/* Copyright 2023 @ Keychron (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
|
||||||
|
|
||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
enum {
|
||||||
|
KC_LOPTN = QK_KB_0,
|
||||||
|
KC_ROPTN,
|
||||||
|
KC_LCMMD,
|
||||||
|
KC_RCMMD,
|
||||||
|
KC_MCTRL,
|
||||||
|
KC_LNPAD,
|
||||||
|
KC_TASK_VIEW,
|
||||||
|
KC_FILE_EXPLORER,
|
||||||
|
KC_SCREEN_SHOT,
|
||||||
|
KC_CORTANA,
|
||||||
|
#ifdef WIN_LOCK_SCREEN_ENABLE
|
||||||
|
KC_WIN_LOCK_SCREEN,
|
||||||
|
__KC_WIN_LOCK_SCREEN_NEXT,
|
||||||
|
#else
|
||||||
|
__KC_WIN_LOCK_SCREEN_NEXT = KC_CORTANA + 1,
|
||||||
|
#endif
|
||||||
|
#ifdef MAC_LOCK_SCREEN_ENABLE
|
||||||
|
KC_MAC_LOCK_SCREEN = __KC_WIN_LOCK_SCREEN_NEXT,
|
||||||
|
__KC_MAC_LOCK_SCREEN_NEXT,
|
||||||
|
#else
|
||||||
|
__KC_MAC_LOCK_SCREEN_NEXT = __KC_WIN_LOCK_SCREEN_NEXT,
|
||||||
|
#endif
|
||||||
|
KC_SIRI = __KC_MAC_LOCK_SCREEN_NEXT,
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
BT_HST1,
|
||||||
|
BT_HST2,
|
||||||
|
BT_HST3,
|
||||||
|
P2P4G,
|
||||||
|
BAT_LVL,
|
||||||
|
#endif
|
||||||
|
#ifdef ANANLOG_MATRIX
|
||||||
|
PROF1,
|
||||||
|
PROF2,
|
||||||
|
PROF3,
|
||||||
|
#endif
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
BL_SPI,
|
||||||
|
BL_SPD,
|
||||||
|
#endif
|
||||||
|
NEW_SAFE_RANGE,
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifndef LK_WIRELESS_ENABLE
|
||||||
|
#define BT_HST1 KC_TRANS
|
||||||
|
#define BT_HST2 KC_TRANS
|
||||||
|
#define BT_HST3 KC_TRANS
|
||||||
|
#define P2P4G KC_TRANS
|
||||||
|
#define BAT_LVL KC_TRANS
|
||||||
|
#endif
|
||||||
|
#ifndef ANANLOG_MATRIX
|
||||||
|
#define PROF1 KC_TRANS
|
||||||
|
#define PROF2 KC_TRANS
|
||||||
|
#define PROF3 KC_TRANS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define KC_TASK KC_TASK_VIEW
|
||||||
|
#define KC_FILE KC_FILE_EXPLORER
|
||||||
|
#define KC_SNAP KC_SCREEN_SHOT
|
||||||
|
#define KC_CTANA KC_CORTANA
|
||||||
|
#define KC_WLCK KC_WIN_LOCK_SCREEN
|
||||||
|
#define KC_MLCK KC_MAC_LOCK_SCREEN
|
||||||
|
|
||||||
|
typedef struct PACKED {
|
||||||
|
uint8_t len;
|
||||||
|
uint8_t keycode[3];
|
||||||
|
} key_combination_t;
|
||||||
|
|
||||||
|
void keychron_common_init(void);
|
||||||
|
bool process_record_keychron_common(uint16_t keycode, keyrecord_t *record);
|
||||||
|
void keychron_common_task(void);
|
||||||
|
|
||||||
|
#ifdef ENCODER_ENABLE
|
||||||
|
void encoder_cb_init(void);
|
||||||
|
#endif
|
||||||
31
keyboards/keychron/common/keychron_common.mk
Normal file
31
keyboards/keychron/common/keychron_common.mk
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
OPT_DEFS += -DFACTORY_TEST_ENABLE -DAPDAPTIVE_NKRO_ENABLE
|
||||||
|
|
||||||
|
KEYCHRON_COMMON_DIR = common
|
||||||
|
SRC += \
|
||||||
|
$(KEYCHRON_COMMON_DIR)/keychron_task.c \
|
||||||
|
$(KEYCHRON_COMMON_DIR)/keychron_common.c \
|
||||||
|
$(KEYCHRON_COMMON_DIR)/keychron_raw_hid.c \
|
||||||
|
$(KEYCHRON_COMMON_DIR)/factory_test.c \
|
||||||
|
$(KEYCHRON_COMMON_DIR)/eeconfig_kb.c \
|
||||||
|
$(KEYCHRON_COMMON_DIR)/dfu_info.c
|
||||||
|
|
||||||
|
VPATH += $(TOP_DIR)/keyboards/keychron/$(KEYCHRON_COMMON_DIR)
|
||||||
|
|
||||||
|
INFO_RULES_MK = $(shell $(QMK_BIN) generate-rules-mk --quiet --escape --keyboard $(KEYBOARD) --output $(INTERMEDIATE_OUTPUT)/src/info_rules.mk)
|
||||||
|
include $(INFO_RULES_MK)
|
||||||
|
|
||||||
|
include $(TOP_DIR)/keyboards/keychron/$(KEYCHRON_COMMON_DIR)/language/language.mk
|
||||||
|
|
||||||
|
ifeq ($(strip $(DEBOUNCE_TYPE)), custom)
|
||||||
|
include $(TOP_DIR)/keyboards/keychron/$(KEYCHRON_COMMON_DIR)/debounce/debounce.mk
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(strip $(SNAP_CLICK_ENABLE)), yes)
|
||||||
|
include $(TOP_DIR)/keyboards/keychron/$(KEYCHRON_COMMON_DIR)/snap_click/snap_click.mk
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(strip $(KEYCHRON_RGB_ENABLE)), yes)
|
||||||
|
ifeq ($(strip $(RGB_MATRIX_ENABLE)), yes)
|
||||||
|
include $(TOP_DIR)/keyboards/keychron/$(KEYCHRON_COMMON_DIR)/rgb/rgb.mk
|
||||||
|
endif
|
||||||
|
endif
|
||||||
203
keyboards/keychron/common/keychron_raw_hid.c
Normal file
203
keyboards/keychron/common/keychron_raw_hid.c
Normal file
@ -0,0 +1,203 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 QMK_KEYBOARD_H
|
||||||
|
#include "keychron_common.h"
|
||||||
|
#include "keychron_raw_hid.h"
|
||||||
|
#include "raw_hid.h"
|
||||||
|
#include "version.h"
|
||||||
|
#include "language.h"
|
||||||
|
#ifdef FACTORY_TEST_ENABLE
|
||||||
|
# include "factory_test.h"
|
||||||
|
#endif
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
# include "lkbt51.h"
|
||||||
|
#endif
|
||||||
|
#ifdef ANANLOG_MATRIX
|
||||||
|
# include "analog_matrix.h"
|
||||||
|
#endif
|
||||||
|
#ifdef DYNAMIC_DEBOUNCE_ENABLE
|
||||||
|
# include "keychron_debounce.h"
|
||||||
|
#endif
|
||||||
|
#ifdef SNAP_CLICK_ENABLE
|
||||||
|
# include "snap_click.h"
|
||||||
|
#endif
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
# include "wireless.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern void dfu_info_rx(uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
|
void get_support_feature(uint8_t *data) {
|
||||||
|
data[0] = 0;
|
||||||
|
data[1] = FEATURE_DEFAULT_LAYER
|
||||||
|
#ifdef KC_BLUETOOTH_ENABLE
|
||||||
|
| FEATURE_BLUETOOTH
|
||||||
|
#endif
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
| FEATURE_BLUETOOTH | FEATURE_P24G
|
||||||
|
#endif
|
||||||
|
#ifdef ANANLOG_MATRIX
|
||||||
|
| FEATURE_ANALOG_MATRIX
|
||||||
|
#endif
|
||||||
|
#ifdef INFO_CHAGNED_NOTIFY_ENABLE
|
||||||
|
| FEATURE_INFO_CHAGNED_NOTIFY
|
||||||
|
#endif
|
||||||
|
#ifdef DYNAMIC_DEBOUNCE_ENABLE
|
||||||
|
| FEATURE_DYNAMIC_DEBOUNCE
|
||||||
|
#endif
|
||||||
|
#ifdef SNAP_CLICK_ENABLE
|
||||||
|
| FEATURE_SNAP_CLICK
|
||||||
|
#endif
|
||||||
|
#ifdef KEYCHRON_RGB_ENABLE
|
||||||
|
| FEATURE_KEYCHRON_RGB
|
||||||
|
#endif
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_firmware_version(uint8_t *data) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
data[i++] = 'v';
|
||||||
|
if ((DEVICE_VER & 0xF000) != 0) itoa((DEVICE_VER >> 12), (char *)&data[i++], 16);
|
||||||
|
itoa((DEVICE_VER >> 8) & 0xF, (char *)&data[i++], 16);
|
||||||
|
data[i++] = '.';
|
||||||
|
itoa((DEVICE_VER >> 4) & 0xF, (char *)&data[i++], 16);
|
||||||
|
data[i++] = '.';
|
||||||
|
itoa(DEVICE_VER & 0xF, (char *)&data[i++], 16);
|
||||||
|
data[i++] = ' ';
|
||||||
|
memcpy(&data[i], QMK_BUILDDATE, sizeof(QMK_BUILDDATE));
|
||||||
|
i += sizeof(QMK_BUILDDATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__attribute__((weak)) void kc_rgb_matrix_rx(uint8_t *data, uint8_t length) {}
|
||||||
|
|
||||||
|
bool kc_raw_hid_rx(uint8_t *data, uint8_t length) {
|
||||||
|
switch (data[0]) {
|
||||||
|
case KC_GET_PROTOCOL_VERSION:
|
||||||
|
data[1] = PROTOCOL_VERSION;
|
||||||
|
data[2] = 0;
|
||||||
|
data[3] = QMK_COMMAND_SET;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case KC_GET_FIRMWARE_VERSION:
|
||||||
|
get_firmware_version(&data[1]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case KC_GET_SUPPORT_FEATURE:
|
||||||
|
get_support_feature(&data[1]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case KC_GET_DEFAULT_LAYER:
|
||||||
|
data[1] = get_highest_layer(default_layer_state);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0xA7:
|
||||||
|
switch (data[1]) {
|
||||||
|
case MISC_GET_PROTOCOL_VER:
|
||||||
|
data[2] = 0;
|
||||||
|
data[3] = MISC_PROTOCOL_VERSION & 0xFF;
|
||||||
|
data[4] = (MISC_PROTOCOL_VERSION >> 8) & 0xFF;
|
||||||
|
data[5] = MISC_DFU_INFO | MISC_LANGUAGE
|
||||||
|
#ifdef DYNAMIC_DEBOUNCE_ENABLE
|
||||||
|
| MISC_DEBOUNCE
|
||||||
|
#endif
|
||||||
|
#ifdef SNAP_CLICK_ENABLE
|
||||||
|
| MISC_SNAP_CLICK
|
||||||
|
#endif
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
| MISC_WIRELESS_LPM
|
||||||
|
#endif
|
||||||
|
#ifdef HSUSB_8K_ENABLE
|
||||||
|
| MISC_REPORT_REATE
|
||||||
|
#endif
|
||||||
|
;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DFU_INFO_GET:
|
||||||
|
dfu_info_rx(data, length);
|
||||||
|
break;
|
||||||
|
case LANGUAGE_GET ... LANGUAGE_SET:
|
||||||
|
language_rx(data, length);
|
||||||
|
break;
|
||||||
|
|
||||||
|
#if defined(DYNAMIC_DEBOUNCE_ENABLE)
|
||||||
|
case DEBOUNCE_GET ... DEBOUNCE_SET:
|
||||||
|
debounce_rx(data, length);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#if defined(SNAP_CLICK_ENABLE)
|
||||||
|
case SNAP_CLICK_GET_INFO ... SNAP_CLICK_SAVE:
|
||||||
|
snap_click_rx(data, length);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#if defined(LK_WIRELESS_ENABLE) && defined(EECONFIG_BASE_WIRELESS_CONFIG)
|
||||||
|
case WIRELESS_LPM_GET ... WIRELESS_LPM_SET:
|
||||||
|
wireless_raw_hid_rx(data, length);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#if defined(HSUSB_8K_ENABLE)
|
||||||
|
case REPORT_RATE_GET ... REPORT_RATE_SET:
|
||||||
|
report_rate_hid_rx(data, length);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
data[0] = 0xFF;
|
||||||
|
data[1] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
#if defined(KEYCHRON_RGB_ENABLE)
|
||||||
|
case 0xA8:
|
||||||
|
kc_rgb_matrix_rx(data, length);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ANANLOG_MATRIX
|
||||||
|
case 0xA9:
|
||||||
|
analog_matrix_rx(data, length);
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
case 0xAA:
|
||||||
|
lkbt51_dfu_rx(data, length);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#ifdef FACTORY_TEST_ENABLE
|
||||||
|
case 0xAB:
|
||||||
|
factory_test_rx(data, length);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
raw_hid_send(data, length);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(VIA_ENABLE)
|
||||||
|
bool via_command_kb(uint8_t *data, uint8_t length) {
|
||||||
|
return kc_raw_hid_rx(data, length);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
void raw_hid_receive(uint8_t *data, uint8_t length) {
|
||||||
|
kc_raw_hid_rx(data, length);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
65
keyboards/keychron/common/keychron_raw_hid.h
Normal file
65
keyboards/keychron/common/keychron_raw_hid.h
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
#define PROTOCOL_VERSION 0x02
|
||||||
|
#define MISC_PROTOCOL_VERSION 0x0002
|
||||||
|
#define QMK_COMMAND_SET 2
|
||||||
|
|
||||||
|
enum {
|
||||||
|
KC_GET_PROTOCOL_VERSION = 0xA0,
|
||||||
|
KC_GET_FIRMWARE_VERSION = 0xA1,
|
||||||
|
KC_GET_SUPPORT_FEATURE = 0xA2,
|
||||||
|
KC_GET_DEFAULT_LAYER = 0xA3,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
FEATURE_DEFAULT_LAYER = 0x01U << 0,
|
||||||
|
FEATURE_BLUETOOTH = 0x01U << 1,
|
||||||
|
FEATURE_P24G = 0x01U << 2,
|
||||||
|
FEATURE_ANALOG_MATRIX = 0x01U << 3,
|
||||||
|
FEATURE_INFO_CHAGNED_NOTIFY = 0x01U << 4,
|
||||||
|
FEATURE_DYNAMIC_DEBOUNCE = 0x01U << 5,
|
||||||
|
FEATURE_SNAP_CLICK = 0x01U << 6,
|
||||||
|
FEATURE_KEYCHRON_RGB = 0x01U << 7,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
MISC_DFU_INFO = 0x01 << 0,
|
||||||
|
MISC_LANGUAGE = 0x01 << 1,
|
||||||
|
MISC_DEBOUNCE = 0x01 << 2,
|
||||||
|
MISC_SNAP_CLICK = 0x01 << 3,
|
||||||
|
MISC_WIRELESS_LPM = 0x01 << 4,
|
||||||
|
MISC_REPORT_REATE = 0x01 << 5,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
MISC_GET_PROTOCOL_VER = 0x01,
|
||||||
|
DFU_INFO_GET,
|
||||||
|
LANGUAGE_GET,
|
||||||
|
LANGUAGE_SET,
|
||||||
|
DEBOUNCE_GET, // 5
|
||||||
|
DEBOUNCE_SET,
|
||||||
|
SNAP_CLICK_GET_INFO,
|
||||||
|
SNAP_CLICK_GET,
|
||||||
|
SNAP_CLICK_SET,
|
||||||
|
SNAP_CLICK_SAVE, // A
|
||||||
|
WIRELESS_LPM_GET,
|
||||||
|
WIRELESS_LPM_SET,
|
||||||
|
REPORT_RATE_GET,
|
||||||
|
REPORT_RATE_SET,
|
||||||
|
};
|
||||||
141
keyboards/keychron/common/keychron_task.c
Normal file
141
keyboards/keychron/common/keychron_task.c
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/* Copyright 2023~2025 @ Keychron (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 <stdlib.h>
|
||||||
|
#include "keychron_task.h"
|
||||||
|
#include "quantum.h"
|
||||||
|
#include "keychron_common.h"
|
||||||
|
#ifdef FACTORY_TEST_ENABLE
|
||||||
|
# include "factory_test.h"
|
||||||
|
#endif
|
||||||
|
#ifdef RETAIL_DEMO_ENABLE
|
||||||
|
# include "retail_demo.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
__attribute__((weak)) bool process_record_keychron_kb(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool process_record_keychron(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
extern bool process_record_wireless(uint16_t keycode, keyrecord_t * record);
|
||||||
|
if (!process_record_wireless(keycode, record)) return false;
|
||||||
|
#endif
|
||||||
|
#ifdef FACTORY_TEST_ENABLE
|
||||||
|
if (!process_record_factory_test(keycode, record)) return false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef SNAP_CLICK_ENABLE
|
||||||
|
extern bool process_record_snap_click(uint16_t keycode, keyrecord_t * record);
|
||||||
|
if (!process_record_snap_click(keycode, record)) return false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!process_record_keychron_kb(keycode, record)) return false;
|
||||||
|
|
||||||
|
#if defined(KEYCHRON_RGB_ENABLE) && defined(EECONFIG_SIZE_CUSTOM_RGB)
|
||||||
|
# if defined(RETAIL_DEMO_ENABLE)
|
||||||
|
if (!process_record_retail_demo(keycode, record)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
|
||||||
|
extern bool process_record_keychron_rgb(uint16_t keycode, keyrecord_t *record);
|
||||||
|
if (!process_record_keychron_rgb(keycode, record)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(LED_MATRIX_ENABLE)
|
||||||
|
bool led_matrix_indicators_keychron(void) {
|
||||||
|
# ifdef LK_WIRELESS_ENABLE
|
||||||
|
extern bool led_matrix_indicators_bt(void);
|
||||||
|
led_matrix_indicators_bt();
|
||||||
|
# endif
|
||||||
|
# ifdef FACTORY_TEST_ENABLE
|
||||||
|
factory_test_indicator();
|
||||||
|
# endif
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(RGB_MATRIX_ENABLE)
|
||||||
|
bool rgb_matrix_indicators_keychron(void) {
|
||||||
|
# ifdef LK_WIRELESS_ENABLE
|
||||||
|
extern bool rgb_matrix_indicators_bt(void);
|
||||||
|
rgb_matrix_indicators_bt();
|
||||||
|
# endif
|
||||||
|
# ifdef FACTORY_TEST_ENABLE
|
||||||
|
factory_test_indicator();
|
||||||
|
# endif
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
__attribute__((weak)) bool keychron_task_kb(void) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void keychron_task(void) {
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
extern void wireless_tasks(void);
|
||||||
|
wireless_tasks();
|
||||||
|
#endif
|
||||||
|
#ifdef FACTORY_TEST_ENABLE
|
||||||
|
factory_test_task();
|
||||||
|
#endif
|
||||||
|
#if defined(RETAIL_DEMO_ENABLE) && defined(KEYCHRON_RGB_ENABLE) && defined(EECONFIG_SIZE_CUSTOM_RGB)
|
||||||
|
retail_demo_task();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
keychron_common_task();
|
||||||
|
|
||||||
|
keychron_task_kb();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
if (!process_record_user(keycode, record)) return false;
|
||||||
|
|
||||||
|
if (!process_record_keychron(keycode, record)) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
bool rgb_matrix_indicators_kb(void) {
|
||||||
|
if (!rgb_matrix_indicators_user()) return false;
|
||||||
|
|
||||||
|
rgb_matrix_indicators_keychron();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
bool led_matrix_indicators_kb(void) {
|
||||||
|
if (!led_matrix_indicators_user()) return false;
|
||||||
|
|
||||||
|
led_matrix_indicators_keychron();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void housekeeping_task_kb(void) {
|
||||||
|
keychron_task();
|
||||||
|
}
|
||||||
25
keyboards/keychron/common/keychron_task.h
Normal file
25
keyboards/keychron/common/keychron_task.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* Copyright 2022 @ Keychron (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
|
||||||
|
|
||||||
|
#include "stdint.h"
|
||||||
|
#include "action.h"
|
||||||
|
|
||||||
|
bool keychron_task_kb(void);
|
||||||
|
bool process_record_keychron_kb(uint16_t keycode, keyrecord_t *record);
|
||||||
|
|
||||||
|
void keychron_task(void);
|
||||||
20
keyboards/keychron/common/language/eeconfig_language.h
Normal file
20
keyboards/keychron/common/language/eeconfig_language.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
#define EECONFIG_SIZE_LANGUAGE 1
|
||||||
|
|
||||||
60
keyboards/keychron/common/language/language.c
Normal file
60
keyboards/keychron/common/language/language.c
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 <string.h>
|
||||||
|
#include "eeconfig_kb.h"
|
||||||
|
#include "raw_hid.h"
|
||||||
|
#include "eeconfig.h"
|
||||||
|
#include "matrix.h"
|
||||||
|
#include "quantum.h"
|
||||||
|
#include "keychron_raw_hid.h"
|
||||||
|
|
||||||
|
static uint8_t lang;
|
||||||
|
|
||||||
|
static bool language_get(uint8_t *data) {
|
||||||
|
eeprom_read_block(&lang, (uint8_t *)(EECONFIG_BASE_LANGUAGE), sizeof(lang));
|
||||||
|
data[1] = lang;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool language_set(uint8_t *data) {
|
||||||
|
lang = data[0];
|
||||||
|
eeprom_update_block(&lang, (uint8_t *)(EECONFIG_BASE_LANGUAGE), sizeof(lang));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void language_rx(uint8_t *data, uint8_t length) {
|
||||||
|
uint8_t cmd = data[1];
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
switch (cmd) {
|
||||||
|
case LANGUAGE_GET:
|
||||||
|
success = language_get(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case LANGUAGE_SET:
|
||||||
|
success = language_set(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
data[0] = 0xFF;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
data[2] = success ? 0 : 1;
|
||||||
|
}
|
||||||
21
keyboards/keychron/common/language/language.h
Normal file
21
keyboards/keychron/common/language/language.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 language_config_reset(void);
|
||||||
|
void language_rx(uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
7
keyboards/keychron/common/language/language.mk
Normal file
7
keyboards/keychron/common/language/language.mk
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
LANGUAGE_DIR = common/language
|
||||||
|
SRC += \
|
||||||
|
$(LANGUAGE_DIR)/language.c \
|
||||||
|
|
||||||
|
VPATH += $(TOP_DIR)/keyboards/keychron/$(LANGUAGE_DIR)
|
||||||
|
|
||||||
|
OPT_DEFS += -DLANGUAGE_ENABLE
|
||||||
218
keyboards/keychron/common/matrix.c
Normal file
218
keyboards/keychron/common/matrix.c
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
/* Copyright 2023 @ Keychron (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"
|
||||||
|
|
||||||
|
#ifndef HC595_STCP
|
||||||
|
# define HC595_STCP B0
|
||||||
|
#endif
|
||||||
|
#ifndef HC595_SHCP
|
||||||
|
# define HC595_SHCP A1
|
||||||
|
#endif
|
||||||
|
#ifndef HC595_DS
|
||||||
|
# define HC595_DS A7
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HC595_START_INDEX
|
||||||
|
# define HC595_START_INDEX 0
|
||||||
|
#endif
|
||||||
|
#ifndef HC595_END_INDEX
|
||||||
|
# define HC595_END_INDEX 15
|
||||||
|
#endif
|
||||||
|
#ifndef HC595_OFFSET_INDEX
|
||||||
|
# define HC595_OFFSET_INDEX 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(HC595_START_INDEX) && defined(HC595_END_INDEX)
|
||||||
|
# if ((HC595_END_INDEX - HC595_START_INDEX + 1) > 16)
|
||||||
|
# define SIZE_T uint32_t
|
||||||
|
# define UNSELECT_ALL_COL 0xFFFFFFFF
|
||||||
|
# define SELECT_ALL_COL 0x00000000
|
||||||
|
# elif ((HC595_END_INDEX - HC595_START_INDEX + 1) > 8)
|
||||||
|
# define SIZE_T uint16_t
|
||||||
|
# define UNSELECT_ALL_COL 0xFFFF
|
||||||
|
# define SELECT_ALL_COL 0x0000
|
||||||
|
# else
|
||||||
|
# define SIZE_T uint8_t
|
||||||
|
# define UNSELECT_ALL_COL 0xFF
|
||||||
|
# define SELECT_ALL_COL 0x00
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
|
||||||
|
pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
|
||||||
|
|
||||||
|
static inline uint8_t readMatrixPin(pin_t pin) {
|
||||||
|
if (pin != NO_PIN) {
|
||||||
|
return readPin(pin);
|
||||||
|
} else {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void setPinOutput_writeLow(pin_t pin) {
|
||||||
|
setPinOutput(pin);
|
||||||
|
writePinLow(pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void setPinOutput_writeHigh(pin_t pin) {
|
||||||
|
setPinOutput(pin);
|
||||||
|
writePinHigh(pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void HC595_delay(uint16_t n) {
|
||||||
|
while (n-- > 0) {
|
||||||
|
asm volatile("nop" ::: "memory");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void HC595_output(SIZE_T data, bool bit_flag) {
|
||||||
|
uint8_t n = 1;
|
||||||
|
|
||||||
|
ATOMIC_BLOCK_FORCEON {
|
||||||
|
for (uint8_t i = 0; i < (HC595_END_INDEX - HC595_START_INDEX + 1); i++) {
|
||||||
|
if (data & 0x1) {
|
||||||
|
writePinHigh(HC595_DS);
|
||||||
|
} else {
|
||||||
|
writePinLow(HC595_DS);
|
||||||
|
}
|
||||||
|
writePinHigh(HC595_SHCP);
|
||||||
|
HC595_delay(n);
|
||||||
|
writePinLow(HC595_SHCP);
|
||||||
|
HC595_delay(n);
|
||||||
|
if (bit_flag) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
data = data >> 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writePinHigh(HC595_STCP);
|
||||||
|
HC595_delay(n);
|
||||||
|
writePinLow(HC595_STCP);
|
||||||
|
HC595_delay(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void select_col(uint8_t col) {
|
||||||
|
if (col < HC595_START_INDEX || col > HC595_END_INDEX) {
|
||||||
|
setPinOutput_writeLow(col_pins[col]);
|
||||||
|
} else {
|
||||||
|
if (col == HC595_START_INDEX) {
|
||||||
|
HC595_output(0x00, true);
|
||||||
|
if (col < HC595_OFFSET_INDEX) {
|
||||||
|
HC595_output(0x01, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void unselect_col(uint8_t col) {
|
||||||
|
if (col < HC595_START_INDEX || col > HC595_END_INDEX) {
|
||||||
|
#ifdef MATRIX_UNSELECT_DRIVE_HIGH
|
||||||
|
setPinOutput_writeHigh(col_pins[col]);
|
||||||
|
#else
|
||||||
|
setPinInputHigh(col_pins[col]);
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
HC595_output(0x01, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void unselect_cols(void) {
|
||||||
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
|
if (col < HC595_START_INDEX || col > HC595_END_INDEX) {
|
||||||
|
#ifdef MATRIX_UNSELECT_DRIVE_HIGH
|
||||||
|
setPinOutput_writeHigh(col_pins[col]);
|
||||||
|
#else
|
||||||
|
setPinInputHigh(col_pins[col]);
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
if (col == HC595_START_INDEX) {
|
||||||
|
HC595_output(UNSELECT_ALL_COL, false);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void select_all_cols(void) {
|
||||||
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
|
if (col < HC595_START_INDEX || col > HC595_END_INDEX) {
|
||||||
|
setPinOutput_writeLow(col_pins[col]);
|
||||||
|
} else {
|
||||||
|
if (col == HC595_START_INDEX) {
|
||||||
|
HC595_output(SELECT_ALL_COL, false);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
|
||||||
|
// Select col
|
||||||
|
select_col(current_col); // select col
|
||||||
|
HC595_delay(200);
|
||||||
|
|
||||||
|
// For each row...
|
||||||
|
for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
|
||||||
|
// Check row pin state
|
||||||
|
if (readMatrixPin(row_pins[row_index]) == 0) {
|
||||||
|
// Pin LO, set col bit
|
||||||
|
current_matrix[row_index] |= row_shifter;
|
||||||
|
} else {
|
||||||
|
// Pin HI, clear col bit
|
||||||
|
current_matrix[row_index] &= ~row_shifter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unselect col
|
||||||
|
unselect_col(current_col);
|
||||||
|
HC595_delay(200); // wait for all Row signals to go HIGH
|
||||||
|
}
|
||||||
|
|
||||||
|
void matrix_init_custom(void) {
|
||||||
|
setPinOutput(HC595_DS);
|
||||||
|
setPinOutput(HC595_STCP);
|
||||||
|
setPinOutput(HC595_SHCP);
|
||||||
|
|
||||||
|
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||||
|
if (row_pins[x] != NO_PIN) {
|
||||||
|
setPinInputHigh(row_pins[x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unselect_cols();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool matrix_scan_custom(matrix_row_t current_matrix[]) {
|
||||||
|
matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
|
||||||
|
|
||||||
|
// Set col, read rows
|
||||||
|
matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
|
||||||
|
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
|
||||||
|
matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
|
||||||
|
if (changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix));
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void suspend_wakeup_init_kb(void) {
|
||||||
|
// code will run on keyboard wakeup
|
||||||
|
clear_keyboard();
|
||||||
|
}
|
||||||
39
keyboards/keychron/common/rgb/eeconfig_custom_rgb.h
Normal file
39
keyboards/keychron/common/rgb/eeconfig_custom_rgb.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
#include "rgb_matrix_kb_config.h"
|
||||||
|
|
||||||
|
#define OS_INDICATOR_CONFIG_SIZE 4 // sizeof(os_indicator_config_t)
|
||||||
|
|
||||||
|
//#define OS_INDICATOR_CONFIG_OFFSET (PER_KEY_RGB_LED_COLOR_LIST_SIZE + RGB_MATRIX_LED_COUNT)
|
||||||
|
#define RETAIL_DEMO_SIZE 1 // sizeof(retail_demo_enable)
|
||||||
|
|
||||||
|
#define PER_KEY_RGB_TYPE_SIZE 1
|
||||||
|
#define PER_KEY_RGB_LED_COLOR_LIST_SIZE (RGB_MATRIX_LED_COUNT * 3)
|
||||||
|
|
||||||
|
#define MIX_RGB_LAYER_FLAG_SIZE RGB_MATRIX_LED_COUNT
|
||||||
|
#define EFFECT_CONFIG_SIZE 8 // sizeof(effect_config_t)
|
||||||
|
#define EFFECT_LIST_SIZE (EFFECT_LAYERS * EFFECTS_PER_LAYER * EFFECT_CONFIG_SIZE)
|
||||||
|
|
||||||
|
#define EECONFIG_SIZE_CUSTOM_RGB ( \
|
||||||
|
OS_INDICATOR_CONFIG_SIZE \
|
||||||
|
+ RETAIL_DEMO_SIZE \
|
||||||
|
+ PER_KEY_RGB_TYPE_SIZE \
|
||||||
|
+ PER_KEY_RGB_LED_COLOR_LIST_SIZE \
|
||||||
|
+ MIX_RGB_LAYER_FLAG_SIZE \
|
||||||
|
+ EFFECT_LIST_SIZE)
|
||||||
494
keyboards/keychron/common/rgb/keychron_rgb.c
Normal file
494
keyboards/keychron/common/rgb/keychron_rgb.c
Normal file
@ -0,0 +1,494 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 QMK_KEYBOARD_H
|
||||||
|
#include "raw_hid.h"
|
||||||
|
#include "keychron_common.h"
|
||||||
|
#include "keychron_rgb_type.h"
|
||||||
|
#include "eeconfig_kb.h"
|
||||||
|
#include "usb_main.h"
|
||||||
|
#include "color.h"
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
#include "transport.h"
|
||||||
|
#endif
|
||||||
|
#include <lib/lib8tion/lib8tion.h>
|
||||||
|
|
||||||
|
#if defined(KEYCHRON_RGB_ENABLE) && defined(EECONFIG_SIZE_CUSTOM_RGB)
|
||||||
|
|
||||||
|
# define PER_KEY_RGB_VER 0x0001
|
||||||
|
|
||||||
|
# define OFFSET_OS_INDICATOR ((uint8_t *)(EECONFIG_BASE_CUSTOM_RGB))
|
||||||
|
# define OFFSET_RETAIL_DEMO (OFFSET_OS_INDICATOR + sizeof(os_indicator_config_t))
|
||||||
|
# define OFFSET_PER_KEY_RGB_TYPE (OFFSET_RETAIL_DEMO + sizeof(retail_demo_enable))
|
||||||
|
# define OFFSET_PER_KEY_RGBS (OFFSET_PER_KEY_RGB_TYPE + sizeof(per_key_rgb_type))
|
||||||
|
# define OFFSET_LAYER_FLAGS (OFFSET_PER_KEY_RGBS + sizeof(per_key_led))
|
||||||
|
# define OFFSET_EFFECT_LIST (OFFSET_LAYER_FLAGS + sizeof(regions))
|
||||||
|
|
||||||
|
enum {
|
||||||
|
RGB_GET_PROTOCOL_VER = 0x01,
|
||||||
|
RGB_SAVE,
|
||||||
|
GET_INDICATORS_CONFIG,
|
||||||
|
SET_INDICATORS_CONFIG,
|
||||||
|
RGB_GET_LED_COUNT,
|
||||||
|
RGB_GET_LED_IDX,
|
||||||
|
PER_KEY_RGB_GET_TYPE,
|
||||||
|
PER_KEY_RGB_SET_TYPE,
|
||||||
|
PER_KEY_RGB_GET_COLOR,
|
||||||
|
PER_KEY_RGB_SET_COLOR, //10
|
||||||
|
MIXED_EFFECT_RGB_GET_INFO,
|
||||||
|
MIXED_EFFECT_RGB_GET_REGIONS,
|
||||||
|
MIXED_EFFECT_RGB_SET_REGIONS,
|
||||||
|
MIXED_EFFECT_RGB_GET_EFFECT_LIST,
|
||||||
|
MIXED_EFFECT_RGB_SET_EFFECT_LIST,
|
||||||
|
};
|
||||||
|
|
||||||
|
extern uint8_t retail_demo_enable;
|
||||||
|
extern uint8_t per_key_rgb_type;
|
||||||
|
extern HSV per_key_led[RGB_MATRIX_LED_COUNT];
|
||||||
|
extern HSV default_per_key_led[RGB_MATRIX_LED_COUNT];
|
||||||
|
|
||||||
|
extern uint8_t regions[RGB_MATRIX_LED_COUNT];
|
||||||
|
extern uint8_t rgb_regions[RGB_MATRIX_LED_COUNT];
|
||||||
|
extern effect_config_t effect_list[EFFECT_LAYERS][EFFECTS_PER_LAYER];
|
||||||
|
extern uint8_t default_region[RGB_MATRIX_LED_COUNT];
|
||||||
|
|
||||||
|
os_indicator_config_t os_ind_cfg;
|
||||||
|
|
||||||
|
extern void update_mixed_rgb_effect_count(void);
|
||||||
|
|
||||||
|
void eeconfig_reset_custom_rgb(void) {
|
||||||
|
os_ind_cfg.disable.raw = 0;
|
||||||
|
os_ind_cfg.hsv.s = 0;
|
||||||
|
os_ind_cfg.hsv.h = os_ind_cfg.hsv.v = 0xFF;
|
||||||
|
|
||||||
|
eeprom_update_block(&os_ind_cfg, OFFSET_OS_INDICATOR, sizeof(os_ind_cfg));
|
||||||
|
retail_demo_enable = 0;
|
||||||
|
eeprom_read_block(&retail_demo_enable, (uint8_t *)(OFFSET_RETAIL_DEMO), sizeof(retail_demo_enable));
|
||||||
|
per_key_rgb_type = 0;
|
||||||
|
eeprom_update_block(&per_key_rgb_type, OFFSET_PER_KEY_RGB_TYPE, sizeof(per_key_rgb_type));
|
||||||
|
|
||||||
|
memcpy(per_key_led, default_per_key_led, sizeof(per_key_led));
|
||||||
|
eeprom_update_block(per_key_led, OFFSET_PER_KEY_RGBS, sizeof(per_key_led));
|
||||||
|
|
||||||
|
memcpy(regions, default_region, RGB_MATRIX_LED_COUNT);
|
||||||
|
eeprom_update_block(regions, OFFSET_LAYER_FLAGS, sizeof(regions));
|
||||||
|
|
||||||
|
memset(effect_list, 0, sizeof(effect_list));
|
||||||
|
|
||||||
|
effect_list[0][0].effect = 5;
|
||||||
|
effect_list[0][0].sat = 255;
|
||||||
|
effect_list[0][0].speed = 127;
|
||||||
|
effect_list[0][0].time = 5000;
|
||||||
|
|
||||||
|
effect_list[1][0].effect = 2;
|
||||||
|
effect_list[1][0].hue = 0;
|
||||||
|
effect_list[1][0].sat = 255;
|
||||||
|
effect_list[1][0].speed = 127;
|
||||||
|
effect_list[1][0].time = 5000;
|
||||||
|
|
||||||
|
eeprom_update_block(effect_list, OFFSET_EFFECT_LIST, sizeof(effect_list));
|
||||||
|
update_mixed_rgb_effect_count();
|
||||||
|
}
|
||||||
|
|
||||||
|
void eeconfig_init_custom_rgb(void) {
|
||||||
|
memcpy(per_key_led, default_per_key_led, sizeof(per_key_led));
|
||||||
|
eeprom_update_dword(EECONFIG_KEYBOARD, (EECONFIG_KB_DATA_VERSION));
|
||||||
|
|
||||||
|
eeprom_read_block(&os_ind_cfg, OFFSET_OS_INDICATOR, sizeof(os_ind_cfg));
|
||||||
|
eeprom_read_block(&retail_demo_enable, (uint8_t *)(OFFSET_RETAIL_DEMO), sizeof(retail_demo_enable));
|
||||||
|
|
||||||
|
if (os_ind_cfg.hsv.v < 128) os_ind_cfg.hsv.v = 128;
|
||||||
|
// Load per key rgb led
|
||||||
|
eeprom_read_block(&per_key_rgb_type, OFFSET_PER_KEY_RGB_TYPE, sizeof(per_key_rgb_type));
|
||||||
|
eeprom_read_block(per_key_led, OFFSET_PER_KEY_RGBS, sizeof(per_key_led));
|
||||||
|
// Load mixed rgb
|
||||||
|
eeprom_read_block(regions, OFFSET_LAYER_FLAGS, sizeof(regions));
|
||||||
|
eeprom_read_block(effect_list, OFFSET_EFFECT_LIST, sizeof(effect_list));
|
||||||
|
update_mixed_rgb_effect_count();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void rgb_save_retail_demo(void) {
|
||||||
|
eeprom_update_block(&retail_demo_enable, (uint8_t *)(OFFSET_RETAIL_DEMO), sizeof(retail_demo_enable));
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool rgb_get_version(uint8_t *data) {
|
||||||
|
data[1] = PER_KEY_RGB_VER & 0xFF;
|
||||||
|
data[2] = (PER_KEY_RGB_VER >> 8) & 0xFF;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool rgb_get_led_count(uint8_t *data) {
|
||||||
|
data[1] = RGB_MATRIX_LED_COUNT;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool rgb_get_led_idx(uint8_t *data) {
|
||||||
|
uint8_t row = data[0];
|
||||||
|
if (row > MATRIX_ROWS) return false;
|
||||||
|
|
||||||
|
uint8_t led_idx[128];
|
||||||
|
uint32_t row_mask = 0;
|
||||||
|
memcpy(&row_mask, &data[1], 3);
|
||||||
|
|
||||||
|
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
|
||||||
|
led_idx[0] = 0xFF;
|
||||||
|
if (row_mask & (0x01 << c)) {
|
||||||
|
rgb_matrix_map_row_column_to_led(row, c, led_idx);
|
||||||
|
}
|
||||||
|
data[1 + c] = led_idx[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool per_key_rgb_get_type(uint8_t *data) {
|
||||||
|
extern uint8_t per_key_rgb_type;
|
||||||
|
data[1] = per_key_rgb_type;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool per_key_rgb_set_type(uint8_t *data) {
|
||||||
|
uint8_t type = data[0];
|
||||||
|
|
||||||
|
if (type >= PER_KEY_RGB_MAX) return false;
|
||||||
|
|
||||||
|
per_key_rgb_type = data[0];
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool per_key_rgb_get_led_color(uint8_t *data) {
|
||||||
|
uint8_t start = data[0];
|
||||||
|
uint8_t count = data[1];
|
||||||
|
|
||||||
|
if (count > 9) return false;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < count; i++) {
|
||||||
|
data[1 + i * 3] = per_key_led[start + i].h;
|
||||||
|
data[2 + i * 3] = per_key_led[start + i].s;
|
||||||
|
data[3 + i * 3] = per_key_led[start + i].v;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool per_key_rgb_set_led_color(uint8_t *data) {
|
||||||
|
uint8_t start = data[0];
|
||||||
|
uint8_t count = data[1];
|
||||||
|
|
||||||
|
if (count > 9) return false;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < count; i++) {
|
||||||
|
per_key_led[start + i].h = data[2 + i * 3];
|
||||||
|
per_key_led[start + i].s = data[3 + i * 3];
|
||||||
|
per_key_led[start + i].v = data[4 + i * 3];
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mixed_rgb_get_effect_info(uint8_t *data) {
|
||||||
|
data[1] = EFFECT_LAYERS;
|
||||||
|
data[2] = EFFECTS_PER_LAYER;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mixed_rgb_get_regions(uint8_t *data) {
|
||||||
|
uint8_t start = data[0];
|
||||||
|
uint8_t count = data[1];
|
||||||
|
|
||||||
|
if (count > 29 || start + count > RGB_MATRIX_LED_COUNT) return false;
|
||||||
|
memcpy(&data[1], ®ions[start], count);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mixed_rgb_set_regions(uint8_t *data) {
|
||||||
|
uint8_t start = data[0];
|
||||||
|
uint8_t count = data[1];
|
||||||
|
|
||||||
|
if (count > 28 || start + count > RGB_MATRIX_LED_COUNT) return false;
|
||||||
|
for (uint8_t i = 0; i < count; i++)
|
||||||
|
if (data[2 + i] >= EFFECT_LAYERS) return false;
|
||||||
|
|
||||||
|
memcpy(®ions[start], &data[2], count);
|
||||||
|
memcpy(&rgb_regions[start], &data[2], count);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#define EFFECT_DATA_LEN 8
|
||||||
|
|
||||||
|
static bool mixed_rgb_get_effect_list(uint8_t *data) {
|
||||||
|
uint8_t region = data[0];
|
||||||
|
uint8_t start = data[1];
|
||||||
|
uint8_t count = data[2];
|
||||||
|
|
||||||
|
if (count > 3 || region > EFFECT_LAYERS || start + count > EFFECTS_PER_LAYER) return false;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < count; i++) {
|
||||||
|
data[1 + i * EFFECT_DATA_LEN] = effect_list[region][start + i].effect;
|
||||||
|
data[2 + i * EFFECT_DATA_LEN] = effect_list[region][start + i].hue;
|
||||||
|
data[3 + i * EFFECT_DATA_LEN] = effect_list[region][start + i].sat;
|
||||||
|
data[4 + i * EFFECT_DATA_LEN] = effect_list[region][start + i].speed;
|
||||||
|
memcpy(&data[5 + i * EFFECT_DATA_LEN], &effect_list[region][start + i].time, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mixed_rgb_set_effect_list(uint8_t *data) {
|
||||||
|
uint8_t region = data[0];
|
||||||
|
uint8_t start = data[1];
|
||||||
|
uint8_t count = data[2];
|
||||||
|
|
||||||
|
if (count > 3 || region > EFFECT_LAYERS || start + count > EFFECTS_PER_LAYER) return false;
|
||||||
|
for (uint8_t i = 0; i < count; i++) {
|
||||||
|
if (data[3 + i * EFFECT_DATA_LEN] >= RGB_MATRIX_CUSTOM_MIXED_RGB) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < count; i++) {
|
||||||
|
effect_list[region][start + i].effect = data[3 + i * EFFECT_DATA_LEN];
|
||||||
|
effect_list[region][start + i].hue = data[4 + i * EFFECT_DATA_LEN];
|
||||||
|
effect_list[region][start + i].sat = data[5 + i * EFFECT_DATA_LEN];
|
||||||
|
effect_list[region][start + i].speed = data[6 + i * EFFECT_DATA_LEN];
|
||||||
|
memcpy(&effect_list[region][start + i].time, &data[7 + i * EFFECT_DATA_LEN], 4);
|
||||||
|
}
|
||||||
|
update_mixed_rgb_effect_count();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool kc_rgb_save(void) {
|
||||||
|
eeprom_update_block(&os_ind_cfg, OFFSET_OS_INDICATOR, sizeof(os_ind_cfg));
|
||||||
|
eeprom_update_block(&per_key_rgb_type, OFFSET_PER_KEY_RGB_TYPE, sizeof(per_key_rgb_type));
|
||||||
|
eeprom_update_block(per_key_led, OFFSET_PER_KEY_RGBS, RGB_MATRIX_LED_COUNT * sizeof(rgb_led_t));
|
||||||
|
eeprom_update_block(regions, OFFSET_LAYER_FLAGS, RGB_MATRIX_LED_COUNT);
|
||||||
|
eeprom_update_block(effect_list, OFFSET_EFFECT_LIST, sizeof(effect_list));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool get_indicators_config(uint8_t *data) {
|
||||||
|
data[1] = 0
|
||||||
|
#if defined(NUM_LOCK_INDEX) && !defined(DIM_NUM_LOCK)
|
||||||
|
| (1 << 0x00)
|
||||||
|
#endif
|
||||||
|
#if defined(CAPS_LOCK_INDEX) && !defined(DIM_CAPS_LOCK)
|
||||||
|
| (1 << 0x01)
|
||||||
|
#endif
|
||||||
|
#if defined(SCROLL_LOCK_INDEX)
|
||||||
|
| (1 << 0x02)
|
||||||
|
#endif
|
||||||
|
#if defined(COMPOSE_LOCK_INDEX)
|
||||||
|
| (1 << 0x03)
|
||||||
|
#endif
|
||||||
|
#if defined(KANA_LOCK_INDEX)
|
||||||
|
| (1 << 0x04)
|
||||||
|
#endif
|
||||||
|
;
|
||||||
|
data[2] = os_ind_cfg.disable.raw;
|
||||||
|
data[3] = os_ind_cfg.hsv.h;
|
||||||
|
data[4] = os_ind_cfg.hsv.s;
|
||||||
|
data[5] = os_ind_cfg.hsv.v;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool set_indicators_config(uint8_t *data) {
|
||||||
|
os_ind_cfg.disable.raw = data[0];
|
||||||
|
os_ind_cfg.hsv.h = data[1];
|
||||||
|
os_ind_cfg.hsv.s = data[2];
|
||||||
|
os_ind_cfg.hsv.v = data[3];
|
||||||
|
|
||||||
|
if (os_ind_cfg.hsv.v < 128) os_ind_cfg.hsv.v = 128;
|
||||||
|
led_update_kb(host_keyboard_led_state());
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void kc_rgb_matrix_rx(uint8_t *data, uint8_t length) {
|
||||||
|
uint8_t cmd = data[1];
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
switch (cmd) {
|
||||||
|
case RGB_GET_PROTOCOL_VER:
|
||||||
|
success = rgb_get_version(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RGB_SAVE:
|
||||||
|
success = kc_rgb_save();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GET_INDICATORS_CONFIG:
|
||||||
|
success = get_indicators_config(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SET_INDICATORS_CONFIG:
|
||||||
|
success = set_indicators_config(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RGB_GET_LED_COUNT:
|
||||||
|
success = rgb_get_led_count(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RGB_GET_LED_IDX:
|
||||||
|
success = rgb_get_led_idx(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PER_KEY_RGB_GET_TYPE:
|
||||||
|
success = per_key_rgb_get_type(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PER_KEY_RGB_SET_TYPE:
|
||||||
|
success = per_key_rgb_set_type(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PER_KEY_RGB_GET_COLOR:
|
||||||
|
success = per_key_rgb_get_led_color(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PER_KEY_RGB_SET_COLOR:
|
||||||
|
success = per_key_rgb_set_led_color(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MIXED_EFFECT_RGB_GET_INFO:
|
||||||
|
success = mixed_rgb_get_effect_info(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MIXED_EFFECT_RGB_GET_REGIONS:
|
||||||
|
success = mixed_rgb_get_regions(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MIXED_EFFECT_RGB_SET_REGIONS:
|
||||||
|
success = mixed_rgb_set_regions(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MIXED_EFFECT_RGB_GET_EFFECT_LIST:
|
||||||
|
success = mixed_rgb_get_effect_list(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MIXED_EFFECT_RGB_SET_EFFECT_LIST:
|
||||||
|
success = mixed_rgb_set_effect_list(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
data[0] = 0xFF;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
data[2] = success ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void os_state_indicate(void) {
|
||||||
|
# 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
|
||||||
|
|
||||||
|
RGB rgb = hsv_to_rgb(os_ind_cfg.hsv);
|
||||||
|
|
||||||
|
# if defined(NUM_LOCK_INDEX)
|
||||||
|
if (host_keyboard_led_state().num_lock && !os_ind_cfg.disable.num_lock) {
|
||||||
|
rgb_matrix_set_color(NUM_LOCK_INDEX, rgb.r, rgb.g, rgb.b);
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
# if defined(CAPS_LOCK_INDEX)
|
||||||
|
if (host_keyboard_led_state().caps_lock && !os_ind_cfg.disable.caps_lock) {
|
||||||
|
rgb_matrix_set_color(CAPS_LOCK_INDEX, rgb.r, rgb.g, rgb.b);
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
# if defined(SCROLL_LOCK_INDEX)
|
||||||
|
if (host_keyboard_led_state().compose && !os_ind_cfg.disable.scroll_lock) {
|
||||||
|
rgb_matrix_set_color(SCROLL_LOCK_INDEX, rgb.r, rgb.g, rgb.b);
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
# if defined(COMPOSE_LOCK_INDEX)
|
||||||
|
if (host_keyboard_led_state().compose && !os_ind_cfg.disable.compose) {
|
||||||
|
rgb_matrix_set_color(COMPOSE_LOCK_INDEX, rgb.r, rgb.g, rgb.b);
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
# if defined(KANA_LOCK_INDEX)
|
||||||
|
if (host_keyboard_led_state().kana && !os_ind_cfg.disable.kana) {
|
||||||
|
rgb_matrix_set_color(KANA_LOCK_INDEX, rgb.r, rgb.g, rgb.b);
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
(void)rgb;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool process_record_keychron_rgb(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
if (rgb_matrix_get_mode() == RGB_MATRIX_CUSTOM_MIXED_RGB || rgb_matrix_get_mode() == RGB_MATRIX_CUSTOM_PER_KEY_RGB) {
|
||||||
|
switch (keycode) {
|
||||||
|
case RGB_HUI ... RGB_SAD:
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case RGB_SPI:
|
||||||
|
if (rgb_matrix_get_mode() == RGB_MATRIX_CUSTOM_MIXED_RGB) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
|
||||||
|
eeprom_write_byte((uint8_t *)EECONFIG_RGB_MATRIX + offsetof(rgb_config_t, speed), rgb_matrix_config.speed);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RGB_SPD:
|
||||||
|
if (rgb_matrix_get_mode() == RGB_MATRIX_CUSTOM_MIXED_RGB) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
|
||||||
|
eeprom_write_byte((uint8_t *)EECONFIG_RGB_MATRIX + offsetof(rgb_config_t, speed), rgb_matrix_config.speed);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RGB_VAI:
|
||||||
|
# ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL
|
||||||
|
if (!rgb_matrix_config.enable) {
|
||||||
|
rgb_matrix_toggle();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
rgb_matrix_config.hsv.v = qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
|
||||||
|
# ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL
|
||||||
|
while (rgb_matrix_config.hsv.v <= RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL)
|
||||||
|
rgb_matrix_config.hsv.v = qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
|
||||||
|
# endif
|
||||||
|
eeprom_write_byte((uint8_t *)EECONFIG_RGB_MATRIX + offsetof(rgb_config_t, hsv.v), rgb_matrix_config.hsv.v);
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case RGB_VAD:
|
||||||
|
# ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL
|
||||||
|
if (rgb_matrix_config.enable && rgb_matrix_config.hsv.v > RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL)
|
||||||
|
# endif
|
||||||
|
{
|
||||||
|
rgb_matrix_config.hsv.v = qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
|
||||||
|
eeprom_write_byte((uint8_t *)EECONFIG_RGB_MATRIX + offsetof(rgb_config_t, hsv.v), rgb_matrix_config.hsv.v);
|
||||||
|
}
|
||||||
|
# ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL
|
||||||
|
if (rgb_matrix_config.enable && rgb_matrix_config.hsv.v <= RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL) {
|
||||||
|
rgb_matrix_toggle();
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
return false;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
59
keyboards/keychron/common/rgb/keychron_rgb_type.h
Normal file
59
keyboards/keychron/common/rgb/keychron_rgb_type.h
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
#include "color.h"
|
||||||
|
|
||||||
|
enum {
|
||||||
|
PER_KEY_RGB_SOLID,
|
||||||
|
PER_KEY_RGB_BREATHING,
|
||||||
|
PER_KEY_RGB_REATIVE_SIMPLE,
|
||||||
|
PER_KEY_RGB_REATIVE_MULTI_WIDE,
|
||||||
|
PER_KEY_RGB_REATIVE_SPLASH,
|
||||||
|
PER_KEY_RGB_MAX,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct PACKED {
|
||||||
|
uint8_t effect;
|
||||||
|
uint8_t hue;
|
||||||
|
uint8_t sat;
|
||||||
|
uint8_t speed;
|
||||||
|
uint32_t time;
|
||||||
|
} effect_config_t;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
uint8_t raw;
|
||||||
|
struct {
|
||||||
|
bool num_lock : 1;
|
||||||
|
bool caps_lock : 1;
|
||||||
|
bool scroll_lock : 1;
|
||||||
|
bool compose : 1;
|
||||||
|
bool kana : 1;
|
||||||
|
uint8_t reserved : 3;
|
||||||
|
};
|
||||||
|
} os_led_t;
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// typedef struct PACKED HSV2 {
|
||||||
|
// uint8_t h;
|
||||||
|
// uint8_t s;
|
||||||
|
// uint8_t v;
|
||||||
|
// } HSV2;
|
||||||
|
|
||||||
|
typedef struct PACKED {
|
||||||
|
os_led_t disable;
|
||||||
|
HSV hsv;
|
||||||
|
} os_indicator_config_t;
|
||||||
191
keyboards/keychron/common/rgb/mixed_rgb.c
Normal file
191
keyboards/keychron/common/rgb/mixed_rgb.c
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined(KEYCHRON_RGB_ENABLE) && defined(EECONFIG_SIZE_CUSTOM_RGB)
|
||||||
|
|
||||||
|
#include "quantum.h"
|
||||||
|
#include "rgb_matrix.h"
|
||||||
|
#include "keychron_rgb_type.h"
|
||||||
|
|
||||||
|
#define RGB_MATRIX_EFFECT(name, ...) \
|
||||||
|
extern bool name(effect_params_t *params);
|
||||||
|
#include "rgb_matrix_effects.inc"
|
||||||
|
#include "rgb_matrix_kb.inc"
|
||||||
|
#undef RGB_MATRIX_EFFECT
|
||||||
|
|
||||||
|
// PER_KEY_RGB data
|
||||||
|
extern uint8_t per_key_rgb_type;
|
||||||
|
|
||||||
|
// MIXED_RGB data
|
||||||
|
extern uint8_t rgb_regions[RGB_MATRIX_LED_COUNT];
|
||||||
|
uint8_t regions[RGB_MATRIX_LED_COUNT] = {0}; //
|
||||||
|
effect_config_t effect_list[EFFECT_LAYERS][EFFECTS_PER_LAYER];
|
||||||
|
|
||||||
|
uint8_t layer_effect_count[EFFECT_LAYERS] = {0};
|
||||||
|
uint8_t layer_effect_index[EFFECT_LAYERS] = {0};
|
||||||
|
uint32_t layer_effect_timer[EFFECT_LAYERS] = {0};
|
||||||
|
|
||||||
|
// Typing heatmap
|
||||||
|
uint8_t typingHeatmap = 0;
|
||||||
|
|
||||||
|
static bool multiple_rgb_effect_runner(effect_params_t *params);
|
||||||
|
|
||||||
|
void mixed_rgb_reset(void) {
|
||||||
|
typingHeatmap = 0;
|
||||||
|
for (uint8_t i=0; i<EFFECT_LAYERS; i++) {
|
||||||
|
layer_effect_index[i] = 0;
|
||||||
|
layer_effect_timer[i] = timer_read32();
|
||||||
|
|
||||||
|
if (effect_list[i][0].effect == RGB_MATRIX_TYPING_HEATMAP) typingHeatmap |= 0x01 << i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_mixed_rgb_effect_count(void) {
|
||||||
|
for (int8_t layer=0; layer<EFFECT_LAYERS; layer++) {
|
||||||
|
layer_effect_count[layer] = 0;
|
||||||
|
for (uint8_t i=0; i<EFFECTS_PER_LAYER; i++) {
|
||||||
|
if (effect_list[layer][i].effect != 0) ++layer_effect_count[layer];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mixed_rgb_reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mixed_rgb(effect_params_t *params) {
|
||||||
|
|
||||||
|
bool ret;
|
||||||
|
|
||||||
|
extern uint8_t rgb_regions[RGB_MATRIX_LED_COUNT];
|
||||||
|
if (params->init) {
|
||||||
|
memcpy(rgb_regions, regions, RGB_MATRIX_LED_COUNT);
|
||||||
|
memset(layer_effect_index, 0, sizeof(layer_effect_index));
|
||||||
|
|
||||||
|
mixed_rgb_reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int8_t i=EFFECT_LAYERS-1; i>=0; i--) {
|
||||||
|
params->region = i;
|
||||||
|
ret = multiple_rgb_effect_runner(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TRANSITION_TIME 1000
|
||||||
|
|
||||||
|
bool multiple_rgb_effect_runner(effect_params_t *params) {
|
||||||
|
HSV hsv= rgb_matrix_get_hsv();
|
||||||
|
uint8_t backup_value = hsv.v;
|
||||||
|
|
||||||
|
bool transation = false;
|
||||||
|
bool rendering = false;
|
||||||
|
uint8_t layer = params->region;
|
||||||
|
|
||||||
|
uint8_t effect_index = layer_effect_index[layer];
|
||||||
|
|
||||||
|
if (effect_list[layer][effect_index].effect == RGB_MATRIX_TYPING_HEATMAP)
|
||||||
|
typingHeatmap |= 0x01 << layer;
|
||||||
|
else
|
||||||
|
typingHeatmap &= ~(0x01 << layer);
|
||||||
|
|
||||||
|
uint8_t last_effect = effect_list[layer][layer_effect_index[layer]].effect;
|
||||||
|
|
||||||
|
if (layer_effect_count[layer] > 1) {
|
||||||
|
if (timer_elapsed32(layer_effect_timer[layer]) > effect_list[layer][effect_index].time) {
|
||||||
|
layer_effect_timer[layer] = timer_read32();
|
||||||
|
if (++layer_effect_index[layer] >= EFFECTS_PER_LAYER) layer_effect_index[layer] = 0;
|
||||||
|
|
||||||
|
effect_index = layer_effect_index[layer];
|
||||||
|
|
||||||
|
if (effect_list[layer][effect_index].time == 0) return true; //
|
||||||
|
}
|
||||||
|
else if (timer_elapsed32(layer_effect_timer[layer]) > effect_list[layer][effect_index].time - TRANSITION_TIME)
|
||||||
|
{
|
||||||
|
hsv.v = backup_value*(effect_list[layer][effect_index].time - timer_elapsed32(layer_effect_timer[layer]))/TRANSITION_TIME;
|
||||||
|
transation = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timer_elapsed32(layer_effect_timer[layer]) < TRANSITION_TIME)
|
||||||
|
{
|
||||||
|
hsv.v = backup_value*timer_elapsed32(layer_effect_timer[layer])/TRANSITION_TIME;
|
||||||
|
transation = true;
|
||||||
|
}
|
||||||
|
} else if (layer_effect_count[layer] == 1 && effect_list[layer][effect_index].effect == 0) {
|
||||||
|
for (uint8_t i=0; i<EFFECTS_PER_LAYER; i++) {
|
||||||
|
if (effect_list[layer][i].effect != 0) {
|
||||||
|
effect_index = layer_effect_index[params->region] = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t effect = effect_list[layer][effect_index].effect;
|
||||||
|
if (effect == 0) ++layer_effect_index[layer]; // Skip effect 0
|
||||||
|
if (layer_effect_index[layer] >= EFFECTS_PER_LAYER) layer_effect_index[layer] = 0;
|
||||||
|
|
||||||
|
effect = effect_list[layer][effect_index].effect;
|
||||||
|
hsv.h = effect_list[layer][effect_index].hue;
|
||||||
|
hsv.s = effect_list[layer][effect_index].sat;
|
||||||
|
rgb_matrix_sethsv_noeeprom(hsv.h, hsv.s, hsv.v);
|
||||||
|
|
||||||
|
rgb_matrix_set_speed_noeeprom(effect_list[layer][effect_index].speed);
|
||||||
|
|
||||||
|
params->init = last_effect != effect;
|
||||||
|
|
||||||
|
// each effect can opt to do calculations
|
||||||
|
// and/or request PWM buffer updates.
|
||||||
|
switch (effect) {
|
||||||
|
// ---------------------------------------------
|
||||||
|
// -----Begin rgb effect switch case macros-----
|
||||||
|
#define RGB_MATRIX_EFFECT(name, ...) \
|
||||||
|
case RGB_MATRIX_##name: \
|
||||||
|
rendering = name(params); \
|
||||||
|
break;
|
||||||
|
#include "rgb_matrix_effects.inc"
|
||||||
|
#undef RGB_MATRIX_EFFECT
|
||||||
|
|
||||||
|
#if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
|
||||||
|
# define RGB_MATRIX_EFFECT(name, ...) \
|
||||||
|
case RGB_MATRIX_CUSTOM_##name: \
|
||||||
|
rendering = name(params); \
|
||||||
|
break;
|
||||||
|
# ifdef RGB_MATRIX_CUSTOM_KB
|
||||||
|
# include "rgb_matrix_kb.inc"
|
||||||
|
# endif
|
||||||
|
# undef RGB_MATRIX_EFFECT
|
||||||
|
#endif
|
||||||
|
// -----End rgb effect switch case macros-------
|
||||||
|
// ---------------------------------------------
|
||||||
|
}
|
||||||
|
|
||||||
|
if (transation) {
|
||||||
|
rgb_matrix_sethsv_noeeprom(hsv.h, hsv.s, backup_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rendering;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void process_rgb_matrix_kb(uint8_t row, uint8_t col, bool pressed) {
|
||||||
|
if (pressed)
|
||||||
|
{
|
||||||
|
if (rgb_matrix_config.mode == RGB_MATRIX_CUSTOM_MIXED_RGB) {
|
||||||
|
extern void process_rgb_matrix_typing_heatmap(uint8_t row, uint8_t col);
|
||||||
|
if (typingHeatmap) process_rgb_matrix_typing_heatmap(row, col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
160
keyboards/keychron/common/rgb/per_key_rgb.c
Normal file
160
keyboards/keychron/common/rgb/per_key_rgb.c
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 "rgb_matrix.h"
|
||||||
|
#include "keychron_rgb_type.h"
|
||||||
|
#include <math.h>
|
||||||
|
#include <lib/lib8tion/lib8tion.h>
|
||||||
|
|
||||||
|
#if defined(KEYCHRON_RGB_ENABLE)
|
||||||
|
|
||||||
|
// PER_KEY_RGB data
|
||||||
|
uint8_t per_key_rgb_type;
|
||||||
|
HSV per_key_led[RGB_MATRIX_LED_COUNT] = {0};
|
||||||
|
|
||||||
|
bool per_key_rgb_solid(effect_params_t *params) {
|
||||||
|
RGB_MATRIX_USE_LIMITS(led_min, led_max);
|
||||||
|
HSV hsv;
|
||||||
|
|
||||||
|
for (uint8_t i = led_min; i < led_max; i++) {
|
||||||
|
hsv = per_key_led[i];
|
||||||
|
hsv.v = rgb_matrix_config.hsv.v;
|
||||||
|
RGB rgb = hsv_to_rgb(hsv);
|
||||||
|
rgb_matrix_region_set_color(params->region, i, rgb.r, rgb.g, rgb.b);
|
||||||
|
}
|
||||||
|
return rgb_matrix_check_finished_leds(led_max);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool per_key_rgb_breahting(effect_params_t *params) {
|
||||||
|
RGB_MATRIX_USE_LIMITS(led_min, led_max);
|
||||||
|
HSV hsv;
|
||||||
|
uint16_t time = scale16by8(g_rgb_timer, rgb_matrix_config.speed / 8);
|
||||||
|
|
||||||
|
for (uint8_t i = led_min; i < led_max; i++) {
|
||||||
|
hsv = per_key_led[i];
|
||||||
|
hsv.v = scale8(abs8(sin8(time) - 128) * 2, rgb_matrix_config.hsv.v);
|
||||||
|
RGB rgb = hsv_to_rgb(hsv);
|
||||||
|
RGB_MATRIX_TEST_LED_FLAGS();
|
||||||
|
rgb_matrix_region_set_color(params->region, i, rgb.r, rgb.g, rgb.b);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rgb_matrix_check_finished_leds(led_max);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool per_key_rgb_reactive_simple(effect_params_t *params) {
|
||||||
|
RGB_MATRIX_USE_LIMITS(led_min, led_max);
|
||||||
|
|
||||||
|
uint16_t max_tick = 65535 / qadd8(rgb_matrix_config.speed, 1);
|
||||||
|
for (uint8_t i = led_min; i < led_max; i++) {
|
||||||
|
RGB_MATRIX_TEST_LED_FLAGS();
|
||||||
|
uint16_t tick = max_tick;
|
||||||
|
// Reverse search to find most recent key hit
|
||||||
|
for (int8_t j = g_last_hit_tracker.count - 1; j >= 0; j--) {
|
||||||
|
if (g_last_hit_tracker.index[j] == i && g_last_hit_tracker.tick[j] < tick) {
|
||||||
|
tick = g_last_hit_tracker.tick[j];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t offset = scale16by8(tick, qadd8(rgb_matrix_config.speed, 1));
|
||||||
|
HSV hsv = per_key_led[i];
|
||||||
|
|
||||||
|
hsv.v = scale8(255 - offset, rgb_matrix_config.hsv.v);
|
||||||
|
if (per_key_led[i].v < hsv.v)
|
||||||
|
hsv.v = per_key_led[i].v;
|
||||||
|
|
||||||
|
RGB rgb = hsv_to_rgb(hsv);
|
||||||
|
rgb_matrix_region_set_color(params->region, i, rgb.r, rgb.g, rgb.b);
|
||||||
|
}
|
||||||
|
return rgb_matrix_check_finished_leds(led_max);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef HSV (*reactive_splash_f)(HSV hsv, int16_t dx, int16_t dy, uint8_t dist, uint16_t tick);
|
||||||
|
|
||||||
|
bool per_key_rgb_effect_runner_reactive_splash(uint8_t start, effect_params_t* params, reactive_splash_f effect_func) {
|
||||||
|
RGB_MATRIX_USE_LIMITS(led_min, led_max);
|
||||||
|
|
||||||
|
uint8_t count = g_last_hit_tracker.count;
|
||||||
|
for (uint8_t i = led_min; i < led_max; i++) {
|
||||||
|
RGB_MATRIX_TEST_LED_FLAGS();
|
||||||
|
HSV hsv = rgb_matrix_config.hsv;
|
||||||
|
hsv.v = 0;
|
||||||
|
for (uint8_t j = start; j < count; j++) {
|
||||||
|
int16_t dx = g_led_config.point[i].x - g_last_hit_tracker.x[j];
|
||||||
|
int16_t dy = g_led_config.point[i].y - g_last_hit_tracker.y[j];
|
||||||
|
uint8_t dist = sqrt16(dx * dx + dy * dy);
|
||||||
|
uint16_t tick = scale16by8(g_last_hit_tracker.tick[j], qadd8(rgb_matrix_config.speed, 1));
|
||||||
|
hsv = effect_func(hsv, dx, dy, dist, tick);
|
||||||
|
}
|
||||||
|
hsv.h = per_key_led[i].h;
|
||||||
|
hsv.s = per_key_led[i].s;
|
||||||
|
hsv.v = scale8(hsv.v, rgb_matrix_config.hsv.v);
|
||||||
|
if (per_key_led[i].v < hsv.v)
|
||||||
|
hsv.v = per_key_led[i].v;
|
||||||
|
RGB rgb = hsv_to_rgb(hsv);
|
||||||
|
rgb_matrix_region_set_color(params->region, i, rgb.r, rgb.g, rgb.b);
|
||||||
|
}
|
||||||
|
return rgb_matrix_check_finished_leds(led_max);
|
||||||
|
}
|
||||||
|
|
||||||
|
static HSV solid_reactive_wide_math(HSV hsv, int16_t dx, int16_t dy, uint8_t dist, uint16_t tick) {
|
||||||
|
uint16_t effect = tick + dist * 5;
|
||||||
|
if (effect > 255) effect = 255;
|
||||||
|
# ifdef RGB_MATRIX_SOLID_REACTIVE_GRADIENT_MODE
|
||||||
|
hsv.h = scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed, 8) >> 4);
|
||||||
|
# endif
|
||||||
|
hsv.v = qadd8(hsv.v, 255 - effect);
|
||||||
|
return hsv;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool per_key_rgb_reactive_multi_wide(effect_params_t *params) {
|
||||||
|
return per_key_rgb_effect_runner_reactive_splash(0, params, &solid_reactive_wide_math);
|
||||||
|
}
|
||||||
|
|
||||||
|
static HSV SPLASH_math(HSV hsv, int16_t dx, int16_t dy, uint8_t dist, uint16_t tick) {
|
||||||
|
uint16_t effect = tick - dist;
|
||||||
|
if (effect > 255) effect = 255;
|
||||||
|
hsv.h += effect;
|
||||||
|
hsv.v = qadd8(hsv.v, 255 - effect);
|
||||||
|
return hsv;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool per_key_rgb_reactive_splash(effect_params_t *params) {
|
||||||
|
return per_key_rgb_effect_runner_reactive_splash(qsub8(g_last_hit_tracker.count, 1), params, &SPLASH_math);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool per_key_rgb(effect_params_t *params) {
|
||||||
|
switch (per_key_rgb_type) {
|
||||||
|
case PER_KEY_RGB_BREATHING:
|
||||||
|
return per_key_rgb_breahting(params);
|
||||||
|
|
||||||
|
case PER_KEY_RGB_REATIVE_SIMPLE:
|
||||||
|
return per_key_rgb_reactive_simple(params);
|
||||||
|
|
||||||
|
case PER_KEY_RGB_REATIVE_MULTI_WIDE:
|
||||||
|
return per_key_rgb_reactive_multi_wide(params);
|
||||||
|
|
||||||
|
case PER_KEY_RGB_REATIVE_SPLASH:
|
||||||
|
return per_key_rgb_reactive_splash(params);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return per_key_rgb_solid(params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
185
keyboards/keychron/common/rgb/retail_demo.c
Normal file
185
keyboards/keychron/common/rgb/retail_demo.c
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 <string.h>
|
||||||
|
#include "eeconfig_kb.h"
|
||||||
|
#include "retail_demo.h"
|
||||||
|
#include "eeconfig.h"
|
||||||
|
#include "matrix.h"
|
||||||
|
#include "quantum.h"
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
# include "transport.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(RETAIL_DEMO_ENABLE) && defined(KEYCHRON_RGB_ENABLE) && defined(EECONFIG_SIZE_CUSTOM_RGB)
|
||||||
|
|
||||||
|
# ifndef RETAIL_DEMO_KEY_1
|
||||||
|
# ifdef RGB_MATRIX_ENABLE
|
||||||
|
# define RETAIL_DEMO_KEY_1 RGB_HUI
|
||||||
|
# else
|
||||||
|
# define RETAIL_DEMO_KEY_1 KC_D
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# ifndef RETAIL_DEMO_KEY_2
|
||||||
|
# ifdef RGB_MATRIX_ENABLE
|
||||||
|
# define RETAIL_DEMO_KEY_2 RGB_HUD
|
||||||
|
# else
|
||||||
|
# define RETAIL_DEMO_KEY_2 KC_E
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# ifndef EFFECT_DURATION
|
||||||
|
# define EFFECT_DURATION 10000
|
||||||
|
# endif
|
||||||
|
|
||||||
|
enum {
|
||||||
|
KEY_PRESS_FN = 0x01 << 0,
|
||||||
|
KEY_PRESS_D = 0x01 << 1,
|
||||||
|
KEY_PRESS_E = 0x01 << 2,
|
||||||
|
KEY_PRESS_RETAIL_DEMO = KEY_PRESS_FN | KEY_PRESS_D | KEY_PRESS_E,
|
||||||
|
};
|
||||||
|
|
||||||
|
uint8_t retail_demo_enable = 0;
|
||||||
|
static uint8_t retail_demo_combo = 0;
|
||||||
|
static uint32_t retail_demo_timer = 0;
|
||||||
|
|
||||||
|
extern void rgb_save_retail_demo(void);
|
||||||
|
|
||||||
|
bool process_record_retail_demo(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
switch (keycode) {
|
||||||
|
case MO(0)... MO(15):
|
||||||
|
if (record->event.pressed)
|
||||||
|
retail_demo_combo |= KEY_PRESS_FN;
|
||||||
|
else
|
||||||
|
retail_demo_combo &= ~KEY_PRESS_FN;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RETAIL_DEMO_KEY_1:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
retail_demo_combo |= KEY_PRESS_D;
|
||||||
|
if (retail_demo_combo == KEY_PRESS_RETAIL_DEMO) retail_demo_timer = timer_read32();
|
||||||
|
} else {
|
||||||
|
retail_demo_combo &= ~KEY_PRESS_D;
|
||||||
|
retail_demo_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RETAIL_DEMO_KEY_2:
|
||||||
|
if (record->event.pressed) {
|
||||||
|
retail_demo_combo |= KEY_PRESS_E;
|
||||||
|
if (retail_demo_combo == KEY_PRESS_RETAIL_DEMO) retail_demo_timer = timer_read32();
|
||||||
|
} else {
|
||||||
|
retail_demo_combo &= ~KEY_PRESS_E;
|
||||||
|
retail_demo_timer = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (retail_demo_enable && keycode >= RGB_TOG && keycode <= RGB_SPD) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void retail_demo_start(void) {
|
||||||
|
extern bool mixed_rgb_set_regions(uint8_t * data);
|
||||||
|
extern bool mixed_rgb_set_effect_list(uint8_t * data);
|
||||||
|
|
||||||
|
uint8_t index = 0;
|
||||||
|
uint8_t this_count = 28;
|
||||||
|
uint8_t data[31] = {0};
|
||||||
|
|
||||||
|
// Set all LED to region 0
|
||||||
|
while (index < RGB_MATRIX_LED_COUNT - 1) {
|
||||||
|
memset(data, 0, 31);
|
||||||
|
|
||||||
|
if ((index + this_count) >= RGB_MATRIX_LED_COUNT)
|
||||||
|
this_count = RGB_MATRIX_LED_COUNT - 1 - index;
|
||||||
|
else
|
||||||
|
this_count = 28;
|
||||||
|
|
||||||
|
data[0] = index;
|
||||||
|
data[1] = this_count;
|
||||||
|
mixed_rgb_set_regions(data);
|
||||||
|
|
||||||
|
index += this_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t effect_list[5] = {4, 7, 8, 11, 14};
|
||||||
|
// Set effect list
|
||||||
|
for (uint8_t i = 0; i < 5; i++) {
|
||||||
|
data[0] = 0; // regsion
|
||||||
|
data[1] = i; // start
|
||||||
|
data[2] = 1; // count
|
||||||
|
data[3] = effect_list[i]; // effect
|
||||||
|
data[4] = 0; // hue
|
||||||
|
data[5] = 255; // sat
|
||||||
|
data[6] = 127; // speed;
|
||||||
|
data[7] = EFFECT_DURATION & 0xFF;
|
||||||
|
data[8] = (EFFECT_DURATION >> 8) & 0xFF;
|
||||||
|
data[9] = (EFFECT_DURATION >> 16) & 0xFF;
|
||||||
|
data[10] = (EFFECT_DURATION >> 24) & 0xFF;
|
||||||
|
|
||||||
|
mixed_rgb_set_effect_list(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
HSV hsv = rgb_matrix_get_hsv();
|
||||||
|
hsv.v = hsv.s = UINT8_MAX;
|
||||||
|
rgb_matrix_sethsv_noeeprom(hsv.h, hsv.s, hsv.v);
|
||||||
|
rgb_matrix_set_speed_noeeprom(RGB_MATRIX_DEFAULT_SPD);
|
||||||
|
rgb_matrix_mode_noeeprom(RGB_MATRIX_CUSTOM_MIXED_RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
void retail_demo_stop(void) {
|
||||||
|
retail_demo_enable = false;
|
||||||
|
rgb_save_retail_demo();
|
||||||
|
eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void retail_demo_timer_check(void) {
|
||||||
|
if (timer_elapsed32(retail_demo_timer) > 5000) {
|
||||||
|
retail_demo_timer = 0;
|
||||||
|
|
||||||
|
if (retail_demo_combo == KEY_PRESS_RETAIL_DEMO) {
|
||||||
|
retail_demo_combo = 0;
|
||||||
|
retail_demo_enable = !retail_demo_enable;
|
||||||
|
|
||||||
|
if (retail_demo_enable) {
|
||||||
|
# ifdef LK_WIRELESS_ENABLE
|
||||||
|
// Retail demo is allowed only in wireless mode
|
||||||
|
if (get_transport() != TRANSPORT_USB) {
|
||||||
|
retail_demo_enable = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
} else {
|
||||||
|
eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config));
|
||||||
|
}
|
||||||
|
rgb_save_retail_demo();
|
||||||
|
|
||||||
|
if (!retail_demo_enable) {
|
||||||
|
extern void eeconfig_init_custom_rgb(void);
|
||||||
|
eeconfig_init_custom_rgb();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void retail_demo_task(void) {
|
||||||
|
if (retail_demo_timer) retail_demo_timer_check();
|
||||||
|
if (retail_demo_enable && rgb_matrix_get_mode() != RGB_MATRIX_CUSTOM_MIXED_RGB) retail_demo_start();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
25
keyboards/keychron/common/rgb/retail_demo.h
Normal file
25
keyboards/keychron/common/rgb/retail_demo.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
#include "stdint.h"
|
||||||
|
#include "action.h"
|
||||||
|
|
||||||
|
void retail_demo_start(void);
|
||||||
|
void retail_demo_stop(void);
|
||||||
|
|
||||||
|
bool process_record_retail_demo(uint16_t keycode, keyrecord_t * record);
|
||||||
|
void retail_demo_task(void);
|
||||||
14
keyboards/keychron/common/rgb/rgb.mk
Normal file
14
keyboards/keychron/common/rgb/rgb.mk
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
OPT_DEFS += -DKEYCHRON_RGB_ENABLE -DRETAIL_DEMO_ENABLE
|
||||||
|
|
||||||
|
RGB_MATRIX_CUSTOM_KB = yes
|
||||||
|
RGB_MATRIX_DIR = common/rgb
|
||||||
|
|
||||||
|
SRC += \
|
||||||
|
$(RGB_MATRIX_DIR)/keychron_rgb.c \
|
||||||
|
$(RGB_MATRIX_DIR)/per_key_rgb.c \
|
||||||
|
$(RGB_MATRIX_DIR)/mixed_rgb.c \
|
||||||
|
$(RGB_MATRIX_DIR)/retail_demo.c
|
||||||
|
|
||||||
|
VPATH += $(TOP_DIR)/keyboards/keychron/$(RGB_MATRIX_DIR)
|
||||||
|
|
||||||
|
|
||||||
39
keyboards/keychron/common/rgb/rgb_matrix_kb.inc
Normal file
39
keyboards/keychron/common/rgb/rgb_matrix_kb.inc
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 "rgb_matrix_kb_config.h"
|
||||||
|
|
||||||
|
#if defined(KEYCHRON_RGB_ENABLE) && defined(EECONFIG_SIZE_CUSTOM_RGB)
|
||||||
|
//extern bool MIXED_RGB(effect_params_t *params);
|
||||||
|
|
||||||
|
RGB_MATRIX_EFFECT(PER_KEY_RGB)
|
||||||
|
RGB_MATRIX_EFFECT(MIXED_RGB)
|
||||||
|
|
||||||
|
# ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
|
||||||
|
|
||||||
|
bool PER_KEY_RGB(effect_params_t *params) {
|
||||||
|
extern bool per_key_rgb(effect_params_t *params);
|
||||||
|
return per_key_rgb(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MIXED_RGB(effect_params_t *params) {
|
||||||
|
extern bool mixed_rgb(effect_params_t *params);
|
||||||
|
return mixed_rgb(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
26
keyboards/keychron/common/rgb/rgb_matrix_kb_config.h
Normal file
26
keyboards/keychron/common/rgb/rgb_matrix_kb_config.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#ifndef EFFECT_LAYERS
|
||||||
|
#define EFFECT_LAYERS 2
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EFFECTS_PER_LAYER
|
||||||
|
#define EFFECTS_PER_LAYER 5
|
||||||
|
#endif
|
||||||
26
keyboards/keychron/common/snap_click/eeconfig_snap_click.h
Normal file
26
keyboards/keychron/common/snap_click/eeconfig_snap_click.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
#ifndef SNAP_CLICK_COUNT
|
||||||
|
# define SNAP_CLICK_COUNT 20
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define SIZE_OF_SNAP_CLICK_CONFIG_T 3
|
||||||
|
|
||||||
|
#define EECONFIG_SIZE_SNAP_CLICK (SNAP_CLICK_COUNT * SIZE_OF_SNAP_CLICK_CONFIG_T)
|
||||||
|
|
||||||
201
keyboards/keychron/common/snap_click/snap_click.c
Normal file
201
keyboards/keychron/common/snap_click/snap_click.c
Normal file
@ -0,0 +1,201 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 <string.h>
|
||||||
|
#include "eeconfig_kb.h"
|
||||||
|
#include "snap_click.h"
|
||||||
|
#include "raw_hid.h"
|
||||||
|
#include "eeconfig.h"
|
||||||
|
#include "matrix.h"
|
||||||
|
#include "quantum.h"
|
||||||
|
#include "keychron_raw_hid.h"
|
||||||
|
|
||||||
|
#if defined(SNAP_CLICK_ENABLE) &&defined(EECONFIG_SIZE_SNAP_CLICK)
|
||||||
|
|
||||||
|
enum {
|
||||||
|
SNAP_CLICK_TYPE_NONE = 0,
|
||||||
|
SNAP_CLICK_TYPE_REGULAR,
|
||||||
|
SNAP_CLICK_TYPE_LAST_INPUT,
|
||||||
|
SNAP_CLICK_TYPE_FIRST_KEY,
|
||||||
|
SNAP_CLICK_TYPE_SECOND_KEY,
|
||||||
|
SNAP_CLICK_TYPE_NEUTRAL,
|
||||||
|
SNAP_CLICK_TYPE_MAX,
|
||||||
|
};
|
||||||
|
|
||||||
|
#define SC_MASK_BOTH_KEYS_PRESSED 3
|
||||||
|
|
||||||
|
snap_click_config_t snap_click_pair[SNAP_CLICK_COUNT];
|
||||||
|
snap_click_state_t snap_click_state[SNAP_CLICK_COUNT];
|
||||||
|
|
||||||
|
void snap_click_config_reset(void) {
|
||||||
|
memset(snap_click_pair, 0, sizeof(snap_click_pair));
|
||||||
|
eeprom_update_block(snap_click_pair, (uint8_t *)(EECONFIG_BASE_SNAP_CLICK), sizeof(snap_click_pair));
|
||||||
|
}
|
||||||
|
|
||||||
|
void snap_click_init(void) {
|
||||||
|
eeprom_read_block(snap_click_pair, (uint8_t *)(EECONFIG_BASE_SNAP_CLICK), sizeof(snap_click_pair));
|
||||||
|
memset(snap_click_state, 0, sizeof(snap_click_state));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool process_record_snap_click(uint16_t keycode, keyrecord_t * record)
|
||||||
|
{
|
||||||
|
for (uint8_t i=0; i<SNAP_CLICK_COUNT; i++)
|
||||||
|
{
|
||||||
|
snap_click_config_t *p = &snap_click_pair[i];
|
||||||
|
|
||||||
|
if (p->type && (keycode == p->key[0] || keycode == p->key[1]))
|
||||||
|
{
|
||||||
|
snap_click_state_t *pState = &snap_click_state[i];
|
||||||
|
uint8_t index = keycode == p->key[1]; // 0 or 1 of key pair
|
||||||
|
|
||||||
|
if (record->event.pressed) {
|
||||||
|
uint8_t state = 0x01 << index;
|
||||||
|
|
||||||
|
if (pState->state == 0) {
|
||||||
|
// Single key down
|
||||||
|
pState->state_keys = pState->last_single_key = state;
|
||||||
|
} else if ((state & pState->state_keys) == 0) { // TODO: do we need checking?
|
||||||
|
// Both keys are pressed
|
||||||
|
pState->state_keys = SC_MASK_BOTH_KEYS_PRESSED;
|
||||||
|
switch (p->type) {
|
||||||
|
case SNAP_CLICK_TYPE_REGULAR:
|
||||||
|
case SNAP_CLICK_TYPE_LAST_INPUT:
|
||||||
|
unregister_code(p->key[1-index]);
|
||||||
|
register_code(p->key[index]);
|
||||||
|
break;
|
||||||
|
case SNAP_CLICK_TYPE_FIRST_KEY:
|
||||||
|
unregister_code(p->key[1]);
|
||||||
|
register_code(p->key[0]);
|
||||||
|
break;
|
||||||
|
case SNAP_CLICK_TYPE_SECOND_KEY:
|
||||||
|
unregister_code(p->key[0]);
|
||||||
|
register_code(p->key[1]);
|
||||||
|
break;
|
||||||
|
case SNAP_CLICK_TYPE_NEUTRAL:
|
||||||
|
unregister_code(p->key[1-index]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (pState->state_keys == SC_MASK_BOTH_KEYS_PRESSED) {
|
||||||
|
// Snap click active
|
||||||
|
uint8_t state = 0x01 << (1-index);
|
||||||
|
pState->state_keys = pState->last_single_key = state;
|
||||||
|
|
||||||
|
switch (p->type) {
|
||||||
|
case SNAP_CLICK_TYPE_REGULAR:
|
||||||
|
unregister_code(p->key[index]);
|
||||||
|
break;
|
||||||
|
case SNAP_CLICK_TYPE_LAST_INPUT:
|
||||||
|
case SNAP_CLICK_TYPE_FIRST_KEY:
|
||||||
|
case SNAP_CLICK_TYPE_SECOND_KEY:
|
||||||
|
if (is_key_pressed(p->key[index])) {
|
||||||
|
unregister_code(p->key[index]);
|
||||||
|
}
|
||||||
|
if (!is_key_pressed(p->key[1-index])) {
|
||||||
|
register_code(p->key[1-index]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SNAP_CLICK_TYPE_NEUTRAL:
|
||||||
|
register_code(p->key[1-index]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
pState->state = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool snap_click_get_info(uint8_t *data) {
|
||||||
|
data[1] = SNAP_CLICK_COUNT;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool snap_click_get(uint8_t *data) {
|
||||||
|
uint8_t start = data[0];
|
||||||
|
uint8_t count = data[1];
|
||||||
|
|
||||||
|
if (count > 9 || start + count > SNAP_CLICK_COUNT) return false;
|
||||||
|
memcpy(&data[1], &snap_click_pair[start], count * sizeof(snap_click_config_t));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool snap_click_set(uint8_t *data) {
|
||||||
|
uint8_t start = data[0];
|
||||||
|
uint8_t count = data[1];
|
||||||
|
|
||||||
|
if (count > 9 || start + count > SNAP_CLICK_COUNT) return false;
|
||||||
|
for (uint8_t i=0; i<count; i++) {
|
||||||
|
uint8_t offset = 2+sizeof(snap_click_config_t)*i;
|
||||||
|
uint8_t type = data[offset];
|
||||||
|
uint8_t keycode1 = data[offset+1];
|
||||||
|
uint8_t keycode2 = data[offset+2];
|
||||||
|
|
||||||
|
if (type >= SNAP_CLICK_TYPE_MAX)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (type != 0 && (keycode1 == 0 || keycode2 == 0))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memcpy(&snap_click_pair[start], &data[2], count * sizeof(snap_click_config_t));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool snap_click_save(uint8_t *data) {
|
||||||
|
eeprom_update_block(snap_click_pair, (uint8_t *)(EECONFIG_BASE_SNAP_CLICK), sizeof(snap_click_pair));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void snap_click_rx(uint8_t *data, uint8_t length) {
|
||||||
|
uint8_t cmd = data[1];
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
switch (cmd) {
|
||||||
|
case SNAP_CLICK_GET_INFO:
|
||||||
|
success = snap_click_get_info(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SNAP_CLICK_GET:
|
||||||
|
success = snap_click_get(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SNAP_CLICK_SET:
|
||||||
|
success = snap_click_set(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SNAP_CLICK_SAVE:
|
||||||
|
success = snap_click_save(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
data[0] = 0xFF;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
data[2] = success ? 0 : 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
43
keyboards/keychron/common/snap_click/snap_click.h
Normal file
43
keyboards/keychron/common/snap_click/snap_click.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
typedef struct __attribute__((__packed__)) {
|
||||||
|
uint8_t type;
|
||||||
|
uint8_t key[2];
|
||||||
|
} snap_click_config_t;
|
||||||
|
// size = 3 bytes
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
uint8_t state;
|
||||||
|
struct {
|
||||||
|
uint8_t state_key_1:1;
|
||||||
|
uint8_t state_key_2:1;
|
||||||
|
uint8_t last_single_key_1:1;
|
||||||
|
uint8_t last_single_key_2:1;
|
||||||
|
uint8_t reserved:4;
|
||||||
|
};
|
||||||
|
struct {
|
||||||
|
uint8_t state_keys:2;
|
||||||
|
uint8_t last_single_key:2;
|
||||||
|
uint8_t reserved2:4;
|
||||||
|
};
|
||||||
|
} snap_click_state_t;
|
||||||
|
|
||||||
|
void snap_click_config_reset(void);
|
||||||
|
void snap_click_rx(uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
7
keyboards/keychron/common/snap_click/snap_click.mk
Normal file
7
keyboards/keychron/common/snap_click/snap_click.mk
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
SNAP_CLICK_DIR = common/snap_click
|
||||||
|
SRC += \
|
||||||
|
$(SNAP_CLICK_DIR)/snap_click.c \
|
||||||
|
|
||||||
|
VPATH += $(TOP_DIR)/keyboards/keychron/$(SNAP_CLICK_DIR)
|
||||||
|
|
||||||
|
OPT_DEFS += -DSNAP_CLICK_ENABLE
|
||||||
163
keyboards/keychron/common/wireless/bat_level_animation.c
Normal file
163
keyboards/keychron/common/wireless/bat_level_animation.c
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
|
||||||
|
/* Copyright 2023~2025 @ Keychron (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 "wireless.h"
|
||||||
|
#include "indicator.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#if defined(PROTOCOL_CHIBIOS)
|
||||||
|
# include <usb_main.h>
|
||||||
|
#elif if defined(PROTOCOL_LUFA)
|
||||||
|
# include "lufa.h"
|
||||||
|
#endif
|
||||||
|
#include "eeprom.h"
|
||||||
|
|
||||||
|
#if (defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)) && defined(BAT_LEVEL_LED_LIST)
|
||||||
|
|
||||||
|
#ifndef BAT_LEVEL_GROWING_INTERVAL
|
||||||
|
# define BAT_LEVEL_GROWING_INTERVAL 150
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BAT_LEVEL_ON_INTERVAL
|
||||||
|
# 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,
|
||||||
|
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 = 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 <= LED_MATRIX_LED_COUNT; 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 <= RGB_MATRIX_LED_COUNT; 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;
|
||||||
|
indicator_eeconfig_reload();
|
||||||
|
if (indicator_config.value == 0 && !LED_DRIVER_IS_ENABLED()) {
|
||||||
|
indicator_disable();
|
||||||
|
}
|
||||||
|
lpm_timer_reset();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
bat_lvl_ani_timer_buffer = 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
23
keyboards/keychron/common/wireless/bat_level_animation.h
Normal file
23
keyboards/keychron/common/wireless/bat_level_animation.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* Copyright 2023~2025 @ 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);
|
||||||
229
keyboards/keychron/common/wireless/battery.c
Normal file
229
keyboards/keychron/common/wireless/battery.c
Normal file
@ -0,0 +1,229 @@
|
|||||||
|
/* Copyright 2022~2023 @ 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 "wireless.h"
|
||||||
|
#include "battery.h"
|
||||||
|
#include "transport.h"
|
||||||
|
#include "lkbt51.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#include "indicator.h"
|
||||||
|
#include "rtc_timer.h"
|
||||||
|
#include "analog.h"
|
||||||
|
|
||||||
|
#define BATTERY_EMPTY_COUNT 10
|
||||||
|
#define CRITICAL_LOW_COUNT 20
|
||||||
|
|
||||||
|
/* Battery voltage resistive voltage divider setting of MCU */
|
||||||
|
#ifndef RVD_R1
|
||||||
|
# define RVD_R1 10 // Upper side resitor value (uint: KΩ)
|
||||||
|
#endif
|
||||||
|
#ifndef RVD_R2
|
||||||
|
# define RVD_R2 10 // Lower side resitor value (uint: KΩ)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Battery voltage resistive voltage divider setting of Bluetooth */
|
||||||
|
#ifndef LKBT51_RVD_R1
|
||||||
|
# define LKBT51_RVD_R1 560
|
||||||
|
#endif
|
||||||
|
#ifndef LKBT51_RVD_R2
|
||||||
|
# define LKBT51_RVD_R2 499
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef VOLTAGE_TRIM_LED_MATRIX
|
||||||
|
# define VOLTAGE_TRIM_LED_MATRIX 30
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef VOLTAGE_TRIM_RGB_MATRIX
|
||||||
|
# define VOLTAGE_TRIM_RGB_MATRIX 60
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
extern uint8_t g_pwm_buffer[DRIVER_COUNT][192];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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;
|
||||||
|
static uint8_t power_on_sample = 0;
|
||||||
|
|
||||||
|
void battery_init(void) {
|
||||||
|
bat_state = BAT_NOT_CHARGING;
|
||||||
|
#if defined(BAT_CHARGING_PIN)
|
||||||
|
# if (BAT_CHARGING_LEVEL == 0)
|
||||||
|
palSetLineMode(BAT_CHARGING_PIN, PAL_MODE_INPUT_PULLUP);
|
||||||
|
# else
|
||||||
|
palSetLineMode(BAT_CHARGING_PIN, PAL_MODE_INPUT_PULLDOWN);
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef BAT_ADC_ENABLE_PIN
|
||||||
|
palSetLineMode(BAT_ADC_ENABLE_PIN, PAL_MODE_OUTPUT_PUSHPULL);
|
||||||
|
writePin(BAT_ADC_ENABLE_PIN, 1);
|
||||||
|
#endif
|
||||||
|
#ifdef BAT_ADC_PIN
|
||||||
|
palSetLineMode(BAT_ADC_PIN, PAL_MODE_INPUT_ANALOG);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void battery_stop(void) {
|
||||||
|
#if (HAL_USE_ADC)
|
||||||
|
# ifdef BAT_ADC_ENABLE_PIN
|
||||||
|
writePin(BAT_ADC_ENABLE_PIN, 0);
|
||||||
|
# endif
|
||||||
|
# ifdef BAT_ADC_PIN
|
||||||
|
palSetLineMode(BAT_ADC_PIN, PAL_MODE_INPUT_ANALOG);
|
||||||
|
analog_stop(BAT_ADC_PIN);
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((weak)) void battery_measure(void) {
|
||||||
|
lkbt51_read_state_reg(0x05, 0x02);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Calculate the voltage */
|
||||||
|
__attribute__((weak)) void battery_calculate_voltage(bool vol_src_bt, uint16_t value) {
|
||||||
|
uint16_t voltage;
|
||||||
|
|
||||||
|
if (vol_src_bt)
|
||||||
|
voltage = ((uint32_t)value) * (LKBT51_RVD_R1 + LKBT51_RVD_R2) / LKBT51_RVD_R2;
|
||||||
|
else
|
||||||
|
voltage = (uint32_t)value * 3300 / 1024 * (RVD_R1 + RVD_R2) / RVD_R2;
|
||||||
|
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
if (led_matrix_is_enabled()) {
|
||||||
|
uint32_t totalBuf = 0;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < DRIVER_COUNT; i++)
|
||||||
|
for (uint8_t j = 0; j < 192; j++)
|
||||||
|
totalBuf += g_pwm_buffer[i][j];
|
||||||
|
/* We assumpt it is linear relationship*/
|
||||||
|
voltage += (VOLTAGE_TRIM_LED_MATRIX * totalBuf / LED_MATRIX_LED_COUNT / 255);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
if (rgb_matrix_is_enabled()) {
|
||||||
|
uint32_t totalBuf = 0;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < DRIVER_COUNT; i++)
|
||||||
|
for (uint8_t j = 0; j < 192; j++)
|
||||||
|
totalBuf += g_pwm_buffer[i][j];
|
||||||
|
/* We assumpt it is linear relationship*/
|
||||||
|
uint32_t compensation = VOLTAGE_TRIM_RGB_MATRIX * totalBuf / RGB_MATRIX_LED_COUNT / 255 / 3;
|
||||||
|
|
||||||
|
voltage += compensation;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
battery_set_voltage(voltage);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
power_on_sample = VOLTAGE_POWER_ON_MEASURE_COUNT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void battery_check_critical_low(void) {
|
||||||
|
if (voltage < SHUTDOWN_VOLTAGE_VALUE) {
|
||||||
|
if (critical_low <= CRITICAL_LOW_COUNT) {
|
||||||
|
if (++critical_low > CRITICAL_LOW_COUNT) wireless_low_battery_shutdown();
|
||||||
|
}
|
||||||
|
} else if (critical_low <= CRITICAL_LOW_COUNT) {
|
||||||
|
critical_low = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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_WIRELESS) && (wireless_get_state() == WT_CONNECTED || battery_power_on_sample())) {
|
||||||
|
#if defined(BAT_CHARGING_PIN)
|
||||||
|
if (usb_power_connected() && t > VOLTAGE_MEASURE_INTERVAL) {
|
||||||
|
if (readPin(BAT_CHARGING_PIN) == BAT_CHARGING_LEVEL)
|
||||||
|
lkbt51_update_bat_state(BAT_CHARGING);
|
||||||
|
else
|
||||||
|
lkbt51_update_bat_state(BAT_FULL_CHARGED);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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 = rtc_timer_read_ms();
|
||||||
|
if (bat_monitor_timer_buffer > RTC_MAX_TIME) {
|
||||||
|
bat_monitor_timer_buffer = 0;
|
||||||
|
rtc_timer_clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
battery_measure();
|
||||||
|
if (power_on_sample < VOLTAGE_POWER_ON_MEASURE_COUNT) power_on_sample++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((bat_empty || critical_low) && usb_power_connected()) {
|
||||||
|
bat_empty = false;
|
||||||
|
critical_low = false;
|
||||||
|
indicator_battery_low_enable(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
61
keyboards/keychron/common/wireless/battery.h
Normal file
61
keyboards/keychron/common/wireless/battery.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/* Copyright 2022~2023 @ 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_FULL_CHARGED,
|
||||||
|
};
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
#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_stop(void);
|
||||||
|
|
||||||
|
void battery_measure(void);
|
||||||
|
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);
|
||||||
|
bool battery_is_empty(void);
|
||||||
|
bool battery_is_critical_low(void);
|
||||||
|
bool battery_power_on_sample(void);
|
||||||
|
|
||||||
|
void battery_task(void);
|
||||||
20
keyboards/keychron/common/wireless/eeconfig_wireless.h
Normal file
20
keyboards/keychron/common/wireless/eeconfig_wireless.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
#define EECONFIG_SIZE_WIRELESS_CONFIG 4 //sizeof(backlit_disable_time) + sizeof (connected_idle_time)
|
||||||
|
|
||||||
773
keyboards/keychron/common/wireless/indicator.c
Normal file
773
keyboards/keychron/common/wireless/indicator.c
Normal file
@ -0,0 +1,773 @@
|
|||||||
|
/* Copyright 2023~2025 @ 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 "indicator.h"
|
||||||
|
#include "transport.h"
|
||||||
|
#include "battery.h"
|
||||||
|
#include "eeconfig.h"
|
||||||
|
#include "wireless_config.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "rtc_timer.h"
|
||||||
|
#include "keychron_common.h"
|
||||||
|
#include "usb_main.h"
|
||||||
|
#ifdef FACTORY_TEST_ENABLE
|
||||||
|
# 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
|
||||||
|
# include "led_matrix.h"
|
||||||
|
# endif
|
||||||
|
# ifdef RGB_MATRIX_ENABLE
|
||||||
|
# include "rgb_matrix.h"
|
||||||
|
# endif
|
||||||
|
# include "bat_level_animation.h"
|
||||||
|
# include "eeprom.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define HOST_INDEX_MASK 0x0F
|
||||||
|
#define HOST_P2P4G 0x10
|
||||||
|
#define LED_ON 0x80
|
||||||
|
|
||||||
|
// #define RGB_MATRIX_TIMEOUT_INFINITE 0xFFFFFFFF
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
# define DECIDE_TIME(t, duration) (duration == 0 ? LED_MATRIX_TIMEOUT_INFINITE : ((t > duration) ? t : duration))
|
||||||
|
#endif
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
# define DECIDE_TIME(t, duration) (duration == 0 ? RGB_MATRIX_TIMEOUT_INFINITE : ((t > duration) ? t : duration))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define INDICATOR_SET(s) memcpy(&indicator_config, &s##_config, sizeof(indicator_config_t));
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BACKLIGHT_OFF = 0x00,
|
||||||
|
BACKLIGHT_ON_CONNECTED = 0x01,
|
||||||
|
BACKLIGHT_ON_UNCONNECTED = 0x02,
|
||||||
|
};
|
||||||
|
|
||||||
|
extern uint16_t backlit_disable_time;
|
||||||
|
|
||||||
|
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 wt_state_t indicator_state;
|
||||||
|
static uint16_t next_period;
|
||||||
|
static indicator_type_t type;
|
||||||
|
static uint32_t indicator_timer_buffer = 0;
|
||||||
|
|
||||||
|
#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;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
backlight_state_t original_backlight_state;
|
||||||
|
|
||||||
|
# ifdef BT_HOST_LED_MATRIX_LIST
|
||||||
|
static uint8_t bt_host_led_matrix_list[BT_HOST_DEVICES_COUNT] = BT_HOST_LED_MATRIX_LIST;
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# ifdef P2P4G_HOST_LED_MATRIX_LIST
|
||||||
|
static uint8_t p2p4g_host_led_matrix_list[P2P4G_HOST_DEVICES_COUNT] = P2P4G_HOST_LED_MATRIX_LIST;
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef BT_HOST_LED_PIN_LIST
|
||||||
|
static pin_t bt_led_pin_list[BT_HOST_DEVICES_COUNT] = BT_HOST_LED_PIN_LIST;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef P24G_HOST_LED_PIN_LIST
|
||||||
|
static pin_t p24g_led_pin_list[P24G_HOST_DEVICES_COUNT] = P24G_HOST_LED_PIN_LIST;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef LED_MATRIX_ENABLE
|
||||||
|
# define LED_DRIVER led_matrix_driver
|
||||||
|
# define LED_INDICATORS_KB led_matrix_indicators_bt
|
||||||
|
# define LED_INDICATORS_USER led_matrix_indicators_user
|
||||||
|
# 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 SET_LED_P24G(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)); \
|
||||||
|
if (!led_matrix_eeconfig.mode) { \
|
||||||
|
eeconfig_update_led_matrix_default(); \
|
||||||
|
}
|
||||||
|
# define LED_DRIVER_ALLOW_SHUTDOWN led_matrix_driver_allow_shutdown
|
||||||
|
# define LED_DRIVER_SHUTDOWN led_matrix_driver_shutdown
|
||||||
|
# define LED_DRIVER_EXIT_SHUTDOWN led_matrix_driver_exit_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
|
||||||
|
# define LED_DRIVER_TIMEOUTED led_matrix_timeouted
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
# define LED_DRIVER rgb_matrix_driver
|
||||||
|
# define LED_INDICATORS_KB rgb_matrix_indicators_bt
|
||||||
|
# define LED_INDICATORS_USER rgb_matrix_indicators_user
|
||||||
|
# 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 SET_LED_P24G(idx) rgb_matrix_set_color(idx, 0, 255, 0)
|
||||||
|
# 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)); \
|
||||||
|
if (!rgb_matrix_config.mode) { \
|
||||||
|
eeconfig_update_rgb_matrix_default(); \
|
||||||
|
}
|
||||||
|
# define LED_DRIVER_ALLOW_SHUTDOWN rgb_matrix_driver_allow_shutdown
|
||||||
|
# define LED_DRIVER_SHUTDOWN rgb_matrix_driver_shutdown
|
||||||
|
# define LED_DRIVER_EXIT_SHUTDOWN rgb_matrix_driver_exit_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
|
||||||
|
# define LED_DRIVER_TIMEOUTED rgb_matrix_timeouted
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool LED_INDICATORS_KB(void);
|
||||||
|
|
||||||
|
void indicator_init(void) {
|
||||||
|
memset(&indicator_config, 0, sizeof(indicator_config));
|
||||||
|
|
||||||
|
#if defined(BT_HOST_LED_PIN_LIST)
|
||||||
|
for (uint8_t i = 0; i < BT_HOST_DEVICES_COUNT; i++) {
|
||||||
|
setPinOutput(bt_led_pin_list[i]);
|
||||||
|
writePin(bt_led_pin_list[i], !HOST_LED_PIN_ON_STATE);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef P24G_HOST_LED_PIN_LIST
|
||||||
|
for (uint8_t i = 0; i < P24G_HOST_DEVICES_COUNT; i++) {
|
||||||
|
setPinOutput(p24g_led_pin_list[i]);
|
||||||
|
writePin(p24g_led_pin_list[i], !HOST_LED_PIN_ON_STATE);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef COMMON_BT_LED_PIN
|
||||||
|
setPinOutput(COMMON_BT_LED_PIN);
|
||||||
|
writePin(COMMON_BT_LED_PIN, !COMMON_BT_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef COMMON_P24G_LED_PIN
|
||||||
|
setPinOutput(COMMON_P24G_LED_PIN);
|
||||||
|
writePin(COMMON_P24G_LED_PIN, !COMMON_BT_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef BAT_LOW_LED_PIN
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
void indicator_enable(void) {
|
||||||
|
if (!LED_DRIVER_IS_ENABLED()) {
|
||||||
|
LED_DRIVER_ENABLE_NOEEPROM();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void indicator_disable(void) {
|
||||||
|
LED_DRIVER_DISABLE_NOEEPROM();
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_reset_backlit_time(void) {
|
||||||
|
LED_DRIVER_DISABLE_TIME_RESET();
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_set_backlit_timeout(uint32_t time) {
|
||||||
|
LED_DRIVER_DISABLE_TIMEOUT_SET(time);
|
||||||
|
indicator_reset_backlit_time();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool indicator_is_enabled(void) {
|
||||||
|
return LED_DRIVER_IS_ENABLED();
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_eeconfig_reload(void) {
|
||||||
|
LED_DRIVER_EECONFIG_RELOAD();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool indicator_is_running(void) {
|
||||||
|
return
|
||||||
|
#if defined(BAT_LOW_LED_PIN) || defined(SPACE_KEY_LOW_BAT_IND)
|
||||||
|
bat_low_ind_state ||
|
||||||
|
#endif
|
||||||
|
!!indicator_config.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void indicator_timer_cb(void *arg) {
|
||||||
|
if (*(indicator_type_t *)arg != INDICATOR_LAST) type = *(indicator_type_t *)arg;
|
||||||
|
|
||||||
|
bool time_up = false;
|
||||||
|
switch (type) {
|
||||||
|
case INDICATOR_NONE:
|
||||||
|
break;
|
||||||
|
case INDICATOR_OFF:
|
||||||
|
next_period = 0;
|
||||||
|
time_up = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case INDICATOR_ON:
|
||||||
|
if (indicator_config.value) {
|
||||||
|
if (indicator_config.elapsed == 0) {
|
||||||
|
indicator_config.value |= LED_ON;
|
||||||
|
|
||||||
|
if (indicator_config.duration) {
|
||||||
|
indicator_config.elapsed += indicator_config.duration;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
time_up = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case INDICATOR_ON_OFF:
|
||||||
|
if (indicator_config.value) {
|
||||||
|
if (indicator_config.elapsed == 0) {
|
||||||
|
indicator_config.value |= LED_ON;
|
||||||
|
next_period = indicator_config.on_time;
|
||||||
|
} else {
|
||||||
|
indicator_config.value = indicator_config.value & 0x1F;
|
||||||
|
next_period = indicator_config.duration - indicator_config.on_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((indicator_config.duration == 0 || indicator_config.elapsed <= indicator_config.duration) && next_period != 0) {
|
||||||
|
indicator_config.elapsed += next_period;
|
||||||
|
} else {
|
||||||
|
time_up = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case INDICATOR_BLINK:
|
||||||
|
if (indicator_config.value) {
|
||||||
|
if (indicator_config.value & LED_ON) {
|
||||||
|
indicator_config.value = indicator_config.value & 0x1F;
|
||||||
|
next_period = indicator_config.off_time;
|
||||||
|
} else {
|
||||||
|
indicator_config.value |= LED_ON;
|
||||||
|
next_period = indicator_config.on_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((indicator_config.duration == 0 || indicator_config.elapsed <= indicator_config.duration) && next_period != 0) {
|
||||||
|
indicator_config.elapsed += next_period;
|
||||||
|
} else {
|
||||||
|
time_up = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
time_up = true;
|
||||||
|
|
||||||
|
next_period = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BT_HOST_LED_PIN_LIST) || defined(P24G_HOST_LED_PIN_LIST) || defined(COMMON_BT_LED_PIN) || defined(COMMON_P24G_LED_PIN)
|
||||||
|
if (indicator_config.value) {
|
||||||
|
uint8_t idx = (indicator_config.value & HOST_INDEX_MASK) - 1;
|
||||||
|
# if defined(BT_HOST_LED_PIN_LIST) || defined(P24G_HOST_LED_PIN_LIST)
|
||||||
|
pin_t *led_lin_list = NULL;
|
||||||
|
# endif
|
||||||
|
# if defined(COMMON_BT_LED_PIN) || defined(COMMON_P24G_LED_PIN)
|
||||||
|
pin_t led_pin = NO_PIN;
|
||||||
|
# endif
|
||||||
|
uint8_t led_count;
|
||||||
|
# if defined(P24G_HOST_LED_PIN_LIST) || defined(COMMON_P24G_LED_PIN)
|
||||||
|
if (indicator_config.value & HOST_P2P4G) {
|
||||||
|
if (idx < P24G_HOST_DEVICES_COUNT) {
|
||||||
|
# if defined(P24G_HOST_LED_PIN_LIST)
|
||||||
|
led_lin_list = p24g_led_pin_list;
|
||||||
|
# endif
|
||||||
|
# if defined(COMMON_P24G_LED_PIN)
|
||||||
|
led_pin = COMMON_P24G_LED_PIN;
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
led_count = P24G_HOST_DEVICES_COUNT;
|
||||||
|
} else
|
||||||
|
# endif
|
||||||
|
{
|
||||||
|
if (idx < BT_HOST_DEVICES_COUNT) {
|
||||||
|
# if defined(BT_HOST_LED_PIN_LIST)
|
||||||
|
led_lin_list = bt_led_pin_list;
|
||||||
|
# endif
|
||||||
|
# if defined(COMMON_BT_LED_PIN)
|
||||||
|
led_pin = COMMON_BT_LED_PIN;
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
led_count = BT_HOST_DEVICES_COUNT;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BT_HOST_LED_PIN_LIST) || defined(P24G_HOST_LED_PIN_LIST)
|
||||||
|
for (uint8_t i = 0; i < led_count; i++) {
|
||||||
|
if (i != idx) {
|
||||||
|
if (led_lin_list) writePin(led_lin_list[idx], !HOST_LED_PIN_ON_STATE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if ((indicator_config.value & LED_ON) && !time_up) {
|
||||||
|
if (led_lin_list) writePin(led_lin_list[idx], HOST_LED_PIN_ON_STATE);
|
||||||
|
# if defined(COMMON_BT_LED_PIN) || defined(COMMON_P24G_LED_PIN)
|
||||||
|
if (led_pin != NO_PIN) writePin(led_pin, COMMON_BT_LED_PIN_ON_STATE);
|
||||||
|
# endif
|
||||||
|
} else {
|
||||||
|
if (led_lin_list) writePin(led_lin_list[idx], !HOST_LED_PIN_ON_STATE);
|
||||||
|
# if defined(COMMON_BT_LED_PIN) || defined(COMMON_P24G_LED_PIN)
|
||||||
|
if (led_pin != NO_PIN) writePin(led_pin, !COMMON_BT_LED_PIN_ON_STATE);
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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 & 0x1F;
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
LED_INDICATORS_KB();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
indicator_config.value = 0;
|
||||||
|
lpm_timer_reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (indicator_config.value == 0) {
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
indicator_eeconfig_reload();
|
||||||
|
if (!LED_DRIVER_IS_ENABLED()) indicator_disable();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (host_index == 24) host_index = HOST_P2P4G | 0x01;
|
||||||
|
|
||||||
|
if (current_host != host_index && state != WT_DISCONNECTED) {
|
||||||
|
host_index_changed = true;
|
||||||
|
current_host = 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;
|
||||||
|
(void)pre_state;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
indicator_timer_buffer = timer_read32();
|
||||||
|
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
/* Turn on backlight mode for indicator */
|
||||||
|
indicator_enable();
|
||||||
|
indicator_reset_backlit_time();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
switch (state) {
|
||||||
|
case WT_DISCONNECTED:
|
||||||
|
#if defined(BT_HOST_LED_PIN_LIST)
|
||||||
|
if ((host_index & HOST_P2P4G) != HOST_P2P4G) writePin(bt_led_pin_list[(host_index & HOST_INDEX_MASK) - 1], !HOST_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
#if defined(P24G_HOST_LED_PIN_LIST)
|
||||||
|
if (host_index & HOST_P2P4G) writePin(p24g_led_pin_list[(host_index & HOST_INDEX_MASK) - 1], !HOST_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
#ifdef COMMON_BT_LED_PIN
|
||||||
|
writePin(COMMON_BT_LED_PIN, !COMMON_BT_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
#ifdef COMMON_P24G_LED_PIN
|
||||||
|
writePin(COMMON_P24G_LED_PIN, !COMMON_BT_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);
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
if (battery_is_critical_low()) {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WT_CONNECTED:
|
||||||
|
if (indicator_state != WT_CONNECTED) {
|
||||||
|
INDICATOR_SET(connected);
|
||||||
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index;
|
||||||
|
indicator_timer_cb((void *)&indicator_config.type);
|
||||||
|
}
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
indicator_set_backlit_timeout(DECIDE_TIME(backlit_disable_time * 1000, indicator_config.duration));
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WT_PARING:
|
||||||
|
INDICATOR_SET(pairing);
|
||||||
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : LED_ON | host_index;
|
||||||
|
indicator_timer_cb((void *)&indicator_config.type);
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WT_RECONNECTING:
|
||||||
|
INDICATOR_SET(reconnecting);
|
||||||
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : LED_ON | host_index;
|
||||||
|
indicator_timer_cb((void *)&indicator_config.type);
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WT_SUSPEND:
|
||||||
|
INDICATOR_SET(disconnected);
|
||||||
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index;
|
||||||
|
indicator_timer_cb((void *)&indicator_config.type);
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
# ifdef FACTORY_TEST_ENABLE
|
||||||
|
if (factory_reset_indicating())
|
||||||
|
indicator_set_backlit_timeout(3000);
|
||||||
|
else
|
||||||
|
# endif
|
||||||
|
{
|
||||||
|
indicator_set_backlit_timeout(1000);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(BT_HOST_LED_PIN_LIST)
|
||||||
|
for (uint8_t i = 0; i < BT_HOST_DEVICES_COUNT; i++) writePin(bt_led_pin_list[i], !HOST_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
#if defined(P24G_HOST_LED_PIN_LIST)
|
||||||
|
for (uint8_t i = 0; i < P24G_HOST_DEVICES_COUNT; i++) writePin(p24g_led_pin_list[i], !HOST_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
#ifdef COMMON_BT_LED_PIN
|
||||||
|
writePin(COMMON_BT_LED_PIN, !COMMON_BT_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
#ifdef COMMON_P24G_LED_PIN
|
||||||
|
writePin(COMMON_P24G_LED_PIN, !COMMON_BT_LED_PIN_ON_STATE);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
indicator_state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_stop(void) {
|
||||||
|
indicator_config.value = 0;
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
indicator_eeconfig_reload();
|
||||||
|
|
||||||
|
if (indicator_is_enabled()) {
|
||||||
|
indicator_enable();
|
||||||
|
} else {
|
||||||
|
indicator_disable();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_battery_low_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)
|
||||||
|
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() : 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(BAT_LOW_LED_PIN)
|
||||||
|
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
|
||||||
|
# endif
|
||||||
|
# if defined(SPACE_KEY_LOW_BAT_IND)
|
||||||
|
indicator_eeconfig_reload();
|
||||||
|
if (!LED_DRIVER_IS_ENABLED()) indicator_disable();
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_battery_low(void) {
|
||||||
|
#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(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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void indicator_task(void) {
|
||||||
|
#if 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
indicator_battery_low();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
__attribute__((weak)) void os_state_indicate(void) {
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# if defined(NUM_LOCK_INDEX)
|
||||||
|
if (host_keyboard_led_state().num_lock) {
|
||||||
|
# if defined(DIM_NUM_LOCK)
|
||||||
|
SET_LED_OFF(NUM_LOCK_INDEX);
|
||||||
|
# else
|
||||||
|
SET_LED_ON(NUM_LOCK_INDEX);
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LED_INDICATORS_KB(void) {
|
||||||
|
if (get_transport() & TRANSPORT_WIRELESS) {
|
||||||
|
/* Prevent backlight flash caused by key activities */
|
||||||
|
if (battery_is_critical_low()) {
|
||||||
|
SET_ALL_LED_OFF();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
uint8_t idx_list[] = LOW_BAT_IND_INDEX;
|
||||||
|
for (uint8_t i = 0; i < sizeof(idx_list); i++) {
|
||||||
|
if (bat_low_ind_state & LED_ON) {
|
||||||
|
SET_LED_LOW_BAT(idx_list[i]);
|
||||||
|
} else {
|
||||||
|
SET_LED_OFF(idx_list[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# if (defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)) && defined(BAT_LEVEL_LED_LIST)
|
||||||
|
if (bat_level_animiation_actived()) {
|
||||||
|
bat_level_animiation_indicate();
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
static uint8_t last_host_index = 0xFF;
|
||||||
|
|
||||||
|
if (indicator_config.value) {
|
||||||
|
uint8_t host_index = indicator_config.value & HOST_INDEX_MASK;
|
||||||
|
|
||||||
|
if (indicator_config.highlight) {
|
||||||
|
SET_ALL_LED_OFF();
|
||||||
|
} else if (last_host_index != host_index) {
|
||||||
|
if (indicator_config.value & HOST_P2P4G)
|
||||||
|
SET_LED_OFF(p2p4g_host_led_matrix_list[host_index - 1]);
|
||||||
|
else
|
||||||
|
SET_LED_OFF(bt_host_led_matrix_list[host_index - 1]);
|
||||||
|
last_host_index = host_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (indicator_config.value & LED_ON) {
|
||||||
|
# ifdef P2P4G_HOST_LED_MATRIX_LIST
|
||||||
|
if (indicator_config.value & HOST_P2P4G)
|
||||||
|
SET_LED_P24G(p2p4g_host_led_matrix_list[host_index - 1]);
|
||||||
|
else
|
||||||
|
# endif
|
||||||
|
SET_LED_BT(bt_host_led_matrix_list[host_index - 1]);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
# ifdef P2P4G_HOST_LED_MATRIX_LIST
|
||||||
|
if (indicator_config.value & HOST_P2P4G)
|
||||||
|
SET_LED_OFF(p2p4g_host_led_matrix_list[host_index - 1]);
|
||||||
|
else
|
||||||
|
# endif
|
||||||
|
SET_LED_OFF(bt_host_led_matrix_list[host_index - 1]);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
os_state_indicate();
|
||||||
|
|
||||||
|
} else
|
||||||
|
os_state_indicate();
|
||||||
|
|
||||||
|
if (!LED_INDICATORS_USER()) return true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool led_update_kb(led_t led_state) {
|
||||||
|
bool res = led_update_user(led_state);
|
||||||
|
if (res) {
|
||||||
|
led_update_ports(led_state);
|
||||||
|
|
||||||
|
if (!LED_DRIVER_IS_ENABLED() || (LED_DRIVER_IS_ENABLED() && LED_DRIVER_TIMEOUTED())) {
|
||||||
|
# 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();
|
||||||
|
# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
|
||||||
|
if (LED_DRIVER_ALLOW_SHUTDOWN()) LED_DRIVER_SHUTDOWN();
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LED_NONE_INDICATORS_KB(void) {
|
||||||
|
# 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
|
||||||
|
|
||||||
|
os_state_indicate();
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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;
|
||||||
|
# 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
|
||||||
114
keyboards/keychron/common/wireless/indicator.h
Normal file
114
keyboards/keychron/common/wireless/indicator.h
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/* Copyright 2023~2025 @ 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
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "wireless.h"
|
||||||
|
|
||||||
|
/* Indication of pairing */
|
||||||
|
#ifndef INDICATOR_CONFIG_PARING
|
||||||
|
# define INDICATOR_CONFIG_PARING {INDICATOR_BLINK, 1000, 1000, 0, true, 0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Indication on Connected */
|
||||||
|
#ifndef INDICATOR_CONFIG_CONNECTD
|
||||||
|
# define INDICATOR_CONFIG_CONNECTD {INDICATOR_ON_OFF, 2000, 250, 2000, true, 0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Reconnecting indication */
|
||||||
|
#ifndef INDICATOR_CONFIG_RECONNECTING
|
||||||
|
# define INDICATOR_CONFIG_RECONNECTING {INDICATOR_BLINK, 100, 100, 600, true, 0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Disconnected indication */
|
||||||
|
#ifndef INDICATOR_CONFIG_DISCONNECTED
|
||||||
|
# define INDICATOR_CONFIG_DISCONNECTED {INDICATOR_NONE, 100, 100, 600, false, 0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Uint: Second */
|
||||||
|
#ifndef DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT
|
||||||
|
# define DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT 40
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Uint: Second, the timer restarts on key activities. */
|
||||||
|
#ifndef CONNECTED_BACKLIGHT_DISABLE_TIMEOUT
|
||||||
|
# define CONNECTED_BACKLIGHT_DISABLE_TIMEOUT 600
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Uint: ms */
|
||||||
|
#ifndef LOW_BAT_LED_BLINK_PERIOD
|
||||||
|
# define LOW_BAT_LED_BLINK_PERIOD 1000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LOW_BAT_LED_BLINK_TIMES
|
||||||
|
# define LOW_BAT_LED_BLINK_TIMES 5
|
||||||
|
#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
|
||||||
|
|
||||||
|
#ifndef P24G_HOST_DEVICES_COUNT
|
||||||
|
# define P24G_HOST_DEVICES_COUNT 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
INDICATOR_NONE,
|
||||||
|
INDICATOR_OFF,
|
||||||
|
INDICATOR_ON,
|
||||||
|
INDICATOR_ON_OFF,
|
||||||
|
INDICATOR_BLINK,
|
||||||
|
INDICATOR_LAST,
|
||||||
|
} indicator_type_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
indicator_type_t type;
|
||||||
|
uint32_t on_time;
|
||||||
|
uint32_t off_time;
|
||||||
|
uint32_t duration;
|
||||||
|
bool highlight;
|
||||||
|
uint8_t value;
|
||||||
|
uint32_t elapsed;
|
||||||
|
} indicator_config_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t value;
|
||||||
|
bool saved;
|
||||||
|
} backlight_state_t;
|
||||||
|
|
||||||
|
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_reset_backlit_time(void);
|
||||||
|
bool indicator_hook_key(uint16_t keycode);
|
||||||
|
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);
|
||||||
|
void indicator_battery_low_enable(bool enable);
|
||||||
|
|
||||||
|
void indicator_task(void);
|
||||||
156
keyboards/keychron/common/wireless/keychron_wireless_common.c
Normal file
156
keyboards/keychron/common/wireless/keychron_wireless_common.c
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
/* Copyright 2022~2025 @ Keychron (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 QMK_KEYBOARD_H
|
||||||
|
#ifdef LK_WIRELESS_ENABLE
|
||||||
|
# include "lkbt51.h"
|
||||||
|
# include "wireless.h"
|
||||||
|
# include "indicator.h"
|
||||||
|
# include "transport.h"
|
||||||
|
# include "battery.h"
|
||||||
|
# include "bat_level_animation.h"
|
||||||
|
# include "lpm.h"
|
||||||
|
# include "keychron_wireless_common.h"
|
||||||
|
# include "keychron_task.h"
|
||||||
|
#endif
|
||||||
|
#include "keychron_common.h"
|
||||||
|
|
||||||
|
bool firstDisconnect = true;
|
||||||
|
|
||||||
|
static uint32_t pairing_key_timer;
|
||||||
|
static uint8_t host_idx = 0;
|
||||||
|
extern uint32_t connected_idle_time;
|
||||||
|
|
||||||
|
bool process_record_keychron_wireless(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
static uint8_t host_idx;
|
||||||
|
|
||||||
|
switch (keycode) {
|
||||||
|
case BT_HST1 ... BT_HST3:
|
||||||
|
if (get_transport() == TRANSPORT_BLUETOOTH) {
|
||||||
|
if (record->event.pressed) {
|
||||||
|
host_idx = keycode - BT_HST1 + 1;
|
||||||
|
|
||||||
|
pairing_key_timer = timer_read32();
|
||||||
|
wireless_connect_ex(host_idx, 0);
|
||||||
|
} else {
|
||||||
|
host_idx = 0;
|
||||||
|
pairing_key_timer = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case P2P4G:
|
||||||
|
if (get_transport() == TRANSPORT_P2P4) {
|
||||||
|
if (record->event.pressed) {
|
||||||
|
host_idx = P24G_INDEX;
|
||||||
|
|
||||||
|
pairing_key_timer = timer_read32();
|
||||||
|
} else {
|
||||||
|
host_idx = 0;
|
||||||
|
pairing_key_timer = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#if (defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)) && defined(BAT_LEVEL_LED_LIST)
|
||||||
|
case BAT_LVL:
|
||||||
|
if ((get_transport() & TRANSPORT_WIRELESS) && !usb_power_connected()) {
|
||||||
|
bat_level_animiation_start(battery_get_percentage());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_param_init(void) {
|
||||||
|
/* Set bluetooth device name */
|
||||||
|
lkbt51_set_local_name(PRODUCT);
|
||||||
|
wait_ms(3);
|
||||||
|
// clang-format off
|
||||||
|
/* Set bluetooth parameters */
|
||||||
|
module_param_t param = {.event_mode = 0x02,
|
||||||
|
.connected_idle_timeout = connected_idle_time,
|
||||||
|
.pairing_timeout = 180,
|
||||||
|
.pairing_mode = 0,
|
||||||
|
.reconnect_timeout = 5,
|
||||||
|
.report_rate = 90,
|
||||||
|
.vendor_id_source = 1,
|
||||||
|
.verndor_id = 0x3434, // Must be 0x3434
|
||||||
|
.product_id = PRODUCT_ID};
|
||||||
|
// clang-format on
|
||||||
|
lkbt51_set_param(¶m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_enter_reset_kb(uint8_t reason) {
|
||||||
|
lkbt51_param_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
lkbt51_param_init();
|
||||||
|
if (get_transport() == TRANSPORT_BLUETOOTH) wireless_connect();
|
||||||
|
firstDisconnect = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void keychron_wireless_common_task(void) {
|
||||||
|
if (pairing_key_timer) {
|
||||||
|
if (timer_elapsed32(pairing_key_timer) > 2000) {
|
||||||
|
pairing_key_timer = 0;
|
||||||
|
wireless_pairing_ex(host_idx, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_pre_task(void) {
|
||||||
|
static uint8_t mode = 0;
|
||||||
|
static uint32_t time = 0;
|
||||||
|
|
||||||
|
if (time == 0) {
|
||||||
|
if ((readPin(BT_MODE_SELECT_PIN) << 1 | readPin(P2P4_MODE_SELECT_PIN)) != mode) {
|
||||||
|
mode = readPin(BT_MODE_SELECT_PIN) << 1 | readPin(P2P4_MODE_SELECT_PIN);
|
||||||
|
time = timer_read32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((time && timer_elapsed32(time) > 100) || get_transport() == TRANSPORT_NONE) {
|
||||||
|
if ((readPin(BT_MODE_SELECT_PIN) << 1 | readPin(P2P4_MODE_SELECT_PIN)) == mode) {
|
||||||
|
time = 0;
|
||||||
|
|
||||||
|
switch (mode) {
|
||||||
|
case 0x01:
|
||||||
|
set_transport(TRANSPORT_BLUETOOTH);
|
||||||
|
break;
|
||||||
|
case 0x02:
|
||||||
|
set_transport(TRANSPORT_P2P4);
|
||||||
|
break;
|
||||||
|
case 0x03:
|
||||||
|
set_transport(TRANSPORT_USB);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mode = readPin(BT_MODE_SELECT_PIN) << 1 | readPin(P2P4_MODE_SELECT_PIN);
|
||||||
|
time = timer_read32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,26 @@
|
|||||||
|
/* Copyright 2023 @ Keychron (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 "stdint.h"
|
||||||
|
#ifdef VIA_ENABLE
|
||||||
|
# include "via.h"
|
||||||
|
#endif
|
||||||
|
#include "quantum_keycodes.h"
|
||||||
|
|
||||||
|
void lkbt51_param_init(void);
|
||||||
|
|
||||||
|
bool process_record_keychron_wireless(uint16_t keycode, keyrecord_t *record);
|
||||||
|
void keychron_wireless_common_task(void);
|
||||||
875
keyboards/keychron/common/wireless/lkbt51.c
Normal file
875
keyboards/keychron/common/wireless/lkbt51.c
Normal file
@ -0,0 +1,875 @@
|
|||||||
|
/* Copyright 2023 @ Keychron (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 "lkbt51.h"
|
||||||
|
#include "wireless.h"
|
||||||
|
#include "wireless_event_type.h"
|
||||||
|
#include "battery.h"
|
||||||
|
#include "raw_hid.h"
|
||||||
|
#include "report_buffer.h"
|
||||||
|
#include "factory_test.h"
|
||||||
|
|
||||||
|
extern void factory_test_send(uint8_t* payload, uint8_t length);
|
||||||
|
|
||||||
|
#ifndef RAW_EPSIZE
|
||||||
|
# define RAW_EPSIZE 32
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef SPI_SCK_PIN
|
||||||
|
# define SPI_SCK_PIN A5
|
||||||
|
#endif
|
||||||
|
#ifndef SPI_MISO_PIN
|
||||||
|
# define SPI_MISO_PIN A6
|
||||||
|
#endif
|
||||||
|
#ifndef SPI_MOSI_PIN
|
||||||
|
# define SPI_MOSI_PIN A7
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef SPI_CLK_PAL_MODE
|
||||||
|
# define SPI_CLK_PAL_MODE 5
|
||||||
|
#endif
|
||||||
|
#ifndef SPI_MISO_PAL_MODE
|
||||||
|
# define SPI_MISO_PAL_MODE 5
|
||||||
|
#endif
|
||||||
|
#ifndef SPI_MOSI_PAL_MODE
|
||||||
|
# define SPI_MOSI_PAL_MODE 5
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LKBT51_INT_INPUT_PIN
|
||||||
|
# error "LKBT51_INT_INPUT_PIN is not defined"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LKBT51_TX_RETRY_COUNT
|
||||||
|
# define LKBT51_TX_RETRY_COUNT 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
enum {
|
||||||
|
/* HID Report */
|
||||||
|
LKBT51_CMD_SEND_KB = 0x11,
|
||||||
|
LKBT51_CMD_SEND_KB_NKRO = 0x12,
|
||||||
|
LKBT51_CMD_SEND_CONSUMER = 0x13,
|
||||||
|
LKBT51_CMD_SEND_SYSTEM = 0x14,
|
||||||
|
LKBT51_CMD_SEND_FN = 0x15, // Not used currently
|
||||||
|
LKBT51_CMD_SEND_MOUSE = 0x16,
|
||||||
|
LKBT51_CMD_SEND_BOOT_KB = 0x17,
|
||||||
|
/* Bluetooth connections */
|
||||||
|
LKBT51_CMD_PAIRING = 0x21,
|
||||||
|
LKBT51_CMD_CONNECT = 0x22,
|
||||||
|
LKBT51_CMD_DISCONNECT = 0x23,
|
||||||
|
LKBT51_CMD_SWITCH_HOST = 0x24,
|
||||||
|
LKBT51_CMD_READ_STATE_REG = 0x25,
|
||||||
|
/* Battery */
|
||||||
|
LKBT51_CMD_BATTERY_MANAGE = 0x31,
|
||||||
|
LKBT51_CMD_UPDATE_BAT_LVL = 0x32,
|
||||||
|
LKBT51_CMD_UPDATE_BAT_STATE = 0x33,
|
||||||
|
/* Set/get parameters */
|
||||||
|
LKBT51_CMD_GET_MODULE_INFO = 0x40,
|
||||||
|
LKBT51_CMD_SET_CONFIG = 0x41,
|
||||||
|
LKBT51_CMD_GET_CONFIG = 0x42,
|
||||||
|
LKBT51_CMD_SET_BDA = 0x43,
|
||||||
|
LKBT51_CMD_GET_BDA = 0x44,
|
||||||
|
LKBT51_CMD_SET_NAME = 0x45,
|
||||||
|
LKBT51_CMD_GET_NAME = 0x46,
|
||||||
|
LKBT51_CMD_WRTE_CSTM_DATA = 0x49,
|
||||||
|
/* DFU */
|
||||||
|
LKBT51_CMD_GET_DFU_VER = 0x60,
|
||||||
|
LKBT51_CMD_HAND_SHAKE_TOKEN = 0x61,
|
||||||
|
LKBT51_CMD_START_DFU = 0x62,
|
||||||
|
LKBT51_CMD_SEND_FW_DATA = 0x63,
|
||||||
|
LKBT51_CMD_VERIFY_CRC32 = 0x64,
|
||||||
|
LKBT51_CMD_SWITCH_FW = 0x65,
|
||||||
|
/* Factory test */
|
||||||
|
LKBT51_CMD_FACTORY_RESET = 0x71,
|
||||||
|
LKBT51_CMD_IO_TEST = 0x72,
|
||||||
|
LKBT51_CMD_RADIO_TEST = 0x73,
|
||||||
|
/* Event */
|
||||||
|
LKBT51_EVT_LKBT51_CMD_RECEIVED = 0xA1,
|
||||||
|
LKBT51_EVT_OTA_RSP = 0xA3,
|
||||||
|
LKBT51_CONNECTION_EVT_ACK = 0xA4,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
LKBT51_EVT_ACK = 0xA1,
|
||||||
|
LKBT51_EVT_QUERY_RSP = 0xA2,
|
||||||
|
LKBT51_EVT_RESET = 0xB0,
|
||||||
|
LKBT51_EVT_LE_CONNECTION = 0xB1,
|
||||||
|
LKBT51_EVT_HOST_TYPE = 0xB2,
|
||||||
|
LKBT51_EVT_CONNECTION = 0xB3,
|
||||||
|
LKBT51_EVT_HID_EVENT = 0xB4,
|
||||||
|
LKBT51_EVT_BATTERY = 0xB5,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
LKBT51_CONNECTED = 0x20,
|
||||||
|
LKBT51_DISCOVERABLE = 0x21,
|
||||||
|
LKBT51_RECONNECTING = 0x22,
|
||||||
|
LKBT51_DISCONNECTED = 0x23,
|
||||||
|
LKBT51_PINCODE_ENTRY = 0x24,
|
||||||
|
LKBT51_EXIT_PINCODE_ENTRY = 0x25,
|
||||||
|
LKBT51_SLEEP = 0x26
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
ACK_SUCCESS = 0x00,
|
||||||
|
ACK_CHECKSUM_ERROR,
|
||||||
|
ACK_FIFO_HALF_WARNING,
|
||||||
|
ACK_FIFO_FULL_ERROR,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum{
|
||||||
|
LK_EVT_MSK_CONNECTION = 0x01 << 0,
|
||||||
|
LK_EVT_MSK_LED = 0x01 << 1,
|
||||||
|
LK_EVT_MSK_BATT = 0x01 << 2,
|
||||||
|
LK_EVT_MSK_RESET = 0x01 << 3,
|
||||||
|
LK_EVT_MSK_RPT_INTERVAL = 0x01 << 4,
|
||||||
|
LK_EVT_MSK_MD = 0x01 << 7,
|
||||||
|
};
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
static uint8_t payload[PACKET_MAX_LEN];
|
||||||
|
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 = {
|
||||||
|
lkbt51_init,
|
||||||
|
lkbt51_connect,
|
||||||
|
lkbt51_become_discoverable,
|
||||||
|
lkbt51_disconnect,
|
||||||
|
lkbt51_send_keyboard,
|
||||||
|
lkbt51_send_nkro,
|
||||||
|
lkbt51_send_consumer,
|
||||||
|
lkbt51_send_system,
|
||||||
|
lkbt51_send_mouse,
|
||||||
|
lkbt51_update_bat_lvl,
|
||||||
|
lkbt51_task
|
||||||
|
};
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
/* Init SPI */
|
||||||
|
const SPIConfig spicfg = {
|
||||||
|
.circular = false,
|
||||||
|
.slave = false,
|
||||||
|
.data_cb = NULL,
|
||||||
|
.error_cb = NULL,
|
||||||
|
.ssport = PAL_PORT(BLUETOOTH_INT_OUTPUT_PIN),
|
||||||
|
.sspad = PAL_PAD(BLUETOOTH_INT_OUTPUT_PIN),
|
||||||
|
.cr1 = SPI_CR1_MSTR | SPI_CR1_BR_1 | SPI_CR1_BR_0,
|
||||||
|
.cr2 = 0U,
|
||||||
|
};
|
||||||
|
|
||||||
|
void lkbt51_init(bool wakeup_from_low_power_mode) {
|
||||||
|
#ifdef LKBT51_RESET_PIN
|
||||||
|
if (!wakeup_from_low_power_mode) {
|
||||||
|
setPinOutput(LKBT51_RESET_PIN);
|
||||||
|
writePinLow(LKBT51_RESET_PIN);
|
||||||
|
wait_ms(1);
|
||||||
|
writePinHigh(LKBT51_RESET_PIN);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if (HAL_USE_SPI == TRUE)
|
||||||
|
if (WT_DRIVER.state == SPI_UNINIT) {
|
||||||
|
setPinOutput(SPI_SCK_PIN);
|
||||||
|
writePinHigh(SPI_SCK_PIN);
|
||||||
|
|
||||||
|
palSetLineMode(SPI_SCK_PIN, PAL_MODE_ALTERNATE(SPI_CLK_PAL_MODE));
|
||||||
|
palSetLineMode(SPI_MISO_PIN, PAL_MODE_ALTERNATE(SPI_MISO_PAL_MODE));
|
||||||
|
palSetLineMode(SPI_MOSI_PIN, PAL_MODE_ALTERNATE(SPI_MOSI_PAL_MODE));
|
||||||
|
|
||||||
|
if (wakeup_from_low_power_mode) {
|
||||||
|
spiInit();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
spiInit();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
setPinOutput(BLUETOOTH_INT_OUTPUT_PIN);
|
||||||
|
writePinHigh(BLUETOOTH_INT_OUTPUT_PIN);
|
||||||
|
|
||||||
|
setPinInputHigh(LKBT51_INT_INPUT_PIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void lkbt51_wake(void) {
|
||||||
|
if (timer_elapsed32(wake_time) > 3000) {
|
||||||
|
wake_time = timer_read32();
|
||||||
|
|
||||||
|
palWriteLine(BLUETOOTH_INT_OUTPUT_PIN, 0);
|
||||||
|
wait_ms(10);
|
||||||
|
palWriteLine(BLUETOOTH_INT_OUTPUT_PIN, 1);
|
||||||
|
wait_ms(300);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_send_protocol_ver(uint16_t ver) {
|
||||||
|
uint8_t pkt[PACKET_MAX_LEN] = {0};
|
||||||
|
memset(pkt, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
uint8_t i = 0;
|
||||||
|
|
||||||
|
pkt[i++] = 0x84;
|
||||||
|
pkt[i++] = 0x7e;
|
||||||
|
pkt[i++] = 0x00;
|
||||||
|
pkt[i++] = 0x00;
|
||||||
|
pkt[i++] = 0xAA;
|
||||||
|
pkt[i++] = 0x54;
|
||||||
|
pkt[i++] = ver & 0xFF;
|
||||||
|
pkt[i++] = (ver >> 8) & 0xFF;
|
||||||
|
pkt[i++] = (uint8_t)(~0x54);
|
||||||
|
pkt[i++] = (uint8_t)(~0xAA);
|
||||||
|
|
||||||
|
#if HAL_USE_SPI
|
||||||
|
expect_len = 10;
|
||||||
|
spiStart(&WT_DRIVER, &spicfg);
|
||||||
|
spiSelect(&WT_DRIVER);
|
||||||
|
spiSend(&WT_DRIVER, i, pkt);
|
||||||
|
spiUnselectI(&WT_DRIVER);
|
||||||
|
spiStop(&WT_DRIVER);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_send_cmd(uint8_t* payload, uint8_t len, bool ack_enable, bool retry) {
|
||||||
|
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;
|
||||||
|
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
checksum += payload[i];
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
pkt[i++] = 0x84;
|
||||||
|
pkt[i++] = 0x7e;
|
||||||
|
pkt[i++] = 0x00;
|
||||||
|
pkt[i++] = 0x00;
|
||||||
|
pkt[i++] = 0xAA;
|
||||||
|
pkt[i++] = ack_enable ? 0x56 : 0x55;
|
||||||
|
pkt[i++] = len + 2;
|
||||||
|
pkt[i++] = ~(len + 2) & 0xFF;
|
||||||
|
pkt[i++] = sn;
|
||||||
|
|
||||||
|
memcpy(pkt + i, payload, len);
|
||||||
|
i += len;
|
||||||
|
pkt[i++] = checksum & 0xFF;
|
||||||
|
pkt[i++] = (checksum >> 8) & 0xFF;
|
||||||
|
#if HAL_USE_SPI
|
||||||
|
if ((payload[0] & 0xF0) == 0x60)
|
||||||
|
expect_len = 64;
|
||||||
|
else
|
||||||
|
expect_len = 64;
|
||||||
|
|
||||||
|
spiStart(&WT_DRIVER, &spicfg);
|
||||||
|
spiSelect(&WT_DRIVER);
|
||||||
|
spiSend(&WT_DRIVER, i, pkt);
|
||||||
|
spiUnselectI(&WT_DRIVER);
|
||||||
|
spiStop(&WT_DRIVER);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_read(uint8_t* payload, uint8_t len) {
|
||||||
|
uint8_t i;
|
||||||
|
uint8_t pkt[PACKET_MAX_LEN] = {0};
|
||||||
|
memset(pkt, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
pkt[i++] = 0x84;
|
||||||
|
pkt[i++] = 0x7f;
|
||||||
|
pkt[i++] = 0x00;
|
||||||
|
pkt[i++] = 0x80;
|
||||||
|
|
||||||
|
i += len;
|
||||||
|
|
||||||
|
#if HAL_USE_SPI
|
||||||
|
spiStart(&WT_DRIVER, &spicfg);
|
||||||
|
spiSelect(&WT_DRIVER);
|
||||||
|
spiExchange(&WT_DRIVER, i, pkt, payload);
|
||||||
|
spiUnselect(&WT_DRIVER);
|
||||||
|
spiStop(&WT_DRIVER);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_send_keyboard(uint8_t* report) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_SEND_KB;
|
||||||
|
memcpy(payload + i, report, 8);
|
||||||
|
i += 8;
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_send_nkro(uint8_t* report) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_SEND_KB_NKRO;
|
||||||
|
memcpy(payload + i, report, 20); // NKRO report lenght is limited to 20 bytes
|
||||||
|
i += 20;
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_send_consumer(uint16_t report) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_SEND_CONSUMER;
|
||||||
|
payload[i++] = report & 0xFF;
|
||||||
|
payload[i++] = ((report) >> 8) & 0xFF;
|
||||||
|
i += 4; // QMK doesn't send multiple consumer reports, just skip 2nd and 3rd consumer reports
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_send_system(uint16_t report) {
|
||||||
|
uint8_t hid_usage = report & 0xFF;
|
||||||
|
|
||||||
|
if (hid_usage < 0x81 || hid_usage > 0x83) return;
|
||||||
|
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_SEND_SYSTEM;
|
||||||
|
payload[i++] = 0x01 << (hid_usage - 0x81);
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_send_mouse(uint8_t* report) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_SEND_MOUSE; // Cmd type
|
||||||
|
payload[i++] = report[1]; // Button
|
||||||
|
payload[i++] = report[2]; // X
|
||||||
|
payload[i++] = (report[2] & 0x80) ? 0xff : 0x00; // ckbt51 use 16bit report, set high byte
|
||||||
|
payload[i++] = report[3]; // Y
|
||||||
|
payload[i++] = (report[3] & 0x80) ? 0xff : 0x00; // ckbt51 use 16bit report, set high byte
|
||||||
|
payload[i++] = report[4]; // V wheel
|
||||||
|
payload[i++] = report[5]; // H wheel
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Send ack to connection event, wireless module will retry 2 times if no ack received */
|
||||||
|
void lkbt51_send_conn_evt_ack(void) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CONNECTION_EVT_ACK;
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_become_discoverable(uint8_t host_idx, void* param) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
pairing_param_t default_pairing_param = {0, 0, PAIRING_MODE_LESC_OR_SSP, BT_MODE_CLASSIC, 0, NULL};
|
||||||
|
|
||||||
|
if (param == NULL) {
|
||||||
|
param = &default_pairing_param;
|
||||||
|
}
|
||||||
|
pairing_param_t* p = (pairing_param_t*)param;
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_PAIRING; // Cmd type
|
||||||
|
payload[i++] = host_idx; // Host Index
|
||||||
|
payload[i++] = p->timeout & 0xFF; // Timeout
|
||||||
|
payload[i++] = (p->timeout >> 8) & 0xFF;
|
||||||
|
payload[i++] = p->pairingMode;
|
||||||
|
payload[i++] = p->BRorLE; // BR/LE
|
||||||
|
payload[i++] = p->txPower; // LE TX POWER
|
||||||
|
if (p->leName) {
|
||||||
|
memcpy(&payload[i], p->leName, strlen(p->leName));
|
||||||
|
i += strlen(p->leName);
|
||||||
|
}
|
||||||
|
|
||||||
|
lkbt51_wake();
|
||||||
|
lkbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Timeout : 2 ~ 255 seconds */
|
||||||
|
void lkbt51_connect(uint8_t hostIndex, uint16_t timeout) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_CONNECT;
|
||||||
|
payload[i++] = hostIndex; // Host index
|
||||||
|
payload[i++] = timeout & 0xFF; // Timeout
|
||||||
|
payload[i++] = (timeout >> 8) & 0xFF;
|
||||||
|
|
||||||
|
lkbt51_wake();
|
||||||
|
lkbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_disconnect(void) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
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);
|
||||||
|
wait_ms(70);
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_switch_host(uint8_t hostIndex) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_SWITCH_HOST;
|
||||||
|
payload[i++] = hostIndex;
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_read_state_reg(uint8_t reg, uint8_t len) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_READ_STATE_REG;
|
||||||
|
payload[i++] = reg_offset = reg;
|
||||||
|
payload[i++] = len;
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_update_bat_lvl(uint8_t bat_lvl) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_UPDATE_BAT_LVL;
|
||||||
|
payload[i++] = bat_lvl;
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_update_bat_state(uint8_t bat_state) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_UPDATE_BAT_STATE;
|
||||||
|
payload[i++] = bat_state;
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_get_info(module_info_t* info) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_GET_MODULE_INFO;
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_set_param(module_param_t* param) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_SET_CONFIG;
|
||||||
|
memcpy(payload + i, param, sizeof(module_param_t));
|
||||||
|
i += sizeof(module_param_t);
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_get_param(module_param_t* param) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_GET_CONFIG;
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_set_local_name(const char* name) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
uint8_t len = strlen(name);
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_SET_NAME;
|
||||||
|
memcpy(payload + i, name, len);
|
||||||
|
i += len;
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_get_local_name(void) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_GET_NAME;
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_factory_reset(uint8_t p2p4g_clr_msk) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
|
||||||
|
payload[i++] = LKBT51_CMD_FACTORY_RESET;
|
||||||
|
payload[i++] = p2p4g_clr_msk;
|
||||||
|
|
||||||
|
lkbt51_wake();
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
factory_reset = timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_int_pin_test(bool enable) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
payload[i++] = LKBT51_CMD_IO_TEST;
|
||||||
|
payload[i++] = enable;
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_radio_test(uint8_t channel) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
payload[i++] = LKBT51_CMD_RADIO_TEST;
|
||||||
|
payload[i++] = channel;
|
||||||
|
payload[i++] = 0;
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool lkbt51_read_customize_data(uint8_t* data, uint8_t len) {
|
||||||
|
uint8_t i;
|
||||||
|
uint8_t buf[20] = {0};
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
buf[i++] = 0x84;
|
||||||
|
buf[i++] = 0x7a;
|
||||||
|
buf[i++] = 0x00;
|
||||||
|
buf[i++] = 0x80;
|
||||||
|
|
||||||
|
#if HAL_USE_SPI
|
||||||
|
spiStart(&WT_DRIVER, &spicfg);
|
||||||
|
spiSelect(&WT_DRIVER);
|
||||||
|
spiExchange(&WT_DRIVER, 20, buf, payload);
|
||||||
|
uint16_t state = buf[5] | (buf[6] << 8);
|
||||||
|
if (state == 0x9527) spiExchange(&WT_DRIVER, len, data, payload);
|
||||||
|
spiUnselect(&WT_DRIVER);
|
||||||
|
spiStop(&WT_DRIVER);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_write_customize_data(uint8_t* data, uint8_t len) {
|
||||||
|
uint8_t i;
|
||||||
|
uint8_t pkt[PACKET_MAX_LEN] = {0};
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
pkt[i++] = 0x84;
|
||||||
|
pkt[i++] = 0x7a;
|
||||||
|
pkt[i++] = 0x00;
|
||||||
|
pkt[i++] = 0x00;
|
||||||
|
|
||||||
|
#if HAL_USE_SPI
|
||||||
|
spiStart(&WT_DRIVER, &spicfg);
|
||||||
|
spiSelect(&WT_DRIVER);
|
||||||
|
spiSend(&WT_DRIVER, i, pkt);
|
||||||
|
spiSend(&WT_DRIVER, len, data);
|
||||||
|
spiUnselectI(&WT_DRIVER);
|
||||||
|
spiStop(&WT_DRIVER);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
memset(payload, 0, PACKET_MAX_LEN);
|
||||||
|
payload[i++] = LKBT51_CMD_WRTE_CSTM_DATA;
|
||||||
|
|
||||||
|
lkbt51_send_cmd(payload, i, false, false);
|
||||||
|
}
|
||||||
|
#ifdef RAW_ENABLE
|
||||||
|
void lkbt51_dfu_tx(uint8_t rsp, uint8_t* data, uint8_t len, uint8_t sn) {
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
uint8_t buf[RAW_EPSIZE] = {0};
|
||||||
|
uint8_t i = 0;
|
||||||
|
|
||||||
|
buf[i++] = 0x03;
|
||||||
|
buf[i++] = 0xAA;
|
||||||
|
buf[i++] = 0x57;
|
||||||
|
buf[i++] = len;
|
||||||
|
buf[i++] = ~len;
|
||||||
|
buf[i++] = sn;
|
||||||
|
buf[i++] = rsp;
|
||||||
|
memcpy(&buf[i], data, len);
|
||||||
|
i += len;
|
||||||
|
|
||||||
|
for (uint8_t k = 0; k < i; k++)
|
||||||
|
checksum += buf[i];
|
||||||
|
|
||||||
|
raw_hid_send(buf, RAW_EPSIZE);
|
||||||
|
|
||||||
|
if (len > 25) {
|
||||||
|
i = 0;
|
||||||
|
memset(buf, 0, RAW_EPSIZE);
|
||||||
|
buf[i++] = 0x03;
|
||||||
|
memcpy(&buf[i], data + 25, len - 25);
|
||||||
|
i = i + len - 25;
|
||||||
|
raw_hid_send(buf, RAW_EPSIZE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
void lkbt51_dfu_rx(uint8_t* data, uint8_t length) {
|
||||||
|
if (data[0] == 0xAA && (data[1] == 0x55 || data[1] == 0x56) && data[2] == (~data[3] & 0xFF)) {
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
uint8_t payload_len = data[2];
|
||||||
|
|
||||||
|
/* Check payload_len validity */
|
||||||
|
if (payload_len > RAW_EPSIZE - PACKECT_HEADER_LEN) return;
|
||||||
|
|
||||||
|
uint8_t* payload = &data[PACKECT_HEADER_LEN];
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < payload_len - 2; i++) {
|
||||||
|
checksum += payload[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Verify checksum */
|
||||||
|
if ((checksum & 0xFF) != payload[payload_len - 2] || checksum >> 8 != payload[payload_len - 1]) return;
|
||||||
|
static uint8_t sn = 0;
|
||||||
|
|
||||||
|
bool retry = true;
|
||||||
|
if (sn != data[4]) {
|
||||||
|
sn = data[4];
|
||||||
|
retry = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((payload[0] & 0xF0) == 0x60) {
|
||||||
|
lkbt51_wake();
|
||||||
|
lkbt51_send_cmd(payload, payload_len - 2, data[1] == 0x56, retry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ack_handler(uint8_t* data, uint8_t len) {
|
||||||
|
switch (data[1]) {
|
||||||
|
case LKBT51_CMD_SEND_KB:
|
||||||
|
case LKBT51_CMD_SEND_KB_NKRO:
|
||||||
|
case LKBT51_CMD_SEND_CONSUMER:
|
||||||
|
case LKBT51_CMD_SEND_SYSTEM:
|
||||||
|
case LKBT51_CMD_SEND_MOUSE:
|
||||||
|
switch (data[2]) {
|
||||||
|
case ACK_SUCCESS:
|
||||||
|
report_buffer_set_retry(0);
|
||||||
|
report_buffer_set_inverval(connection_interval);
|
||||||
|
break;
|
||||||
|
case ACK_FIFO_HALF_WARNING:
|
||||||
|
report_buffer_set_retry(0);
|
||||||
|
report_buffer_set_inverval(connection_interval + 5);
|
||||||
|
break;
|
||||||
|
case ACK_FIFO_FULL_ERROR:
|
||||||
|
report_buffer_set_inverval(connection_interval + 10);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void query_rsp_handler(uint8_t* data, uint8_t len) {
|
||||||
|
if (data[2]) return;
|
||||||
|
|
||||||
|
switch (data[1]) {
|
||||||
|
case LKBT51_CMD_IO_TEST:
|
||||||
|
factory_test_send(data, len);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void lkbt51_event_handler(uint8_t evt_type, uint8_t* data, uint8_t len, uint8_t sn) {
|
||||||
|
wireless_event_t event = {0};
|
||||||
|
|
||||||
|
switch (evt_type) {
|
||||||
|
case LKBT51_EVT_ACK:
|
||||||
|
ack_handler(data, len);
|
||||||
|
break;
|
||||||
|
case LKBT51_EVT_RESET:
|
||||||
|
kc_printf("LKBT51_EVT_RESET\n");
|
||||||
|
event.evt_type = EVT_RESET;
|
||||||
|
event.params.reason = data[0];
|
||||||
|
break;
|
||||||
|
case LKBT51_EVT_LE_CONNECTION:
|
||||||
|
kc_printf("LKBT51_EVT_LE_CONNECTION\n");
|
||||||
|
break;
|
||||||
|
case LKBT51_EVT_HOST_TYPE:
|
||||||
|
kc_printf("LKBT51_EVT_HOST_TYPE\n");
|
||||||
|
break;
|
||||||
|
case LKBT51_EVT_HID_EVENT:
|
||||||
|
kc_printf("LKBT51_EVT_HID_EVENT\n");
|
||||||
|
event.evt_type = EVT_HID_INDICATOR;
|
||||||
|
event.params.led = data[0];
|
||||||
|
break;
|
||||||
|
case LKBT51_EVT_QUERY_RSP:
|
||||||
|
kc_printf("LKBT51_EVT_QUERY_RSP\n\r");
|
||||||
|
query_rsp_handler(data, len);
|
||||||
|
break;
|
||||||
|
case LKBT51_EVT_OTA_RSP:
|
||||||
|
#ifdef RAW_ENABLE
|
||||||
|
kc_printf("LKBT51_EVT_OTA_RSP\n");
|
||||||
|
lkbt51_dfu_tx(LKBT51_EVT_OTA_RSP, data, len, sn);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
kc_printf("Unknown event!!!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.evt_type) wireless_event_enqueue(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lkbt51_task(void) {
|
||||||
|
#define VALID_DATA_START_INDEX 4
|
||||||
|
#define BUFFER_SIZE 64
|
||||||
|
|
||||||
|
static bool wait_for_new_pkt = true;
|
||||||
|
static uint8_t len = 0xff;
|
||||||
|
static uint8_t sn = 0;
|
||||||
|
|
||||||
|
if (readPin(LKBT51_INT_INPUT_PIN) == 0) {
|
||||||
|
uint8_t buf[BUFFER_SIZE] = {0};
|
||||||
|
lkbt51_read(buf, expect_len);
|
||||||
|
|
||||||
|
uint8_t* pbuf = buf + VALID_DATA_START_INDEX;
|
||||||
|
|
||||||
|
if (pbuf[0] == 0xAA && pbuf[1] == 0x54 && pbuf[4] == (uint8_t)(~0x54) && pbuf[5] == (uint8_t)(~0xAA)) {
|
||||||
|
uint16_t protol_ver = pbuf[3] << 8 | pbuf[2];
|
||||||
|
kc_printf("protol_ver: %x\n\r", protol_ver);
|
||||||
|
(void)protol_ver;
|
||||||
|
} else if (pbuf[0] == 0xAA) {
|
||||||
|
wireless_event_t event = {0};
|
||||||
|
uint8_t evt_mask = pbuf[1];
|
||||||
|
|
||||||
|
if (evt_mask & LK_EVT_MSK_RESET) {
|
||||||
|
event.evt_type = EVT_RESET;
|
||||||
|
event.params.reason = pbuf[2];
|
||||||
|
wireless_event_enqueue(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (evt_mask & LK_EVT_MSK_CONNECTION) {
|
||||||
|
lkbt51_send_conn_evt_ack();
|
||||||
|
switch (pbuf[2]) {
|
||||||
|
case LKBT51_CONNECTED:
|
||||||
|
event.evt_type = EVT_CONNECTED;
|
||||||
|
break;
|
||||||
|
case LKBT51_DISCOVERABLE:
|
||||||
|
event.evt_type = EVT_DISCOVERABLE;
|
||||||
|
break;
|
||||||
|
case LKBT51_RECONNECTING:
|
||||||
|
event.evt_type = EVT_RECONNECTING;
|
||||||
|
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;
|
||||||
|
break;
|
||||||
|
case LKBT51_EXIT_PINCODE_ENTRY:
|
||||||
|
event.evt_type = EVT_EXIT_BT_PINCODE_ENTRY;
|
||||||
|
break;
|
||||||
|
case LKBT51_SLEEP:
|
||||||
|
event.evt_type = EVT_SLEEP;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
event.params.hostIndex = pbuf[3];
|
||||||
|
|
||||||
|
wireless_event_enqueue(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (evt_mask & LK_EVT_MSK_LED) {
|
||||||
|
memset(&event, 0, sizeof(event));
|
||||||
|
event.evt_type = EVT_HID_INDICATOR;
|
||||||
|
event.params.led = pbuf[4];
|
||||||
|
wireless_event_enqueue(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (evt_mask & LK_EVT_MSK_RPT_INTERVAL) {
|
||||||
|
uint32_t interval;
|
||||||
|
if (pbuf[8] & 0x80) {
|
||||||
|
interval = (pbuf[8] & 0x7F) * 1250;
|
||||||
|
} else {
|
||||||
|
interval = (pbuf[8] & 0x7F) * 125;
|
||||||
|
}
|
||||||
|
|
||||||
|
connection_interval = interval / 1000;
|
||||||
|
if (connection_interval > 7) connection_interval /= 3;
|
||||||
|
|
||||||
|
memset(&event, 0, sizeof(event));
|
||||||
|
event.evt_type = EVT_CONECTION_INTERVAL;
|
||||||
|
event.params.interval = connection_interval;
|
||||||
|
wireless_event_enqueue(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (evt_mask & LK_EVT_MSK_BATT) {
|
||||||
|
battery_calculate_voltage(true, pbuf[6] << 8 | pbuf[5]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pbuf = buf;
|
||||||
|
if (wait_for_new_pkt) {
|
||||||
|
for (uint8_t i = 10; i < BUFFER_SIZE - 5; i++) {
|
||||||
|
if (buf[i] == 0xAA && buf[i + 1] == 0x57 // Packet Head
|
||||||
|
&& (~buf[i + 2] & 0xFF) == buf[i + 3]) { // Check wheather len is valid
|
||||||
|
len = buf[i + 2];
|
||||||
|
sn = buf[i + 4];
|
||||||
|
pbuf = &buf[i + 5];
|
||||||
|
wait_for_new_pkt = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!wait_for_new_pkt && BUFFER_SIZE - 5 >= len) {
|
||||||
|
wait_for_new_pkt = true;
|
||||||
|
|
||||||
|
uint16_t checksum = 0;
|
||||||
|
for (int i = 0; i < len - 2; i++) {
|
||||||
|
checksum += pbuf[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((checksum & 0xff) == pbuf[len - 2] && ((checksum >> 8) & 0xff) == pbuf[len - 1]) {
|
||||||
|
lkbt51_event_handler(pbuf[0], pbuf + 1, len - 3, sn);
|
||||||
|
} else {
|
||||||
|
// TODO: Error handle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
131
keyboards/keychron/common/wireless/lkbt51.h
Normal file
131
keyboards/keychron/common/wireless/lkbt51.h
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
/* Copyright 2023 @ Keychron (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
|
||||||
|
|
||||||
|
#include "stdint.h"
|
||||||
|
#include "hal.h"
|
||||||
|
|
||||||
|
#ifndef WT_DRIVER
|
||||||
|
# define WT_DRIVER SPID1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Error checking
|
||||||
|
#if HAL_USE_SPI == FALSE
|
||||||
|
# error "Please enable SPI to use LKBT51"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !STM32_SPI_USE_SPI1 && !STM32_SPI_USE_SPI2 && !STM32_SPI_USE_SPI3
|
||||||
|
# error "WT driver activated but no SPI peripheral assigned"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define PACKECT_HEADER_LEN 5
|
||||||
|
#define BDA_LEN 6
|
||||||
|
#define PACKET_MAX_LEN 64
|
||||||
|
#define P24G_INDEX 24
|
||||||
|
|
||||||
|
enum {
|
||||||
|
PAIRING_MODE_DEFAULT = 0x00,
|
||||||
|
PAIRING_MODE_JUST_WORK,
|
||||||
|
PAIRING_MODE_PASSKEY_ENTRY,
|
||||||
|
PAIRING_MODE_LESC_OR_SSP,
|
||||||
|
PAIRING_MODE_INVALID,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BT_MODE_DEFAUL,
|
||||||
|
BT_MODE_CLASSIC,
|
||||||
|
BT_MODE_LE,
|
||||||
|
BT_MODE_INVALID,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t hostIndex;
|
||||||
|
uint16_t timeout; /* Pairing timeout, valid value range from 30 to 3600 seconds, 0 for default */
|
||||||
|
uint8_t pairingMode; /* 0: default, 1: Just Works, 2: Passkey Entry */
|
||||||
|
uint8_t BRorLE; /* Only available for dual mode module. Keep 0 for single mode module */
|
||||||
|
uint8_t txPower; /* Only available for BLE module */
|
||||||
|
const char* leName; /* Only available for BLE module */
|
||||||
|
} pairing_param_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t type;
|
||||||
|
uint16_t full_votage;
|
||||||
|
uint16_t empty_voltage;
|
||||||
|
uint16_t shutdown_voltage;
|
||||||
|
} battery_param_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t model_name[11];
|
||||||
|
uint8_t mode;
|
||||||
|
uint8_t bluetooth_version;
|
||||||
|
uint8_t firmware_version[11];
|
||||||
|
uint8_t hardware_version[11];
|
||||||
|
uint16_t cmd_set_verson;
|
||||||
|
} __attribute__((packed)) module_info_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t event_mode; /* Must be 0x02 */
|
||||||
|
uint16_t connected_idle_timeout;
|
||||||
|
uint16_t pairing_timeout; /* Range: 30 ~ 3600 second, 0 for default */
|
||||||
|
uint8_t pairing_mode; /* 0: default, 1: Just Works, 2: Passkey Entry */
|
||||||
|
uint16_t reconnect_timeout; /* 0: default, 0xFF: Unlimited time, 2 ~ 254 seconds */
|
||||||
|
uint8_t report_rate; /* 90 or 133 */
|
||||||
|
uint8_t rsvd1;
|
||||||
|
uint8_t rsvd2;
|
||||||
|
uint8_t vendor_id_source; /* 0: From Bluetooth SIG, 1: From USB-IF */
|
||||||
|
uint16_t verndor_id; /* No effect, the vendor ID is 0x3434 */
|
||||||
|
uint16_t product_id;
|
||||||
|
/* Below parametes is only available for BLE module */
|
||||||
|
uint16_t le_connection_interval_min;
|
||||||
|
uint16_t le_connection_interval_max;
|
||||||
|
uint16_t le_connection_interval_timeout;
|
||||||
|
} __attribute__((packed)) module_param_t;
|
||||||
|
|
||||||
|
void lkbt51_init(bool wakeup_from_low_power_mode);
|
||||||
|
void lkbt51_send_protocol_ver(uint16_t ver);
|
||||||
|
|
||||||
|
void lkbt51_send_cmd(uint8_t* payload, uint8_t len, bool ack_enable, bool retry);
|
||||||
|
|
||||||
|
void lkbt51_send_keyboard(uint8_t* report);
|
||||||
|
void lkbt51_send_nkro(uint8_t* report);
|
||||||
|
void lkbt51_send_consumer(uint16_t report);
|
||||||
|
void lkbt51_send_system(uint16_t report);
|
||||||
|
void lkbt51_send_mouse(uint8_t* report);
|
||||||
|
|
||||||
|
void lkbt51_become_discoverable(uint8_t host_idx, void* param);
|
||||||
|
void lkbt51_connect(uint8_t hostIndex, uint16_t timeout);
|
||||||
|
void lkbt51_disconnect(void);
|
||||||
|
void lkbt51_switch_host(uint8_t hostIndex);
|
||||||
|
void lkbt51_read_state_reg(uint8_t reg, uint8_t len);
|
||||||
|
|
||||||
|
void lkbt51_update_bat_lvl(uint8_t bat_lvl);
|
||||||
|
void lkbt51_update_bat_state(uint8_t bat_state);
|
||||||
|
|
||||||
|
void lkbt51_get_info(module_info_t* info);
|
||||||
|
void lkbt51_set_param(module_param_t* param);
|
||||||
|
void lkbt51_get_param(module_param_t* param);
|
||||||
|
void lkbt51_set_local_name(const char* name);
|
||||||
|
void lkbt51_get_local_name(void);
|
||||||
|
|
||||||
|
void lkbt51_factory_reset(uint8_t p2p4g_clr_msk);
|
||||||
|
void lkbt51_int_pin_test(bool enable);
|
||||||
|
void lkbt51_dfu_rx(uint8_t* data, uint8_t length);
|
||||||
|
void lkbt51_radio_test(uint8_t channel);
|
||||||
|
void lkbt51_write_customize_data(uint8_t* data, uint8_t len);
|
||||||
|
bool lkbt51_read_customize_data(uint8_t* data, uint8_t len);
|
||||||
|
|
||||||
|
void lkbt51_task(void);
|
||||||
298
keyboards/keychron/common/wireless/lpm.c
Normal file
298
keyboards/keychron/common/wireless/lpm.c
Normal file
@ -0,0 +1,298 @@
|
|||||||
|
/* Copyright 2022~2023 @ 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
*
|
||||||
|
* Filename: lpm.c
|
||||||
|
*
|
||||||
|
* Description: Contains low power mode implementation
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include "quantum.h"
|
||||||
|
#if defined(PROTOCOL_CHIBIOS)
|
||||||
|
# include <usb_main.h>
|
||||||
|
#endif
|
||||||
|
#include "debounce.h"
|
||||||
|
#include "wireless.h"
|
||||||
|
#include "indicator.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#include "transport.h"
|
||||||
|
#include "battery.h"
|
||||||
|
#include "report_buffer.h"
|
||||||
|
#include "keychron_common.h"
|
||||||
|
|
||||||
|
extern matrix_row_t matrix[MATRIX_ROWS];
|
||||||
|
extern wt_func_t wireless_transport;
|
||||||
|
|
||||||
|
static uint32_t lpm_timer_buffer;
|
||||||
|
static bool lpm_time_up = false;
|
||||||
|
#ifndef OPTICAL_SWITCH
|
||||||
|
static matrix_row_t empty_matrix[MATRIX_ROWS] = {0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
pin_t pins_row[MATRIX_ROWS] = MATRIX_ROW_PINS;
|
||||||
|
pin_t pins_col[MATRIX_COLS] = MATRIX_COL_PINS;
|
||||||
|
;
|
||||||
|
|
||||||
|
__attribute__((weak)) void select_all_cols(void) {
|
||||||
|
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
|
||||||
|
if (pins_col[i] == NO_PIN) continue;
|
||||||
|
setPinOutput(pins_col[i]);
|
||||||
|
writePinLow(pins_col[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void lpm_timer_reset(void) {
|
||||||
|
lpm_time_up = false;
|
||||||
|
lpm_timer_buffer = timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
void lpm_timer_stop(void) {
|
||||||
|
lpm_time_up = false;
|
||||||
|
lpm_timer_buffer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool lpm_any_matrix_action(void) {
|
||||||
|
#ifdef OPTICAL_SWITCH
|
||||||
|
bool any_key = false;
|
||||||
|
for (uint8_t i = 0; i < MATRIX_ROWS; i++)
|
||||||
|
if (matrix_get_row(i) != 0) {
|
||||||
|
any_key = true;
|
||||||
|
}
|
||||||
|
return any_key;
|
||||||
|
#else
|
||||||
|
return memcmp(matrix, empty_matrix, sizeof(empty_matrix));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Implement of entering low power mode and wakeup varies per mcu or platform */
|
||||||
|
__attribute__((weak)) void enter_power_mode(pm_t mode) {}
|
||||||
|
|
||||||
|
__attribute__((weak)) bool usb_power_connected(void) {
|
||||||
|
#ifdef USB_POWER_SENSE_PIN
|
||||||
|
return readPin(USB_POWER_SENSE_PIN) == USB_POWER_CONNECTED_LEVEL;
|
||||||
|
#else
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((weak)) bool lpm_is_kb_idle(void) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((weak)) bool lpm_set(pm_t mode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool pre_enter_low_power_mode(pm_t mode) {
|
||||||
|
#if defined(KEEP_USB_CONNECTION_IN_WIRELESS_MODE)
|
||||||
|
/* Don't enter low power mode if attached to the host */
|
||||||
|
if (mode > PM_SLEEP && usb_power_connected()) return false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!lpm_set(mode)) return false;
|
||||||
|
|
||||||
|
#if defined(KEEP_USB_CONNECTION_IN_WIRELESS_MODE)
|
||||||
|
/* Usb unit is actived and running, stop and disconnect first */
|
||||||
|
usbStop(&USBD1);
|
||||||
|
usbDisconnectBus(&USBD1);
|
||||||
|
|
||||||
|
/* Isolate USB to save power.*/
|
||||||
|
// PWR->CR2 &= ~PWR_CR2_USV; /*PWR_CR2_USV is available on STM32L4x2xx and STM32L4x3xx devices only. */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
palEnableLineEvent(LKBT51_INT_INPUT_PIN, PAL_EVENT_MODE_FALLING_EDGE);
|
||||||
|
#ifdef USB_POWER_SENSE_PIN
|
||||||
|
palEnableLineEvent(USB_POWER_SENSE_PIN, PAL_EVENT_MODE_BOTH_EDGES);
|
||||||
|
#endif
|
||||||
|
#ifdef P2P4_MODE_SELECT_PIN
|
||||||
|
palEnableLineEvent(P2P4_MODE_SELECT_PIN, PAL_EVENT_MODE_BOTH_EDGES);
|
||||||
|
#endif
|
||||||
|
#ifdef BT_MODE_SELECT_PIN
|
||||||
|
palEnableLineEvent(BT_MODE_SELECT_PIN, PAL_EVENT_MODE_BOTH_EDGES);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef OPTICAL_SWITCH
|
||||||
|
|
||||||
|
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||||
|
if (pins_row[x] != NO_PIN) {
|
||||||
|
writePinLow(pins_row[x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||||
|
if (pins_col[x] != NO_PIN) {
|
||||||
|
setPinInputLow(pins_col[x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
|
||||||
|
/* Enable key matrix wake up */
|
||||||
|
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||||
|
if (pins_row[x] != NO_PIN) {
|
||||||
|
palEnableLineEvent(pins_row[x], PAL_EVENT_MODE_BOTH_EDGES);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
select_all_cols();
|
||||||
|
|
||||||
|
#if (HAL_USE_SPI == TRUE)
|
||||||
|
palSetLineMode(SPI_SCK_PIN, PAL_MODE_INPUT_PULLDOWN);
|
||||||
|
palSetLineMode(SPI_MISO_PIN, PAL_MODE_INPUT_PULLDOWN);
|
||||||
|
palSetLineMode(SPI_MOSI_PIN, PAL_MODE_INPUT_PULLDOWN);
|
||||||
|
#endif
|
||||||
|
palSetLineMode(A12, PAL_MODE_INPUT_PULLDOWN);
|
||||||
|
palSetLineMode(A11, PAL_MODE_INPUT_PULLDOWN);
|
||||||
|
|
||||||
|
#if defined(DIP_SWITCH_PINS)
|
||||||
|
# define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(pin_t))
|
||||||
|
static pin_t dip_switch_pad[] = DIP_SWITCH_PINS;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
|
||||||
|
setPinInputLow(dip_switch_pad[i]);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
battery_stop();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void lpm_wakeup(void) {
|
||||||
|
palSetLineMode(A11, PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING | PAL_MODE_ALTERNATE(10U));
|
||||||
|
palSetLineMode(A12, PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING | PAL_MODE_ALTERNATE(10U));
|
||||||
|
|
||||||
|
#if (HAL_USE_SPI == TRUE)
|
||||||
|
palSetLineMode(SPI_SCK_PIN, PAL_MODE_ALTERNATE(5));
|
||||||
|
palSetLineMode(SPI_MISO_PIN, PAL_MODE_ALTERNATE(5));
|
||||||
|
palSetLineMode(SPI_MOSI_PIN, PAL_MODE_ALTERNATE(5));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
halInit();
|
||||||
|
|
||||||
|
#if defined(DIP_SWITCH_PINS)
|
||||||
|
/* Init dip switch as early as possible, and read it later. */
|
||||||
|
dip_switch_init();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ENCODER_ENABLE
|
||||||
|
encoder_cb_init();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (wireless_transport.init) wireless_transport.init(true);
|
||||||
|
battery_init();
|
||||||
|
|
||||||
|
/* Disable all wake up pins */
|
||||||
|
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||||
|
if (pins_row[x] != NO_PIN) {
|
||||||
|
palDisableLineEvent(pins_row[x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
palDisableLineEvent(LKBT51_INT_INPUT_PIN);
|
||||||
|
#ifdef P2P4_MODE_SELECT_PIN
|
||||||
|
palDisableLineEvent(P2P4_MODE_SELECT_PIN);
|
||||||
|
#endif
|
||||||
|
#ifdef BT_MODE_SELECT_PIN
|
||||||
|
palDisableLineEvent(BT_MODE_SELECT_PIN);
|
||||||
|
#endif
|
||||||
|
#ifdef USB_POWER_SENSE_PIN
|
||||||
|
palDisableLineEvent(USB_POWER_SENSE_PIN);
|
||||||
|
|
||||||
|
# if defined(KEEP_USB_CONNECTION_IN_WIRELESS_MODE)
|
||||||
|
if (usb_power_connected()) {
|
||||||
|
usb_event_queue_init();
|
||||||
|
init_usb_driver(&USB_DRIVER);
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* Call debounce_free() to avoiding memory leak of debounce_counters as debounce_init()
|
||||||
|
invoked in matrix_init() alloc new memory to debounce_counters */
|
||||||
|
debounce_free();
|
||||||
|
matrix_init();
|
||||||
|
|
||||||
|
#ifdef ENABLE_RGB_MATRIX_PIXEL_RAIN
|
||||||
|
extern void PIXEL_RAIN_init(void);
|
||||||
|
PIXEL_RAIN_init();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ENABLE_RGB_MATRIX_PIXEL_FLOW
|
||||||
|
extern void PIXEL_FLOW_init(void);
|
||||||
|
PIXEL_FLOW_init();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ENABLE_RGB_MATRIX_PIXEL_FRACTAL
|
||||||
|
extern void PIXEL_FRACTAL_init(void);
|
||||||
|
PIXEL_FRACTAL_init();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(DIP_SWITCH_PINS)
|
||||||
|
dip_switch_read(true);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void lpm_task(void) {
|
||||||
|
if (!lpm_time_up && sync_timer_elapsed32(lpm_timer_buffer) > RUN_MODE_PROCESS_TIME) {
|
||||||
|
lpm_time_up = true;
|
||||||
|
lpm_timer_buffer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (usb_power_connected() && USBD1.state == USB_STOP) {
|
||||||
|
usb_event_queue_init();
|
||||||
|
init_usb_driver(&USB_DRIVER);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((get_transport() == TRANSPORT_BLUETOOTH || get_transport() == TRANSPORT_P2P4) && lpm_time_up && !indicator_is_running() && lpm_is_kb_idle()) {
|
||||||
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||||
|
if (
|
||||||
|
# ifdef LED_MATRIX_ENABLE
|
||||||
|
!led_matrix_is_enabled() ||
|
||||||
|
(led_matrix_is_enabled() && led_matrix_is_driver_shutdown())
|
||||||
|
# endif
|
||||||
|
# ifdef RGB_MATRIX_ENABLE
|
||||||
|
!rgb_matrix_is_enabled() ||
|
||||||
|
(rgb_matrix_is_enabled() && rgb_matrix_is_driver_shutdown())
|
||||||
|
# endif
|
||||||
|
)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
if (!lpm_any_matrix_action()) {
|
||||||
|
if (pre_enter_low_power_mode(LOW_POWER_MODE)) {
|
||||||
|
enter_power_mode(LOW_POWER_MODE);
|
||||||
|
|
||||||
|
lpm_wakeup();
|
||||||
|
lpm_timer_reset();
|
||||||
|
report_buffer_init();
|
||||||
|
lpm_set(PM_RUN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
keyboards/keychron/common/wireless/lpm.h
Normal file
36
keyboards/keychron/common/wireless/lpm.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* Copyright 2023 @ 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
|
||||||
|
|
||||||
|
#ifndef RUN_MODE_PROCESS_TIME
|
||||||
|
# define RUN_MODE_PROCESS_TIME 1000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
PM_RUN,
|
||||||
|
PM_SLEEP,
|
||||||
|
PM_STOP,
|
||||||
|
PM_STANDBY,
|
||||||
|
} pm_t;
|
||||||
|
|
||||||
|
void lpm_init(void);
|
||||||
|
void lpm_timer_reset(void);
|
||||||
|
void lpm_timer_stop(void);
|
||||||
|
bool usb_power_connected(void);
|
||||||
|
bool lpm_is_kb_idle(void);
|
||||||
|
void enter_power_mode(pm_t mode);
|
||||||
|
void lpm_task(void);
|
||||||
114
keyboards/keychron/common/wireless/lpm_stm32f401.c
Normal file
114
keyboards/keychron/common/wireless/lpm_stm32f401.c
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/* Copyright 2023 @ 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
*
|
||||||
|
* Filename: lpm_stm32f401.c
|
||||||
|
*
|
||||||
|
* Description: Contains low power mode implementation
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include "quantum.h"
|
||||||
|
#include <usb_main.h>
|
||||||
|
#include "wireless.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#include "lpm_stm32f401.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
static pm_t power_mode = PM_RUN;
|
||||||
|
|
||||||
|
bool lpm_set(pm_t mode) {
|
||||||
|
bool ret = true;
|
||||||
|
|
||||||
|
switch (mode) {
|
||||||
|
case PM_SLEEP:
|
||||||
|
/* Wake source: Any interrupt or event */
|
||||||
|
if (power_mode != PM_RUN)
|
||||||
|
ret = false;
|
||||||
|
else
|
||||||
|
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PM_STOP:
|
||||||
|
/* Wake source: Reset pin, all I/Os, BOR, PVD, PVM, RTC, LCD, IWDG,
|
||||||
|
COMPx, USARTx, LPUART1, I2Cx, LPTIMx, USB, SWPMI */
|
||||||
|
if (power_mode != PM_RUN)
|
||||||
|
ret = false;
|
||||||
|
else {
|
||||||
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
PWR->CR |=
|
||||||
|
#if STOP_MODE_MAIN_REGULATOR_LOW_VOLTAGE
|
||||||
|
PWR_CR_MRLVDS |
|
||||||
|
#endif
|
||||||
|
#if STOP_MODE_LOW_POWER_REGULATOR_LOW_VOLTAG
|
||||||
|
PWR_CR_LPLVDS |
|
||||||
|
#endif
|
||||||
|
#if STOP_MODE_FLASH_POWER_DOWN
|
||||||
|
PWR_CR_FPDS |
|
||||||
|
#endif
|
||||||
|
#if STOP_MODE_LOW_POWER_DEEPSLEEP
|
||||||
|
PWR_CR_LPDS |
|
||||||
|
#endif
|
||||||
|
0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PM_STANDBY:
|
||||||
|
if (power_mode != PM_RUN)
|
||||||
|
ret = false;
|
||||||
|
else {
|
||||||
|
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
power_mode = mode;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void enter_power_mode(pm_t mode) {
|
||||||
|
#if STM32_HSE_ENABLED
|
||||||
|
/* Switch to HSI */
|
||||||
|
RCC->CFGR = (RCC->CFGR & (~STM32_SW_MASK)) | STM32_SW_HSI;
|
||||||
|
while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW_HSI << 2))
|
||||||
|
;
|
||||||
|
|
||||||
|
/* Set HSE off */
|
||||||
|
RCC->CR &= ~RCC_CR_HSEON;
|
||||||
|
while ((RCC->CR & RCC_CR_HSERDY))
|
||||||
|
;
|
||||||
|
|
||||||
|
/* To avoid power consumption of floating GPIO */
|
||||||
|
palSetLineMode(H0, PAL_MODE_INPUT_PULLDOWN);
|
||||||
|
palSetLineMode(H1, PAL_MODE_INPUT_PULLDOWN);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
__WFI();
|
||||||
|
|
||||||
|
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
|
||||||
|
|
||||||
|
writePinLow(BLUETOOTH_INT_OUTPUT_PIN);
|
||||||
|
stm32_clock_init();
|
||||||
|
writePinHigh(BLUETOOTH_INT_OUTPUT_PIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
void usb_power_connect(void) {}
|
||||||
|
|
||||||
|
void usb_power_disconnect(void) {}
|
||||||
33
keyboards/keychron/common/wireless/lpm_stm32f401.h
Normal file
33
keyboards/keychron/common/wireless/lpm_stm32f401.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* Copyright 2023 @ 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
|
||||||
|
|
||||||
|
#ifndef STOP_MODE_MAIN_REGULATOR_LOW_VOLTAGE
|
||||||
|
# define STOP_MODE_MAIN_REGULATOR_LOW_VOLTAGE TRUE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef STOP_MODE_LOW_POWER_REGULATOR_LOW_VOLTAG
|
||||||
|
# define STOP_MODE_LOW_POWER_REGULATOR_LOW_VOLTAG TRUE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef STOP_MODE_FLASH_POWER_DOWN
|
||||||
|
# define STOP_MODE_FLASH_POWER_DOWN TRUE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef STOP_MODE_LOW_POWER_DEEPSLEEP
|
||||||
|
# define STOP_MODE_LOW_POWER_DEEPSLEEP TRUE
|
||||||
|
#endif
|
||||||
144
keyboards/keychron/common/wireless/report_buffer.c
Normal file
144
keyboards/keychron/common/wireless/report_buffer.c
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
/* Copyright 2023 @ 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 "report_buffer.h"
|
||||||
|
#include "wireless.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
|
||||||
|
/* The report buffer is mainly used to fix key press lost issue of macro
|
||||||
|
* when wireless module fifo isn't large enough. The maximun macro
|
||||||
|
* string length is determined by this queue size, and should be
|
||||||
|
* REPORT_BUFFER_QUEUE_SIZE devided by 2 since each character is implemented
|
||||||
|
* by sending a key pressing then a key releasing report.
|
||||||
|
* Please note that it cosume sizeof(report_buffer_t) * REPORT_BUFFER_QUEUE_SIZE
|
||||||
|
* bytes RAM, with default setting, used RAM size is
|
||||||
|
* sizeof(report_buffer_t) * 256 = 34* 256 = 8704 bytes
|
||||||
|
*/
|
||||||
|
#ifndef REPORT_BUFFER_QUEUE_SIZE
|
||||||
|
# define REPORT_BUFFER_QUEUE_SIZE 256
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern wt_func_t wireless_transport;
|
||||||
|
|
||||||
|
/* report_interval value should be less than bluetooth connection interval because
|
||||||
|
* it takes some time for communicating between mcu and bluetooth module. Carefully
|
||||||
|
* set this value to feed the bt module so that we don't lost the key report nor lost
|
||||||
|
* the anchor point of bluetooth interval. The bluetooth connection interval varies
|
||||||
|
* if BLE is used, invoke report_buffer_set_inverval() to update the value
|
||||||
|
*/
|
||||||
|
uint8_t report_interval = DEFAULT_2P4G_REPORT_INVERVAL_MS;
|
||||||
|
|
||||||
|
static uint32_t report_timer_buffer = 0;
|
||||||
|
uint32_t retry_time_buffer = 0;
|
||||||
|
report_buffer_t report_buffer_queue[REPORT_BUFFER_QUEUE_SIZE];
|
||||||
|
uint16_t report_buffer_queue_head;
|
||||||
|
uint16_t report_buffer_queue_tail;
|
||||||
|
report_buffer_t kb_rpt;
|
||||||
|
uint8_t retry = 0;
|
||||||
|
|
||||||
|
void report_buffer_task(void);
|
||||||
|
|
||||||
|
void report_buffer_init(void) {
|
||||||
|
// Initialise the report queue
|
||||||
|
memset(&report_buffer_queue, 0, sizeof(report_buffer_queue));
|
||||||
|
report_buffer_queue_head = 0;
|
||||||
|
report_buffer_queue_tail = 0;
|
||||||
|
retry = 0;
|
||||||
|
report_timer_buffer = timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool report_buffer_enqueue(report_buffer_t *report) {
|
||||||
|
uint16_t next = (report_buffer_queue_head + 1) % REPORT_BUFFER_QUEUE_SIZE;
|
||||||
|
if (next == report_buffer_queue_tail) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
report_buffer_queue[report_buffer_queue_head] = *report;
|
||||||
|
report_buffer_queue_head = next;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool report_buffer_dequeue(report_buffer_t *report) {
|
||||||
|
if (report_buffer_queue_head == report_buffer_queue_tail) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
*report = report_buffer_queue[report_buffer_queue_tail];
|
||||||
|
report_buffer_queue_tail = (report_buffer_queue_tail + 1) % REPORT_BUFFER_QUEUE_SIZE;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool report_buffer_is_empty() {
|
||||||
|
return report_buffer_queue_head == report_buffer_queue_tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
void report_buffer_update_timer(void) {
|
||||||
|
report_timer_buffer = timer_read32();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool report_buffer_next_inverval(void) {
|
||||||
|
return timer_elapsed32(report_timer_buffer) > report_interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
void report_buffer_set_inverval(uint8_t interval) {
|
||||||
|
// OG_TRACE("report_buffer_set_inverval: %d\n\r", interval);
|
||||||
|
report_interval = interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t report_buffer_get_retry(void) {
|
||||||
|
return retry;
|
||||||
|
}
|
||||||
|
|
||||||
|
void report_buffer_set_retry(uint8_t times) {
|
||||||
|
retry = times;
|
||||||
|
}
|
||||||
|
|
||||||
|
void report_buffer_task(void) {
|
||||||
|
if (wireless_get_state() == WT_CONNECTED && (!report_buffer_is_empty() || retry) && report_buffer_next_inverval()) {
|
||||||
|
bool pending_data = false;
|
||||||
|
|
||||||
|
if (!retry) {
|
||||||
|
if (report_buffer_dequeue(&kb_rpt) && kb_rpt.type != REPORT_TYPE_NONE) {
|
||||||
|
if (timer_read32() > 2) {
|
||||||
|
pending_data = true;
|
||||||
|
retry = RETPORT_RETRY_COUNT;
|
||||||
|
retry_time_buffer = timer_read32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (timer_elapsed32(retry_time_buffer) > 2) {
|
||||||
|
pending_data = true;
|
||||||
|
--retry;
|
||||||
|
retry_time_buffer = timer_read32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pending_data) {
|
||||||
|
#if defined(NKRO_ENABLE) && defined(WIRELESS_NKRO_ENABLE)
|
||||||
|
if (kb_rpt.type == REPORT_TYPE_NKRO && wireless_transport.send_nkro) {
|
||||||
|
wireless_transport.send_nkro(&kb_rpt.nkro.mods);
|
||||||
|
} else if (kb_rpt.type == REPORT_TYPE_KB && wireless_transport.send_keyboard)
|
||||||
|
wireless_transport.send_keyboard(&kb_rpt.keyboard.mods);
|
||||||
|
#else
|
||||||
|
if (kb_rpt.type == REPORT_TYPE_KB && wireless_transport.send_keyboard) wireless_transport.send_keyboard(&kb_rpt.keyboard.mods);
|
||||||
|
#endif
|
||||||
|
if (kb_rpt.type == REPORT_TYPE_CONSUMER && wireless_transport.send_consumer) wireless_transport.send_consumer(kb_rpt.consumer);
|
||||||
|
report_timer_buffer = timer_read32();
|
||||||
|
lpm_timer_reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
61
keyboards/keychron/common/wireless/report_buffer.h
Normal file
61
keyboards/keychron/common/wireless/report_buffer.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/* Copyright 2022~2023 @ 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
|
||||||
|
|
||||||
|
#include "report.h"
|
||||||
|
|
||||||
|
/* Default report interval value */
|
||||||
|
#ifndef DEFAULT_BLE_REPORT_INVERVAL_MS
|
||||||
|
# define DEFAULT_BLE_REPORT_INVERVAL_MS 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Default report interval value */
|
||||||
|
#ifndef DEFAULT_2P4G_REPORT_INVERVAL_MS
|
||||||
|
# define DEFAULT_2P4G_REPORT_INVERVAL_MS 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Default report interval value */
|
||||||
|
#ifndef RETPORT_RETRY_COUNT
|
||||||
|
# define RETPORT_RETRY_COUNT 30
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum {
|
||||||
|
REPORT_TYPE_NONE,
|
||||||
|
REPORT_TYPE_KB,
|
||||||
|
REPORT_TYPE_NKRO,
|
||||||
|
REPORT_TYPE_CONSUMER,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t type;
|
||||||
|
union {
|
||||||
|
report_keyboard_t keyboard;
|
||||||
|
report_nkro_t nkro;
|
||||||
|
uint16_t consumer;
|
||||||
|
};
|
||||||
|
} report_buffer_t;
|
||||||
|
|
||||||
|
void report_buffer_init(void);
|
||||||
|
bool report_buffer_enqueue(report_buffer_t *report);
|
||||||
|
bool report_buffer_dequeue(report_buffer_t *report);
|
||||||
|
bool report_buffer_is_empty(void);
|
||||||
|
void report_buffer_update_timer(void);
|
||||||
|
bool report_buffer_next_inverval(void);
|
||||||
|
void report_buffer_set_inverval(uint8_t interval);
|
||||||
|
uint8_t report_buffer_get_retry(void);
|
||||||
|
void report_buffer_set_retry(uint8_t times);
|
||||||
|
void report_buffer_task(void);
|
||||||
43
keyboards/keychron/common/wireless/rtc_timer.c
Normal file
43
keyboards/keychron/common/wireless/rtc_timer.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* Copyright 2023 @ 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 "hal.h"
|
||||||
|
|
||||||
|
#if (HAL_USE_RTC)
|
||||||
|
|
||||||
|
# include "rtc_timer.h"
|
||||||
|
|
||||||
|
void rtc_timer_init(void) {
|
||||||
|
rtc_timer_clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void rtc_timer_clear(void) {
|
||||||
|
RTCDateTime tm = {0, 0, 0, 0, 0, 0};
|
||||||
|
rtcSetTime(&RTCD1, &tm);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t rtc_timer_read_ms(void) {
|
||||||
|
RTCDateTime tm;
|
||||||
|
rtcGetTime(&RTCD1, &tm);
|
||||||
|
|
||||||
|
return tm.millisecond;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t rtc_timer_elapsed_ms(uint32_t last) {
|
||||||
|
return TIMER_DIFF_32(rtc_timer_read_ms(), last);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
35
keyboards/keychron/common/wireless/rtc_timer.h
Normal file
35
keyboards/keychron/common/wireless/rtc_timer.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* Copyright 2023 @ 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
|
||||||
|
|
||||||
|
#include "timer.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define RTC_MAX_TIME (24 * 3600 * 1000) // Set to 1 day
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void rtc_timer_init(void);
|
||||||
|
void rtc_timer_clear(void);
|
||||||
|
uint32_t rtc_timer_read_ms(void);
|
||||||
|
uint32_t rtc_timer_elapsed_ms(uint32_t last);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
259
keyboards/keychron/common/wireless/transport.c
Normal file
259
keyboards/keychron/common/wireless/transport.c
Normal file
@ -0,0 +1,259 @@
|
|||||||
|
/* Copyright 2022~2023 @ 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 "wireless.h"
|
||||||
|
#include "indicator.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#include "mousekey.h"
|
||||||
|
#if defined(PROTOCOL_CHIBIOS)
|
||||||
|
# include <usb_main.h>
|
||||||
|
#endif
|
||||||
|
#include "transport.h"
|
||||||
|
#include "lkbt51.h"
|
||||||
|
|
||||||
|
#ifndef REINIT_LED_DRIVER
|
||||||
|
# define REINIT_LED_DRIVER 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(PROTOCOL_CHIBIOS)
|
||||||
|
extern host_driver_t chibios_driver;
|
||||||
|
#endif
|
||||||
|
extern host_driver_t wireless_driver;
|
||||||
|
extern keymap_config_t keymap_config;
|
||||||
|
extern wt_func_t wireless_transport;
|
||||||
|
|
||||||
|
static transport_t transport = TRANSPORT_NONE;
|
||||||
|
|
||||||
|
#ifdef NKRO_ENABLE
|
||||||
|
nkro_t nkro = {false, false};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void transport_changed(transport_t new_transport);
|
||||||
|
|
||||||
|
__attribute__((weak)) void bt_transport_enable(bool enable) {
|
||||||
|
if (enable) {
|
||||||
|
// if (host_get_driver() != &wireless_driver) {
|
||||||
|
host_set_driver(&wireless_driver);
|
||||||
|
|
||||||
|
/* Disconnect and reconnect to sync the wireless state
|
||||||
|
* TODO: query wireless state to sync
|
||||||
|
*/
|
||||||
|
wireless_disconnect();
|
||||||
|
|
||||||
|
uint32_t t = timer_read32();
|
||||||
|
while (timer_elapsed32(t) < 50) {
|
||||||
|
wireless_transport.task();
|
||||||
|
}
|
||||||
|
// wireless_connect();
|
||||||
|
wireless_connect_ex(30, 0);
|
||||||
|
// TODO: Clear USB report
|
||||||
|
//}
|
||||||
|
} else {
|
||||||
|
indicator_stop();
|
||||||
|
|
||||||
|
if (wireless_get_state() == WT_CONNECTED && transport == TRANSPORT_BLUETOOTH) {
|
||||||
|
report_keyboard_t empty_report = {0};
|
||||||
|
wireless_driver.send_keyboard(&empty_report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((weak)) void p24g_transport_enable(bool enable) {
|
||||||
|
if (enable) {
|
||||||
|
// if (host_get_driver() != &wireless_driver) {
|
||||||
|
host_set_driver(&wireless_driver);
|
||||||
|
|
||||||
|
/* Disconnect and reconnect to sync the wireless state
|
||||||
|
* TODO: query bluetooth state to sync
|
||||||
|
*/
|
||||||
|
wireless_disconnect();
|
||||||
|
|
||||||
|
uint32_t t = timer_read32();
|
||||||
|
while (timer_elapsed32(t) < 50) {
|
||||||
|
wireless_transport.task();
|
||||||
|
}
|
||||||
|
wireless_connect_ex(P24G_INDEX, 0);
|
||||||
|
// wireless_connect();
|
||||||
|
// TODO: Clear USB report
|
||||||
|
//}
|
||||||
|
} else {
|
||||||
|
indicator_stop();
|
||||||
|
|
||||||
|
if (wireless_get_state() == WT_CONNECTED && transport == TRANSPORT_P2P4) {
|
||||||
|
report_keyboard_t empty_report = {0};
|
||||||
|
wireless_driver.send_keyboard(&empty_report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((weak)) void usb_power_connect(void) {}
|
||||||
|
__attribute__((weak)) void usb_power_disconnect(void) {}
|
||||||
|
|
||||||
|
__attribute__((weak)) void usb_transport_enable(bool enable) {
|
||||||
|
if (enable) {
|
||||||
|
if (host_get_driver() != &chibios_driver) {
|
||||||
|
#if !defined(KEEP_USB_CONNECTION_IN_WIRELESS_MODE)
|
||||||
|
usb_power_connect();
|
||||||
|
usb_start(&USBD1);
|
||||||
|
#endif
|
||||||
|
host_set_driver(&chibios_driver);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (USB_DRIVER.state == USB_ACTIVE) {
|
||||||
|
report_keyboard_t empty_report = {0};
|
||||||
|
chibios_driver.send_keyboard(&empty_report);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined(KEEP_USB_CONNECTION_IN_WIRELESS_MODE)
|
||||||
|
usbStop(&USBD1);
|
||||||
|
usbDisconnectBus(&USBD1);
|
||||||
|
usb_power_disconnect();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_transport(transport_t new_transport) {
|
||||||
|
if (transport != new_transport) {
|
||||||
|
if (transport == TRANSPORT_USB || ((transport != TRANSPORT_USB) && wireless_get_state() == WT_CONNECTED)) clear_keyboard();
|
||||||
|
|
||||||
|
transport = new_transport;
|
||||||
|
|
||||||
|
switch (transport) {
|
||||||
|
case TRANSPORT_USB:
|
||||||
|
usb_transport_enable(true);
|
||||||
|
bt_transport_enable(false);
|
||||||
|
wait_ms(5);
|
||||||
|
p24g_transport_enable(false);
|
||||||
|
wireless_disconnect();
|
||||||
|
lpm_timer_stop();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TRANSPORT_BLUETOOTH:
|
||||||
|
p24g_transport_enable(false);
|
||||||
|
wait_ms(1);
|
||||||
|
bt_transport_enable(true);
|
||||||
|
usb_transport_enable(false);
|
||||||
|
lpm_timer_reset();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TRANSPORT_P2P4:
|
||||||
|
bt_transport_enable(false);
|
||||||
|
wait_ms(1);
|
||||||
|
p24g_transport_enable(true);
|
||||||
|
usb_transport_enable(false);
|
||||||
|
lpm_timer_reset();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
transport_changed(transport);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transport_t get_transport(void) {
|
||||||
|
return transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if (REINIT_LED_DRIVER)
|
||||||
|
/* Changing transport may cause bronw-out reset of led driver
|
||||||
|
* withoug MCU reset, which lead backlight to not work,
|
||||||
|
* reinit the led driver workgound this issue */
|
||||||
|
static void reinit_led_drvier(void) {
|
||||||
|
/* Wait circuit to discharge for a while */
|
||||||
|
systime_t start = chVTGetSystemTime();
|
||||||
|
while (chTimeI2MS(chVTTimeElapsedSinceX(start)) < 100) {
|
||||||
|
};
|
||||||
|
|
||||||
|
# ifdef LED_MATRIX_ENABLE
|
||||||
|
led_matrix_init();
|
||||||
|
# endif
|
||||||
|
# ifdef RGB_MATRIX_ENABLE
|
||||||
|
rgb_matrix_init();
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void transport_changed(transport_t new_transport) {
|
||||||
|
kc_printf("transport_changed %d\n\r", new_transport);
|
||||||
|
indicator_init();
|
||||||
|
|
||||||
|
#if (REINIT_LED_DRIVER)
|
||||||
|
reinit_led_drvier();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_TIMEOUT)
|
||||||
|
# if (RGB_MATRIX_TIMEOUT > 0)
|
||||||
|
rgb_matrix_disable_timeout_set(RGB_MATRIX_TIMEOUT_INFINITE);
|
||||||
|
rgb_matrix_disable_time_reset();
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_TIMEOUT)
|
||||||
|
# if (LED_MATRIX_TIMEOUT > 0)
|
||||||
|
led_matrix_disable_timeout_set(LED_MATRIX_TIMEOUT_INFINITE);
|
||||||
|
led_matrix_disable_time_reset();
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void usb_remote_wakeup(void) {
|
||||||
|
if (USB_DRIVER.state == USB_SUSPENDED) {
|
||||||
|
while (USB_DRIVER.state == USB_SUSPENDED) {
|
||||||
|
wireless_pre_task();
|
||||||
|
if (get_transport() != TRANSPORT_USB) {
|
||||||
|
suspend_wakeup_init_quantum();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/* Do this in the suspended state */
|
||||||
|
suspend_power_down(); // on AVR this deep sleeps for 15ms
|
||||||
|
/* Remote wakeup */
|
||||||
|
if (suspend_wakeup_condition()
|
||||||
|
#ifdef ENCODER_ENABLE
|
||||||
|
|| encoder_read()
|
||||||
|
#endif
|
||||||
|
) {
|
||||||
|
usbWakeupHost(&USB_DRIVER);
|
||||||
|
wait_ms(300);
|
||||||
|
#ifdef MOUSEKEY_ENABLE
|
||||||
|
// Wiggle to wakeup
|
||||||
|
mousekey_on(KC_MS_LEFT);
|
||||||
|
mousekey_send();
|
||||||
|
wait_ms(10);
|
||||||
|
mousekey_on(KC_MS_RIGHT);
|
||||||
|
mousekey_send();
|
||||||
|
wait_ms(10);
|
||||||
|
mousekey_off((KC_MS_RIGHT));
|
||||||
|
mousekey_send();
|
||||||
|
#else
|
||||||
|
set_mods(0x02);
|
||||||
|
send_keyboard_report();
|
||||||
|
wait_ms(10);
|
||||||
|
del_mods(0x02);
|
||||||
|
send_keyboard_report();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Woken up */
|
||||||
|
// variables has been already cleared by the wakeup hook
|
||||||
|
send_keyboard_report();
|
||||||
|
#ifdef MOUSEKEY_ENABLE
|
||||||
|
mousekey_send();
|
||||||
|
#endif /* MOUSEKEY_ENABLE */
|
||||||
|
usb_event_queue_task();
|
||||||
|
}
|
||||||
|
}
|
||||||
42
keyboards/keychron/common/wireless/transport.h
Normal file
42
keyboards/keychron/common/wireless/transport.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/* Copyright 2022~2023 @ 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
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
TRANSPORT_NONE,
|
||||||
|
TRANSPORT_USB = 0x01 << 0,
|
||||||
|
TRANSPORT_BLUETOOTH = 0x01 << 1,
|
||||||
|
TRANSPORT_P2P4 = 0x01 << 2,
|
||||||
|
TRANSPORT_MAX,
|
||||||
|
} transport_t;
|
||||||
|
|
||||||
|
#ifdef NKRO_ENABLE
|
||||||
|
typedef struct {
|
||||||
|
bool usb : 1;
|
||||||
|
bool bluetooth : 1;
|
||||||
|
} nkro_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define TRANSPORT_WIRELESS (TRANSPORT_BLUETOOTH | TRANSPORT_P2P4)
|
||||||
|
|
||||||
|
void set_transport(transport_t new_transport);
|
||||||
|
transport_t get_transport(void);
|
||||||
|
|
||||||
|
void usb_power_connect(void);
|
||||||
|
void usb_power_disconnect(void);
|
||||||
|
void usb_transport_enable(bool enable);
|
||||||
|
void usb_remote_wakeup(void);
|
||||||
657
keyboards/keychron/common/wireless/wireless.c
Normal file
657
keyboards/keychron/common/wireless/wireless.c
Normal file
@ -0,0 +1,657 @@
|
|||||||
|
/* Copyright 2023 @ 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 "wireless.h"
|
||||||
|
#include "report_buffer.h"
|
||||||
|
#include "lpm.h"
|
||||||
|
#include "battery.h"
|
||||||
|
#include "indicator.h"
|
||||||
|
#include "transport.h"
|
||||||
|
#include "rtc_timer.h"
|
||||||
|
#include "keychron_wireless_common.h"
|
||||||
|
#include "keychron_task.h"
|
||||||
|
#include "wireless_config.h"
|
||||||
|
#include "keychron_raw_hid.h"
|
||||||
|
|
||||||
|
extern uint8_t pairing_indication;
|
||||||
|
extern host_driver_t chibios_driver;
|
||||||
|
extern report_buffer_t kb_rpt;
|
||||||
|
extern uint32_t retry_time_buffer;
|
||||||
|
extern uint8_t retry;
|
||||||
|
|
||||||
|
static uint8_t host_index = 0;
|
||||||
|
static uint8_t led_state = 0;
|
||||||
|
|
||||||
|
extern wt_func_t wireless_transport;
|
||||||
|
static wt_state_t wireless_state = WT_RESET;
|
||||||
|
static bool pincodeEntry = false;
|
||||||
|
uint8_t wireless_report_protocol = true;
|
||||||
|
|
||||||
|
uint16_t backlit_disable_time = CONNECTED_BACKLIGHT_DISABLE_TIMEOUT;
|
||||||
|
uint16_t connected_idle_time = CONNECTED_IDLE_TIME;
|
||||||
|
|
||||||
|
/* declarations */
|
||||||
|
uint8_t wreless_keyboard_leds(void);
|
||||||
|
void wireless_send_keyboard(report_keyboard_t *report);
|
||||||
|
void wireless_send_nkro(report_nkro_t *report);
|
||||||
|
void wireless_send_mouse(report_mouse_t *report);
|
||||||
|
void wireless_send_extra(report_extra_t *report);
|
||||||
|
bool process_record_wireless(uint16_t keycode, keyrecord_t *record);
|
||||||
|
|
||||||
|
/* host struct */
|
||||||
|
host_driver_t wireless_driver = {wreless_keyboard_leds, wireless_send_keyboard, wireless_send_nkro, wireless_send_mouse, wireless_send_extra};
|
||||||
|
|
||||||
|
#define WT_EVENT_QUEUE_SIZE 16
|
||||||
|
wireless_event_t wireless_event_queue[WT_EVENT_QUEUE_SIZE];
|
||||||
|
uint8_t wireless_event_queue_head;
|
||||||
|
uint8_t wireless_event_queue_tail;
|
||||||
|
|
||||||
|
bool wireless_lpm_set(uint8_t *data);
|
||||||
|
|
||||||
|
void wireless_event_queue_init(void) {
|
||||||
|
// Initialise the event queue
|
||||||
|
memset(&wireless_event_queue, 0, sizeof(wireless_event_queue));
|
||||||
|
wireless_event_queue_head = 0;
|
||||||
|
wireless_event_queue_tail = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wireless_event_enqueue(wireless_event_t event) {
|
||||||
|
uint8_t next = (wireless_event_queue_head + 1) % WT_EVENT_QUEUE_SIZE;
|
||||||
|
if (next == wireless_event_queue_tail) {
|
||||||
|
/* Override the first report */
|
||||||
|
wireless_event_queue_tail = (wireless_event_queue_tail + 1) % WT_EVENT_QUEUE_SIZE;
|
||||||
|
}
|
||||||
|
wireless_event_queue[wireless_event_queue_head] = event;
|
||||||
|
wireless_event_queue_head = next;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool wireless_event_dequeue(wireless_event_t *event) {
|
||||||
|
if (wireless_event_queue_head == wireless_event_queue_tail) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*event = wireless_event_queue[wireless_event_queue_tail];
|
||||||
|
wireless_event_queue_tail = (wireless_event_queue_tail + 1) % WT_EVENT_QUEUE_SIZE;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(EECONFIG_BASE_WIRELESS_CONFIG)
|
||||||
|
void wireless_config_reset(void) {
|
||||||
|
uint8_t data[4] = { 0 };
|
||||||
|
|
||||||
|
uint16_t backlit_disable_time = CONNECTED_BACKLIGHT_DISABLE_TIMEOUT;
|
||||||
|
uint16_t connected_idle_time = CONNECTED_IDLE_TIME;
|
||||||
|
|
||||||
|
memcpy(&data[0], &backlit_disable_time, sizeof(backlit_disable_time));
|
||||||
|
memcpy(&data[2], &connected_idle_time, sizeof(connected_idle_time));
|
||||||
|
wireless_lpm_set(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_config_load(void) {
|
||||||
|
uint8_t offset = 0;
|
||||||
|
eeprom_read_block(&backlit_disable_time, (uint8_t *)(EECONFIG_BASE_WIRELESS_CONFIG+offset), sizeof(backlit_disable_time));
|
||||||
|
offset += sizeof(backlit_disable_time);
|
||||||
|
eeprom_read_block(&connected_idle_time, (uint8_t *)(EECONFIG_BASE_WIRELESS_CONFIG+offset), sizeof(connected_idle_time));
|
||||||
|
|
||||||
|
if (backlit_disable_time == 0)
|
||||||
|
backlit_disable_time = CONNECTED_BACKLIGHT_DISABLE_TIMEOUT;
|
||||||
|
else if (backlit_disable_time < 5 ) backlit_disable_time = 5;
|
||||||
|
|
||||||
|
if (connected_idle_time == 0)
|
||||||
|
connected_idle_time = CONNECTED_IDLE_TIME;
|
||||||
|
else if (connected_idle_time < 30 ) connected_idle_time = 30;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_config_save(void) {
|
||||||
|
uint8_t offset = 0;
|
||||||
|
eeprom_update_block(&backlit_disable_time, (uint8_t *)(EECONFIG_BASE_WIRELESS_CONFIG+offset), sizeof(backlit_disable_time));
|
||||||
|
offset += sizeof(backlit_disable_time);
|
||||||
|
eeprom_update_block(&connected_idle_time, (uint8_t *)(EECONFIG_BASE_WIRELESS_CONFIG+offset), sizeof(connected_idle_time));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Bluetooth init.
|
||||||
|
*/
|
||||||
|
void wireless_init(void) {
|
||||||
|
wireless_state = WT_INITIALIZED;
|
||||||
|
|
||||||
|
wireless_event_queue_init();
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
report_buffer_init();
|
||||||
|
#endif
|
||||||
|
indicator_init();
|
||||||
|
#ifdef BLUETOOTH_INT_INPUT_PIN
|
||||||
|
setPinInputHigh(BLUETOOTH_INT_INPUT_PIN);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
battery_init();
|
||||||
|
lpm_init();
|
||||||
|
#if HAL_USE_RTC
|
||||||
|
rtc_timer_init();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(EECONFIG_BASE_WIRELESS_CONFIG)
|
||||||
|
wireless_config_load();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Bluetooth trasponrt init. Bluetooth module driver shall use this function to register a callback
|
||||||
|
* to its implementation.
|
||||||
|
*/
|
||||||
|
void wireless_set_transport(wt_func_t *transport) {
|
||||||
|
if (transport) memcpy(&wireless_transport, transport, sizeof(wt_func_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enter pairing with current host index
|
||||||
|
*/
|
||||||
|
void wireless_pairing(void) {
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
wireless_pairing_ex(0, NULL);
|
||||||
|
wireless_state = WT_PARING;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enter pairing with specified host index and param
|
||||||
|
*/
|
||||||
|
void wireless_pairing_ex(uint8_t host_idx, void *param) {
|
||||||
|
kc_printf("wireless_pairing_ex %d\n\r", host_idx);
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
if (wireless_transport.pairing_ex) wireless_transport.pairing_ex(host_idx, param);
|
||||||
|
wireless_state = WT_PARING;
|
||||||
|
|
||||||
|
host_index = host_idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initiate connection request to paired host
|
||||||
|
*/
|
||||||
|
void wireless_connect(void) {
|
||||||
|
/* Work around empty report after wakeup, which leads to reconneect/disconnected loop */
|
||||||
|
if (battery_is_critical_low() || timer_read32() == 0) return;
|
||||||
|
|
||||||
|
if (wireless_state == WT_RECONNECTING && !indicator_is_running()) {
|
||||||
|
indicator_set(wireless_state, host_index);
|
||||||
|
}
|
||||||
|
wireless_transport.connect_ex(0, 0);
|
||||||
|
wireless_state = WT_RECONNECTING;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initiate connection request to paired host with argument
|
||||||
|
*/
|
||||||
|
void wireless_connect_ex(uint8_t host_idx, uint16_t timeout) {
|
||||||
|
kc_printf("wireless_connect_ex %d\n\r", host_idx);
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
if (host_idx != 0) {
|
||||||
|
/* Do nothing when trying to connect to current connected host*/
|
||||||
|
if (host_index == host_idx && wireless_state == WT_CONNECTED) return;
|
||||||
|
|
||||||
|
host_index = host_idx;
|
||||||
|
led_state = 0;
|
||||||
|
}
|
||||||
|
wireless_transport.connect_ex(host_idx, timeout);
|
||||||
|
wireless_state = WT_RECONNECTING;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initiate a disconnection */
|
||||||
|
void wireless_disconnect(void) {
|
||||||
|
kc_printf("wireless_disconnect\n\r");
|
||||||
|
if (wireless_transport.disconnect) wireless_transport.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Called when the BT device is reset. */
|
||||||
|
static void wireless_enter_reset(uint8_t reason) {
|
||||||
|
kc_printf("wireless_enter_reset\n\r");
|
||||||
|
wireless_state = WT_RESET;
|
||||||
|
wireless_enter_reset_kb(reason);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Enters discoverable state. Upon entering this state we perform the following actions:
|
||||||
|
* - change state to WT_PARING
|
||||||
|
* - set pairing indication
|
||||||
|
*/
|
||||||
|
static void wireless_enter_discoverable(uint8_t host_idx) {
|
||||||
|
kc_printf("wireless_enter_discoverable: %d\n\r", host_idx);
|
||||||
|
host_index = host_idx;
|
||||||
|
|
||||||
|
wireless_state = WT_PARING;
|
||||||
|
indicator_set(wireless_state, host_idx);
|
||||||
|
wireless_enter_discoverable_kb(host_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enters reconnecting state. Upon entering this state we perform the following actions:
|
||||||
|
* - change state to RECONNECTING
|
||||||
|
* - set reconnect indication
|
||||||
|
*/
|
||||||
|
static void wireless_enter_reconnecting(uint8_t host_idx) {
|
||||||
|
host_index = host_idx;
|
||||||
|
|
||||||
|
kc_printf("wireless_reconnecting %d\n\r", host_idx);
|
||||||
|
wireless_state = WT_RECONNECTING;
|
||||||
|
indicator_set(wireless_state, host_idx);
|
||||||
|
wireless_enter_reconnecting_kb(host_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Enters connected state. Upon entering this state we perform the following actions:
|
||||||
|
* - change state to CONNECTED
|
||||||
|
* - set connected indication
|
||||||
|
* - enable NKRO if it is support
|
||||||
|
*/
|
||||||
|
static void wireless_enter_connected(uint8_t host_idx) {
|
||||||
|
kc_printf("wireless_connected %d\n\r", host_idx);
|
||||||
|
|
||||||
|
wireless_state = WT_CONNECTED;
|
||||||
|
indicator_set(wireless_state, host_idx);
|
||||||
|
host_index = host_idx;
|
||||||
|
|
||||||
|
clear_keyboard();
|
||||||
|
|
||||||
|
/* Enable NKRO since it may be disabled in pin code entry */
|
||||||
|
#if defined(NKRO_ENABLE) && !defined(WIRELESS_NKRO_ENABLE)
|
||||||
|
keymap_config.nkro = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
wireless_enter_connected_kb(host_idx);
|
||||||
|
if (battery_is_empty()) {
|
||||||
|
indicator_battery_low_enable(true);
|
||||||
|
}
|
||||||
|
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, uint8_t reason) {
|
||||||
|
kc_printf("wireless_disconnected %d, %d\n\r", host_idx, reason);
|
||||||
|
|
||||||
|
uint8_t previous_state = wireless_state;
|
||||||
|
led_state = 0;
|
||||||
|
if (get_transport() & TRANSPORT_WIRELESS)
|
||||||
|
led_update_kb((led_t)led_state);
|
||||||
|
|
||||||
|
wireless_state = WT_DISCONNECTED;
|
||||||
|
|
||||||
|
if (previous_state == WT_CONNECTED) {
|
||||||
|
lpm_timer_reset();
|
||||||
|
indicator_set(WT_SUSPEND, host_idx);
|
||||||
|
} else {
|
||||||
|
indicator_set(wireless_state, host_idx);
|
||||||
|
#if defined(RGB_MATRIX_ENABLE) || defined(LED_MATRIX_ENABLE)
|
||||||
|
if (reason && (get_transport() & TRANSPORT_WIRELESS)) {
|
||||||
|
indicator_set_backlit_timeout(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT*1000);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
report_buffer_init();
|
||||||
|
#endif
|
||||||
|
retry = 0;
|
||||||
|
wireless_enter_disconnected_kb(host_idx, reason);
|
||||||
|
|
||||||
|
indicator_battery_low_enable(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Enter pin code entry state. */
|
||||||
|
static void wireless_enter_bluetooth_pin_code_entry(void) {
|
||||||
|
#if defined(NKRO_ENABLE)
|
||||||
|
keymap_config.nkro = FALSE;
|
||||||
|
#endif
|
||||||
|
pincodeEntry = true;
|
||||||
|
wireless_enter_bluetooth_pin_code_entry_kb();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Exit pin code entry state. */
|
||||||
|
static void wireless_exit_bluetooth_pin_code_entry(void) {
|
||||||
|
#if defined(NKRO_ENABLE) || defined(WIRELESS_NKRO_ENABLE)
|
||||||
|
keymap_config.raw = eeconfig_read_keymap();
|
||||||
|
#endif
|
||||||
|
pincodeEntry = false;
|
||||||
|
wireless_exit_bluetooth_pin_code_entry_kb();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Enters disconnected state. Upon entering this state we perform the following actions:
|
||||||
|
* - change state to DISCONNECTED
|
||||||
|
* - set disconnected indication
|
||||||
|
*/
|
||||||
|
static void wireless_enter_sleep(void) {
|
||||||
|
kc_printf("wireless_enter_sleep %d\n\r", wireless_state);
|
||||||
|
|
||||||
|
led_state = 0;
|
||||||
|
|
||||||
|
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);
|
||||||
|
indicator_battery_low_enable(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__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, 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) {}
|
||||||
|
|
||||||
|
/* */
|
||||||
|
static void wireless_hid_set_protocol(bool report_protocol) {
|
||||||
|
wireless_report_protocol = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t wreless_keyboard_leds(void) {
|
||||||
|
if (wireless_state == WT_CONNECTED) {
|
||||||
|
return led_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern keymap_config_t keymap_config;
|
||||||
|
|
||||||
|
void wireless_send_keyboard(report_keyboard_t *report) {
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
if (wireless_state == WT_PARING && !pincodeEntry) return;
|
||||||
|
|
||||||
|
if (wireless_state == WT_CONNECTED || (wireless_state == WT_PARING && pincodeEntry)) {
|
||||||
|
if (wireless_transport.send_keyboard) {
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
bool empty = report_buffer_is_empty();
|
||||||
|
|
||||||
|
report_buffer_t report_buffer;
|
||||||
|
report_buffer.type = REPORT_TYPE_KB;
|
||||||
|
memcpy(&report_buffer.keyboard, report, sizeof(report_keyboard_t));
|
||||||
|
report_buffer_enqueue(&report_buffer);
|
||||||
|
|
||||||
|
if (empty)
|
||||||
|
report_buffer_task();
|
||||||
|
#else
|
||||||
|
wireless_transport.send_keyboard(&report->mods);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} else if (wireless_state != WT_RESET) {
|
||||||
|
wireless_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_send_nkro(report_nkro_t *report) {
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
if (wireless_state == WT_PARING && !pincodeEntry) return;
|
||||||
|
|
||||||
|
if (wireless_state == WT_CONNECTED || (wireless_state == WT_PARING && pincodeEntry)) {
|
||||||
|
if (wireless_transport.send_nkro) {
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
bool empty = report_buffer_is_empty();
|
||||||
|
|
||||||
|
report_buffer_t report_buffer;
|
||||||
|
report_buffer.type = REPORT_TYPE_NKRO;
|
||||||
|
memcpy(&report_buffer.nkro, report, sizeof(report_nkro_t));
|
||||||
|
report_buffer_enqueue(&report_buffer);
|
||||||
|
|
||||||
|
if (empty)
|
||||||
|
report_buffer_task();
|
||||||
|
#else
|
||||||
|
wireless_transport.send_nkro(&report->mods);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} else if (wireless_state != WT_RESET) {
|
||||||
|
wireless_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_send_mouse(report_mouse_t *report) {
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
if (wireless_state == WT_CONNECTED) {
|
||||||
|
if (wireless_transport.send_mouse) wireless_transport.send_mouse((uint8_t *)report);
|
||||||
|
} else if (wireless_state != WT_RESET) {
|
||||||
|
wireless_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_send_system(uint16_t data) {
|
||||||
|
if (wireless_state == WT_CONNECTED) {
|
||||||
|
if (wireless_transport.send_system) wireless_transport.send_system(data);
|
||||||
|
} else if (wireless_state != WT_RESET) {
|
||||||
|
wireless_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_send_consumer(uint16_t data) {
|
||||||
|
if (wireless_state == WT_CONNECTED) {
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
if (report_buffer_is_empty() && report_buffer_next_inverval()) {
|
||||||
|
if (wireless_transport.send_consumer) wireless_transport.send_consumer(data);
|
||||||
|
report_buffer_update_timer();
|
||||||
|
} else {
|
||||||
|
report_buffer_t report_buffer;
|
||||||
|
report_buffer.type = REPORT_TYPE_CONSUMER;
|
||||||
|
report_buffer.consumer = data;
|
||||||
|
report_buffer_enqueue(&report_buffer);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if (wireless_transport.send_consumer) wireless_transport.send_consumer(data);
|
||||||
|
#endif
|
||||||
|
} else if (wireless_state != WT_RESET) {
|
||||||
|
wireless_connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_send_extra(report_extra_t *report) {
|
||||||
|
if (battery_is_critical_low()) return;
|
||||||
|
|
||||||
|
if (report->report_id == REPORT_ID_SYSTEM) {
|
||||||
|
wireless_send_system(report->usage);
|
||||||
|
} else if (report->report_id == REPORT_ID_CONSUMER) {
|
||||||
|
wireless_send_consumer(report->usage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_low_battery_shutdown(void) {
|
||||||
|
indicator_battery_low_enable(false);
|
||||||
|
|
||||||
|
|
||||||
|
report_buffer_init();
|
||||||
|
clear_keyboard(); //
|
||||||
|
wait_ms(50); // wait a while for bt module to free buffer by sending report
|
||||||
|
|
||||||
|
// Release all keys by sending empty reports
|
||||||
|
if (keymap_config.nkro) {
|
||||||
|
report_nkro_t empty_nkro_report;
|
||||||
|
memset(&empty_nkro_report, 0, sizeof(empty_nkro_report));
|
||||||
|
wireless_transport.send_nkro(&empty_nkro_report.mods);
|
||||||
|
} else {
|
||||||
|
report_keyboard_t empty_report;
|
||||||
|
memset(&empty_report, 0, sizeof(empty_report));
|
||||||
|
wireless_transport.send_keyboard(&empty_report.mods);
|
||||||
|
}
|
||||||
|
wait_ms(10);
|
||||||
|
wireless_transport.send_consumer(0);
|
||||||
|
wait_ms(10);
|
||||||
|
report_mouse_t empty_mouse_report;
|
||||||
|
memset(&empty_mouse_report, 0, sizeof(empty_mouse_report));
|
||||||
|
wireless_transport.send_mouse((uint8_t *)&empty_mouse_report);
|
||||||
|
wait_ms(300); // Wait for bt module to send all buffered report
|
||||||
|
|
||||||
|
wireless_disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_event_task(void) {
|
||||||
|
wireless_event_t event;
|
||||||
|
while (wireless_event_dequeue(&event)) {
|
||||||
|
switch (event.evt_type) {
|
||||||
|
case EVT_RESET:
|
||||||
|
wireless_enter_reset(event.params.reason);
|
||||||
|
break;
|
||||||
|
case EVT_CONNECTED:
|
||||||
|
wireless_enter_connected(event.params.hostIndex);
|
||||||
|
break;
|
||||||
|
case EVT_DISCOVERABLE:
|
||||||
|
wireless_enter_discoverable(event.params.hostIndex);
|
||||||
|
break;
|
||||||
|
case EVT_RECONNECTING:
|
||||||
|
wireless_enter_reconnecting(event.params.hostIndex);
|
||||||
|
break;
|
||||||
|
case EVT_DISCONNECTED:
|
||||||
|
wireless_enter_disconnected(event.params.hostIndex, event.data);
|
||||||
|
break;
|
||||||
|
case EVT_BT_PINCODE_ENTRY:
|
||||||
|
wireless_enter_bluetooth_pin_code_entry();
|
||||||
|
break;
|
||||||
|
case EVT_EXIT_BT_PINCODE_ENTRY:
|
||||||
|
wireless_exit_bluetooth_pin_code_entry();
|
||||||
|
break;
|
||||||
|
case EVT_SLEEP:
|
||||||
|
wireless_enter_sleep();
|
||||||
|
break;
|
||||||
|
case EVT_HID_INDICATOR:
|
||||||
|
led_state = event.params.led;
|
||||||
|
break;
|
||||||
|
case EVT_HID_SET_PROTOCOL:
|
||||||
|
wireless_hid_set_protocol(event.params.protocol);
|
||||||
|
break;
|
||||||
|
case EVT_CONECTION_INTERVAL:
|
||||||
|
report_buffer_set_inverval(event.params.interval);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_task(void) {
|
||||||
|
wireless_transport.task();
|
||||||
|
wireless_event_task();
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
report_buffer_task();
|
||||||
|
#endif
|
||||||
|
indicator_task();
|
||||||
|
keychron_wireless_common_task();
|
||||||
|
battery_task();
|
||||||
|
lpm_task();
|
||||||
|
}
|
||||||
|
|
||||||
|
void send_string_task(void) {
|
||||||
|
if ((get_transport() & TRANSPORT_WIRELESS) && wireless_get_state() == WT_CONNECTED) {
|
||||||
|
wireless_transport.task();
|
||||||
|
#ifndef DISABLE_REPORT_BUFFER
|
||||||
|
report_buffer_task();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wt_state_t wireless_get_state(void) {
|
||||||
|
return wireless_state;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool process_record_wireless(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
if (get_transport() & TRANSPORT_WIRELESS) {
|
||||||
|
lpm_timer_reset();
|
||||||
|
|
||||||
|
if (battery_is_empty() && wireless_get_state() == WT_CONNECTED && record->event.pressed) {
|
||||||
|
indicator_battery_low_enable(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!process_record_keychron_wireless(keycode, record)) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(EECONFIG_BASE_WIRELESS_CONFIG)
|
||||||
|
bool wireless_lpm_get(uint8_t *data) {
|
||||||
|
uint8_t index = 1;
|
||||||
|
memcpy(&data[index], &backlit_disable_time, sizeof(backlit_disable_time));
|
||||||
|
index += sizeof(backlit_disable_time);
|
||||||
|
memcpy(&data[index], &connected_idle_time, sizeof(connected_idle_time));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wireless_lpm_set(uint8_t *data) {
|
||||||
|
uint8_t index = 0;
|
||||||
|
|
||||||
|
memcpy(&backlit_disable_time, &data[index], sizeof(backlit_disable_time));
|
||||||
|
index += sizeof(backlit_disable_time);
|
||||||
|
memcpy(&connected_idle_time, &data[index], sizeof(connected_idle_time));
|
||||||
|
|
||||||
|
if (backlit_disable_time < 5 || connected_idle_time < 60) {
|
||||||
|
wireless_config_load();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
wireless_config_save();
|
||||||
|
|
||||||
|
// Reset backlight timeout
|
||||||
|
if ((get_transport() & TRANSPORT_WIRELESS) && wireless_state == WT_CONNECTED)
|
||||||
|
{
|
||||||
|
indicator_set_backlit_timeout(backlit_disable_time*1000);
|
||||||
|
indicator_reset_backlit_time();
|
||||||
|
|
||||||
|
// Wiggle mouse to reset bluetooth module timer
|
||||||
|
mousekey_on(KC_MS_LEFT);
|
||||||
|
mousekey_send();
|
||||||
|
wait_ms(10);
|
||||||
|
mousekey_on(KC_MS_RIGHT);
|
||||||
|
mousekey_send();
|
||||||
|
wait_ms(10);
|
||||||
|
mousekey_off((KC_MS_RIGHT));
|
||||||
|
mousekey_send();
|
||||||
|
wait_ms(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update bluetooth module param
|
||||||
|
lkbt51_param_init();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wireless_raw_hid_rx(uint8_t *data, uint8_t length) {
|
||||||
|
uint8_t cmd = data[1];
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
switch (cmd) {
|
||||||
|
case WIRELESS_LPM_GET:
|
||||||
|
success = wireless_lpm_get(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WIRELESS_LPM_SET:
|
||||||
|
success = wireless_lpm_set(&data[2]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
data[0] = 0xFF;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
data[2] = success ? 0 : 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
106
keyboards/keychron/common/wireless/wireless.h
Normal file
106
keyboards/keychron/common/wireless/wireless.h
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
/* Copyright 2023~2025 @ 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
|
||||||
|
|
||||||
|
#include "wireless_event_type.h"
|
||||||
|
#include "action.h"
|
||||||
|
|
||||||
|
#ifdef KC_DEBUG
|
||||||
|
# define kc_printf dprintf
|
||||||
|
#else
|
||||||
|
# define kc_printf(format, ...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Low power mode */
|
||||||
|
#ifndef LOW_POWER_MODE
|
||||||
|
# define LOW_POWER_MODE PM_STOP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Wake pin used for blueooth module/controller to wake up MCU in low power mode*/
|
||||||
|
#ifndef BLUETOOTH_INT_INPUT_PIN
|
||||||
|
# define WAKE_PIN A5
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
/* Type of an enumeration of the possible wireless transport state.*/
|
||||||
|
typedef enum {
|
||||||
|
WT_RESET,
|
||||||
|
WT_INITIALIZED, // 1
|
||||||
|
WT_DISCONNECTED, // 2
|
||||||
|
WT_CONNECTED, // 3
|
||||||
|
WT_PARING, // 4
|
||||||
|
WT_RECONNECTING, // 5
|
||||||
|
WT_SUSPEND
|
||||||
|
} wt_state_t;
|
||||||
|
|
||||||
|
//extern event_listener_t wireless_driver;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void (*init)(bool);
|
||||||
|
void (*connect_ex)(uint8_t, uint16_t);
|
||||||
|
void (*pairing_ex)(uint8_t, void *);
|
||||||
|
void (*disconnect)(void);
|
||||||
|
void (*send_keyboard)(uint8_t *);
|
||||||
|
void (*send_nkro)(uint8_t *);
|
||||||
|
void (*send_consumer)(uint16_t);
|
||||||
|
void (*send_system)(uint16_t);
|
||||||
|
void (*send_mouse)(uint8_t *);
|
||||||
|
void (*update_bat_level)(uint8_t);
|
||||||
|
void (*task)(void);
|
||||||
|
} wt_func_t;
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
extern void register_wt_tasks(void);
|
||||||
|
|
||||||
|
void wireless_init(void);
|
||||||
|
void wireless_config_reset(void);
|
||||||
|
|
||||||
|
void wireless_set_transport(wt_func_t *transport);
|
||||||
|
void wireless(void);
|
||||||
|
|
||||||
|
bool wireless_event_enqueue(wireless_event_t event);
|
||||||
|
|
||||||
|
void wireless_connect(void);
|
||||||
|
void wireless_connect_ex(uint8_t host_idx, uint16_t timeout);
|
||||||
|
void wireless_disconnect(void);
|
||||||
|
|
||||||
|
void wireless_pairing(void);
|
||||||
|
void wireless_pairing_ex(uint8_t host_idx, void *param);
|
||||||
|
// bool bluetooth_is_activated(void);
|
||||||
|
|
||||||
|
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, 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);
|
||||||
|
|
||||||
|
void wireless_task(void);
|
||||||
|
void wireless_pre_task(void);
|
||||||
|
void wireless_post_task(void);
|
||||||
|
void send_string_task(void);
|
||||||
|
|
||||||
|
wt_state_t wireless_get_state(void);
|
||||||
|
|
||||||
|
void wireless_low_battery_shutdown(void);
|
||||||
|
|
||||||
|
bool process_record_wireless(uint16_t keycode, keyrecord_t *record);
|
||||||
|
|
||||||
|
void wireless_raw_hid_rx(uint8_t *data, uint8_t length);
|
||||||
|
|
||||||
21
keyboards/keychron/common/wireless/wireless.mk
Normal file
21
keyboards/keychron/common/wireless/wireless.mk
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
OPT_DEFS += -DLK_WIRELESS_ENABLE -DWIRELESS_CONFIG_ENABLE
|
||||||
|
OPT_DEFS += -DNO_USB_STARTUP_CHECK
|
||||||
|
OPT_DEFS += -DCORTEX_ENABLE_WFI_IDLE=TRUE
|
||||||
|
|
||||||
|
WIRELESS_DIR = common/wireless
|
||||||
|
SRC += \
|
||||||
|
$(WIRELESS_DIR)/wireless.c \
|
||||||
|
$(WIRELESS_DIR)/report_buffer.c \
|
||||||
|
$(WIRELESS_DIR)/lkbt51.c \
|
||||||
|
$(WIRELESS_DIR)/indicator.c \
|
||||||
|
$(WIRELESS_DIR)/wireless_main.c \
|
||||||
|
$(WIRELESS_DIR)/transport.c \
|
||||||
|
$(WIRELESS_DIR)/lpm.c \
|
||||||
|
$(WIRELESS_DIR)/lpm_stm32f401.c \
|
||||||
|
$(WIRELESS_DIR)/battery.c \
|
||||||
|
$(WIRELESS_DIR)/bat_level_animation.c \
|
||||||
|
$(WIRELESS_DIR)/rtc_timer.c \
|
||||||
|
$(WIRELESS_DIR)/keychron_wireless_common.c
|
||||||
|
|
||||||
|
VPATH += $(TOP_DIR)/keyboards/keychron/$(WIRELESS_DIR)
|
||||||
|
|
||||||
31
keyboards/keychron/common/wireless/wireless_config.h
Normal file
31
keyboards/keychron/common/wireless/wireless_config.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* Copyright 2023~2025 @ 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
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
//
|
||||||
|
#ifndef BT_HOST_DEVICES_COUNT
|
||||||
|
# define BT_HOST_DEVICES_COUNT 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define P2P4G_HOST_DEVICES_COUNT 1
|
||||||
|
|
||||||
|
// Uint: Second, the timer restarts on key activities.
|
||||||
|
#ifndef CONNECTED_IDLE_TIME
|
||||||
|
# define CONNECTED_IDLE_TIME 7200
|
||||||
|
#endif
|
||||||
45
keyboards/keychron/common/wireless/wireless_event_type.h
Normal file
45
keyboards/keychron/common/wireless/wireless_event_type.h
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/* Copyright 2023~2025 @ 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
|
||||||
|
|
||||||
|
/* Type of an enumeration of the possible wireless events.*/
|
||||||
|
typedef enum {
|
||||||
|
EVT_NONE = 0,
|
||||||
|
EVT_RESET,
|
||||||
|
EVT_DISCOVERABLE,
|
||||||
|
EVT_RECONNECTING,
|
||||||
|
EVT_CONNECTED,
|
||||||
|
EVT_DISCONNECTED,
|
||||||
|
EVT_BT_PINCODE_ENTRY,
|
||||||
|
EVT_EXIT_BT_PINCODE_ENTRY,
|
||||||
|
EVT_SLEEP,
|
||||||
|
EVT_HID_SET_PROTOCOL,
|
||||||
|
EVT_HID_INDICATOR,
|
||||||
|
EVT_CONECTION_INTERVAL,
|
||||||
|
} event_type_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
event_type_t evt_type; /*The type of the event. */
|
||||||
|
union {
|
||||||
|
uint8_t reason; /* Parameters to WT_RESET event */
|
||||||
|
uint8_t hostIndex; /* Parameters to connection event from EVT_DISCOVERABLE to EVT_DISCONECTED */
|
||||||
|
uint8_t led; /* Parameters to EVT_HID_INDICATOR event */
|
||||||
|
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;
|
||||||
36
keyboards/keychron/common/wireless/wireless_main.c
Normal file
36
keyboards/keychron/common/wireless/wireless_main.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* Copyright 2023 @ 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 "wireless.h"
|
||||||
|
#include "transport.h"
|
||||||
|
#include "factory_test.h"
|
||||||
|
#include "keychron_task.h"
|
||||||
|
|
||||||
|
__attribute__((weak)) void wireless_pre_task(void) {}
|
||||||
|
__attribute__((weak)) void wireless_post_task(void) {}
|
||||||
|
|
||||||
|
bool wireless_tasks(void) {
|
||||||
|
wireless_pre_task();
|
||||||
|
wireless_task();
|
||||||
|
wireless_post_task();
|
||||||
|
|
||||||
|
/* usb_remote_wakeup() should be invoked last so that we have chance
|
||||||
|
* to switch to wireless after start-up when usb is not connected
|
||||||
|
*/
|
||||||
|
if (get_transport() == TRANSPORT_USB) usb_remote_wakeup();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
55
keyboards/keychron/k5_max/ansi/rgb/config.h
Normal file
55
keyboards/keychron/k5_max/ansi/rgb/config.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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
|
||||||
|
|
||||||
|
#ifdef RGB_MATRIX_ENABLE
|
||||||
|
/* RGB Matrix driver configuration */
|
||||||
|
# define DRIVER_COUNT 2
|
||||||
|
# define RGB_MATRIX_LED_COUNT 108
|
||||||
|
|
||||||
|
# define SPI_SCK_PIN A5
|
||||||
|
# define SPI_MISO_PIN A6
|
||||||
|
# define SPI_MOSI_PIN A7
|
||||||
|
|
||||||
|
# define DRIVER_CS_PINS \
|
||||||
|
{ B8, B9 }
|
||||||
|
# define SNLED23751_SPI_DIVISOR 16
|
||||||
|
# define SPI_DRIVER SPID1
|
||||||
|
|
||||||
|
/* Scan phase of led driver set as MSKPHASE_9CHANNEL(defined as 0x03 in snled27351.h) */
|
||||||
|
# define PHASE_CHANNEL MSKPHASE_12CHANNEL
|
||||||
|
|
||||||
|
/* Set LED driver current */
|
||||||
|
# define SNLED27351_CURRENT_TUNE \
|
||||||
|
{ 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12 }
|
||||||
|
|
||||||
|
/* Set to infinit, which is use in USB mode by default */
|
||||||
|
# define RGB_MATRIX_TIMEOUT RGB_MATRIX_TIMEOUT_INFINITE
|
||||||
|
|
||||||
|
/* Allow shutdown of led driver to save power */
|
||||||
|
# define RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE
|
||||||
|
/* Turn off backlight on low brightness to save power */
|
||||||
|
# define RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL 48
|
||||||
|
|
||||||
|
/* Indications */
|
||||||
|
# define LOW_BAT_IND_INDEX \
|
||||||
|
{ 98 }
|
||||||
|
|
||||||
|
# define RGB_MATRIX_KEYPRESSES
|
||||||
|
# define RGB_MATRIX_FRAMEBUFFER_EFFECTS
|
||||||
|
|
||||||
|
#endif
|
||||||
36
keyboards/keychron/k5_max/ansi/rgb/info.json
Normal file
36
keyboards/keychron/k5_max/ansi/rgb/info.json
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"usb": {
|
||||||
|
"pid": "0x0A50",
|
||||||
|
"device_version": "1.0.0"
|
||||||
|
},
|
||||||
|
"features": {
|
||||||
|
"rgb_matrix": true
|
||||||
|
},
|
||||||
|
"rgb_matrix": {
|
||||||
|
"driver": "snled27351_spi",
|
||||||
|
"sleep": true,
|
||||||
|
"animations": {
|
||||||
|
"band_spiral_val": true,
|
||||||
|
"breathing": true,
|
||||||
|
"cycle_all": true,
|
||||||
|
"cycle_left_right": true,
|
||||||
|
"cycle_out_in": true,
|
||||||
|
"cycle_out_in_dual": true,
|
||||||
|
"cycle_pinwheel": true,
|
||||||
|
"cycle_spiral": true,
|
||||||
|
"cycle_up_down": true,
|
||||||
|
"digital_rain": true,
|
||||||
|
"dual_beacon": true,
|
||||||
|
"jellybean_raindrops": true,
|
||||||
|
"pixel_rain": true,
|
||||||
|
"rainbow_beacon": true,
|
||||||
|
"rainbow_moving_chevron": true,
|
||||||
|
"solid_reactive_multinexus": true,
|
||||||
|
"solid_reactive_multiwide": true,
|
||||||
|
"solid_reactive_simple": true,
|
||||||
|
"solid_splash": true,
|
||||||
|
"splash": true,
|
||||||
|
"typing_heatmap": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
68
keyboards/keychron/k5_max/ansi/rgb/keymaps/default/keymap.c
Normal file
68
keyboards/keychron/k5_max/ansi/rgb/keymaps/default/keymap.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/* Copyright 2024 @ Keychron (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 QMK_KEYBOARD_H
|
||||||
|
#include "keychron_common.h"
|
||||||
|
|
||||||
|
enum layers {
|
||||||
|
MAC_BASE,
|
||||||
|
MAC_FN,
|
||||||
|
WIN_BASE,
|
||||||
|
WIN_FN,
|
||||||
|
};
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
|
||||||
|
[MAC_BASE] = LAYOUT_108_ansi(
|
||||||
|
KC_ESC, KC_BRID, KC_BRIU, KC_MCTRL, KC_LNPAD, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_SNAP, KC_SIRI, RGB_MOD, KC_F13, KC_F14, KC_F15, KC_F16,
|
||||||
|
KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, KC_NUM, KC_PSLS, KC_PAST, KC_PMNS,
|
||||||
|
KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, KC_P7, KC_P8, KC_P9,
|
||||||
|
KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, KC_P4, KC_P5, KC_P6, KC_PPLS,
|
||||||
|
KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, KC_P1, KC_P2, KC_P3,
|
||||||
|
KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, KC_ROPTN,MO(MAC_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT, KC_P0, KC_PDOT, KC_PENT),
|
||||||
|
|
||||||
|
[MAC_FN] = LAYOUT_108_ansi(
|
||||||
|
_______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, _______, _______, RGB_TOG, _______, _______, _______, _______,
|
||||||
|
_______, BT_HST1, BT_HST2, BT_HST3, P2P4G, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
|
||||||
|
RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
|
||||||
|
_______, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
|
||||||
|
_______, _______, _______, _______, _______, BAT_LVL, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______,
|
||||||
|
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______),
|
||||||
|
|
||||||
|
[WIN_BASE] = LAYOUT_108_ansi(
|
||||||
|
KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_CTANA, RGB_MOD, _______, _______, _______, _______,
|
||||||
|
KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, KC_NUM, KC_PSLS, KC_PAST, KC_PMNS,
|
||||||
|
KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, KC_P7, KC_P8, KC_P9,
|
||||||
|
KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, KC_P4, KC_P5, KC_P6, KC_PPLS,
|
||||||
|
KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, KC_P1, KC_P2, KC_P3,
|
||||||
|
KC_LCTL, KC_LWIN, KC_LALT, KC_SPC, KC_RALT, KC_RWIN, MO(WIN_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT, KC_P0, KC_PDOT, KC_PENT),
|
||||||
|
|
||||||
|
[WIN_FN] = LAYOUT_108_ansi(
|
||||||
|
_______, KC_BRID, KC_BRIU, KC_TASK, KC_FILE, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, _______, _______, RGB_TOG, _______, _______, _______, _______,
|
||||||
|
_______, BT_HST1, BT_HST2, BT_HST3, P2P4G, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
|
||||||
|
RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
|
||||||
|
_______, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
|
||||||
|
_______, _______, _______, _______, _______, BAT_LVL, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______,
|
||||||
|
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______),
|
||||||
|
};
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
|
||||||
|
if (!process_record_keychron_common(keycode, record)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user