diff --git a/keyboards/keychron/bluetooth/report_buffer.c b/keyboards/keychron/bluetooth/report_buffer.c index 7494c42b975f..7884090aafe1 100644 --- a/keyboards/keychron/bluetooth/report_buffer.c +++ b/keyboards/keychron/bluetooth/report_buffer.c @@ -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) { diff --git a/keyboards/keychron/common/debounce/keychron_debounce.c b/keyboards/keychron/common/debounce/keychron_debounce.c index 6296d85b8171..8a94b4353998 100644 --- a/keyboards/keychron/common/debounce/keychron_debounce.c +++ b/keyboards/keychron/common/debounce/keychron_debounce.c @@ -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; } diff --git a/keyboards/keychron/common/rgb/per_key_rgb.c b/keyboards/keychron/common/rgb/per_key_rgb.c index 8cf18af82484..f16b15550442 100644 --- a/keyboards/keychron/common/rgb/per_key_rgb.c +++ b/keyboards/keychron/common/rgb/per_key_rgb.c @@ -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]; @@ -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; diff --git a/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/config.h b/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/config.h new file mode 100644 index 000000000000..fada1caf5461 --- /dev/null +++ b/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/config.h @@ -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 diff --git a/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/keymap.c b/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/keymap.c new file mode 100644 index 000000000000..48ede17d261c --- /dev/null +++ b/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/keymap.c @@ -0,0 +1,276 @@ +/* Copyright 2023 @ Keychron (https://www.keychron.com) + * Copyright 2026 José A Miranda Velez + * + * 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); + } +} diff --git a/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/rgb_matrix_user.inc b/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/rgb_matrix_user.inc new file mode 100644 index 000000000000..04f83bec77a3 --- /dev/null +++ b/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/rgb_matrix_user.inc @@ -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 diff --git a/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/rules.mk b/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/rules.mk new file mode 100644 index 000000000000..8f95cac9f2eb --- /dev/null +++ b/keyboards/keychron/q5_pro/ansi_encoder/keymaps/rgbj/rules.mk @@ -0,0 +1,3 @@ +VIA_ENABLE = yes +ENCODER_MAP_ENABLE = yes +RGB_MATRIX_CUSTOM_USER = yes diff --git a/keyboards/keychron/q5_pro/config.h b/keyboards/keychron/q5_pro/config.h index da0e1ba5b757..e75c0c7ea32c 100644 --- a/keyboards/keychron/q5_pro/config.h +++ b/keyboards/keychron/q5_pro/config.h @@ -16,6 +16,8 @@ #pragma once +#include "eeconfig_kb.h" + /* turn off effects when suspended */ #define RGB_DISABLE_WHEN_USB_SUSPENDED @@ -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 diff --git a/keyboards/keychron/q5_pro/q5_pro.c b/keyboards/keychron/q5_pro/q5_pro.c index 28c94399d1b4..e029574222b9 100644 --- a/keyboards/keychron/q5_pro/q5_pro.c +++ b/keyboards/keychron/q5_pro/q5_pro.c @@ -283,6 +283,14 @@ void battery_calculte_voltage(uint16_t value) { battery_set_voltage(voltage); } +/* Keymaps that speak the host lighting protocol override this. Stock maps leave it false + * so VIA still owns every other command. */ +__attribute__((weak)) bool q5_pro_host_lighting_rx(uint8_t *data, uint8_t length) { + (void)data; + (void)length; + return false; +} + bool via_command_kb(uint8_t *data, uint8_t length) { switch (data[0]) { #ifdef KC_BLUETOOTH_ENABLE @@ -296,6 +304,11 @@ bool via_command_kb(uint8_t *data, uint8_t length) { break; #endif default: + /* Claim host lighting commands here. If they fall through, VIA echoes the + * packet, and a colour frame (0x24) comes straight back. */ + if (q5_pro_host_lighting_rx(data, length)) { + return true; + } return false; } diff --git a/keyboards/keychron/q5_pro/rules.mk b/keyboards/keychron/q5_pro/rules.mk index f995372f9c4a..db9c4843aa1f 100644 --- a/keyboards/keychron/q5_pro/rules.mk +++ b/keyboards/keychron/q5_pro/rules.mk @@ -1,6 +1,13 @@ +# common.mk compiles Keychron's debounce. Tell QMK not to also link its own. +DEBOUNCE_TYPE = custom + # Enter lower-power sleep mode when on the ChibiOS idle thread OPT_DEFS += -DCORTEX_ENABLE_WFI_IDLE=TRUE OPT_DEFS += -DNO_USB_STARTUP_CHECK -DENABLE_FACTORY_TEST include keyboards/keychron/bluetooth/bluetooth.mk include keyboards/keychron/common/common.mk + +# eeconfig_kb.h includes this header. The language module is optional, but the +# header has to be on the include path or the keyboard does not compile. +VPATH += $(TOP_DIR)/keyboards/keychron/common/language diff --git a/quantum/rgb_matrix/animations/breathing_anim.h b/quantum/rgb_matrix/animations/breathing_anim.h index ac73a7cd839e..05e3257a8f6b 100644 --- a/quantum/rgb_matrix/animations/breathing_anim.h +++ b/quantum/rgb_matrix/animations/breathing_anim.h @@ -6,7 +6,7 @@ bool BREATHING(effect_params_t* params) { RGB_MATRIX_USE_LIMITS(led_min, led_max); HSV hsv = rgb_matrix_config.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); hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v); RGB rgb = rgb_matrix_hsv_to_rgb(hsv); for (uint8_t i = led_min; i < led_max; i++) { diff --git a/quantum/rgb_matrix/animations/flower_blooming_anim.h b/quantum/rgb_matrix/animations/flower_blooming_anim.h index 135bff7e7096..4c383e61188f 100644 --- a/quantum/rgb_matrix/animations/flower_blooming_anim.h +++ b/quantum/rgb_matrix/animations/flower_blooming_anim.h @@ -23,7 +23,7 @@ typedef HSV (*flower_blooming_f)(HSV hsv, uint8_t i, uint8_t time); bool effect_runner_bloom(effect_params_t* params, flower_blooming_f effect_func) { RGB_MATRIX_USE_LIMITS(led_min, led_max); - uint8_t time = scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed / 10, 1)); + uint8_t time = rgb_matrix_anim_time(qadd8(rgb_matrix_config.speed / 10, 1)); for (uint8_t i = led_min; i < led_max; i++) { RGB_MATRIX_TEST_LED_FLAGS(); if (g_led_config.point[i].y > k_rgb_matrix_center.y) { diff --git a/quantum/rgb_matrix/animations/hue_breathing_anim.h b/quantum/rgb_matrix/animations/hue_breathing_anim.h index 0d9c4d651c75..9e86efe4034f 100644 --- a/quantum/rgb_matrix/animations/hue_breathing_anim.h +++ b/quantum/rgb_matrix/animations/hue_breathing_anim.h @@ -8,7 +8,7 @@ bool HUE_BREATHING(effect_params_t* params) { RGB_MATRIX_USE_LIMITS(led_min, led_max); uint8_t huedelta = 12; HSV hsv = rgb_matrix_config.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); hsv.h = hsv.h + scale8(abs8(sin8(time) - 128) * 2, huedelta); RGB rgb = hsv_to_rgb(hsv); for (uint8_t i = led_min; i < led_max; i++) { diff --git a/quantum/rgb_matrix/animations/jellybean_raindrops_anim.h b/quantum/rgb_matrix/animations/jellybean_raindrops_anim.h index 55ae931d4dec..f9975318cdd4 100644 --- a/quantum/rgb_matrix/animations/jellybean_raindrops_anim.h +++ b/quantum/rgb_matrix/animations/jellybean_raindrops_anim.h @@ -13,7 +13,7 @@ bool JELLYBEAN_RAINDROPS(effect_params_t* params) { RGB_MATRIX_USE_LIMITS(led_min, led_max); if (!params->init) { // Change one LED every tick, make sure speed is not 0 - if (scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed, 16)) % 5 == 0) { + if (rgb_matrix_anim_time(qadd8(rgb_matrix_config.speed, 16)) % 5 == 0) { jellybean_raindrops_set_color(random8_max(RGB_MATRIX_LED_COUNT), params); } } else { diff --git a/quantum/rgb_matrix/animations/raindrops_anim.h b/quantum/rgb_matrix/animations/raindrops_anim.h index 57b2219ddd76..acc0b11a42a7 100644 --- a/quantum/rgb_matrix/animations/raindrops_anim.h +++ b/quantum/rgb_matrix/animations/raindrops_anim.h @@ -23,7 +23,7 @@ bool RAINDROPS(effect_params_t* params) { RGB_MATRIX_USE_LIMITS(led_min, led_max); if (!params->init) { // Change one LED every tick, make sure speed is not 0 - if (scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed, 16)) % 10 == 0) { + if (rgb_matrix_anim_time(qadd8(rgb_matrix_config.speed, 16)) % 10 == 0) { raindrops_set_color(random8_max(RGB_MATRIX_LED_COUNT), params); } } else { diff --git a/quantum/rgb_matrix/animations/riverflow_anim.h b/quantum/rgb_matrix/animations/riverflow_anim.h index bd21e5823221..19a96d028b9c 100644 --- a/quantum/rgb_matrix/animations/riverflow_anim.h +++ b/quantum/rgb_matrix/animations/riverflow_anim.h @@ -8,7 +8,8 @@ bool RIVERFLOW(effect_params_t* params) { RGB_MATRIX_USE_LIMITS(led_min, led_max); for (uint8_t i = led_min; i < led_max; i++) { HSV hsv = rgb_matrix_config.hsv; - uint16_t time = scale16by8(g_rgb_timer + (i * 315), rgb_matrix_config.speed / 8); + uint8_t scale = rgb_matrix_config.speed / 8; + uint16_t time = rgb_matrix_anim_time(scale) + scale16by8(i * 315, scale); hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v); RGB rgb = rgb_matrix_hsv_to_rgb(hsv); RGB_MATRIX_TEST_LED_FLAGS(); diff --git a/quantum/rgb_matrix/animations/runners/effect_runner_dx_dy.h b/quantum/rgb_matrix/animations/runners/effect_runner_dx_dy.h index 0469ed1c1ba4..277e9a165d1e 100644 --- a/quantum/rgb_matrix/animations/runners/effect_runner_dx_dy.h +++ b/quantum/rgb_matrix/animations/runners/effect_runner_dx_dy.h @@ -5,7 +5,7 @@ typedef HSV (*dx_dy_f)(HSV hsv, int16_t dx, int16_t dy, uint8_t time); bool effect_runner_dx_dy(effect_params_t* params, dx_dy_f effect_func) { RGB_MATRIX_USE_LIMITS(led_min, led_max); - uint8_t time = scale16by8(g_rgb_timer, rgb_matrix_config.speed / 2); + uint8_t time = rgb_matrix_anim_time(rgb_matrix_config.speed / 2); for (uint8_t i = led_min; i < led_max; i++) { RGB_MATRIX_TEST_LED_FLAGS(); int16_t dx = g_led_config.point[i].x - k_rgb_matrix_center.x; diff --git a/quantum/rgb_matrix/animations/runners/effect_runner_dx_dy_dist.h b/quantum/rgb_matrix/animations/runners/effect_runner_dx_dy_dist.h index a5dd84f6401a..f78d30148792 100644 --- a/quantum/rgb_matrix/animations/runners/effect_runner_dx_dy_dist.h +++ b/quantum/rgb_matrix/animations/runners/effect_runner_dx_dy_dist.h @@ -5,7 +5,7 @@ typedef HSV (*dx_dy_dist_f)(HSV hsv, int16_t dx, int16_t dy, uint8_t dist, uint8 bool effect_runner_dx_dy_dist(effect_params_t* params, dx_dy_dist_f effect_func) { RGB_MATRIX_USE_LIMITS(led_min, led_max); - uint8_t time = scale16by8(g_rgb_timer, rgb_matrix_config.speed / 2); + uint8_t time = rgb_matrix_anim_time(rgb_matrix_config.speed / 2); for (uint8_t i = led_min; i < led_max; i++) { RGB_MATRIX_TEST_LED_FLAGS(); int16_t dx = g_led_config.point[i].x - k_rgb_matrix_center.x; diff --git a/quantum/rgb_matrix/animations/runners/effect_runner_i.h b/quantum/rgb_matrix/animations/runners/effect_runner_i.h index 49147149940c..34362ef356d7 100644 --- a/quantum/rgb_matrix/animations/runners/effect_runner_i.h +++ b/quantum/rgb_matrix/animations/runners/effect_runner_i.h @@ -5,7 +5,7 @@ typedef HSV (*i_f)(HSV hsv, uint8_t i, uint8_t time); bool effect_runner_i(effect_params_t* params, i_f effect_func) { RGB_MATRIX_USE_LIMITS(led_min, led_max); - uint8_t time = scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed / 4, 1)); + uint8_t time = rgb_matrix_anim_time(qadd8(rgb_matrix_config.speed / 4, 1)); for (uint8_t i = led_min; i < led_max; i++) { RGB_MATRIX_TEST_LED_FLAGS(); RGB rgb = rgb_matrix_hsv_to_rgb(effect_func(rgb_matrix_config.hsv, i, time)); diff --git a/quantum/rgb_matrix/animations/runners/effect_runner_reactive.h b/quantum/rgb_matrix/animations/runners/effect_runner_reactive.h index baca2fbc5490..b1eeb8f3cd71 100644 --- a/quantum/rgb_matrix/animations/runners/effect_runner_reactive.h +++ b/quantum/rgb_matrix/animations/runners/effect_runner_reactive.h @@ -7,7 +7,7 @@ typedef HSV (*reactive_f)(HSV hsv, uint16_t offset); bool effect_runner_reactive(effect_params_t* params, reactive_f effect_func) { RGB_MATRIX_USE_LIMITS(led_min, led_max); - uint16_t max_tick = 65535 / qadd8(rgb_matrix_config.speed, 1); + uint16_t max_tick = 255; for (uint8_t i = led_min; i < led_max; i++) { RGB_MATRIX_TEST_LED_FLAGS(); uint16_t tick = max_tick; @@ -19,8 +19,7 @@ bool effect_runner_reactive(effect_params_t* params, reactive_f effect_func) { } } - uint16_t offset = scale16by8(tick, qadd8(rgb_matrix_config.speed, 1)); - RGB rgb = rgb_matrix_hsv_to_rgb(effect_func(rgb_matrix_config.hsv, offset)); + RGB rgb = rgb_matrix_hsv_to_rgb(effect_func(rgb_matrix_config.hsv, tick)); rgb_matrix_region_set_color(params->region, i, rgb.r, rgb.g, rgb.b); } return rgb_matrix_check_finished_leds(led_max); diff --git a/quantum/rgb_matrix/animations/runners/effect_runner_reactive_splash.h b/quantum/rgb_matrix/animations/runners/effect_runner_reactive_splash.h index 8e549e32fcdd..b2d5fdef8b13 100644 --- a/quantum/rgb_matrix/animations/runners/effect_runner_reactive_splash.h +++ b/quantum/rgb_matrix/animations/runners/effect_runner_reactive_splash.h @@ -16,7 +16,7 @@ bool effect_runner_reactive_splash(uint8_t start, effect_params_t* params, react 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)); + uint16_t tick = g_last_hit_tracker.tick[j]; hsv = effect_func(hsv, dx, dy, dist, tick); } hsv.v = scale8(hsv.v, rgb_matrix_config.hsv.v); diff --git a/quantum/rgb_matrix/animations/runners/effect_runner_sin_cos_i.h b/quantum/rgb_matrix/animations/runners/effect_runner_sin_cos_i.h index 27cd9f4d022e..21b71bc1267f 100644 --- a/quantum/rgb_matrix/animations/runners/effect_runner_sin_cos_i.h +++ b/quantum/rgb_matrix/animations/runners/effect_runner_sin_cos_i.h @@ -5,7 +5,7 @@ typedef HSV (*sin_cos_i_f)(HSV hsv, int8_t sin, int8_t cos, uint8_t i, uint8_t t bool effect_runner_sin_cos_i(effect_params_t* params, sin_cos_i_f effect_func) { RGB_MATRIX_USE_LIMITS(led_min, led_max); - uint16_t time = scale16by8(g_rgb_timer, rgb_matrix_config.speed / 4); + uint16_t time = rgb_matrix_anim_time(rgb_matrix_config.speed / 4); int8_t cos_value = cos8(time) - 128; int8_t sin_value = sin8(time) - 128; for (uint8_t i = led_min; i < led_max; i++) { diff --git a/quantum/rgb_matrix/animations/solid_reactive_anim.h b/quantum/rgb_matrix/animations/solid_reactive_anim.h index edf604135031..412bfe5e3314 100644 --- a/quantum/rgb_matrix/animations/solid_reactive_anim.h +++ b/quantum/rgb_matrix/animations/solid_reactive_anim.h @@ -5,7 +5,7 @@ RGB_MATRIX_EFFECT(SOLID_REACTIVE) static HSV SOLID_REACTIVE_math(HSV hsv, uint16_t offset) { # 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.h += qsub8(130, offset); return hsv; diff --git a/quantum/rgb_matrix/animations/solid_reactive_cross.h b/quantum/rgb_matrix/animations/solid_reactive_cross.h index a18d6b03dd6b..f46b5d2f1046 100644 --- a/quantum/rgb_matrix/animations/solid_reactive_cross.h +++ b/quantum/rgb_matrix/animations/solid_reactive_cross.h @@ -20,7 +20,7 @@ static HSV SOLID_REACTIVE_CROSS_math(HSV hsv, int16_t dx, int16_t dy, uint8_t di effect += dx > dy ? dy : dx; 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; diff --git a/quantum/rgb_matrix/animations/solid_reactive_nexus.h b/quantum/rgb_matrix/animations/solid_reactive_nexus.h index 53cc008616de..46ccc23e8987 100644 --- a/quantum/rgb_matrix/animations/solid_reactive_nexus.h +++ b/quantum/rgb_matrix/animations/solid_reactive_nexus.h @@ -17,7 +17,7 @@ static HSV SOLID_REACTIVE_NEXUS_math(HSV hsv, int16_t dx, int16_t dy, uint8_t di if (dist > 72) effect = 255; if ((dx > 8 || dx < -8) && (dy > 8 || dy < -8)) effect = 255; # ifdef RGB_MATRIX_SOLID_REACTIVE_GRADIENT_MODE - hsv.h = scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed, 8) >> 4) + dy / 4; + hsv.h = rgb_matrix_anim_time(qadd8(rgb_matrix_config.speed, 8) >> 4) + dy / 4; # else hsv.h = rgb_matrix_config.hsv.h + dy / 4; # endif diff --git a/quantum/rgb_matrix/animations/solid_reactive_simple_anim.h b/quantum/rgb_matrix/animations/solid_reactive_simple_anim.h index 7f4e48747a6c..909f3ecf9bf4 100644 --- a/quantum/rgb_matrix/animations/solid_reactive_simple_anim.h +++ b/quantum/rgb_matrix/animations/solid_reactive_simple_anim.h @@ -5,7 +5,7 @@ RGB_MATRIX_EFFECT(SOLID_REACTIVE_SIMPLE) static HSV SOLID_REACTIVE_SIMPLE_math(HSV hsv, uint16_t offset) { # 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 = scale8(255 - offset, hsv.v); return hsv; diff --git a/quantum/rgb_matrix/animations/solid_reactive_wide.h b/quantum/rgb_matrix/animations/solid_reactive_wide.h index feca1266483d..c71e66156931 100644 --- a/quantum/rgb_matrix/animations/solid_reactive_wide.h +++ b/quantum/rgb_matrix/animations/solid_reactive_wide.h @@ -15,7 +15,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; diff --git a/quantum/rgb_matrix/animations/starlight_anim.h b/quantum/rgb_matrix/animations/starlight_anim.h index db94bb3f0b3d..64adcf5f8909 100644 --- a/quantum/rgb_matrix/animations/starlight_anim.h +++ b/quantum/rgb_matrix/animations/starlight_anim.h @@ -3,7 +3,7 @@ RGB_MATRIX_EFFECT(STARLIGHT) # ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS void set_starlight_color(int i, effect_params_t* params) { - uint16_t time = scale16by8(g_rgb_timer, rgb_matrix_config.speed / 8); + uint16_t time = rgb_matrix_anim_time(rgb_matrix_config.speed / 8); HSV hsv = rgb_matrix_config.hsv; hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v); RGB rgb = hsv_to_rgb(hsv); @@ -12,7 +12,7 @@ void set_starlight_color(int i, effect_params_t* params) { bool STARLIGHT(effect_params_t* params) { if (!params->init) { - if (scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed, 5)) % 5 == 0) { + if (rgb_matrix_anim_time(qadd8(rgb_matrix_config.speed, 5)) % 5 == 0) { int rand_led = rand() % RGB_MATRIX_LED_COUNT; set_starlight_color(rand_led, params); } diff --git a/quantum/rgb_matrix/animations/starlight_dual_hue_anim.h b/quantum/rgb_matrix/animations/starlight_dual_hue_anim.h index d9ccdfb7b093..9ab8e39d249b 100644 --- a/quantum/rgb_matrix/animations/starlight_dual_hue_anim.h +++ b/quantum/rgb_matrix/animations/starlight_dual_hue_anim.h @@ -3,7 +3,7 @@ RGB_MATRIX_EFFECT(STARLIGHT_DUAL_HUE) # ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS void set_starlight_dual_hue_color(int i, effect_params_t* params) { - uint16_t time = scale16by8(g_rgb_timer, rgb_matrix_config.speed / 8); + uint16_t time = rgb_matrix_anim_time(rgb_matrix_config.speed / 8); HSV hsv = rgb_matrix_config.hsv; hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v); hsv.h = hsv.h + (rand() % (30 + 1 - -30) + -30); @@ -13,7 +13,7 @@ void set_starlight_dual_hue_color(int i, effect_params_t* params) { bool STARLIGHT_DUAL_HUE(effect_params_t* params) { if (!params->init) { - if (scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed, 5)) % 5 == 0) { + if (rgb_matrix_anim_time(qadd8(rgb_matrix_config.speed, 5)) % 5 == 0) { int rand_led = rand() % RGB_MATRIX_LED_COUNT; set_starlight_dual_hue_color(rand_led, params); } diff --git a/quantum/rgb_matrix/animations/starlight_dual_sat_anim.h b/quantum/rgb_matrix/animations/starlight_dual_sat_anim.h index b01229b09abf..ff6250e24cb9 100644 --- a/quantum/rgb_matrix/animations/starlight_dual_sat_anim.h +++ b/quantum/rgb_matrix/animations/starlight_dual_sat_anim.h @@ -3,7 +3,7 @@ RGB_MATRIX_EFFECT(STARLIGHT_DUAL_SAT) # ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS void set_starlight_dual_sat_color(int i, effect_params_t* params) { - uint16_t time = scale16by8(g_rgb_timer, rgb_matrix_config.speed / 8); + uint16_t time = rgb_matrix_anim_time(rgb_matrix_config.speed / 8); HSV hsv = rgb_matrix_config.hsv; hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v); hsv.s = hsv.s + (rand() % (30 + 1 - -30) + -30); @@ -13,7 +13,7 @@ void set_starlight_dual_sat_color(int i, effect_params_t* params) { bool STARLIGHT_DUAL_SAT(effect_params_t* params) { if (!params->init) { - if (scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed, 5)) % 5 == 0) { + if (rgb_matrix_anim_time(qadd8(rgb_matrix_config.speed, 5)) % 5 == 0) { int rand_led = rand() % RGB_MATRIX_LED_COUNT; set_starlight_dual_sat_color(rand_led, params); } diff --git a/quantum/rgb_matrix/rgb_matrix.c b/quantum/rgb_matrix/rgb_matrix.c index 4e8d2bacb567..43279a3b0d5b 100644 --- a/quantum/rgb_matrix/rgb_matrix.c +++ b/quantum/rgb_matrix/rgb_matrix.c @@ -66,6 +66,26 @@ __attribute__((weak)) RGB rgb_matrix_hsv_to_rgb(HSV hsv) { // globals rgb_config_t rgb_matrix_config; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr uint32_t g_rgb_timer; + +uint16_t rgb_matrix_anim_time(uint8_t scale) { + static uint32_t accum; + static uint32_t last_ms; + static uint32_t last_stamp; + + /* One phase for the running effect. The next frame adds real elapsed + * time times the current speed, so a slider does not jump the pattern. */ + if (g_rgb_timer != last_stamp) { + uint32_t now = sync_timer_read32(); + uint32_t delta = now - last_ms; + if (last_ms != 0 && delta <= 250) { + accum += delta * scale; + } + last_ms = now; + last_stamp = g_rgb_timer; + } + return (uint16_t)(accum >> 8); +} + #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS uint8_t g_rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}}; #endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS @@ -297,12 +317,16 @@ static void rgb_task_timers(void) { // Update double buffer last hit timers #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED uint8_t count = last_hit_buffer.count; + /* Age is stored already scaled by speed. A later speed change only + * affects the next step, so a splash does not jump. */ + uint16_t delta16 = deltaTime > 250 ? 16 : (uint16_t)deltaTime; + uint16_t step = scale16by8(delta16, qadd8(rgb_matrix_config.speed, 1)); for (uint8_t i = 0; i < count; ++i) { - if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) { + if (step > (uint16_t)(UINT16_MAX - last_hit_buffer.tick[i])) { last_hit_buffer.count--; continue; } - last_hit_buffer.tick[i] += deltaTime; + last_hit_buffer.tick[i] += step; } #endif // RGB_MATRIX_KEYREACTIVE_ENABLED } @@ -332,10 +356,6 @@ static void rgb_task_render(uint8_t effect) { rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable); if (effect != rgb_last_effect) { memset(rgb_regions, 0, RGB_MATRIX_LED_COUNT); - rgb_config_t rgb_cfg; - eeprom_read_block(&rgb_cfg, EECONFIG_RGB_MATRIX, sizeof(rgb_cfg)); - rgb_matrix_config.hsv = rgb_cfg.hsv; - rgb_matrix_config.speed = rgb_cfg.speed; } if (rgb_effect_params.flags != rgb_matrix_config.flags) { @@ -608,6 +628,9 @@ void rgb_matrix_disable(void) { } void rgb_matrix_disable_noeeprom(void) { + if (rgb_matrix_effect_locked(0)) { + return; + } if (rgb_matrix_config.enable) rgb_task_state = STARTING; rgb_matrix_config.enable = 0; } @@ -616,22 +639,34 @@ uint8_t rgb_matrix_is_enabled(void) { return rgb_matrix_config.enable; } +__attribute__((weak)) bool rgb_matrix_effect_locked(uint8_t mode) { + (void)mode; + return false; +} + void rgb_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) { if (!rgb_matrix_config.enable) { return; } - if (mode < 1) { - rgb_matrix_config.mode = 1; - } else if (mode >= RGB_MATRIX_EFFECT_MAX) { - rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1; - } else { - rgb_matrix_config.mode = mode; + uint8_t next = mode; + if (next < 1) { + next = 1; + } else if (next >= RGB_MATRIX_EFFECT_MAX) { + next = RGB_MATRIX_EFFECT_MAX - 1; + } + if (rgb_matrix_effect_locked(next)) { + return; + } + /* Same effect again (the Launcher sends it with every slider tick) + * must not restart the animation. */ + if (rgb_matrix_config.mode != next) { + rgb_matrix_config.mode = next; + rgb_task_state = STARTING; } - rgb_task_state = STARTING; if (write_to_eeprom) { - uint8_t mode = (rgb_matrix_config.mode << 2) | rgb_matrix_config.enable; - eeprom_write_byte((uint8_t*)EECONFIG_RGB_MATRIX, mode); + uint8_t packed = (rgb_matrix_config.mode << 2) | rgb_matrix_config.enable; + eeprom_write_byte((uint8_t*)EECONFIG_RGB_MATRIX, packed); } dprintf("rgb matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.mode); } diff --git a/quantum/rgb_matrix/rgb_matrix.h b/quantum/rgb_matrix/rgb_matrix.h index 824859815985..37619a56f33d 100644 --- a/quantum/rgb_matrix/rgb_matrix.h +++ b/quantum/rgb_matrix/rgb_matrix.h @@ -328,6 +328,14 @@ extern const rgb_matrix_driver_t rgb_matrix_driver; extern rgb_config_t rgb_matrix_config; extern uint32_t g_rgb_timer; + +/* Phase that advances with speed. Changing speed keeps the current frame + * and only changes how fast the next frames move. */ +uint16_t rgb_matrix_anim_time(uint8_t scale); + +/* Keymaps can hold the current effect. Return true to refuse `mode`. */ +bool rgb_matrix_effect_locked(uint8_t mode); + extern led_config_t g_led_config; #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED extern last_hit_t g_last_hit_tracker;