Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions keyboards/keychron/bluetooth/report_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ void report_buffer_task(void) {
if (bluetooth_get_state() == BLUETOOTH_CONNECTED && (!report_buffer_is_empty() || retry) && report_buffer_next_inverval()) {
bool pending_data = false;

/* Send each report once. Arming a blind retry here repeated the
* key-down up to 30 times before the key-up could leave the queue,
* so the computer still had the key held and typed it again. A full
* module fifo still sets retry from the ack handler. */
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();
}
pending_data = true;
}
} else {
if (sync_timer_elapsed32(retry_time_buffer) > 7) {
Expand Down
4 changes: 3 additions & 1 deletion keyboards/keychron/common/debounce/keychron_debounce.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ void debounce_set(uint8_t new_debounce_type, uint8_t time, bool force);
*/

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);
if (debounce_func.debounce) {
return debounce_func.debounce(raw, cooked, num_rows, changed);
}

return false;
}
Expand Down
4 changes: 2 additions & 2 deletions keyboards/keychron/common/rgb/per_key_rgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool per_key_rgb_solid(effect_params_t *params) {
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);
uint16_t time = rgb_matrix_anim_time(rgb_matrix_config.speed / 8);

for (uint8_t i = led_min; i < led_max; i++) {
hsv = per_key_led[i];
Expand Down Expand Up @@ -116,7 +116,7 @@ static HSV solid_reactive_wide_math(HSV hsv, int16_t dx, int16_t dy, uint8_t dis
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);
hsv.h = rgb_matrix_anim_time(qadd8(rgb_matrix_config.speed, 8) >> 4);
# endif
hsv.v = qadd8(hsv.v, 255 - effect);
return hsv;
Expand Down
19 changes: 19 additions & 0 deletions keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

/* One tap was coming out as two of the same character, sometimes right
* away ("Thiss") and sometimes after later letters ("a acat", "cat a").
*
* Eager debounce reports the press on the first sample, then ignores that
* key only until the window ends. A later scan that sees the key again is
* a new character. A longer eager window just moves that hole.
*
* Defer waits until the contact has stayed in the new state for this whole
* window. A one-scan glitch never becomes a letter. 46ms still lets a
* normal tap through, including holding a key for OS repeat. */
#define DEFAULT_DEBOUNCE_TYPE DEBOUNCE_SYM_DEFER_PER_KEY
#define DEBOUNCE 46

/* After this long with no host colour frames, leave host mode and restore
* the onboard effect from EEPROM. Long enough that a slow render tick does
* not bounce back to hardware mid-effect. */
#define HOST_LIGHTING_TIMEOUT_MS 3000
276 changes: 276 additions & 0 deletions keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/keymap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
/* Copyright 2023 @ Keychron (https://www.keychron.com)
* Copyright 2026 José A Miranda Velez <admin@rgbjunkie.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.
*/

#include QMK_KEYBOARD_H
#include "eeconfig_kb.h"
#include "keychron_debounce.h"
#include "raw_hid.h"
#include "transport.h"

extern void debounce_set(uint8_t new_debounce_type, uint8_t time, bool force);

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_102_ansi(
KC_ESC, KC_BRID, KC_BRIU, KC_MCTL, KC_LPAD, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_DEL, KC_F13, KC_F14, KC_F15, KC_MUTE,
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_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_PGDN, KC_P7, KC_P8, KC_P9, KC_PPLS,
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_HOME, KC_P4, KC_P5, KC_P6,
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_PENT,
KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, MO(MAC_FN), KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT, KC_P0, KC_PDOT ),
[MAC_FN] = LAYOUT_102_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, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
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_102_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_DEL, KC_F13, KC_F14, KC_F15, KC_MUTE,
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_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_PGDN, KC_P7, KC_P8, KC_P9, KC_PPLS,
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_HOME, KC_P4, KC_P5, KC_P6,
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_PENT,
KC_LCTL, KC_LWIN, KC_LALT, KC_SPC, KC_RALT, MO(WIN_FN), KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT, KC_P0, KC_PDOT ),
[WIN_FN] = LAYOUT_102_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, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
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, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ ),
};

#if defined(ENCODER_MAP_ENABLE)
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][2] = {
[MAC_BASE] = {ENCODER_CCW_CW(KC_VOLD, KC_VOLU)},
[MAC_FN] = {ENCODER_CCW_CW(RGB_VAD, RGB_VAI)},
[WIN_BASE] = {ENCODER_CCW_CW(KC_VOLD, KC_VOLU)},
[WIN_FN] = {ENCODER_CCW_CW(RGB_VAD, RGB_VAI)},
};
#endif // ENCODER_MAP_ENABLE
// clang-format on

/* Host lighting commands. The report id is stripped before we see the packet,
* so byte 0 is the command. Replies put the command back in byte 0. The host
* read includes the report id, so our byte 0 lands in the host's byte 1. */
enum {
HOST_GET_QMK_VERSION = 0x21,
HOST_GET_PROTOCOL_VERSION = 0x22,
HOST_GET_UNIQUE_IDENTIFIER = 0x23,
HOST_STREAM_RGB_DATA = 0x24,
HOST_MODE_ENABLE = 0x25,
HOST_MODE_DISABLE = 0x26,
HOST_GET_TOTAL_LEDS = 0x27,
HOST_GET_FIRMWARE_TYPE = 0x28,
};

static uint8_t host_packet[32];
static bool host_lighting_active = false;
static uint32_t host_lighting_last_activity = 0;

#ifndef HOST_LIGHTING_TIMEOUT_MS
# define HOST_LIGHTING_TIMEOUT_MS 3000
#endif

static void host_reply(uint8_t command, uint8_t b1, uint8_t b2, uint8_t b3) {
memset(host_packet, 0, sizeof(host_packet));
host_packet[0] = command;
host_packet[1] = b1;
host_packet[2] = b2;
host_packet[3] = b3;
raw_hid_send(host_packet, sizeof(host_packet));
}

static void host_lighting_leave(void) {
if (!host_lighting_active) {
return;
}
host_lighting_active = false;
rgb_matrix_reload_from_eeprom();
}

/* Switch to the host effect and clear the last onboard frame. Without the
* blackout, HOSTLIGHT keeps flushing the previous animation until the first
* colour packets arrive, so the board flickers between the two painters. */
static void host_lighting_enter(void) {
bool already = host_lighting_active && rgb_matrix_is_enabled() && rgb_matrix_get_mode() == RGB_MATRIX_CUSTOM_HOSTLIGHT;

host_lighting_active = true;
host_lighting_last_activity = timer_read32();
rgb_matrix_enable_noeeprom();
rgb_matrix_mode_noeeprom(RGB_MATRIX_CUSTOM_HOSTLIGHT);
if (!already) {
rgb_matrix_set_color_all(0, 0, 0);
}
}

/* Num Lock is LED 32, Caps Lock is LED 55. While that lock is on, both this
* packet and the indicator task must write the same white. If the packet
* writes the effect colour instead, it lands after the indicator and the key
* flickers. */
static bool host_lock_led_is_lit(uint8_t led) {
#if defined(NUM_LOCK_INDEX)
if (led == NUM_LOCK_INDEX && host_keyboard_led_state().num_lock) {
return true;
}
#endif
#if defined(CAPS_LOCK_INDEX) && !defined(DIM_CAPS_LOCK)
if (led == CAPS_LOCK_INDEX && host_keyboard_led_state().caps_lock) {
return true;
}
#endif
#if defined(SCROLL_LOCK_INDEX)
if (led == SCROLL_LOCK_INDEX && host_keyboard_led_state().scroll_lock) {
return true;
}
#endif
return false;
}

static void host_led_streaming(uint8_t *data) {
uint8_t index = data[1];
uint8_t numberofleds = data[2];

/* Stay silent on a bad frame. A reply uses the colour command, and the
* next handshake can read that leftover report as its answer. */
if ((uint16_t)index + numberofleds > RGB_MATRIX_LED_COUNT || numberofleds >= 10) {
return;
}

for (uint8_t i = 0; i < numberofleds; i++) {
uint8_t led = index + i;
uint8_t offset = (i * 3) + 3;
if (host_lock_led_is_lit(led)) {
rgb_matrix_set_color(led, 255, 255, 255);
} else {
rgb_matrix_set_color(led, data[offset], data[offset + 1], data[offset + 2]);
}
}
}

bool q5_pro_host_lighting_rx(uint8_t *data, uint8_t length) {
(void)length;

switch (data[0]) {
case HOST_GET_QMK_VERSION:
host_reply(HOST_GET_QMK_VERSION, 0, 26, 0);
return true;
case HOST_GET_PROTOCOL_VERSION:
host_reply(HOST_GET_PROTOCOL_VERSION, 1, 0, 6);
return true;
case HOST_GET_UNIQUE_IDENTIFIER:
host_reply(HOST_GET_UNIQUE_IDENTIFIER, 0, 0, 0);
return true;
case HOST_STREAM_RGB_DATA:
/* No reply. A reply is just our own frame echoed back, and the host
* would answer that with another frame. Enter host mode on the
* first colour frame too: if enable is late, the onboard effect
* would keep repainting over these pixels and the board flickers. */
host_lighting_enter();
host_led_streaming(data);
return true;
case HOST_MODE_ENABLE:
host_lighting_enter();
return true;
case HOST_MODE_DISABLE:
host_lighting_leave();
return true;
case HOST_GET_TOTAL_LEDS:
host_reply(HOST_GET_TOTAL_LEDS, RGB_MATRIX_LED_COUNT, 0, 0);
return true;
case HOST_GET_FIRMWARE_TYPE:
/* 2 is the VIA firmware type the host already accepts. */
host_reply(HOST_GET_FIRMWARE_TYPE, 2, 0, 0);
return true;
default:
return false;
}
}

/* Paint the lock keys white on every frame, including while the host is
* streaming. The packet writes the same white, so the key stays solid. */
void os_state_indicate(void) {
#if defined(NUM_LOCK_INDEX)
if (host_keyboard_led_state().num_lock) {
rgb_matrix_set_color(NUM_LOCK_INDEX, 255, 255, 255);
}
#endif
#if defined(CAPS_LOCK_INDEX)
if (host_keyboard_led_state().caps_lock) {
# if defined(DIM_CAPS_LOCK)
rgb_matrix_set_color(CAPS_LOCK_INDEX, 0, 0, 0);
# else
rgb_matrix_set_color(CAPS_LOCK_INDEX, 255, 255, 255);
# endif
}
#endif
#if defined(SCROLL_LOCK_INDEX)
if (host_keyboard_led_state().scroll_lock) {
rgb_matrix_set_color(SCROLL_LOCK_INDEX, 255, 255, 255);
}
#endif
}

/* While a host is streaming, keep its effect selected. The lighting keys and
* the Launcher would otherwise load an onboard effect and the two painters
* flicker against each other. */
bool rgb_matrix_effect_locked(uint8_t mode) {
return host_lighting_active && mode != RGB_MATRIX_CUSTOM_HOSTLIGHT;
}

/* The running debounce is stored on the keyboard. A flash does not replace
* it, and a stored time of 0 turns debounce off. Boot always puts this
* keymap's defer window back, including over an eager window saved earlier. */
void keyboard_post_init_user(void) {
uint8_t *cfg = (uint8_t *)EECONFIG_BASE_DYNAMIC_DEBOUNCE;

debounce_set(DEBOUNCE_SYM_DEFER_PER_KEY, DEBOUNCE, true);
eeprom_update_byte(cfg, DEBOUNCE_SYM_DEFER_PER_KEY);
eeprom_update_byte(cfg + 1, DEBOUNCE);
}

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
if (host_lighting_active && IS_RGB_KEYCODE(keycode)) {
return false;
}
/* Wireless NKRO scrambles reports on this board. Keep Bluetooth on 6KRO
* even if Fn+N is pressed. USB NKRO is unchanged. */
if (record->event.pressed && get_transport() == TRANSPORT_BLUETOOTH) {
switch (keycode) {
case QK_MAGIC_TOGGLE_NKRO:
case QK_MAGIC_NKRO_ON:
return false;
}
}
return true;
}

void housekeeping_task_user(void) {
if (!host_lighting_active) {
return;
}
/* App quit, crash, or sleep: colour frames stop, so give the onboard
* effect back instead of freezing on the last host frame. */
if (timer_elapsed32(host_lighting_last_activity) > HOST_LIGHTING_TIMEOUT_MS) {
host_lighting_leave();
return;
}
if (!rgb_matrix_is_enabled() || rgb_matrix_get_mode() != RGB_MATRIX_CUSTOM_HOSTLIGHT) {
rgb_matrix_enable_noeeprom();
rgb_matrix_mode_noeeprom(RGB_MATRIX_CUSTOM_HOSTLIGHT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RGB_MATRIX_EFFECT(HOSTLIGHT)
#ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS

/* Host frames call rgb_matrix_set_color() themselves. This effect must not
* repaint, or the next flush replaces the frame that just arrived. */
bool HOSTLIGHT(effect_params_t *params) {
RGB_MATRIX_USE_LIMITS(led_min, led_max);
return rgb_matrix_check_finished_leds(led_max);
}

#endif // RGB_MATRIX_CUSTOM_EFFECT_IMPLS
3 changes: 3 additions & 0 deletions keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VIA_ENABLE = yes
ENCODER_MAP_ENABLE = yes
RGB_MATRIX_CUSTOM_USER = yes
5 changes: 2 additions & 3 deletions keyboards/keychron/q5_pro/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#pragma once

#include "eeconfig_kb.h"

/* turn off effects when suspended */
#define RGB_DISABLE_WHEN_USB_SUSPENDED

Expand Down Expand Up @@ -62,9 +64,6 @@
/* Keep USB connection in blueooth mode */
# define KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE

/* Enable bluetooth NKRO */
# define BLUETOOTH_NKRO_ENABLE

/* Raw hid command for factory test and bluetooth DFU */
# define RAW_HID_CMD 0xAA ... 0xAB
#else
Expand Down
Loading