From 39383894e535796d1d742abe7fe34e14ffabdafd Mon Sep 17 00:00:00 2001 From: yangdigi Date: Wed, 8 Jan 2025 19:19:31 +0800 Subject: [PATCH] Pearly_v2: enable key_override --- keyboards/ydkb/pearly_v2/config.h | 46 +--- .../ydkb/pearly_v2/keymaps/vial/config.h | 3 +- .../ydkb/pearly_v2/keymaps/vial/keymap.c | 14 +- .../ydkb/pearly_v2/keymaps/vial/rules.mk | 2 +- .../ydkb/pearly_v2/keymaps/vial/vial.json | 32 +-- keyboards/ydkb/pearly_v2/led_fn.c | 62 ++++- keyboards/ydkb/pearly_v2/matrix.c | 7 +- keyboards/ydkb/pearly_v2/rgblight.c | 248 ++++++++---------- keyboards/ydkb/pearly_v2/rgblight.h | 34 ++- 9 files changed, 218 insertions(+), 230 deletions(-) diff --git a/keyboards/ydkb/pearly_v2/config.h b/keyboards/ydkb/pearly_v2/config.h index 4905f655760..8b1a513e0d0 100644 --- a/keyboards/ydkb/pearly_v2/config.h +++ b/keyboards/ydkb/pearly_v2/config.h @@ -1,12 +1,10 @@ #pragma once #include "config_common.h" +#include "config_ble51.h" /* USB Device descriptor parameter */ -#define FW_VER_DATE DO69 -#define CONTACT(x,y) x##y -#define CONTACT2(x,y) CONTACT(x,y) -#define FW_VER CONTACT2(VIAL_, FW_VER_DATE) +#define FW_VER_DATE DP18 #define VENDOR_ID 0x9D5B #define PRODUCT_ID 0x2242 #define DEVICE_VER 0x0001 @@ -26,11 +24,8 @@ #define TAPPING_TOGGLE 2 -/* key combination for command */ -#define IS_COMMAND() ( \ - (get_mods() == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) || \ - (get_mods() == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_LCTRL) | MOD_BIT(KC_RSHIFT))) \ -) + +#define DEFAULT_6KRO // macOS's Capslock switching between Chinese and English has compatibility issues with NKRO #define ws2812_PORTREG PORTD #define ws2812_DDRREG DDRD @@ -38,34 +33,6 @@ #define RGBLED_NUM 12 // Number of LEDs #define RGBLIGHT_MODES 14 //less rgblight mode to save some space for vial -/* disable command for default layer */ -#define MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS 0 -#define MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS 0 - - -#if defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__) - #define UCSR1D _SFR_MEM8(0xCB) - #define RTSEN 0 - #define CTSEN 1 - - #define SERIAL_UART_BAUD 76800 - #define SERIAL_UART_DATA UDR1 - #define SERIAL_UART_UBRR ((F_CPU/(8.0*SERIAL_UART_BAUD)-1+0.5)) - #define SERIAL_UART_RXD_VECT USART1_RX_vect - #define SERIAL_UART_TXD_READY (UCSR1A&(1<>8); \ - UCSR1B |= (1<. #include "stdint.h" #include "led.h" #include "via.h" +#include "rgblight.h" #include "command.h" #include "ble51.h" -#include "rgblight.h" -#if 0 -void led_set_user(uint8_t usb_led) +void indicator_task(void) { - if (usb_led & (1<event.pressed) { @@ -45,9 +81,11 @@ void post_process_record_user(uint16_t keycode, keyrecord_t *record) { KC_V, // 2 Output Battery Value KC_L // 3 Lock Mode }; - if (keycode >= USER00) { + // RGB_TOG 0x5cc1, RGB_VAI 0x5cc9, RGB_VAD 0x5cca + // Toggle, M+, M-, HUE+, HUE-, SAT+, SAT-, VAL+, VAL- + if (keycode >= RGB_TOG && keycode <= RGB_VAD) rgblight_action(keycode - RGB_TOG); + else if (keycode >= USER00) { if (keycode < USER04) command_extra(userx_to_command[keycode-USER00]); - else if (keycode <= USER12) rgblight_action(keycode - USER04); } } } \ No newline at end of file diff --git a/keyboards/ydkb/pearly_v2/matrix.c b/keyboards/ydkb/pearly_v2/matrix.c index 6c95107d27e..9177434394b 100644 --- a/keyboards/ydkb/pearly_v2/matrix.c +++ b/keyboards/ydkb/pearly_v2/matrix.c @@ -206,8 +206,9 @@ static void select_row(uint8_t row) bool suspend_wakeup_condition(void) { + uint8_t matrix_keys_down = matrix_scan(); if (BLE51_PowerState >= 10) { - uint8_t matrix_keys_down = matrix_scan(); + //uint8_t matrix_keys_down = matrix_scan(); if (matrix_keys_down == 2) { // K14 F, K17 J if (matrix_debouncing[1][4] == 0xff && matrix_debouncing[1][7] == 0xff) return true; @@ -216,6 +217,9 @@ bool suspend_wakeup_condition(void) return false; } else { + #if 1 + if (matrix_keys_down) return true; + #else select_all_rows(); _delay_us(6); for (uint8_t i = 0; i < MATRIX_COLS; i++) { @@ -223,6 +227,7 @@ bool suspend_wakeup_condition(void) return true; } } + #endif } return false; } diff --git a/keyboards/ydkb/pearly_v2/rgblight.c b/keyboards/ydkb/pearly_v2/rgblight.c index bcfddb63e74..6ae5b1bf47a 100644 --- a/keyboards/ydkb/pearly_v2/rgblight.c +++ b/keyboards/ydkb/pearly_v2/rgblight.c @@ -8,58 +8,49 @@ #include "debug.h" #include "lufa.h" #include "ble51.h" -#include "ble51_task.h" #include "led.h" #include "quantum.h" +#define RGBLIGHT_FADING_ONOFF_ENABLE +#define RGBLIGHT_ALL_DISPLAY_CONNECTION_ENABLE #define rgblight_timer_init() do { DDRD |= (1<<4);} while(0) -#define rgblight_timer_enable() do { PORTD &= ~(1<<4);} while(0) -#define rgblight_timer_disable() do { PORTD |= (1<<4);} while(0) +void rgblight_timer_enable(void) { PORTD &= ~(1<<4);} +void rgblight_timer_disable(void) { PORTD |= (1<<4);} #define rgblight_timer_enabled (~PORTD & (1<<4)) #define RGBLED_TEMP RGBLED_NUM -#define INDICATOR_NUM 0 -struct cRGB rgbled[RGBLED_NUM+INDICATOR_NUM+1]; -struct cRGB *led = &rgbled[INDICATOR_NUM]; +#define RGB_INDICATOR_NUM 0 + +struct cRGB rgbled[RGBLED_NUM+RGB_INDICATOR_NUM+1]; +struct cRGB *led = &rgbled[RGB_INDICATOR_NUM]; //const uint8_t RGBLED_BREATHING_TABLE[128] PROGMEM= {0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,17,18,20,21,23,25,27,29,31,33,35,37,40,42,44,47,49,52,54,57,59,62,65,67,70,73,76,79,82,85,88,90,93,97,100,103,106,109,112,115,118,121,124,127,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173,176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215,218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244,245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255}; const uint8_t RGBLED_BREATHING_TABLE[64] PROGMEM= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255}; -static uint8_t rgb_fading_index = 0; //ʹ RGBLED_BREATHING_TABLE - -rgblight_config_t rgblight_config = {.enable = 1, .mode = 8,.hue = 640, .sat = 255, .val = 255}; +#ifdef RGBLIGHT_FADING_ONOFF_ENABLE +static uint8_t rgb_fading_index = 0; //使用 RGBLED_BREATHING_TABLE +#endif +rgblight_config_t rgblight_config = {.enable = 0, .mode = 3,.hue = 192, .sat = 255, .val = 255}; -uint16_t hue_fix(uint16_t hue) -{ - // hue needs to be 0x100 to 0x3ff - hue += 0x300; - while (hue > 0x3ff) hue -= 0x300; - return hue; -} - -void sethsv(uint16_t hue, uint8_t saturation, uint8_t brightness, struct cRGB *led1) +void sethsv(uint8_t hue, uint8_t saturation, uint8_t brightness, struct cRGB *led1) { /* original code: https://blog.adafruit.com/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/ - when calculating hue, it may below 0. - So I save hue as 0x100 to 0x3ff (256 to 1023) instead of (0 to 767). - And n changes from 0-2 to 1-3. + use 8bit hue, hue16 = hue*3 to reach 0 to 767. */ - uint8_t r, g, b; + //uint8_t r, g, b; uint8_t temp[5]; - // uint8_t n = (hue >> 8) % 3; - hue = hue_fix(hue); - uint8_t n = hue >> 8; - if (n > 3) return; // 0 would be error. just leave it. - uint8_t x = ((((hue & 255) * saturation) >> 8) * brightness) >> 8; + uint16_t hue16 = hue*3; + + uint8_t n = hue16 >> 8; + uint8_t x = ((((hue16 & 255) * saturation) >> 8) * brightness) >> 8; uint8_t s = ((256 - saturation) * brightness) >> 8; - temp[0] = temp[3] = s; - temp[1] = temp[4] = x + s; - temp[2] = brightness - x; - r = temp[n + 1]; - g = temp[n]; - b = temp[n - 1]; - setrgb(r,g,b, led1); + + //temp[n] g r b as struct cRGB of ws2812. save 18B + temp[0] = temp[3] = x + s; + temp[1] = temp[4] = brightness - x; + temp[2] = s; + memcpy(led1, &temp[n], 3); } void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) @@ -84,7 +75,7 @@ void eeconfig_write_rgblight_default(void) { dprintf("eeconfig_write_rgblight_default\n"); - eeconfig_write_rgblight(rgblight_config.raw); //rgblight_config.raw); + eeconfig_write_rgblight(rgblight_config.raw); } void eeconfig_debug_rgblight(void) { @@ -99,7 +90,7 @@ void eeconfig_debug_rgblight(void) { void rgblight_init(void) { dprintf("rgblight_init start!\n"); -#if 1 +#if 1 //54B if (!eeconfig_is_enabled()) { dprintf("rgblight_init eeconfig is not enabled.\n"); eeconfig_init(); @@ -109,9 +100,9 @@ void rgblight_init(void) rgblight_config.raw = eeconfig_read_rgblight(); if (rgblight_config.val == 0) rgblight_config.val = 127; - eeconfig_debug_rgblight(); // display current eeprom values + eeconfig_debug_rgblight(); - rgblight_timer_init(); // setup the timer + rgblight_timer_init(); rgblight_mode(rgblight_config.mode); } @@ -122,18 +113,24 @@ uint8_t limit_value_0to255(int16_t value) { else return value; } -void rgblight_mode(int8_t mode) +void rgblight_mode(uint8_t mode) { // rgb off, new way to save about 60B if (!rgblight_config.enable) { - //rgblight_clear(); - //rgblight_set(); - rgblight_timer_disable(); +#ifdef RGBLIGHT_FADING_ONOFF_ENABLE + // 如果使用fading,关闭时,所有灯需要清0,防止下次启用时先闪亮一下 + rgblight_clear(); + rgblight_set(); rgb_fading_index = 0; +#endif + rgblight_timer_disable(); } else { // rgb on - if (mode < 0) mode = RGBLIGHT_MODES - 1; - else if (mode >= RGBLIGHT_MODES) mode = 0; + #if 1 //less than 0 always be 0, can save 6B + if (mode == 0xff) mode = RGBLIGHT_MODES - 1; + else + #endif + if (mode >= RGBLIGHT_MODES) mode = 0; rgblight_config.mode = mode; dprintf("rgblight mode: %u\n", rgblight_config.mode); @@ -147,7 +144,7 @@ void rgblight_mode(int8_t mode) inline void rgblight_toggle(void) { - rgblight_config.enable ^= 1; + rgblight_config.enable = !rgblight_config.enable; dprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable); rgblight_mode(rgblight_config.mode); } @@ -155,17 +152,18 @@ void rgblight_toggle(void) void rgblight_action(uint8_t action) { - /* 0 toggle - 1 mode- 2 mode+ - 3 hue- 4 hue+ - 5 sat- 6 sat+ - 7 val- 8 val+ + /* QMK: + before - + 0 toggle + 1 mode+ 2 mode- + 3 hue+ 4 hue- + 5 sat+ 6 sat- + 7 val+ 8 val- */ - uint16_t hue = rgblight_config.hue; + uint8_t hue = rgblight_config.hue; uint8_t sat = rgblight_config.sat; uint8_t val = rgblight_config.val; - int8_t increament = 1; - if (action & 1) increament = -1; + int8_t increament = -1; + if (action & 1) increament = 1; if (get_mods() & MOD_BIT(KC_LSHIFT)) { increament *= -1; } @@ -195,7 +193,7 @@ void rgblight_action(uint8_t action) if (action >= 3) rgblight_sethsv(hue, sat, val); } -void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) +void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val) { if (rgblight_config.enable) { sethsv(hue, sat, val, &led[RGBLED_TEMP]); @@ -206,9 +204,9 @@ void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) } } -void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) +void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val) { - hue = hue_fix(hue); + //hue = hue_fix(hue); if (rgblight_config.enable) { #if 0 // will do rgblight_sethsv_noeeprom() in rgblight_task if (rgblight_config.mode == 1) { @@ -225,71 +223,86 @@ void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) eeconfig_write_rgblight(rgblight_config.raw); } +void rgblight_set_all_as(struct cRGB *led1) +{ + for (uint8_t i=0; i<(RGBLED_NUM + RGB_INDICATOR_NUM); i++) { + rgbled[i] = *led1; + } +} + void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { - for (uint8_t i=0; i<(RGBLED_NUM + INDICATOR_NUM); i++) { + for (uint8_t i=0; i<(RGBLED_NUM + RGB_INDICATOR_NUM); i++) { rgbled[i].r = r; rgbled[i].g = g; rgbled[i].b = b; } - //rgblight_set(); + rgblight_set(); } void rgblight_clear(void) { - memset(rgbled, 0, sizeof(rgbled)); + memset(&rgbled[RGB_INDICATOR_NUM], 0, RGBLED_NUM * 3); } #define RGB_FADING_STEP 2 void rgblight_set(void) { +#ifdef RGBLIGHT_FADING_ONOFF_ENABLE + // 淡入淡出,灯少所以不用降低亮度。连接指示时不用降低亮度 bool rgb_fading = (rgb_fading_index < (63 - RGB_FADING_STEP) || kb_idle_times >= 13); if (rgb_fading) { if (kb_idle_times >= 13 && rgb_fading_index >= RGB_FADING_STEP) rgb_fading_index -= RGB_FADING_STEP; // fading in else if (kb_idle_times < 13) rgb_fading_index += RGB_FADING_STEP; // fading out - // õ׵Ƶ뵭 + // 设置底灯淡入淡出 uint8_t *p = (uint8_t *)(&led[0]); for (uint8_t i=0;i 11 + case 10 ... 13: + rgblight_effect_breathing(rgblight_config.mode-9); //(1 to 4) break; + #endif #if RGBLIGHT_MODES > 15 - case 15 ... 20: + case 14 ... 19: rgblight_effect_snake(rgblight_config.mode-14); // 0 to 5 (0 to 2) break; #endif #if RGBLIGHT_MODES > 21 - case 21 ... 23: - rgblight_effect_knight(rgblight_config.mode-19); + case 20 ... 22: + rgblight_effect_knight(rgblight_config.mode-19); // 1 to 3 break; #endif } - } else { - rgblight_timer_disable(); - } } } @@ -306,26 +319,24 @@ void rgblight_effect_breathing(uint8_t interval) } } +static uint8_t current_hue = 0; + void rgblight_effect_rainbow_mood(uint8_t interval) { - static uint16_t current_hue = 0; rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val); - current_hue = hue_fix(current_hue + interval * 3); + current_hue += interval; } void rgblight_effect_rainbow_swirl(uint8_t interval) { - static uint16_t current_hue=0; - uint16_t hue; - uint8_t i; - uint8_t interval2 = interval/2; - if (interval & 1) interval2 *= -1; - for (i=0; i 15 @@ -333,19 +344,17 @@ void rgblight_effect_snake(uint8_t interval) { static int8_t pos = 0 - RGBLIGHT_EFFECT_SNAKE_LENGTH; static uint8_t led_step = 0; - uint8_t i; int8_t increament = 1; uint8_t interval2 = interval/2; // speed if (++led_step > interval2) { led_step = 0; rgblight_clear(); if (interval%2) increament = -1; - for (i=0; i= RGBLED_NUM) target_led -= RGBLED_NUM; - sethsv(rgblight_config.hue+i*50, rgblight_config.sat, rgblight_config.val, &led[target_led]); - //sethsv(rgblight_config.hue+i*50, rgblight_config.sat, rgblight_config.val, &led[(pos+i*increament+RGBLED_NUM)%RGBLED_NUM]); + sethsv(rgblight_config.hue+i*16, rgblight_config.sat, rgblight_config.val, &led[target_led]); } pos += increament; if (pos > RGBLED_NUM) pos = 0; @@ -359,15 +368,12 @@ void rgblight_effect_snake(uint8_t interval) void rgblight_effect_knight(uint8_t interval) { static int8_t pos = RGBLED_NUM - 1; - static uint8_t sled_step = 0; - static uint16_t current_hue=0; - uint8_t i; + static uint8_t time_step = 0; static int8_t increament = 1; - if (++sled_step > interval) { + if (++time_step > interval) { bool need_update = 0; - sled_step = 0; rgblight_clear(); - for (i=0; i= 0){ need_update = 1; @@ -378,9 +384,10 @@ void rgblight_effect_knight(uint8_t interval) pos += increament; if (pos <= 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH || pos >= RGBLED_NUM) { increament *= -1; - current_hue = hue_fix(current_hue + 40); + current_hue = current_hue + 16; sethsv(current_hue, rgblight_config.sat, rgblight_config.val, &led[RGBLED_TEMP]); } + time_step = 0; } } #endif @@ -388,55 +395,26 @@ void rgblight_effect_knight(uint8_t interval) void suspend_power_down_action(void) { PORTB &= ~(1<<2); - rgblight_timer_disable(); //RGB_VCC off + rgblight_timer_disable(); +#ifdef RGBLIGHT_FADING_ONOFF_ENABLE rgb_fading_index = 0; +#endif } void suspend_wakeup_init_action(void) { rgblight_init(); + ledmapu_state_restore(); } void hook_keyboard_loop() { if (BLE51_PowerState > 1) return; static uint16_t rgb_update_timer = 0; - static uint8_t steps = 0; if (timer_elapsed(rgb_update_timer) > 40) { rgb_update_timer = timer_read(); - if (!display_connection_status_check_times) rgblight_task(); - - if ((steps++ & 0b11)) return; - //led_status_task(); - // run every 4*40 = 160ms - PORTB &= ~(1<<2); //caps off - if (ble51_boot_on && (low_battery || display_connection_status_check_times)) { - rgblight_timer_enable(); - rgblight_clear(); - rgb_fading_index = 63; //not fading for all leds. - // 320ms on,320ms off. bt connected: 320ms*3 on, 320ms off. - if (low_battery) { - rgblight_timer_disable(); - if (steps & 0b1000) PORTB |= (1<<2); - } else { - if (bt_connected) { - if (steps & 0b11000) { - PORTB |= (1<<2); - rgblight_setrgb(0,128,0); //green - } - } else { - if (steps & 0b1000) { - PORTB |= (1<<2); - rgblight_setrgb(0,0,128); //blue - } - } - } - ws2812_setleds(rgbled); - } - // capslock - else if (host_keyboard_leds() & (1<