diff --git a/keyboards/ydkb/ble660c_980c/config.h b/keyboards/ydkb/ble660c_980c/config.h index 2ef9008b286..5ccdf66a2c8 100644 --- a/keyboards/ydkb/ble660c_980c/config.h +++ b/keyboards/ydkb/ble660c_980c/config.h @@ -1,11 +1,10 @@ #pragma once #include "config_common.h" +#include "config_ble51.h" /* USB Device descriptor parameter */ -#define FW_VER QMK_DM4S -#define FW_VER_VIA VIA_DM4S -#define FW_VER_VIAL VIAL_DMCL +#define FW_VER_DATE DO5U #define VENDOR_ID 0x9D5B #define PRODUCT_ID 0x19C1 #define DEVICE_VER 0x0001 @@ -13,79 +12,24 @@ #define PRODUCT BLE660C/BLE980C (FW_VER) +/* matrix size */ #define MATRIX_ROWS 7 #define MATRIX_COLS 16 - - -#define TAPPING_TOGGLE 2 - -#define TAPPING_TERM 200 -#define IGNORE_MOD_TAP_INTERRUPT // this makes it possible to do rolling combos (zx) with keys that convert to other keys on hold (z becomes ctrl when you hold it, and when this option isn't enabled, z rapidly followed by x actually sends Ctrl-x. That's bad.) - - -/* 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))) \ -) -/* disable command for default layer */ -#define MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS 0 -#define MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS 0 - -/* fix space cadet rollover issue */ -#define DISABLE_SPACE_CADET_ROLLOVER - -#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<. extern bool is_980c; +void led_all_off(void) { + DDRB |= (1<<6 |1<<5); + if (is_980c) { + PORTB &= ~(1<<6 |1<<5); + DDRD |= (1<<7); + PORTD &= ~(1<<7); + } else { + PORTB |= (1<<6 |1<<5); + } +} + +#if 0 void led_set_user(uint8_t usb_led) { + if (is_980c) { + PORTD &= ~(1<<7); + PORTB &= ~(1<<6 | 1<<5); + } else { + PORTB |= (1<<6 | 1<<5); + } if (usb_led & (1<event.pressed) { - switch (keycode) { - case USER00: - command_extra(KC_U); - break; - case USER01: //RESET - command_extra(KC_B); - break; - case USER02: //BATTERY LEVEL - command_extra(KC_V); - break; - case USER03: //LOCK MODE - command_extra(KC_L); - break; - } + static const uint8_t userx_to_command[4] = { + KC_U, // 0 Host Switch + KC_B, // 1 Reset + KC_V, // 2 Output Battery Value + KC_L // 3 Lock Mode + }; + if (keycode >= USER00 && keycode < USER04) command_extra(userx_to_command[keycode-USER00]); } } \ No newline at end of file diff --git a/keyboards/ydkb/ble660c_980c/matrix.c b/keyboards/ydkb/ble660c_980c/matrix.c index 43e17a7d789..52ab3f891eb 100644 --- a/keyboards/ydkb/ble660c_980c/matrix.c +++ b/keyboards/ydkb/ble660c_980c/matrix.c @@ -1,5 +1,5 @@ /* -Copyright 2011 Jun Wako +Copyright 2023 YANG 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 @@ -38,61 +38,82 @@ along with this program. If not, see . #include "switch_board.h" +// 这个时间即使加到800,980c也无法正常。但是980c加入22uf的电容后,200也可以。 +static uint8_t matrix_start_delay_timer = 200; // matrix state buffer(1:on, 0:off) static matrix_row_t *matrix; static matrix_row_t *matrix_prev; -static matrix_row_t _matrix0[MATRIX_ROWS]; -static matrix_row_t _matrix1[MATRIX_ROWS]; +static matrix_row_t _matrix0[MATRIX_ROWS] = {0}; +static matrix_row_t _matrix1[MATRIX_ROWS] = {0}; static uint8_t matrix_keys_down = 0; + +static uint16_t matrix_scan_timestamp = 0; static uint8_t fc_matrix_rows; +struct { + uint8_t rows; + uint8_t final_col; +} hhkb_matrix; + +void matrix_scan_user(void) {} +__attribute__ ((weak)) +void matrix_scan_kb(void) { + matrix_scan_user(); + hook_keyboard_loop(); +} + static uint8_t wake_scan = 0; bool is_980c = 0; -bool is_ver22 = 0; - void hook_early_init() { +#if 0 // 使用PF6判断 980C,舍弃此方法 DDRF &= ~(1<= 4) { + if (BT_POWERED == 0) { if (++sleep_timer < 80) return false; else sleep_timer = 0; } @@ -244,7 +281,7 @@ bool suspend_wakeup_condition(void) wake_scan = 1; matrix_scan(); //tp1685 fix - if (BLE51_PowerState >= 4) { + if (BT_POWERED == 0) { matrix_scan(); } if (matrix_scan() == 100) { @@ -257,70 +294,81 @@ bool suspend_wakeup_condition(void) void suspend_power_down_action(void) { KEY_POWER_OFF(); - if (is_980c) { - PORTD &= ~(1<<7); - PORTB &= ~(1<<6 | 1<<5); // turn off all leds. - } else { - PORTB |= (1<<6 | 1<<5); // turn off all leds. - } + led_all_off(); } void suspend_wakeup_init_action(void) { - if (BLE51_PowerState >=4) { - if (is_980c) { - PORTD |= (1<<7); - PORTB |= (1<<6 |1<<5); - } else { - PORTB &= ~(1<<6 |1<<5); - } - _delay_ms(400); + if (BT_POWERED == 0) { + matrix_start_delay_timer = 200; // matrix board works delay display_connection_status_check_times = 1; } } -void ble51_task_user(void) +void hook_keyboard_loop() { - static uint8_t ble51_task_steps = 0; + if (BLE51_PowerState > 1) return; + static uint8_t steps = 0; static uint16_t battery_timer = 0; - if (timer_elapsed(battery_timer) > 150) { + if (timer_elapsed(battery_timer) > 160) { battery_timer = timer_read(); - ble51_task_steps++; - if (low_battery) { - if (ble51_task_steps > 3) { - ble51_task_steps = (low_battery == 1)? 3:0; //value 1 is extremely low battery - if (PINE&(1<= 5) || ble51_task_steps >= 11 ) { - ble51_task_steps = 0; + + if (host_keyboard_leds() & (1< + +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 . +*/ + +#include +#include +#include +#include "backlight.h" +#include "timer.h" + +extern bool no_backlight; + +void backlight_init_ports(void) { + DDRD &= ~(1<<0); + PORTD |= (1<<0); +} + +void backlight_task(void) { + return; +} + +void backlight_user_enable(void) +{ + + DDRD |= (1<<0); + PORTD &= ~(1<<0); +} + +void backlight_user_disable(void) +{ + DDRD &= ~(1<<0); + PORTD |= (1<<0); +} + +void backlight_set(uint8_t level) +{ + if (no_backlight) return; + if (level) backlight_user_enable(); + else backlight_user_disable(); +} + diff --git a/keyboards/ydkb/chicory/chicory.h b/keyboards/ydkb/chicory/chicory.h new file mode 100644 index 00000000000..bd6e6bce6c3 --- /dev/null +++ b/keyboards/ydkb/chicory/chicory.h @@ -0,0 +1,8 @@ +#pragma once + +#include "quantum.h" +#include +#include + +#define XXX KC_NO + diff --git a/keyboards/ydkb/chicory/config.h b/keyboards/ydkb/chicory/config.h new file mode 100644 index 00000000000..174215b935b --- /dev/null +++ b/keyboards/ydkb/chicory/config.h @@ -0,0 +1,101 @@ +#pragma once + +#include "config_common.h" + +/* USB Device descriptor parameter */ +#define FW_VER_DATE DO5U +#define CONTACT(x,y) x##y //https://blog.csdn.net/aiynmimi/article/details/123486956 +#define CONTACT2(x,y) CONTACT(x,y) +#define FW_VER CONTACT2(VIAL_, FW_VER_DATE) +#define VENDOR_ID 0x9D5B +#define PRODUCT_ID 0x1840 +#define DEVICE_VER 0x0001 +#define MANUFACTURER YDKB +#if CONSOLE_ENABLE +#define PRODUCT Chicory Debug (FW_VER) +#else +#define PRODUCT Chicory (FW_VER) +#endif + + +#define MATRIX_ROWS 4 +#define MATRIX_COLS 11 + + + +#define BACKLIGHT_LEVELS 1 +#define TAPPING_TOGGLE 3 + + +/* 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 ws2812_PORTREG PORTD +#define ws2812_DDRREG DDRD +#define ws2812_pin PD1 +#define RGBLED_NUM 12 // Number of LEDs + +/* 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 +#include "quantum.h" +#include "stdint.h" +#include "led.h" +#include "via.h" +#include "command.h" +#include "ble51.h" +#include "rgblight.h" + + +void led_set_user(uint8_t usb_led) +{ + if (usb_led & (1<event.pressed) { + switch (keycode) { + case USER00: + command_extra(KC_U); + break; + case USER01: //RESET + command_extra(KC_B); + break; + case USER02: //BATTERY LEVEL + command_extra(KC_V); + break; + case USER03: //LOCK MODE + command_extra(KC_L); + break; + case USER04 ... USER11: + rgblight_action(keycode - USER04); + break; + } + } +} \ No newline at end of file diff --git a/keyboards/ydkb/chicory/light_ws2812.c b/keyboards/ydkb/chicory/light_ws2812.c new file mode 100644 index 00000000000..739c9ed8776 --- /dev/null +++ b/keyboards/ydkb/chicory/light_ws2812.c @@ -0,0 +1,157 @@ +/* +* light weight WS2812 lib V2.0b +* +* Controls WS2811/WS2812/WS2812B RGB-LEDs +* Author: Tim (cpldcpu@gmail.com) +* +* Jan 18th, 2014 v2.0b Initial Version +* +* License: GNU GPL v2 (see License.txt) +*/ + +#include "light_ws2812.h" +#include +#include +#include + +void ws2812_setleds(struct cRGB *ledarray) +{ + ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR + ws2812_sendarray_mask((uint8_t *)(ledarray), _BV(ws2812_pin)); + //_delay_us(50); +} + + +// Timing in ns +#define w_zeropulse 350 +#define w_onepulse 900 +#define w_totalperiod 1250 + +// Fixed cycles used by the inner loop +#define w_fixedlow 2 +#define w_fixedhigh 4 +#define w_fixedtotal 8 + +// Insert NOPs to match the timing, if possible +#define w_zerocycles (((F_CPU/1000)*w_zeropulse )/1000000) +#define w_onecycles (((F_CPU/1000)*w_onepulse +500000)/1000000) +#define w_totalcycles (((F_CPU/1000)*w_totalperiod +500000)/1000000) + +// w1 - nops between rising edge and falling edge - low +#define w1 (w_zerocycles-w_fixedlow) +// w2 nops between fe low and fe high +#define w2 (w_onecycles-w_fixedhigh-w1) +// w3 nops to complete loop +#define w3 (w_totalcycles-w_fixedtotal-w1-w2) + +#if w1>0 + #define w1_nops w1 +#else + #define w1_nops 0 +#endif + +// The only critical timing parameter is the minimum pulse length of the "0" +// Warn or throw error if this timing can not be met with current F_CPU settings. +#define w_lowtime ((w1_nops+w_fixedlow)*1000000)/(F_CPU/1000) +#if w_lowtime>550 + #error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?" +#elif w_lowtime>450 + #warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)." + #warning "Please consider a higher clockspeed, if possible" +#endif + +#if w2>0 +#define w2_nops w2 +#else +#define w2_nops 0 +#endif + +#if w3>0 +#define w3_nops w3 +#else +#define w3_nops 0 +#endif + +#define w_nop1 "nop \n\t" +#define w_nop2 "rjmp .+0 \n\t" +#define w_nop4 w_nop2 w_nop2 +#define w_nop8 w_nop4 w_nop4 +#define w_nop16 w_nop8 w_nop8 + +void ws2812_sendarray_mask(uint8_t *data ,uint8_t maskhi) +{ + uint8_t curbyte,ctr,masklo; + uint8_t sreg_prev; + uint8_t datlen = RGBLED_NUM*3; + + masklo =~maskhi&ws2812_PORTREG; + maskhi |= ws2812_PORTREG; + sreg_prev=SREG; + cli(); + + while (datlen--) { + curbyte=*data++; + + asm volatile( + " ldi %0,8 \n\t" + "loop%=: \n\t" + " out %2,%3 \n\t" // '1' [01] '0' [01] - re +#if (w1_nops&1) +w_nop1 +#endif +#if (w1_nops&2) +w_nop2 +#endif +#if (w1_nops&4) +w_nop4 +#endif +#if (w1_nops&8) +w_nop8 +#endif +#if (w1_nops&16) +w_nop16 +#endif + " sbrs %1,7 \n\t" // '1' [03] '0' [02] + " out %2,%4 \n\t" // '1' [--] '0' [03] - fe-low + " lsl %1 \n\t" // '1' [04] '0' [04] +#if (w2_nops&1) + w_nop1 +#endif +#if (w2_nops&2) + w_nop2 +#endif +#if (w2_nops&4) + w_nop4 +#endif +#if (w2_nops&8) + w_nop8 +#endif +#if (w2_nops&16) + w_nop16 +#endif + " out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high +#if (w3_nops&1) +w_nop1 +#endif +#if (w3_nops&2) +w_nop2 +#endif +#if (w3_nops&4) +w_nop4 +#endif +#if (w3_nops&8) +w_nop8 +#endif +#if (w3_nops&16) +w_nop16 +#endif + + " dec %0 \n\t" // '1' [+2] '0' [+2] + " brne loop%=\n\t" // '1' [+3] '0' [+4] + : "=&d" (ctr) + : "r" (curbyte), "I" (_SFR_IO_ADDR(ws2812_PORTREG)), "r" (maskhi), "r" (masklo) + ); + } + + SREG=sreg_prev; +} diff --git a/keyboards/ydkb/chicory/light_ws2812.h b/keyboards/ydkb/chicory/light_ws2812.h new file mode 100644 index 00000000000..08187ac90a9 --- /dev/null +++ b/keyboards/ydkb/chicory/light_ws2812.h @@ -0,0 +1,66 @@ +/* + * light weight WS2812 lib include + * + * Version 2.0a3 - Jan 18th 2014 + * Author: Tim (cpldcpu@gmail.com) + * + * Please do not change this file! All configuration is handled in "ws2812_config.h" + * + * License: GNU GPL v2 (see License.txt) + + + */ + +#ifndef LIGHT_WS2812_H_ +#define LIGHT_WS2812_H_ + +#include +#include +//#include "ws2812_config.h" + +/* + * Structure of the LED array + */ + +struct cRGB { uint8_t g; uint8_t r; uint8_t b; }; + +/* User Interface + * + * Input: + * ledarray: An array of GRB data describing the LED colors + * number_of_leds: The number of LEDs to write + * pinmask (optional): Bitmask describing the output bin. e.g. _BV(PB0) + * + * The functions will perform the following actions: + * - Set the data-out pin as output + * - Send out the LED data + * - Wait 50µs to reset the LEDs + */ + +void ws2812_setleds (struct cRGB *ledarray); +//void ws2812_setleds (struct cRGB *ledarray, uint16_t number_of_leds); +//void ws2812_setleds_pin(struct cRGB *ledarray, uint16_t number_of_leds,uint8_t pinmask); + +/* + * Old interface / Internal functions + * + * The functions take a byte-array and send to the data output as WS2812 bitstream. + * The length is the number of bytes to send - three per LED. + */ + +//void ws2812_sendarray (uint8_t *array,uint16_t length); +void ws2812_sendarray_mask(uint8_t *array, uint8_t pinmask); + + +/* + * Internal defines + */ + +#ifndef CONCAT +#define CONCAT(a, b) a ## b +#endif +#define CONCAT_EXP(a, b) CONCAT(a, b) + +// #define ws2812_PORTREG CONCAT_EXP(PORT,ws2812_port) +// #define ws2812_DDRREG CONCAT_EXP(DDR,ws2812_port) + +#endif /* LIGHT_WS2812_H_ */ diff --git a/keyboards/ydkb/chicory/matrix.c b/keyboards/ydkb/chicory/matrix.c new file mode 100644 index 00000000000..896fb3a06df --- /dev/null +++ b/keyboards/ydkb/chicory/matrix.c @@ -0,0 +1,312 @@ +/* +Copyright 2023 YANG + +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 . +*/ + +/* + * scan matrix + */ +#include +#include +#include +#include +#include "action.h" +#include "print.h" +#include "debug.h" +#include "util.h" +#include "matrix.h" +#include "debounce_pk.h" +#include "ble51.h" +#include "ble51_task.h" +#include "rgblight.h" +#include "backlight.h" +#include "timer.h" +#include "wait.h" + +bool is_ble_version = 1; +bool is_ver2 = 0; +bool no_backlight = 0; +bool no_rgblight = 0; + +extern rgblight_config_t rgblight_config; +static matrix_row_t matrix[MATRIX_ROWS] = {0}; + +static uint16_t matrix_scan_timestamp = 0; +static uint8_t matrix_debouncing[MATRIX_ROWS][MATRIX_COLS] = {0}; +static uint8_t now_debounce_dn_mask = DEBOUNCE_NK_MASK; +static bool matrix_idle = false; +static bool first_key_scan = false; +static bool is_matrix_active(void); + +static void select_row(uint8_t row); +static void select_all_rows(void); +static uint8_t get_key(uint8_t col); +static void init_cols(void); +static void unselect_rows(void); + +static void init_cols(void); +__attribute__ ((weak)) +void matrix_scan_user(void) {} + +__attribute__ ((weak)) +void matrix_scan_kb(void) { + matrix_scan_user(); + hook_keyboard_loop(); +} + +void hook_early_init(void) { + DDRD &= ~(1<<0); + PORTD |= (1<<0); + WAIT_MS(2); + if (~PIND & (1<<0)) { + is_ver2 = 1; + no_backlight = 1; + no_rgblight = 1; + } + if (pgm_read_byte(0x7ff9) == 0xB0) { + is_ble_version = 0; + ble51_boot_on = 0; + } else { + if (ble_reset_key == 0xBBAA) { + if (ble51_boot_on) { + // PD3,TX_MCU to RX_51 + DDRD |= (1<<3 ); + PORTD &= ~(1<<3); + bt_power_init(); + // Light CapsLED + DDRB |= (1<<7); + PORTB |= (1<<7); + _delay_ms(5000); + // turn off bt and ready to reinit it. + turn_off_bt(); + } + } + } +} + +void matrix_init(void) +{ + //software ble reset + if (ble_reset_key == 0xBBAA) { + ble_reset_key = 0; + ble51_factory_reset(); + } + + // led init + DDRB |= (1<<7); + PORTB &= ~(1<<7); + + init_cols(); + rgblight_init(); +} + +uint8_t matrix_scan(void) +{ + if (matrix_idle) { + if (is_matrix_active() == false) return 1; + else { + matrix_idle = false; + first_key_scan = true; + } + } + + //scan matrix every 1ms except first_key_scan. maybe not necessary for some avr kbd. + #if 1 + if (!first_key_scan) { + uint16_t time_check = timer_read(); + if (matrix_scan_timestamp == time_check) return 1; + matrix_scan_timestamp = time_check; + } + #endif + + uint8_t *debounce = &matrix_debouncing[0][0]; + uint8_t matrix_up_keys = MATRIX_ROWS * MATRIX_COLS; + for (uint8_t row=0; row> 1) | key; + + + //if ((*debounce > 0) && (*debounce < 0xff)) { + if (1) { //always update matrix[row], it costs only a little time. + matrix_row_t *p_row = &matrix[row]; + matrix_row_t col_mask = ((matrix_row_t)1 << col); + if (*debounce >= now_debounce_dn_mask) { //debounce KEY DOWN + *p_row |= col_mask; + } else if (*debounce <= DEBOUNCE_UP_MASK) { //debounce KEY UP + *p_row &= ~col_mask; + if (*debounce == 0) matrix_up_keys--; + } + } + } + + //if key pressed, update kb_idle_times + //if (matrix[row] > 0) { + // if (!rgblight_config.enable && !backlight_config.enable) kb_idle_times = 12; + // else kb_idle_times = 0; + //} + } + + // 最终检测一次是不是完全没有按键按下 + if (matrix_up_keys == 0) { + select_all_rows(); + matrix_idle = true; + } else { + if (!rgblight_config.enable) kb_idle_times = 12; + else kb_idle_times = 0; + // 从空闲到检测第一个按键时多扫一次 + if (first_key_scan) { + first_key_scan = false; + matrix_scan(); + } + //unselect_rows(); + } + + matrix_scan_quantum(); + return 1; +} + + +inline +bool matrix_is_on(uint8_t row, uint8_t col) +{ + return (matrix[row] & ((matrix_row_t)1<= 10) { + matrix_scan(); + // K14 F, K17 J + uint8_t *debounce = &matrix_debouncing[0][0]; + uint8_t matrix_keys_down = 0; + for (uint8_t i=0; i< MATRIX_ROWS * MATRIX_COLS; i++, *debounce++) { + if (*debounce > 0) { + if (i == KP(1,4) || i == KP(1,7)) matrix_keys_down += 100; + else matrix_keys_down++; + } + } + if (matrix_keys_down == 200) { // only two keys down. + if (!ble51_boot_on) command_extra(KC_W); + return true; + } else if (!ble51_boot_on && matrix_keys_down) return true; //蓝牙功能关闭时唤醒电脑 + } else { + select_all_rows(); + _delay_us(6); + return is_matrix_active(); + } + return false; +} + +void bootmagic_lite(void) +{ + //do nothing + return; + +} + + +void hook_nkro_change(void) { + //return; + uint8_t kbd_nkro = keymap_config.nkro; + if (!kbd_nkro) { // kbd_nkro before hook_nkro_change() + now_debounce_dn_mask = DEBOUNCE_NK_MASK; + } else { + now_debounce_dn_mask = DEBOUNCE_DN_MASK; + } + type_num(kbd_nkro?6:0); +} \ No newline at end of file diff --git a/keyboards/ydkb/chicory/rgblight.c b/keyboards/ydkb/chicory/rgblight.c new file mode 100644 index 00000000000..0992acaf418 --- /dev/null +++ b/keyboards/ydkb/chicory/rgblight.c @@ -0,0 +1,467 @@ +#include +#include +#include +#include "progmem.h" +#include "timer.h" +#include "action.h" +#include "rgblight.h" +#include "debug.h" +#include "lufa.h" +#include "ble51.h" +#include "ble51_task.h" +#include "backlight.h" +#include "led.h" +#include "quantum.h" + +#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) +#define rgblight_timer_enabled (~PORTD & (1<<4)) +#define RGBLED_TEMP RGBLED_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}; + +//battery +extern bool no_rgblight; + +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}; + +struct cRGB led[RGBLED_NUM+1]; + + +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) +{ + /* + 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. + */ + 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; + 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); +} + +void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) +{ + (*led1).r = r; + (*led1).g = g; + (*led1).b = b; +} + + +uint32_t eeconfig_read_rgblight(void) +{ + return eeprom_read_dword(EECONFIG_RGBLIGHT); +} + +void eeconfig_write_rgblight(uint32_t val) +{ + eeprom_update_dword(EECONFIG_RGBLIGHT, val); +} + +void eeconfig_write_rgblight_default(void) +{ + dprintf("eeconfig_write_rgblight_default\n"); + + eeconfig_write_rgblight(rgblight_config.raw); //rgblight_config.raw); +} + +void eeconfig_debug_rgblight(void) { + dprintf("rgblight_config eeprom\n"); + dprintf("enable = %d\n", rgblight_config.enable); + dprintf("mode = %d\n", rgblight_config.mode); + dprintf("hue = %d\n", rgblight_config.hue); + dprintf("sat = %d\n", rgblight_config.sat); + dprintf("val = %d\n", rgblight_config.val); +} + +void rgblight_init(void) +{ + dprintf("rgblight_init start!\n"); +#if 1 + if (!eeconfig_is_enabled()) { + dprintf("rgblight_init eeconfig is not enabled.\n"); + eeconfig_init(); + eeconfig_write_rgblight_default(); + } +#endif + rgblight_config.raw = eeconfig_read_rgblight(); + if (rgblight_config.val == 0) rgblight_config.val = 127; + + eeconfig_debug_rgblight(); // display current eeprom values + + rgblight_timer_init(); // setup the timer + + rgblight_mode(rgblight_config.mode); +} + + +void rgblight_mode(int8_t mode) +{ + // rgb off, new way to save about 60B + if (!rgblight_config.enable) { + //rgblight_clear(); + //rgblight_set(); + rgblight_timer_disable(); + rgb_fading_index = 0; + } else { + // rgb on + if (mode < 0) mode = RGBLIGHT_MODES - 1; + else if (mode >= RGBLIGHT_MODES) mode = 0; + rgblight_config.mode = mode; + dprintf("rgblight mode: %u\n", rgblight_config.mode); + + rgblight_timer_enable(); + } + // save config + eeconfig_write_rgblight(rgblight_config.raw); + rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); +} + +inline +void rgblight_toggle(void) +{ + rgblight_config.enable ^= 1; + dprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable); + rgblight_mode(rgblight_config.mode); +} + + +void rgblight_action(uint8_t action) +{ + /* 0 toggle + 1 mode- 2 mode+ + 3 hue- 4 hue+ + 5 sat- 6 sat+ + 7 val- 8 val+ + */ + uint16_t hue = rgblight_config.hue; + int16_t sat = rgblight_config.sat; + int16_t val = rgblight_config.val; + int8_t increament = 1; + if (action & 1) increament = -1; + if (get_mods() & MOD_BIT(KC_LSHIFT)) { + increament *= -1; + } + switch (action) { + case 0: + rgblight_toggle(); + break; + case 1: + case 2: + rgblight_mode(rgblight_config.mode + increament); + break; + case 3: + case 4: + hue = rgblight_config.hue + RGBLIGHT_HUE_STEP * increament; + break; + case 5: + case 6: + sat = rgblight_config.sat + RGBLIGHT_SAT_STEP * increament; + if (sat > 255) sat = 255; + if (sat < 0) sat = 0; + break; + case 7: + case 8: + val = rgblight_config.val + RGBLIGHT_VAL_STEP * increament; + if (val > 255) val = 255; + if (val < 0) val = 0; + break; + default: + break; + } + if (action >= 3) rgblight_sethsv(hue, sat, val); +} + +void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) +{ + if (rgblight_config.enable) { + sethsv(hue, sat, val, &led[RGBLED_TEMP]); + for (uint8_t i=0; i< RGBLED_NUM; i++) { + led[i] = led[RGBLED_TEMP]; + } + rgblight_set(); + } +} + +void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) +{ + hue = hue_fix(hue); + if (rgblight_config.enable) { + if (rgblight_config.mode == 1) { + // same static color + rgblight_sethsv_noeeprom(hue, sat, val); + } + rgblight_config.hue = hue; + rgblight_config.sat = sat; + rgblight_config.val = val; + eeconfig_write_rgblight(rgblight_config.raw); + dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val); + } +} + +void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) +{ + for (uint8_t i=0;i= 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 = &led[0]; + for (uint8_t i=0;i 126) { + increament *= -1; + } +} + +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); +} + +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 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]); + } + pos += increament; + if (pos > RGBLED_NUM) pos = 0; + else if (pos < 0 ) pos = RGBLED_NUM; + rgblight_set(); + } +} + +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 int8_t increament = 1; + if (++sled_step > interval) { + bool need_update = 0; + sled_step = 0; + rgblight_clear(); + for (i=0; i= 0){ + need_update = 1; + led[target_col] = led[RGBLED_TEMP]; + } + } + if (need_update) rgblight_set(); //Keep the first or last col on when increament changes. + pos += increament; + if (pos <= 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH || pos >= RGBLED_NUM) { + increament *= -1; + current_hue = hue_fix(current_hue + 40); + sethsv(current_hue, rgblight_config.sat, rgblight_config.val, &led[RGBLED_TEMP]); + } + } +} + +void suspend_power_down_action(void) +{ + PORTB &= ~(1<<7); + backlight_user_disable(); + rgblight_timer_disable(); //RGB_VCC off + rgb_fading_index = 0; +} + +void suspend_wakeup_init_action(void) +{ + if (!low_battery) { + backlight_init(); + rgblight_init(); + } + if (BT_POWERED == 0) display_connection_status_check_times = 1; +} + +void hook_keyboard_loop() +{ + static uint16_t rgb_update_timer = 0; + if (rgblight_timer_enabled && timer_elapsed(rgb_update_timer) > 40) { + rgb_update_timer = timer_read(); + if (!display_connection_status_check_times || !ble51_boot_on) rgblight_task(); + } +} + +void ble51_task_user(void) +{ + static uint8_t ble51_task_steps = 0; + static uint16_t battery_timer = 0; + if (timer_elapsed(battery_timer) > 150) { + battery_timer = timer_read(); + ble51_task_steps++; + if (low_battery) { + if (ble51_task_steps > 3) { + ble51_task_steps = (low_battery == 1)? 3:0; //value 1 is extremely low battery + if (USB_DeviceState != DEVICE_STATE_Configured) { //not charging + backlight_disable(); + rgblight_timer_disable(); + PORTB ^= (1<<7); + } else { + low_battery = 0; + suspend_wakeup_init_action(); + } + } + } else if (display_connection_status_check_times) { + rgblight_timer_enable(); + if (ble51_task_steps == 1) { + PORTB &= ~(1<<7); + rgblight_clear(); + rgblight_set(); + } else if (ble51_task_steps == 3) { + PORTB |= (1<<7); + uint8_t g_color = bt_connected? 128:0; + uint8_t b_color = bt_connected? 0:128; + rgblight_setrgb(0,g_color,b_color); + } + if ((!bt_connected && ble51_task_steps >= 5) || ble51_task_steps >= 11) { + ble51_task_steps = 0; + } + } + } +} diff --git a/keyboards/ydkb/chicory/rgblight.h b/keyboards/ydkb/chicory/rgblight.h new file mode 100644 index 00000000000..906f9495ea0 --- /dev/null +++ b/keyboards/ydkb/chicory/rgblight.h @@ -0,0 +1,83 @@ +#ifndef RGBLIGHT_H +#define RGBLIGHT_H + +#ifndef RGBLIGHT_MODES +#define RGBLIGHT_MODES 23 +#endif + +#ifndef RGBLIGHT_EFFECT_SNAKE_LENGTH +#define RGBLIGHT_EFFECT_SNAKE_LENGTH 4 +#endif + +#ifndef RGBLIGHT_EFFECT_KNIGHT_LENGTH +#define RGBLIGHT_EFFECT_KNIGHT_LENGTH 4 +#endif +#ifndef RGBLIGHT_EFFECT_KNIGHT_OFFSET +#define RGBLIGHT_EFFECT_KNIGHT_OFFSET RGBLED_NUM +#endif + +#ifndef RGBLIGHT_EFFECT_DUALKNIGHT_LENGTH +#define RGBLIGHT_EFFECT_DUALKNIGHT_LENGTH 4 +#endif + +#ifndef RGBLIGHT_HUE_STEP +#define RGBLIGHT_HUE_STEP 20 +#endif +#ifndef RGBLIGHT_SAT_STEP +#define RGBLIGHT_SAT_STEP 17 +#endif +#ifndef RGBLIGHT_VAL_STEP +#define RGBLIGHT_VAL_STEP 17 +#endif + +#define RGBLED_TIMER_TOP F_CPU/(256*32) + +#include +#include +#include "eeconfig.h" +#include "light_ws2812.h" + +typedef union { + uint32_t raw; + struct { + uint16_t hue :10; + uint16_t enable :1; + uint16_t mode :5; + uint8_t sat :8; + uint8_t val :8; + }; +} rgblight_config_t; + +void rgblight_init(void); +void rgblight_action(uint8_t action); +void rgblight_toggle(void); +void sw_bottom_sync_toggle(void); +void rgblight_mode(int8_t mode); +void rgblight_set(void); +void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val); +void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b); + +uint32_t eeconfig_read_rgblight(void); +void eeconfig_write_rgblight(uint32_t val); +void eeconfig_write_rgblight_default(void); +void eeconfig_debug_rgblight(void); + +void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1); +void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1); +void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val); + +void rgblight_clear(void); +void rgblight_timer_init(void); +void rgblight_timer_enable(void); +void rgblight_timer_disable(void); +void rgblight_timer_toggle(void); +void rgblight_task(void); +void rgblight_effect_breathing(uint8_t interval); +void rgblight_effect_rainbow_mood(uint8_t interval); +void rgblight_effect_rainbow_swirl(uint8_t interval); +void rgblight_effect_snake(uint8_t interval); +void rgblight_effect_knight(uint8_t interval); + +void hook_keyboard_loop(void); + +#endif diff --git a/keyboards/ydkb/chicory/rules.mk b/keyboards/ydkb/chicory/rules.mk new file mode 100644 index 00000000000..b5c8aa01d89 --- /dev/null +++ b/keyboards/ydkb/chicory/rules.mk @@ -0,0 +1,34 @@ +# MCU name +MCU = atmega32u4 + +# Processor frequency +F_CPU = 8000000 + +# Bootloader selection +BOOTLOADER = bootloadhid +BOOTLOADER_SIZE = 2048 + +# Build Options +# change yes to no to disable +# +CUSTOM_MATRIX = yes # Custom matrix file +UNICODE_ENABLE = yes # Unicode +BOOTMAGIC_ENABLE = yes # Enable Bootmagic Lite +MOUSEKEY_ENABLE = yes # Mouse keys +EXTRAKEY_ENABLE = yes # Audio control and System control +CONSOLE_ENABLE = no # Console for debug +COMMAND_ENABLE = yes # Commands for debug and configuration +NKRO_ENABLE = yes # Enable N-Key Rollover +BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality +RGBLIGHT_ENABLE = no +LTO_ENABLE = yes +BACKLIGHT_DRIVER = custom + +# project specific files +SRC ?= matrix.c \ + led_fn.c \ + light_ws2812.c \ + backlight_user.c \ + rgblight.c + +include $(TMK_DIR)/protocol/ble51.mk diff --git a/keyboards/ydkb/00common/debounce_pk.h b/keyboards/ydkb/debounce_pk.h similarity index 100% rename from keyboards/ydkb/00common/debounce_pk.h rename to keyboards/ydkb/debounce_pk.h diff --git a/keyboards/ydkb/duang60v1/backlight_user.c b/keyboards/ydkb/duang60v1/backlight_user.c new file mode 100644 index 00000000000..ec2eacc7c1d --- /dev/null +++ b/keyboards/ydkb/duang60v1/backlight_user.c @@ -0,0 +1,52 @@ +/* +Copyright 2020 YANG + +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 . +*/ + +#include +#include +#include +#include "backlight.h" +#include "timer.h" + + +void backlight_init_ports(void) { + DDRC |= (1<<6); + //PORTC |= (1<<6); +} + +void backlight_task(void) { + return; +} + +void backlight_user_enable(void) +{ + + //DDRC |= (1<<6); + PORTC &= ~(1<<6); +} + +void backlight_user_disable(void) +{ + //DDRC &= ~(1<<6); + PORTC |= (1<<6); +} + +void backlight_set(uint8_t level) +{ + if (level) backlight_user_enable(); + else backlight_user_disable(); +} + diff --git a/keyboards/ydkb/duang60v1/config.h b/keyboards/ydkb/duang60v1/config.h index 33d36879ee5..b779c712353 100644 --- a/keyboards/ydkb/duang60v1/config.h +++ b/keyboards/ydkb/duang60v1/config.h @@ -1,11 +1,10 @@ #pragma once #include "config_common.h" +#include "config_ble51.h" /* USB Device descriptor parameter */ -#define FW_VER QMK_DMCM -#define FW_VER_VIA VIA_DMCM -#define FW_VER_VIAL VIAL_DMCM +#define FW_VER_DATE DO6A #define VENDOR_ID 0x9D5B #define PRODUCT_ID 0x2060 #define DEVICE_VER 0x0001 @@ -13,90 +12,34 @@ #define PRODUCT Duang60v1 (FW_VER) +/* matrix size */ #define MATRIX_ROWS 5 //595 #define MATRIX_COLS 16 - -#define TAPPING_TOGGLE 2 - -#define TAPPING_TERM 200 -#define IGNORE_MOD_TAP_INTERRUPT // this makes it possible to do rolling combos (zx) with keys that convert to other keys on hold (z becomes ctrl when you hold it, and when this option isn't enabled, z rapidly followed by x actually sends Ctrl-x. That's bad.) - #define BACKLIGHT_PIN C6 -#define BACKLIGHT_LEVELS 6 +#define BACKLIGHT_LEVELS 1 #define BACKLIGHT_ON_STATE 0 -/* 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 ws2812_PORTREG PORTD #define ws2812_DDRREG DDRD #define ws2812_pin PD0 #define RGBLED_NUM 4 // 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 - -/* fix space cadet rollover issue */ -#define DISABLE_SPACE_CADET_ROLLOVER - -#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 "rgblight.h" -void led_set_user(uint8_t usb_led) -{ - if (usb_led & (1<event.pressed) { - switch (keycode) { - case USER00: - command_extra(KC_U); - break; - case USER01: //RESET - command_extra(KC_B); - break; - case USER02: //BATTERY LEVEL - command_extra(KC_V); - break; - case USER03: //LOCK MODE - command_extra(KC_L); - break; - case USER04 ... USER12: - rgblight_action(keycode - USER04); - break; + static const uint8_t userx_to_command[4] = { + KC_U, // 0 Host Switch + KC_B, // 1 Reset + KC_V, // 2 Output Battery Value + KC_L // 3 Lock Mode + }; + 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/duang60v1/matrix.c b/keyboards/ydkb/duang60v1/matrix.c index d63e54ce2f3..90147555c4f 100644 --- a/keyboards/ydkb/duang60v1/matrix.c +++ b/keyboards/ydkb/duang60v1/matrix.c @@ -1,3 +1,23 @@ +/* +Copyright 2023 YANG + +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 . +*/ + +/* + * scan matrix + */ #include #include #include @@ -13,6 +33,7 @@ #include "keyboard.h" #include "timer.h" #include "matrix.h" +#include "debounce_pk.h" #include "suspend.h" #include "lufa.h" #include "rgblight.h" @@ -20,18 +41,14 @@ #include "ble51_task.h" #include "switch_board.h" -#define DEBOUNCE_DN_MASK (uint8_t)(~(0x80 >> 5)) -#define DEBOUNCE_UP_MASK (uint8_t)(0x80 >> 5) - -bool is_ble_version = 1; -extern rgblight_config_t rgblight_config; static matrix_row_t matrix[MATRIX_ROWS] = {0}; static uint8_t matrix_current_row = 0; static uint16_t matrix_scan_timestamp = 0; static uint8_t matrix_debouncing[MATRIX_ROWS][MATRIX_COLS] = {0}; +static uint8_t now_debounce_dn_mask = DEBOUNCE_NK_MASK; static uint8_t encoder_state_prev[2][2] = {0}; static void select_key(uint8_t mode); static uint8_t get_key(uint8_t col); @@ -46,51 +63,39 @@ void matrix_scan_kb(void) { hook_keyboard_loop(); } -inline -uint8_t matrix_rows(void) -{ - return MATRIX_ROWS; -} - -inline -uint8_t matrix_cols(void) -{ - return MATRIX_COLS; -} void hook_early_init() { - if (pgm_read_byte(0x7e65) == 0x4c) { - // USB Only version - is_ble_version = 0; + // PE6 for BLE Reset, PE2 for BT_SW + DDRE = 0; + PORTE |= (1<<2); + WAIT_MS(6); + + if ( (pgm_read_byte(0x7e65) == 0x4c) || (~PINE & (1<<2)) ) { + // USB Only version or BTSW_OFF ble51_boot_on = 0; } else { - // PE6 for BLE Reset, PE2 for BT_SW - DDRE &= ~(1<<6 | 1<<2); - PORTE |= (1<<6 | 1<<2); - _delay_ms(2); - if (~PINE & (1<<2)) ble51_boot_on = 0; //BLE Reset if (ble_reset_key == 0xBBAA) { - if (ble51_boot_on) { - // PE6 for BLE Reset - DDRE |= (1<<6); - PORTE &= ~(1<<6); - bt_power_init(); - // light CapsLED - DDRD |= (1<<4); - PORTD |= (1<<4); - _delay_ms(5000); - bootloader_jump(); - } + ble_reset_key = 0; + // PE6 for BLE Reset + DDRE |= (1<<6); + PORTE &= ~(1<<6); + bt_power_init(); + // light CapsLED + DDRD |= (1<<4); + PORTD |= (1<<4); + _delay_ms(5000); + bootloader_jump(); } } } void matrix_init(void) { + // led init DDRD |= (1<<4); - PORTD &= ~(1<<4); + //PORTD &= ~(1<<4); init_cols(); rgblight_init(); } @@ -98,15 +103,16 @@ void matrix_init(void) uint8_t matrix_scan(void) { - uint16_t time_check = timer_read(); - if (matrix_scan_timestamp == time_check) return 1; - matrix_scan_timestamp = time_check; + //uint16_t time_check = timer_read(); + //if (matrix_scan_timestamp == time_check) return 1; + //matrix_scan_timestamp = time_check; + uint8_t matrix_keys_down = 0; select_key(0); uint8_t *debounce = &matrix_debouncing[0][0]; - for (uint8_t row=0; row> 1) | key; if (real_col >= 8) select_key(1); - + //if ((*debounce > 0) && (*debounce < 255)) { if (1) { matrix_row_t *p_row = &matrix[row]; matrix_row_t col_mask = ((matrix_row_t)1 << real_col); - if (*debounce >= DEBOUNCE_DN_MASK) { + if (*debounce >= DEBOUNCE_DN_MASK) { //debounce KEY DOWN *p_row |= col_mask; - } else if (*debounce <= DEBOUNCE_UP_MASK) { + } else if (*debounce <= DEBOUNCE_UP_MASK) { //debounce KEY UP *p_row &= ~col_mask; } - } - } - if (matrix[row] > 0) { - if (!rgblight_config.enable) kb_idle_times = 12; - else kb_idle_times = 0; + } + if (*debounce) matrix_keys_down++; } } - + + if (matrix_keys_down) { + if (BLE_LIGHT_ON == 0) kb_idle_times = 12; + else kb_idle_times = 0; + } + matrix_scan_quantum(); - return 1; + return matrix_keys_down; } @@ -151,10 +159,10 @@ matrix_row_t matrix_get_row(uint8_t row) void matrix_print(void) { - print("\nr/c 01234567\n"); + print("\nr/c 0123456789ABCDEF\n"); for (uint8_t row = 0; row < MATRIX_ROWS; row++) { print_hex8(row); print(": "); - print_bin_reverse8(matrix_get_row(row)); + print_bin_reverse16(matrix_get_row(row)); print("\n"); } } @@ -172,7 +180,7 @@ void init_cols(void) { //595 pin DDRB |= (1<= 10) { //lock mode - matrix_scan(); - // Left21 F, Right22 J. KP(2,2) KP(2,5) - uint8_t *debounce = &matrix_debouncing[0][0]; - uint8_t matrix_keys_down = 0; - for (uint8_t i=0; i< MATRIX_ROWS * MATRIX_COLS; i++, *debounce++) { - if (*debounce > 0) { - if (i == KP(2,2) || i == KP(2,5)) matrix_keys_down += 100; - else matrix_keys_down++; - } - } - if (matrix_keys_down == 200) { - return true; - } - } else { - //check encoder - DS_PL_LO(); - for (uint8_t i = 0; i < 2; i++) { - for (uint8_t j = 0; j < 8; j++) { - CLOCK_PULSE(); - DS_PL_HI(); - } - _delay_us(6); - uint8_t encoder_state_0 = PINF&(1<= 10) {//lock mode + if (matrix_keys_down == 2) { + // Left21 F, Right22 J. KP(2,2) KP(2,5) + if (matrix_debouncing[2][2] == 0xff && matrix_debouncing[2][5] == 0xff) return true; } + if (!ble51_boot_on) return true; //蓝牙功能关闭时唤醒电脑 + return false; } - return false; + + return matrix_keys_down; +} + +void bootmagic_lite(void) +{ + //do nothing + return; + } + +void hook_nkro_change(void) +{ + return; + uint8_t kbd_nkro = keymap_config.nkro; + type_num(kbd_nkro?6:0); +} \ No newline at end of file diff --git a/keyboards/ydkb/duang60v1/pre_compile_via/ydkb_duang60v1_via.zip b/keyboards/ydkb/duang60v1/pre_compile_via/ydkb_duang60v1_via.zip deleted file mode 100644 index e382408f30c..00000000000 Binary files a/keyboards/ydkb/duang60v1/pre_compile_via/ydkb_duang60v1_via.zip and /dev/null differ diff --git a/keyboards/ydkb/duang60v1/rgblight.c b/keyboards/ydkb/duang60v1/rgblight.c index 061337719a8..76b6e6859b3 100644 --- a/keyboards/ydkb/duang60v1/rgblight.c +++ b/keyboards/ydkb/duang60v1/rgblight.c @@ -9,7 +9,6 @@ #include "lufa.h" #include "ble51.h" #include "ble51_task.h" -#include "backlight.h" #include "led.h" #include "quantum.h" @@ -17,37 +16,79 @@ #define rgblight_timer_enable() do { PORTD &= ~(1<<1);} while(0) #define rgblight_timer_disable() do { PORTD |= (1<<1);} while(0) #define rgblight_timer_enabled (~PORTD & (1<<1)) + #define RGBLED_TEMP RGBLED_NUM +#define INDICATOR_NUM 0 +struct cRGB rgbled[RGBLED_NUM+INDICATOR_NUM+1]; +struct cRGB *led = &rgbled[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}; -//battery -extern bool no_rgblight; -extern bool is_ble_version; - rgblight_config_t rgblight_config = {.enable = 1, .mode = 8,.hue = 640, .sat = 255, .val = 255}; -struct cRGB led[RGBLED_NUM+1]; +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/ + use 8bit hue, hue16 = hue*3 to reach 0 to 767. + */ + uint8_t r, g, b; + uint8_t temp[5]; + 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[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); +} + +#if 0 // my old code. the new way use 8bit hue saves about 134B +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) { /* 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. */ uint8_t r, g, b; uint8_t temp[5]; - uint8_t n = (hue >> 8) % 3; + // 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; uint8_t s = ((256 - saturation) * brightness) >> 8; +#if 0 //temp[n-1] b g r temp[0] = temp[3] = s; temp[1] = temp[4] = x + s; temp[2] = brightness - x; - r = temp[n + 2]; - g = temp[n + 1]; - b = temp[n]; + r = temp[n + 1]; + g = temp[n]; + b = temp[n - 1]; setrgb(r,g,b, led1); +#else //temp[n-1] 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-1], 3); +#endif } +#endif void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) { @@ -86,7 +127,7 @@ void eeconfig_debug_rgblight(void) { void rgblight_init(void) { dprintf("rgblight_init start!\n"); -#if 0 +#if 1 if (!eeconfig_is_enabled()) { dprintf("rgblight_init eeconfig is not enabled.\n"); eeconfig_init(); @@ -103,25 +144,33 @@ void rgblight_init(void) rgblight_mode(rgblight_config.mode); } +uint8_t limit_value_0to255(int16_t value) { + if (value > 255) return 255; + else if (value < 0) return 0; + else return value; +} void rgblight_mode(int8_t mode) { - // rgb off + // rgb off, new way to save about 60B if (!rgblight_config.enable) { //rgblight_clear(); //rgblight_set(); rgblight_timer_disable(); } else { // rgb on + #if 1 //less than 0 can always be 0. save 8B if (mode < 0) mode = RGBLIGHT_MODES - 1; - else if (mode >= RGBLIGHT_MODES) mode = 0; + else + #endif + if (mode >= RGBLIGHT_MODES) mode = 0; rgblight_config.mode = mode; dprintf("rgblight mode: %u\n", rgblight_config.mode); rgblight_timer_enable(); } - // save config - eeconfig_write_rgblight(rgblight_config.raw); + // save config. rgblight_sethsv() will save config. + //eeconfig_write_rgblight(rgblight_config.raw); rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } @@ -142,9 +191,9 @@ void rgblight_action(uint8_t action) 5 sat- 6 sat+ 7 val- 8 val+ */ - uint16_t hue = rgblight_config.hue; - int16_t sat = rgblight_config.sat; - int16_t val = rgblight_config.val; + 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; if (get_mods() & MOD_BIT(KC_LSHIFT)) { @@ -160,19 +209,15 @@ void rgblight_action(uint8_t action) break; case 3: case 4: - hue = (rgblight_config.hue + 768 + RGBLIGHT_HUE_STEP * increament) % 768; + hue = rgblight_config.hue + RGBLIGHT_HUE_STEP * increament; break; case 5: case 6: - sat = rgblight_config.sat + RGBLIGHT_SAT_STEP * increament; - if (sat > 255) sat = 255; - if (sat < 0) sat = 0; + sat = limit_value_0to255(rgblight_config.sat + RGBLIGHT_SAT_STEP * increament); break; case 7: case 8: - val = rgblight_config.val + RGBLIGHT_VAL_STEP * increament; - if (val > 255) val = 255; - if (val < 0) val = 0; + val = limit_value_0to255(rgblight_config.val + RGBLIGHT_VAL_STEP * increament); break; default: break; @@ -180,58 +225,62 @@ 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]); for (uint8_t i=0; i< RGBLED_NUM; i++) { led[i] = led[RGBLED_TEMP]; } - rgblight_set(); + //rgblight_set(); } } -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); if (rgblight_config.enable) { + #if 0 // will do rgblight_sethsv_noeeprom() in rgblight_task if (rgblight_config.mode == 1) { // same static color rgblight_sethsv_noeeprom(hue, sat, val); } + #endif rgblight_config.hue = hue; rgblight_config.sat = sat; rgblight_config.val = val; - eeconfig_write_rgblight(rgblight_config.raw); dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } + // when rgblight_config.enable == 0, just save config.raw. + eeconfig_write_rgblight(rgblight_config.raw); } void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { - for (uint8_t i=0;i 15 case 15 ... 20: - rgblight_effect_snake(rgblight_config.mode-13); + rgblight_effect_snake(rgblight_config.mode-14); // 0 to 5 (0 to 2) break; #endif #if RGBLIGHT_MODES > 21 @@ -257,6 +306,7 @@ void rgblight_task(void) break; #endif } + rgblight_set(); } else { rgblight_timer_disable(); } @@ -276,29 +326,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 = (current_hue + interval * 3) % 768; + 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; - for (i=0; i 15 @@ -314,12 +359,15 @@ void rgblight_effect_snake(uint8_t interval) rgblight_clear(); if (interval%2) increament = -1; for (i=0; i= RGBLED_NUM) target_led -= 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; else if (pos < 0 ) pos = RGBLED_NUM; - rgblight_set(); + //rgblight_set(); } } #endif @@ -329,7 +377,6 @@ 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 int8_t increament = 1; if (++sled_step > interval) { @@ -337,17 +384,17 @@ void rgblight_effect_knight(uint8_t interval) sled_step = 0; rgblight_clear(); for (i=0; i= 0){ + int8_t target_col = pos+i; + if (target_col < RGBLED_NUM && target_col >= 0){ need_update = 1; - uint8_t tmp_col = pos+i; - led[tmp_col%RGBLED_NUM] = led[RGBLED_TEMP]; + led[target_col] = led[RGBLED_TEMP]; } } if (need_update) rgblight_set(); //Keep the first or last col on when increament changes. pos += increament; if (pos <= 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH || pos >= RGBLED_NUM) { increament *= -1; - current_hue = (current_hue + 40) % 768; + current_hue = current_hue + 16; sethsv(current_hue, rgblight_config.sat, rgblight_config.val, &led[RGBLED_TEMP]); } } @@ -358,7 +405,6 @@ void suspend_power_down_action(void) { PORTD &= ~(1<<4); DDRC &= ~(1<<6); //backlight_disable(); - rgblight_timer_disable(); //RGB_VCC off } @@ -366,52 +412,51 @@ void suspend_wakeup_init_action(void) { rgblight_init(); DDRC |= (1<<6); - if (BLE51_PowerState >= 4) display_connection_status_check_times = 1; } void hook_keyboard_loop() { + if (BLE51_PowerState > 1) return; static uint16_t rgb_update_timer = 0; - if (rgblight_timer_enabled && timer_elapsed(rgb_update_timer) > 40) { + static uint8_t steps = 0; + if (timer_elapsed(rgb_update_timer) > 40) { rgb_update_timer = timer_read(); - if (!display_connection_status_check_times || !ble51_boot_on) rgblight_task(); - } -} + if (!display_connection_status_check_times) rgblight_task(); -void ble51_task_user(void) -{ - static uint8_t ble51_task_steps = 0; - static uint16_t battery_timer = 0; - if (timer_elapsed(battery_timer) > 150) { - battery_timer = timer_read(); - ble51_task_steps++; - if (low_battery) { - if (ble51_task_steps > 3) { - ble51_task_steps = (low_battery == 1)? 3:0; //value 1 is extremely low battery - if (PINC & (1<<6)) { //not charging - backlight_disable(); - rgblight_timer_disable(); - PORTB ^= (1<<6); + if ((steps++ & 0b11)) return; + //led_status_task(); + // run every 4*40 = 160ms + PORTD &= ~(1<<4); //led off + if (ble51_boot_on && (low_battery || display_connection_status_check_times)) { + rgblight_timer_enable(); + rgblight_clear(); + // 320ms on,320ms off. bt connected: 320ms*3 on, 320ms off. + if (low_battery) { + rgblight_timer_disable(); + if (steps & 0b1000) PORTD |= (1<<4); + } else { + if (bt_connected) { + if (steps & 0b11000) { + PORTD |= (1<<4); + rgblight_setrgb(0,128,0); //green + } } else { - low_battery = 0; - suspend_wakeup_init_action(); + if (steps & 0b1000) { + PORTD |= (1<<4); + rgblight_setrgb(0,0,128); //blue + } } } - } else if (display_connection_status_check_times) { - rgblight_timer_enable(); - if (ble51_task_steps == 1) { - PORTD &= ~(1<<4); - rgblight_clear(); - rgblight_set(); - } else if (ble51_task_steps == 3) { - PORTD |= (1<<4); - uint8_t g_color = bt_connected? 128:0; - uint8_t b_color = bt_connected? 0:128; - rgblight_setrgb(0,g_color,b_color); - } - if ((!bt_connected && ble51_task_steps >= 5) || ble51_task_steps >= 11) { - ble51_task_steps = 0; - } + ws2812_setleds(rgbled); + } + // capslock + else if (host_keyboard_leds() & (1<. #include "rgblight.h" -void led_set_user(uint8_t usb_led) -{ - if (usb_led & (1<event.pressed) { - switch (keycode) { - case USER00: - command_extra(KC_U); - break; - case USER01: //RESET - command_extra(KC_B); - break; - case USER02: //BATTERY LEVEL - command_extra(KC_V); - break; - case USER03: //LOCK MODE - command_extra(KC_L); - break; - case USER04 ... USER12: - rgblight_action(keycode - USER04); - break; + static const uint8_t userx_to_command[4] = { + KC_U, // 0 Host Switch + KC_B, // 1 Reset + KC_V, // 2 Output Battery Value + KC_L // 3 Lock Mode + }; + 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/duang60v2/matrix.c b/keyboards/ydkb/duang60v2/matrix.c index 3b7071f7bf7..0e98698a3cd 100644 --- a/keyboards/ydkb/duang60v2/matrix.c +++ b/keyboards/ydkb/duang60v2/matrix.c @@ -1,18 +1,34 @@ +/* +Copyright 2023 YANG + +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 . +*/ + +/* + * scan matrix + */ #include #include -#include -#include #include #include -#include "avr_config.h" #include "print.h" #include "debug.h" #include "util.h" -#include "action.h" #include "command.h" -#include "keyboard.h" #include "timer.h" #include "matrix.h" +#include "debounce_pk.h" #include "suspend.h" #include "lufa.h" #include "rgblight.h" @@ -20,17 +36,12 @@ #include "ble51_task.h" #include "switch_board.h" -#define DEBOUNCE_DN_MASK (uint8_t)(~(0x80 >> 5)) -#define DEBOUNCE_UP_MASK (uint8_t)(0x80 >> 5) - -bool is_ble_version = 1; -bool no_rgblight; -extern rgblight_config_t rgblight_config; static matrix_row_t matrix[MATRIX_ROWS] = {0}; static uint16_t matrix_scan_timestamp = 0; static uint8_t matrix_debouncing[MATRIX_ROWS][MATRIX_COLS] = {0}; +static uint8_t now_debounce_dn_mask = DEBOUNCE_NK_MASK; static void select_key(uint8_t mode); static uint8_t get_key(void); @@ -44,29 +55,19 @@ void matrix_scan_kb(void) { hook_keyboard_loop(); } -inline -uint8_t matrix_rows(void) -{ - return MATRIX_ROWS; -} - -inline -uint8_t matrix_cols(void) -{ - return MATRIX_COLS; -} +extern bool no_rgblight; void hook_early_init() { // rama kara version has no rgblight. PF0 - DDRF &= ~(1<<0); - PORTF |= (1<<0); - _delay_ms(2); - if (~PINF & (1<<0)) no_rgblight = 1; + // fly60 has no rgblight and bt-sw low on. PF1 + DDRF &= ~(1<<1 | 1<<0); + PORTF |= (1<<1 | 1<<0); + WAIT_MS(2); + if (~PINF & (1<<1 | 1<<0)) no_rgblight = 1; if (pgm_read_byte(0x7ea4) == 0x91) { // USB Only version - is_ble_version = 0; ble51_boot_on = 0; } else { // PD4 for BLE Reset, PF7 for BT_SW @@ -74,15 +75,20 @@ void hook_early_init() PORTD |= (1<<4); DDRF &= ~(1<<7); PORTF |= (1<<7); - _delay_ms(2); - if (~PINF & (1<<7)) ble51_boot_on = 0; + WAIT_MS(2); + //从硬件开关判断蓝牙状态 + if (~PINF & (1<<1)) { // fly60 low-on + if (PINF & (1<<7)) ble51_boot_on = 0; + } else { + if (~PINF & (1<<7)) ble51_boot_on = 0; + } //BLE Reset if (ble_reset_key == 0xBBAA) { ble_reset_key = 0; if (ble51_boot_on) { - // PE6 for BLE Reset - DDRD |= (1<<4); - PORTD &= ~(1<<4); + // PE6 for BLE Reset, PD3 TX + DDRD |= (1<<4 | 1<<3); + PORTD &= ~(1<<4 | 1<<3); bt_power_init(); // light CapsLED DDRB |= (1<<6); @@ -96,8 +102,9 @@ void hook_early_init() void matrix_init(void) { + // led init DDRB |= (1<<6); - PORTB &= ~(1<<6); + //PORTB &= ~(1<<6); init_cols(); rgblight_init(); } @@ -108,33 +115,36 @@ uint8_t matrix_scan(void) uint16_t time_check = timer_read(); if (matrix_scan_timestamp == time_check) return 1; matrix_scan_timestamp = time_check; + uint8_t matrix_keys_down = 0; select_key(0); uint8_t *debounce = &matrix_debouncing[0][0]; - for (uint8_t row=0; row> 1) | key; - + //select next key select_key(1); - if ((*debounce > 0) && (*debounce < 255)) { + if (1) { matrix_row_t *p_row = &matrix[row]; matrix_row_t col_mask = ((matrix_row_t)1 << col); - if (*debounce >= DEBOUNCE_DN_MASK) { + if (*debounce >= DEBOUNCE_DN_MASK) { //debounce KEY DOWN *p_row |= col_mask; - } else if (*debounce <= DEBOUNCE_UP_MASK) { + } else if (*debounce <= DEBOUNCE_UP_MASK) { //debounce KEY UP *p_row &= ~col_mask; } - } - } - if (matrix[row] > 0) { - if (!rgblight_config.enable) kb_idle_times = 12; - else kb_idle_times = 0; + } + if (*debounce) matrix_keys_down++; } } - + + if (matrix_keys_down) { + if (BLE_LIGHT_ON == 0) kb_idle_times = 12; + else kb_idle_times = 0; + } + matrix_scan_quantum(); - return 1; + return matrix_keys_down; } @@ -164,7 +174,7 @@ uint8_t matrix_key_count(void) { uint8_t count = 0; for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - count += bitpop16(matrix[i]); + count += bitpop8(matrix[i]); } return count; } @@ -178,11 +188,19 @@ void init_cols(void) } -static uint8_t get_key(void) { +static uint8_t get_key(void) +{ return PINB&(1<<2) ? 0 : 0x80; } +void select_all_keys(void) +{ + DS_PL_LO(); + for (uint8_t i = 0; i < MATRIX_ROWS * MATRIX_COLS; i++) { + CLOCK_PULSE(); + } +} void unselect_rows(void) { @@ -207,7 +225,7 @@ static void select_key(uint8_t mode) bool suspend_wakeup_condition(void) { - if (BLE51_PowerState >= 10) { //lock mode + if (BLE51_PowerState>= 10) { //lock mode matrix_scan(); // K24 F, K61 J uint8_t *debounce = &matrix_debouncing[0][0]; @@ -223,10 +241,7 @@ bool suspend_wakeup_condition(void) } } else { //check all keys - DS_PL_LO(); - for (uint8_t i = 0; i < MATRIX_ROWS * MATRIX_COLS; i++) { - CLOCK_PULSE(); - } + select_all_keys(); _delay_us(5); if (get_key()) { // return true; @@ -234,3 +249,17 @@ bool suspend_wakeup_condition(void) } return false; } + +void bootmagic_lite(void) +{ + //do nothing + return; + +} + +void hook_nkro_change(void) +{ + return; + uint8_t kbd_nkro = keymap_config.nkro; + type_num(kbd_nkro?6:0); +} diff --git a/keyboards/ydkb/duang60v2/pre_compile_via/ydkb_duang60v2_via.zip b/keyboards/ydkb/duang60v2/pre_compile_via/ydkb_duang60v2_via.zip deleted file mode 100644 index 22ea5fef5bd..00000000000 Binary files a/keyboards/ydkb/duang60v2/pre_compile_via/ydkb_duang60v2_via.zip and /dev/null differ diff --git a/keyboards/ydkb/duang60v2/rgblight.c b/keyboards/ydkb/duang60v2/rgblight.c index e400911abff..49857c690e1 100644 --- a/keyboards/ydkb/duang60v2/rgblight.c +++ b/keyboards/ydkb/duang60v2/rgblight.c @@ -16,34 +16,51 @@ #define rgblight_timer_enable() do { PORTC &= ~(1<<7);} while(0) #define rgblight_timer_disable() do { PORTC |= (1<<7);} while(0) #define rgblight_timer_enabled (~PORTC & (1<<7)) + #define RGBLED_TEMP RGBLED_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}; +#define INDICATOR_NUM 0 +struct cRGB rgbled[RGBLED_NUM+INDICATOR_NUM+1]; +struct cRGB *led = &rgbled[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}; -//battery -extern bool no_rgblight; -extern bool is_ble_version; +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}; +rgblight_config_t rgblight_config = {.enable = 0, .mode = 8,.hue = 640, .sat = 255, .val = 255}; -struct cRGB led[RGBLED_NUM+1]; +bool no_rgblight; +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) { /* 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. */ uint8_t r, g, b; uint8_t temp[5]; - uint8_t n = (hue >> 8) % 3; + // 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; 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 + 2]; - g = temp[n + 1]; - b = temp[n]; + r = temp[n + 1]; + g = temp[n]; + b = temp[n - 1]; setrgb(r,g,b, led1); } @@ -98,29 +115,34 @@ void rgblight_init(void) rgblight_timer_init(); // setup the timer - if (rgblight_config.enable) { - rgblight_mode(rgblight_config.mode); - } else { - rgblight_set(); - rgblight_timer_disable(); - } + rgblight_mode(rgblight_config.mode); } +uint8_t limit_value_0to255(int16_t value) { + if (value > 255) return 255; + else if (value < 0) return 0; + else return value; +} void rgblight_mode(int8_t mode) { + // rgb off, new way to save about 60B if (!rgblight_config.enable) { - return; - } - if (mode < 0) mode = RGBLIGHT_MODES -1; - else if (mode >= RGBLIGHT_MODES) mode = 0; - rgblight_config.mode = mode; + //rgblight_clear(); + //rgblight_set(); + rgblight_timer_disable(); + rgb_fading_index = 0; + } else { + // rgb on + if (mode < 0) mode = RGBLIGHT_MODES - 1; + else if (mode >= RGBLIGHT_MODES) mode = 0; + rgblight_config.mode = mode; + dprintf("rgblight mode: %u\n", rgblight_config.mode); - eeconfig_write_rgblight(rgblight_config.raw); - dprintf("rgblight mode: %u\n", rgblight_config.mode); - if (rgblight_config.enable) { rgblight_timer_enable(); } + // save config. rgblight_sethsv() will save config. + //eeconfig_write_rgblight(rgblight_config.raw); rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } @@ -128,17 +150,8 @@ inline void rgblight_toggle(void) { rgblight_config.enable ^= 1; - eeconfig_write_rgblight(rgblight_config.raw); dprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable); - if (rgblight_config.enable) { - rgblight_mode(rgblight_config.mode); - } else { - rgblight_clear(); - rgblight_set(); - if (USB_DeviceState != DEVICE_STATE_Configured) { - rgblight_timer_disable(); - } - } + rgblight_mode(rgblight_config.mode); } @@ -151,8 +164,8 @@ void rgblight_action(uint8_t action) 7 val- 8 val+ */ uint16_t hue = rgblight_config.hue; - int16_t sat = rgblight_config.sat; - int16_t val = rgblight_config.val; + uint8_t sat = rgblight_config.sat; + uint8_t val = rgblight_config.val; int8_t increament = 1; if (action & 1) increament = -1; if (get_mods() & MOD_BIT(KC_LSHIFT)) { @@ -168,19 +181,15 @@ void rgblight_action(uint8_t action) break; case 3: case 4: - hue = (rgblight_config.hue + 768 + RGBLIGHT_HUE_STEP * increament) % 768; + hue = rgblight_config.hue + RGBLIGHT_HUE_STEP * increament; break; case 5: case 6: - sat = rgblight_config.sat + RGBLIGHT_SAT_STEP * increament; - if (sat > 255) sat = 255; - if (sat < 0) sat = 0; + sat = limit_value_0to255(rgblight_config.sat + RGBLIGHT_SAT_STEP * increament); break; case 7: case 8: - val = rgblight_config.val + RGBLIGHT_VAL_STEP * increament; - if (val > 255) val = 255; - if (val < 0) val = 0; + val = limit_value_0to255(rgblight_config.val + RGBLIGHT_VAL_STEP * increament); break; default: break; @@ -201,38 +210,57 @@ 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) { + hue = hue_fix(hue); if (rgblight_config.enable) { + #if 0 // will do rgblight_sethsv_noeeprom() in rgblight_task if (rgblight_config.mode == 1) { // same static color rgblight_sethsv_noeeprom(hue, sat, val); } + #endif rgblight_config.hue = hue; rgblight_config.sat = sat; rgblight_config.val = val; - eeconfig_write_rgblight(rgblight_config.raw); dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } + // when rgblight_config.enable == 0, just save config.raw. + eeconfig_write_rgblight(rgblight_config.raw); } void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { - for (uint8_t i=0;i= 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 15 case 15 ... 20: - rgblight_effect_snake(rgblight_config.mode-13); + 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); break; + #endif } } else { rgblight_timer_disable(); @@ -279,7 +311,8 @@ void rgblight_effect_breathing(uint8_t interval) static int8_t increament = 1; rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos])); pos = pos + interval*increament; - if (pos < interval || pos+interval > 126) { + if (pos < interval || pos+interval > 62) { + //if (pos < interval || pos+interval > 126) { increament *= -1; } } @@ -288,7 +321,7 @@ 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 = (current_hue + interval * 3) % 768; + current_hue = hue_fix(current_hue + interval * 3); } void rgblight_effect_rainbow_swirl(uint8_t interval) @@ -297,17 +330,16 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) uint16_t hue; uint8_t i; uint8_t interval2 = interval/2; + if (interval & 1) interval2 *= -1; for (i=0; i 15 void rgblight_effect_snake(uint8_t interval) { static int8_t pos = 0 - RGBLIGHT_EFFECT_SNAKE_LENGTH; @@ -320,7 +352,11 @@ void rgblight_effect_snake(uint8_t interval) 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]); } pos += increament; if (pos > RGBLED_NUM) pos = 0; @@ -328,7 +364,9 @@ void rgblight_effect_snake(uint8_t interval) rgblight_set(); } } +#endif +#if RGBLIGHT_MODES > 21 void rgblight_effect_knight(uint8_t interval) { static int8_t pos = RGBLED_NUM - 1; @@ -341,76 +379,79 @@ void rgblight_effect_knight(uint8_t interval) sled_step = 0; rgblight_clear(); for (i=0; i= 0){ + int8_t target_col = pos+i; + if (target_col < RGBLED_NUM && target_col >= 0){ need_update = 1; - uint8_t tmp_col = pos+i; - led[tmp_col%RGBLED_NUM] = led[RGBLED_TEMP]; + led[target_col] = led[RGBLED_TEMP]; } } if (need_update) rgblight_set(); //Keep the first or last col on when increament changes. pos += increament; if (pos <= 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH || pos >= RGBLED_NUM) { increament *= -1; - current_hue = (current_hue + 40) % 768; + current_hue = hue_fix(current_hue + 40); sethsv(current_hue, rgblight_config.sat, rgblight_config.val, &led[RGBLED_TEMP]); } } } +#endif void suspend_power_down_action(void) { PORTB &= ~(1<<6); rgblight_timer_disable(); //RGB_VCC off + rgb_fading_index = 0; } void suspend_wakeup_init_action(void) { rgblight_init(); - if (BLE51_PowerState >= 4) display_connection_status_check_times = 1; } void hook_keyboard_loop() { + if (BLE51_PowerState > 1) return; static uint16_t rgb_update_timer = 0; - if (rgblight_timer_enabled && timer_elapsed(rgb_update_timer) > 40) { + static uint8_t steps = 0; + if (timer_elapsed(rgb_update_timer) > 40) { rgb_update_timer = timer_read(); - if (!display_connection_status_check_times || !ble51_boot_on) rgblight_task(); - } -} + if (!display_connection_status_check_times) rgblight_task(); -void ble51_task_user(void) -{ - static uint8_t ble51_task_steps = 0; - static uint16_t battery_timer = 0; - if (timer_elapsed(battery_timer) > 150) { - battery_timer = timer_read(); - ble51_task_steps++; - if (low_battery) { - if (ble51_task_steps > 3) { - ble51_task_steps = (low_battery == 1)? 3:0; //value 1 is extremely low battery - if (PINC & (1<<6)) { //not charging - rgblight_timer_disable(); - PORTB ^= (1<<6); + if ((steps++ & 0b11)) return; + //led_status_task(); + // run every 4*40 = 160ms + PORTB &= ~(1<<6); //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) PORTC |= (1<<6); + } else { + if (bt_connected) { + if (steps & 0b11000) { + PORTB |= (1<<6); + rgblight_setrgb(0,128,0); //green + } } else { - low_battery = 0; - suspend_wakeup_init_action(); + if (steps & 0b1000) { + PORTB |= (1<<6); + rgblight_setrgb(0,0,128); //blue + } } } - } else if (display_connection_status_check_times) { - rgblight_timer_enable(); - if (ble51_task_steps == 1) { - PORTB &= ~(1<<6); - rgblight_clear(); - rgblight_set(); - } else if (ble51_task_steps == 3) { - PORTB |= (1<<6); - uint8_t g_color = bt_connected? 128:0; - uint8_t b_color = bt_connected? 0:128; - rgblight_setrgb(0,g_color,b_color); - } - if ((!bt_connected && ble51_task_steps >= 5) || ble51_task_steps >= 11) { - ble51_task_steps = 0; - } + rgblight_set(); + } + // capslock + else if (host_keyboard_leds() & (1<. #include "command.h" #include "ble51.h" -extern bool is_980c; +void led_all_off(void) { + DDRF |= (1<event.pressed) { - switch (keycode) { - case USER00: - command_extra(KC_U); - break; - case USER01: //RESET - command_extra(KC_B); - break; - case USER02: //BATTERY LEVEL - command_extra(KC_V); - break; - case USER03: //LOCK MODE - command_extra(KC_L); - break; - case USER04: - if (PORTF & (1<= USER00 && keycode < USER04) command_extra(userx_to_command[keycode-USER00]); + else if (keycode == USER04) DDRF ^= (1<<7); + } + #if 0 + static uint8_t mod_keys_registered; + uint8_t pressed_mods = get_mods(); + switch (keycode) { + // 0x5f8f for Alt+Esc=f4 and RShift+Esc=~ + case 0x5F8F: + if (record->event.pressed) { + if ((pressed_mods & MOD_BIT(KC_RSHIFT)) && (~pressed_mods & MOD_BIT(KC_LCTRL))) { + mod_keys_registered = KC_GRV; + } else if (pressed_mods & MOD_BIT(KC_LALT)) { + mod_keys_registered = KC_F4; } else { - DDRF &= ~(1< +Copyright 2023 YANG 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 @@ -31,6 +31,7 @@ along with this program. If not, see . #include "keyboard.h" #include "timer_avr.h" #include "matrix.h" +#include "debounce_pk.h" #include "suspend.h" #include "lufa.h" #include "ble51.h" @@ -38,69 +39,63 @@ along with this program. If not, see . #include "switch_board.h" - // matrix state buffer(1:on, 0:off) static matrix_row_t *matrix; static matrix_row_t *matrix_prev; -static matrix_row_t _matrix0[MATRIX_ROWS]; -static matrix_row_t _matrix1[MATRIX_ROWS]; +static matrix_row_t _matrix0[MATRIX_ROWS] = {0}; +static matrix_row_t _matrix1[MATRIX_ROWS] = {0}; static uint8_t matrix_keys_down = 0; + + +static uint8_t matrix_debouncing[90] = {0}; //JP matrix is 16*5. Pro is 8*8 + struct { uint8_t rows; uint8_t final_col; } hhkb_matrix; +void matrix_scan_user(void) {} +__attribute__ ((weak)) +void matrix_scan_kb(void) { + matrix_scan_user(); + hook_keyboard_loop(); +} static uint8_t wake_scan = 0; bool is_ver_jp = 0; -bool is_ver25 = 0; + void hook_early_init() { /* led init */ - DDRF |= (1<= 10) _delay_us(3); // 必需,否则无法识别到按键或者连击。但是节能时可以不用。 + + uint8_t key = (~PIND & (1<<7))? 0x80 : 0; // save key state + + *debounce = (*debounce >> 1) | key; + KEY_PREV_OFF(); + KEY_UNABLE(); // 确定在return 100前执行 - if (KEY_STATE()) { - matrix[row] &= ~(1<= DEBOUNCE_DN_MASK) { + kb_idle_times = 0; + matrix_keys_down++; + matrix[row] |= (1< 0) { - if (BLE51_PowerState < 2) { - kb_idle_times = 0; - } else if (BLE51_PowerState < 10) { - return 100; - } - } - - _delay_us(5); - KEY_PREV_OFF(); - KEY_UNABLE(); + //_delay_us(5); //作用未知,去除后似乎无影响。 + //KEY_PREV_OFF(); + //KEY_UNABLE(); if (BLE51_PowerState >= 2) { - _delay_us(10); // scan faster when power saving - if (wake_scan && r == 7 && c == 4) { // wake scan complete + //似乎部分从Lock Mode唤醒还需要这个。 + //如果我在一个C1和C3拆除的HHKB上不使用这个延迟,正常时又会触发2。用10us是可以的,3us会2 + _delay_us(6); // scan faster when power saving + if (wake_scan && row == 7 && col == 6) { // wake scan complete + //if (wake_scan && row == (hhkb_matrix_rows >> 1 )) { // 4的时候US正常(12号发的固件,13号接到有人反应2唤醒时不能按),JP节能时无法唤醒。 wake_scan = 0; return 1; } } else { // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE. // This takes 25us or more to make sure KEY_STATE returns to idle state. - _delay_us(30); + //_delay_us(20); } } } - + // 二级节能唤醒检测 + if (BLE51_PowerState == 4 && matrix_keys_down) return 100; if (BLE51_PowerState >= 10) { //after a whole scan, check F and J when LOCK MODE // jp version, matrix[5,6] is F, matrix[9,6] is J. @@ -215,8 +222,9 @@ uint8_t matrix_scan(void) if (matrix[1] == (1<<5) && matrix[4] == (1<<5)) { return 100; } - } + } else if (!ble51_boot_on && matrix_keys_down) return 100; //蓝牙功能关闭时唤醒电脑 } + matrix_scan_quantum(); return 1; } @@ -239,15 +247,25 @@ void matrix_print(void) bool suspend_wakeup_condition(void) { static uint8_t sleep_timer = 0; - if (BLE51_PowerState >= 4) { + if (BT_POWERED == 0) { if (++sleep_timer < 80) return false; else sleep_timer = 0; } KEY_POWER_ON(); + // 如果不使用wake_scan,前4行的按键节能时无法识别。wake_scan也只需要扫到第5行即可。 wake_scan = 1; matrix_scan(); + + /* some 660c(maybe with tp1685) need one more scan when deep sleep. But maybe there is no hhkb pro2 with tp1685 + * 一个 hhkb pro2 (ajajz),如果隔太久(一晚上),Lock Mode 无法唤醒,尝试加回下面这一条。添加回后他和另一个人测试正常。 */ + if (BT_POWERED == 0) { + matrix_scan(); + //matrix_scan(); //因为加入了防抖,多扫一次。不在这里使用,在matrix.c的防抖里面直接多处理一次 + } + //*/ if (matrix_scan() == 100) { + //KEY_UNABLE(); return true; } KEY_POWER_OFF(); @@ -256,47 +274,60 @@ bool suspend_wakeup_condition(void) void suspend_power_down_action(void) { - KEY_POWER_OFF(); - PORTF &= ~(1<=4) { + if (BT_POWERED == 0) { PORTF |= (1< 1) return; + static uint8_t steps = 0; static uint16_t battery_timer = 0; - if (timer_elapsed(battery_timer) > 150) { + if (timer_elapsed(battery_timer) > 160) { battery_timer = timer_read(); - ble51_task_steps++; - if (low_battery) { - if (ble51_task_steps > 3) { - ble51_task_steps = (low_battery == 1)? 3:0; //value 1 is extremely low battery - if (PINF&(1<= 5) || ble51_task_steps >= 11 ) { - ble51_task_steps = 0; - } } + // capslock + else if (host_keyboard_leds() & (1<. #include "rgblight.h" -void led_set_user(uint8_t usb_led) -{ - if (usb_led & (1<event.pressed) { - switch (keycode) { - case USER00: - command_extra(KC_U); - break; - case USER01: //RESET - command_extra(KC_B); - break; - case USER02: //BATTERY LEVEL - command_extra(KC_V); - break; - case USER03: //LOCK MODE - command_extra(KC_L); - break; - case USER04 ... USER12: - rgblight_action(keycode - USER04); - break; + static const uint8_t userx_to_command[4] = { + KC_U, // 0 Host Switch + KC_B, // 1 Reset + KC_V, // 2 Output Battery Value + KC_L // 3 Lock Mode + }; + if (keycode >= USER00) { + if (keycode < USER04) command_extra(userx_to_command[keycode-USER00]); + else if (keycode <= USER12) rgblight_action(keycode - USER04); } } } diff --git a/keyboards/ydkb/just68v2/matrix.c b/keyboards/ydkb/just68v2/matrix.c index 9efac5a2019..19500257069 100644 --- a/keyboards/ydkb/just68v2/matrix.c +++ b/keyboards/ydkb/just68v2/matrix.c @@ -1,3 +1,23 @@ +/* +Copyright 2023 YANG + +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 . +*/ + +/* + * scan matrix + */ #include #include #include @@ -13,6 +33,7 @@ #include "keyboard.h" #include "timer.h" #include "matrix.h" +#include "debounce_pk.h" #include "suspend.h" #include "lufa.h" #include "rgblight.h" @@ -20,10 +41,6 @@ #include "ble51_task.h" #include "switch_board.h" -#define DEBOUNCE_DN_MASK (uint8_t)(~(0x80 >> 5)) -#define DEBOUNCE_UP_MASK (uint8_t)(0x80 >> 5) - -bool is_ble_version = 1; extern rgblight_config_t rgblight_config; static matrix_row_t matrix[MATRIX_ROWS] = {0}; @@ -33,6 +50,7 @@ static uint8_t matrix_current_row = 0; static uint16_t matrix_scan_timestamp = 0; static uint8_t matrix_debouncing[MATRIX_ROWS][MATRIX_COLS] = {0}; static uint8_t encoder_state_prev[1][2] = {0}; +static uint8_t now_debounce_dn_mask = DEBOUNCE_NK_MASK; static void select_key(uint8_t mode); static uint8_t get_key(uint8_t col); @@ -46,23 +64,10 @@ void matrix_scan_kb(void) { hook_keyboard_loop(); } -inline -uint8_t matrix_rows(void) -{ - return MATRIX_ROWS; -} - -inline -uint8_t matrix_cols(void) -{ - return MATRIX_COLS; -} - void hook_early_init() { if (pgm_read_byte(0x7ea0) == 0x77) { // USB Only version - is_ble_version = 0; ble51_boot_on = 0; } else { // PD1 for BLE Reset, PF0 for BT_SW @@ -84,7 +89,7 @@ void hook_early_init() DDRC |= (1<<6); PORTC |= (1<<6); _delay_ms(5000); - bootloader_jump(); + bootloader_jump(); } } } @@ -93,7 +98,7 @@ void hook_early_init() void matrix_init(void) { DDRC |= (1<<6); - PORTC &= ~(1<<6); + //PORTC &= ~(1<<6); init_cols(); rgblight_init(); } @@ -101,15 +106,16 @@ void matrix_init(void) uint8_t matrix_scan(void) { - uint16_t time_check = timer_read(); - if (matrix_scan_timestamp == time_check) return 1; - matrix_scan_timestamp = time_check; + //uint16_t time_check = timer_read(); + //if (matrix_scan_timestamp == time_check) return 1; + //matrix_scan_timestamp = time_check; + uint8_t matrix_keys_down = 0; select_key(0); uint8_t *debounce = &matrix_debouncing[0][0]; - for (uint8_t row=0; row> 1) | key; if (real_col >= 8) select_key(1); - + //if ((*debounce > 0) && (*debounce < 255)) { if (1) { matrix_row_t *p_row = &matrix[row]; matrix_row_t col_mask = ((matrix_row_t)1 << real_col); - if (*debounce >= DEBOUNCE_DN_MASK) { + if (*debounce >= DEBOUNCE_DN_MASK) { //debounce KEY DOWN *p_row |= col_mask; - } else if (*debounce <= DEBOUNCE_UP_MASK) { + } else if (*debounce <= DEBOUNCE_UP_MASK) { //debounce KEY UP *p_row &= ~col_mask; } } - } - if (matrix[row] > 0) { - if (!rgblight_config.enable) kb_idle_times = 12; - else kb_idle_times = 0; + if (*debounce) matrix_keys_down++; } } - + + if (matrix_keys_down) { + if (BLE_LIGHT_ON == 0) kb_idle_times = 12; + else kb_idle_times = 0; + } + matrix_scan_quantum(); - return 1; + return matrix_keys_down; } @@ -239,48 +247,30 @@ static void select_key(uint8_t mode) bool suspend_wakeup_condition(void) { - if (BLE51_PowerState >= 10) { //lock mode - matrix_scan(); - // ver595: Key1_S24 F (debounce[2][8]), Key2_K20 J (debounce[2][1]) - uint8_t *debounce = &matrix_debouncing[0][0]; - uint8_t matrix_keys_down = 0; - for (uint8_t i=0; i< MATRIX_ROWS * MATRIX_COLS; i++, *debounce++) { - if (*debounce > 0) { - if (i == KP(2,1) || i == KP(2,8)) matrix_keys_down += 100; - else matrix_keys_down++; - } - } - if (matrix_keys_down == 200) { - return true; - } else if (!ble51_boot_on && matrix_keys_down) return true; - } else { - //check encoder and two key - DS_PL_LO(); - for (uint8_t i = 0; i < 2; i++) { - for (uint8_t j = 0; j < 8; j++) { - CLOCK_PULSE(); - DS_PL_HI(); - } - _delay_us(6); - uint8_t key1 = PINF&(1<<5); - uint8_t encoder_state = PINF&(1<<1) ? 0 : 1; - if (encoder_state != encoder_state_prev[0][i] || key1 == 0) { - return true; - } - } + uint8_t matrix_keys_down = matrix_scan(); + if (matrix_keys_down == 0) return false; - //check other keys - for (uint8_t i = 0; i < 5; i++) { - for (uint8_t j = 0; j < 8; j++) { - if (i >= 3 && j == 0) DS_PL_HI(); - else DS_PL_LO(); - CLOCK_PULSE(); - } - } - _delay_us(6); - if ( (PINF&0b100010) < 0b100010) { // - return true; + if (BLE51_PowerState >= 10) {//lock mode + if (matrix_keys_down == 2) { + // ver595: Key1_S24 F (debounce[2][8]), Key2_K20 J (debounce[2][1]) + if (matrix_debouncing[2][8] == 0xff && matrix_debouncing[2][1] == 0xff) return true; } + if (!ble51_boot_on) return true; //ܹرʱѵ + return false; } - return false; + return matrix_keys_down; } + +void bootmagic_lite(void) +{ + //do nothing + return; + +} + +void hook_nkro_change(void) +{ + return; + uint8_t kbd_nkro = keymap_config.nkro; + type_num(kbd_nkro?6:0); +} \ No newline at end of file diff --git a/keyboards/ydkb/just68v2/rgblight.c b/keyboards/ydkb/just68v2/rgblight.c index efbb508d81f..d5ce9e77ccd 100644 --- a/keyboards/ydkb/just68v2/rgblight.c +++ b/keyboards/ydkb/just68v2/rgblight.c @@ -16,33 +16,50 @@ #define rgblight_timer_enable() do { PORTE &= ~(1<<6);} while(0) #define rgblight_timer_disable() do { PORTE |= (1<<6);} while(0) #define rgblight_timer_enabled (~PORTE & (1<<6)) + #define RGBLED_TEMP RGBLED_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}; +#define INDICATOR_NUM 0 +struct cRGB rgbled[RGBLED_NUM+INDICATOR_NUM+1]; +struct cRGB *led = &rgbled[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}; -//battery -extern bool is_ble_version; +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}; -struct cRGB led[RGBLED_NUM+1]; +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) { /* 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. */ uint8_t r, g, b; uint8_t temp[5]; - uint8_t n = (hue >> 8) % 3; + // 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; 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 + 2]; - g = temp[n + 1]; - b = temp[n]; + r = temp[n + 1]; + g = temp[n]; + b = temp[n - 1]; setrgb(r,g,b, led1); } @@ -97,29 +114,34 @@ void rgblight_init(void) rgblight_timer_init(); // setup the timer - if (rgblight_config.enable) { - rgblight_mode(rgblight_config.mode); - } else { - rgblight_set(); - rgblight_timer_disable(); - } + rgblight_mode(rgblight_config.mode); } +uint8_t limit_value_0to255(int16_t value) { + if (value > 255) return 255; + else if (value < 0) return 0; + else return value; +} void rgblight_mode(int8_t mode) { + // rgb off, new way to save about 60B if (!rgblight_config.enable) { - return; - } - if (mode < 0) mode = RGBLIGHT_MODES -1; - else if (mode >= RGBLIGHT_MODES) mode = 0; - rgblight_config.mode = mode; + //rgblight_clear(); + //rgblight_set(); + rgblight_timer_disable(); + rgb_fading_index = 0; + } else { + // rgb on + if (mode < 0) mode = RGBLIGHT_MODES - 1; + else if (mode >= RGBLIGHT_MODES) mode = 0; + rgblight_config.mode = mode; + dprintf("rgblight mode: %u\n", rgblight_config.mode); - eeconfig_write_rgblight(rgblight_config.raw); - dprintf("rgblight mode: %u\n", rgblight_config.mode); - if (rgblight_config.enable) { rgblight_timer_enable(); } + // save config. rgblight_sethsv() will save config. + //eeconfig_write_rgblight(rgblight_config.raw); rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } @@ -127,17 +149,8 @@ inline void rgblight_toggle(void) { rgblight_config.enable ^= 1; - eeconfig_write_rgblight(rgblight_config.raw); dprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable); - if (rgblight_config.enable) { - rgblight_mode(rgblight_config.mode); - } else { - rgblight_clear(); - rgblight_set(); - if (USB_DeviceState != DEVICE_STATE_Configured) { - rgblight_timer_disable(); - } - } + rgblight_mode(rgblight_config.mode); } @@ -150,8 +163,8 @@ void rgblight_action(uint8_t action) 7 val- 8 val+ */ uint16_t hue = rgblight_config.hue; - int16_t sat = rgblight_config.sat; - int16_t val = rgblight_config.val; + uint8_t sat = rgblight_config.sat; + uint8_t val = rgblight_config.val; int8_t increament = 1; if (action & 1) increament = -1; if (get_mods() & MOD_BIT(KC_LSHIFT)) { @@ -167,19 +180,15 @@ void rgblight_action(uint8_t action) break; case 3: case 4: - hue = (rgblight_config.hue + 768 + RGBLIGHT_HUE_STEP * increament) % 768; + hue = rgblight_config.hue + RGBLIGHT_HUE_STEP * increament; break; case 5: case 6: - sat = rgblight_config.sat + RGBLIGHT_SAT_STEP * increament; - if (sat > 255) sat = 255; - if (sat < 0) sat = 0; + sat = limit_value_0to255(rgblight_config.sat + RGBLIGHT_SAT_STEP * increament); break; case 7: case 8: - val = rgblight_config.val + RGBLIGHT_VAL_STEP * increament; - if (val > 255) val = 255; - if (val < 0) val = 0; + val = limit_value_0to255(rgblight_config.val + RGBLIGHT_VAL_STEP * increament); break; default: break; @@ -200,45 +209,60 @@ 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) { + hue = hue_fix(hue); if (rgblight_config.enable) { + #if 0 // will do rgblight_sethsv_noeeprom() in rgblight_task if (rgblight_config.mode == 1) { // same static color rgblight_sethsv_noeeprom(hue, sat, val); } + #endif rgblight_config.hue = hue; rgblight_config.sat = sat; rgblight_config.val = val; - eeconfig_write_rgblight(rgblight_config.raw); dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } + // when rgblight_config.enable == 0, just save config.raw. + eeconfig_write_rgblight(rgblight_config.raw); } void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { - for (uint8_t i=0;i= 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 15 case 15 ... 20: - rgblight_effect_snake(rgblight_config.mode-13); + 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); break; + #endif } } else { rgblight_timer_disable(); @@ -273,7 +301,8 @@ void rgblight_effect_breathing(uint8_t interval) static int8_t increament = 1; rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos])); pos = pos + interval*increament; - if (pos < interval || pos+interval > 126) { + if (pos < interval || pos+interval > 62) { + //if (pos < interval || pos+interval > 126) { increament *= -1; } } @@ -282,7 +311,7 @@ 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 = (current_hue + interval * 3) % 768; + current_hue = hue_fix(current_hue + interval * 3); } void rgblight_effect_rainbow_swirl(uint8_t interval) @@ -291,17 +320,16 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) uint16_t hue; uint8_t i; uint8_t interval2 = interval/2; + if (interval & 1) interval2 *= -1; for (i=0; i 15 void rgblight_effect_snake(uint8_t interval) { static int8_t pos = 0 - RGBLIGHT_EFFECT_SNAKE_LENGTH; @@ -314,7 +342,11 @@ void rgblight_effect_snake(uint8_t interval) 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]); } pos += increament; if (pos > RGBLED_NUM) pos = 0; @@ -322,7 +354,9 @@ void rgblight_effect_snake(uint8_t interval) rgblight_set(); } } +#endif +#if RGBLIGHT_MODES > 21 void rgblight_effect_knight(uint8_t interval) { static int8_t pos = RGBLED_NUM - 1; @@ -335,76 +369,79 @@ void rgblight_effect_knight(uint8_t interval) sled_step = 0; rgblight_clear(); for (i=0; i= 0){ + int8_t target_col = pos+i; + if (target_col < RGBLED_NUM && target_col >= 0){ need_update = 1; - uint8_t tmp_col = pos+i; - led[tmp_col%RGBLED_NUM] = led[RGBLED_TEMP]; + led[target_col] = led[RGBLED_TEMP]; } } if (need_update) rgblight_set(); //Keep the first or last col on when increament changes. pos += increament; if (pos <= 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH || pos >= RGBLED_NUM) { increament *= -1; - current_hue = (current_hue + 40) % 768; + current_hue = hue_fix(current_hue + 40); sethsv(current_hue, rgblight_config.sat, rgblight_config.val, &led[RGBLED_TEMP]); } } } +#endif void suspend_power_down_action(void) { PORTC &= ~(1<<6); rgblight_timer_disable(); //RGB_VCC off + rgb_fading_index = 0; } void suspend_wakeup_init_action(void) { rgblight_init(); - if (BLE51_PowerState >= 4) display_connection_status_check_times = 1; } void hook_keyboard_loop() { + if (BLE51_PowerState > 1) return; static uint16_t rgb_update_timer = 0; - if (rgblight_timer_enabled && timer_elapsed(rgb_update_timer) > 40) { + static uint8_t steps = 0; + if (timer_elapsed(rgb_update_timer) > 40) { rgb_update_timer = timer_read(); - if (!display_connection_status_check_times || !ble51_boot_on) rgblight_task(); - } -} + if (!display_connection_status_check_times) rgblight_task(); -void ble51_task_user(void) -{ - static uint8_t ble51_task_steps = 0; - static uint16_t battery_timer = 0; - if (timer_elapsed(battery_timer) > 150) { - battery_timer = timer_read(); - ble51_task_steps++; - if (low_battery) { - if (ble51_task_steps > 3) { - ble51_task_steps = (low_battery == 1)? 3:0; //value 1 is extremely low battery - if (PINC & (1<<7)) { //not charging - rgblight_timer_disable(); - PORTC ^= (1<<6); + if ((steps++ & 0b11)) return; + //led_status_task(); + // run every 4*40 = 160ms + PORTC &= ~(1<<6); //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) PORTC |= (1<<6); + } else { + if (bt_connected) { + if (steps & 0b11000) { + PORTC |= (1<<6); + rgblight_setrgb(0,128,0); //green + } } else { - low_battery = 0; - suspend_wakeup_init_action(); + if (steps & 0b1000) { + PORTC |= (1<<6); + rgblight_setrgb(0,0,128); //blue + } } } - } else if (display_connection_status_check_times) { - rgblight_timer_enable(); - if (ble51_task_steps == 1) { - PORTC &= ~(1<<6); - rgblight_clear(); - rgblight_set(); - } else if (ble51_task_steps == 3) { - PORTC |= (1<<6); - uint8_t g_color = bt_connected? 255:0; - uint8_t b_color = bt_connected? 0:255; - rgblight_setrgb(0,g_color,b_color); - } - if ((!bt_connected && ble51_task_steps >= 5) || ble51_task_steps >= 11) { - ble51_task_steps = 0; - } + ws2812_setleds(rgbled); + } + // capslock + else if (host_keyboard_leds() & (1< + +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 . +*/ + +#include +#include +#include +#include "backlight.h" +#include "timer.h" + + +void backlight_init_ports(void) { + DDRC &= ~(1<<6); + PORTC |= (1<<6); +} + +void backlight_task(void) { + return; +} + +void backlight_user_enable(void) +{ + + DDRC |= (1<<6); + PORTC &= ~(1<<6); +} + +void backlight_user_disable(void) +{ + DDRC &= ~(1<<6); + PORTC |= (1<<6); +} + +void backlight_set(uint8_t level) +{ + if (level) backlight_user_enable(); + else backlight_user_disable(); +} + diff --git a/keyboards/ydkb/louise/config.h b/keyboards/ydkb/louise/config.h index 64f5633f4bf..dbbf9112b8c 100644 --- a/keyboards/ydkb/louise/config.h +++ b/keyboards/ydkb/louise/config.h @@ -3,9 +3,10 @@ #include "config_common.h" /* USB Device descriptor parameter */ -#define FW_VER QMK_DMCM -#define FW_VER_VIA VIA_DMCM -#define FW_VER_VIAL VIAL_DMCM +#define FW_VER_DATE DO6A +#define CONTACT(x,y) x##y +#define CONTACT2(x,y) CONTACT(x,y) +#define FW_VER CONTACT2(VIAL_, FW_VER_DATE) #define VENDOR_ID 0x9D5B #define PRODUCT_ID 0x2141 #define DEVICE_VER 0x0001 @@ -17,12 +18,6 @@ #define MATRIX_COLS 16 - -#define TAPPING_TOGGLE 2 - -#define TAPPING_TERM 200 -#define IGNORE_MOD_TAP_INTERRUPT // this makes it possible to do rolling combos (zx) with keys that convert to other keys on hold (z becomes ctrl when you hold it, and when this option isn't enabled, z rapidly followed by x actually sends Ctrl-x. That's bad.) - #define BACKLIGHT_PIN C6 #define BACKLIGHT_LEVELS 6 #define BACKLIGHT_ON_STATE 0 @@ -37,13 +32,12 @@ #define ws2812_DDRREG DDRD #define ws2812_pin PD7 #define RGBLED_NUM 8 // 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 -/* fix space cadet rollover issue */ -#define DISABLE_SPACE_CADET_ROLLOVER #if defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__) #define UCSR1D _SFR_MEM8(0xCB) @@ -70,9 +64,10 @@ #endif /* BT Power Control */ #define BT_POWERED (~PORTD & (1<<5)) -#define bt_power_init() do { DDRD |= (1<<5); PORTD &= ~(1<<5);} while(0) -#define turn_off_bt() do { PORTD |= (1<<5); UCSR1B &= ~(1<. #include "rgblight.h" -void led_set_user(uint8_t usb_led) -{ - if (usb_led & (1<event.pressed) { - switch (keycode) { - case USER00: - command_extra(KC_U); - break; - case USER01: //RESET - command_extra(KC_B); - break; - case USER02: //BATTERY LEVEL - command_extra(KC_V); - break; - case USER03: //LOCK MODE - command_extra(KC_L); - break; - case USER04 ... USER11: - rgblight_action(keycode - USER04); - break; + static const uint8_t userx_to_command[4] = { + KC_U, // 0 Host Switch + KC_B, // 1 Reset + KC_V, // 2 Output Battery Value + KC_L // 3 Lock Mode + }; + 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/louise/matrix.c b/keyboards/ydkb/louise/matrix.c index b300371909c..87dc6331669 100644 --- a/keyboards/ydkb/louise/matrix.c +++ b/keyboards/ydkb/louise/matrix.c @@ -1,3 +1,23 @@ +/* +Copyright 2023 YANG + +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 . +*/ + +/* + * scan matrix + */ #include #include #include @@ -13,6 +33,7 @@ #include "keyboard.h" #include "timer.h" #include "matrix.h" +#include "debounce_pk.h" #include "suspend.h" #include "lufa.h" #include "rgblight.h" @@ -20,16 +41,12 @@ #include "ble51_task.h" #include "switch_board.h" -#define DEBOUNCE_DN_MASK (uint8_t)(~(0x80 >> 5)) -#define DEBOUNCE_UP_MASK (uint8_t)(0x80 >> 5) - -bool is_ble_version = 1; -extern rgblight_config_t rgblight_config; static matrix_row_t matrix[MATRIX_ROWS] = {0}; static uint16_t matrix_scan_timestamp = 0; static uint8_t matrix_debouncing[MATRIX_ROWS][MATRIX_COLS] = {0}; +static uint8_t now_debounce_dn_mask = DEBOUNCE_NK_MASK; static void select_key(uint8_t mode); static uint8_t get_key(uint8_t col); @@ -43,18 +60,6 @@ void matrix_scan_kb(void) { hook_keyboard_loop(); } -inline -uint8_t matrix_rows(void) -{ - return MATRIX_ROWS; -} - -inline -uint8_t matrix_cols(void) -{ - return MATRIX_COLS; -} - static void get_key_ready(void) { DDRB &= ~(1<<3); PORTB |= (1<<3); @@ -69,7 +74,6 @@ void hook_early_init() { if (pgm_read_byte(0x7e65) == 0x4c) { // USB Only version - is_ble_version = 0; ble51_boot_on = 0; } else { // PF7 for BLE Reset, PE2 for BT_SW @@ -87,13 +91,12 @@ void hook_early_init() DDRF |= (1<> 1) | key; if (real_col >= 8) select_key(1); - + //if ((*debounce > 0) && (*debounce < 255)) { if (1) { matrix_row_t *p_row = &matrix[row]; matrix_row_t col_mask = ((matrix_row_t)1 << real_col); - if (*debounce >= DEBOUNCE_DN_MASK) { + if (*debounce >= DEBOUNCE_DN_MASK) { //debounce KEY DOWN *p_row |= col_mask; - } else if (*debounce <= DEBOUNCE_UP_MASK) { + } else if (*debounce <= DEBOUNCE_UP_MASK) { //debounce KEY UP *p_row &= ~col_mask; } - } - } - if (matrix[row] > 0) { - if (!rgblight_config.enable) kb_idle_times = 12; - else kb_idle_times = 0; + } + if (*debounce) matrix_keys_down++; } } - + + if (matrix_keys_down) { + if (BLE_LIGHT_ON == 0) kb_idle_times = 12; + else kb_idle_times = 0; + } + matrix_scan_quantum(); - return 1; + return matrix_keys_down; } @@ -189,20 +195,20 @@ void init_cols(void) { //595 pin DDRB |= (1<= 10) { //lock mode - matrix_scan(); - // Key1_S14 F, real debounce p is 1*16+4*2 - // Key2_K14 J, real debounce p is 1*16+4*2+1 - uint8_t *debounce = &matrix_debouncing[0][0]; - uint8_t matrix_keys_down = 0; - for (uint8_t i=0; i< MATRIX_ROWS * MATRIX_COLS; i++, *debounce++) { - if (*debounce > 0) { - if (i == KP(1,8) || i == KP(1,9)) matrix_keys_down += 100; - else matrix_keys_down++; - } - } - if (matrix_keys_down == 200) { - return true; - } - } else { - //check all keys - select_key_ready(); - DS_PL_LO(); - for (uint8_t i = 0; i < MATRIX_ROWS * MATRIX_COLS / 2; i++) { - CLOCK_PULSE(); - } - get_key_ready(); - if (get_key(0) || get_key(8)) { // - return true; + uint8_t matrix_keys_down = matrix_scan(); + if (matrix_keys_down == 0) return false; + + if (BLE51_PowerState >= 10) {//lock mode + if (matrix_keys_down == 2) { + // Key1_S14 F, real debounce p is 1*16+4*2 + // Key2_K14 J, real debounce p is 1*16+4*2+1 + if (matrix_debouncing[1][8] == 0xff && matrix_debouncing[1][9] == 0xff) return true; } + if (!ble51_boot_on) return true; //蓝牙功能关闭时唤醒电脑 + return false; } - return false; + + return matrix_keys_down; +} + +void bootmagic_lite(void) +{ + //do nothing + return; + } + +void hook_nkro_change(void) +{ + return; + uint8_t kbd_nkro = keymap_config.nkro; + type_num(kbd_nkro?6:0); +} \ No newline at end of file diff --git a/keyboards/ydkb/louise/rgblight.c b/keyboards/ydkb/louise/rgblight.c index fc7135fb5e6..3daa4acd81e 100644 --- a/keyboards/ydkb/louise/rgblight.c +++ b/keyboards/ydkb/louise/rgblight.c @@ -17,34 +17,49 @@ #define rgblight_timer_enable() do { PORTD &= ~(1<<6);} while(0) #define rgblight_timer_disable() do { PORTD |= (1<<6);} while(0) #define rgblight_timer_enabled (~PORTD & (1<<6)) + #define RGBLED_TEMP RGBLED_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}; +#define INDICATOR_NUM 0 +struct cRGB rgbled[RGBLED_NUM+INDICATOR_NUM+1]; +struct cRGB *led = &rgbled[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}; -//battery -extern bool no_rgblight; -extern bool is_ble_version; -rgblight_config_t rgblight_config = {.enable = 1, .mode = 8,.hue = 640, .sat = 255, .val = 255}; +rgblight_config_t rgblight_config = {.enable = 0, .mode = 8,.hue = 640, .sat = 255, .val = 255}; -struct cRGB led[RGBLED_NUM+1]; +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) { /* 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. */ uint8_t r, g, b; uint8_t temp[5]; - uint8_t n = (hue >> 8) % 3; + // 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; 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 + 2]; - g = temp[n + 1]; - b = temp[n]; + r = temp[n + 1]; + g = temp[n]; + b = temp[n - 1]; setrgb(r,g,b, led1); } @@ -99,29 +114,33 @@ void rgblight_init(void) rgblight_timer_init(); // setup the timer - if (rgblight_config.enable) { - rgblight_mode(rgblight_config.mode); - } else { - rgblight_set(); - rgblight_timer_disable(); - } + rgblight_mode(rgblight_config.mode); } +uint8_t limit_value_0to255(int16_t value) { + if (value > 255) return 255; + else if (value < 0) return 0; + else return value; +} void rgblight_mode(int8_t mode) { + // rgb off, new way to save about 60B if (!rgblight_config.enable) { - return; - } - if (mode < 0) mode = RGBLIGHT_MODES - 1; - else if (mode >= RGBLIGHT_MODES) mode = 0; - rgblight_config.mode = mode; + rgblight_clear(); + rgblight_set(); + //if (!indicator_state) rgblight_timer_disable(); + } else { + // rgb on + if (mode < 0) mode = RGBLIGHT_MODES - 1; + else if (mode >= RGBLIGHT_MODES) mode = 0; + rgblight_config.mode = mode; + dprintf("rgblight mode: %u\n", rgblight_config.mode); - eeconfig_write_rgblight(rgblight_config.raw); - dprintf("rgblight mode: %u\n", rgblight_config.mode); - if (rgblight_config.enable) { rgblight_timer_enable(); } + // save config. rgblight_sethsv() will save config. + //eeconfig_write_rgblight(rgblight_config.raw); rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } @@ -129,17 +148,8 @@ inline void rgblight_toggle(void) { rgblight_config.enable ^= 1; - eeconfig_write_rgblight(rgblight_config.raw); dprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable); - if (rgblight_config.enable) { - rgblight_mode(rgblight_config.mode); - } else { - rgblight_clear(); - rgblight_set(); - if (USB_DeviceState != DEVICE_STATE_Configured) { - rgblight_timer_disable(); - } - } + rgblight_mode(rgblight_config.mode); } @@ -152,8 +162,8 @@ void rgblight_action(uint8_t action) 7 val- 8 val+ */ uint16_t hue = rgblight_config.hue; - int16_t sat = rgblight_config.sat; - int16_t val = rgblight_config.val; + uint8_t sat = rgblight_config.sat; + uint8_t val = rgblight_config.val; int8_t increament = 1; if (action & 1) increament = -1; if (get_mods() & MOD_BIT(KC_LSHIFT)) { @@ -169,19 +179,15 @@ void rgblight_action(uint8_t action) break; case 3: case 4: - hue = (rgblight_config.hue + 768 + RGBLIGHT_HUE_STEP * increament) % 768; + hue = rgblight_config.hue + RGBLIGHT_HUE_STEP * increament; break; case 5: case 6: - sat = rgblight_config.sat + RGBLIGHT_SAT_STEP * increament; - if (sat > 255) sat = 255; - if (sat < 0) sat = 0; + sat = limit_value_0to255(rgblight_config.sat + RGBLIGHT_SAT_STEP * increament); break; case 7: case 8: - val = rgblight_config.val + RGBLIGHT_VAL_STEP * increament; - if (val > 255) val = 255; - if (val < 0) val = 0; + val = limit_value_0to255(rgblight_config.val + RGBLIGHT_VAL_STEP * increament); break; default: break; @@ -202,43 +208,49 @@ 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) { + hue = hue_fix(hue); if (rgblight_config.enable) { + #if 0 // will do rgblight_sethsv_noeeprom() in rgblight_task if (rgblight_config.mode == 1) { // same static color rgblight_sethsv_noeeprom(hue, sat, val); } + #endif rgblight_config.hue = hue; rgblight_config.sat = sat; rgblight_config.val = val; - eeconfig_write_rgblight(rgblight_config.raw); dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } + // when rgblight_config.enable == 0, just save config.raw. + eeconfig_write_rgblight(rgblight_config.raw); } void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { - for (uint8_t i=0;i 15 case 15 ... 20: - rgblight_effect_snake(rgblight_config.mode-13); + rgblight_effect_snake(rgblight_config.mode-14); // 0 to 5 (0 to 2) break; #endif #if RGBLIGHT_MODES > 21 @@ -264,6 +276,7 @@ void rgblight_task(void) break; #endif } + } } } @@ -274,7 +287,8 @@ void rgblight_effect_breathing(uint8_t interval) static int8_t increament = 1; rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos])); pos = pos + interval*increament; - if (pos < interval || pos+interval > 126) { + if (pos < interval || pos+interval > 62) { + //if (pos < interval || pos+interval > 126) { increament *= -1; } } @@ -283,7 +297,7 @@ 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 = (current_hue + interval * 3) % 768; + current_hue = hue_fix(current_hue + interval * 3); } void rgblight_effect_rainbow_swirl(uint8_t interval) @@ -292,16 +306,13 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) uint16_t hue; uint8_t i; uint8_t interval2 = interval/2; + if (interval & 1) interval2 *= -1; for (i=0; i 15 @@ -317,7 +328,10 @@ void rgblight_effect_snake(uint8_t interval) 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]); } pos += increament; if (pos > RGBLED_NUM) pos = 0; @@ -340,17 +354,17 @@ void rgblight_effect_knight(uint8_t interval) sled_step = 0; rgblight_clear(); for (i=0; i= 0){ + int8_t target_col = pos+i; + if (target_col < RGBLED_NUM && target_col >= 0){ need_update = 1; - uint8_t tmp_col = pos+i; - led[tmp_col%RGBLED_NUM] = led[RGBLED_TEMP]; + led[target_col] = led[RGBLED_TEMP]; } } if (need_update) rgblight_set(); //Keep the first or last col on when increament changes. pos += increament; if (pos <= 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH || pos >= RGBLED_NUM) { increament *= -1; - current_hue = (current_hue + 40) % 768; + current_hue = hue_fix(current_hue + 40); sethsv(current_hue, rgblight_config.sat, rgblight_config.val, &led[RGBLED_TEMP]); } } @@ -362,7 +376,6 @@ void suspend_power_down_action(void) PORTE &= ~(1<<6); PORTF &= ~(1<<0); PORTB &= ~(1<<2); - DDRC &= ~(1<<6); //backlight_disable(); rgblight_timer_disable(); //RGB_VCC off @@ -372,53 +385,48 @@ void suspend_wakeup_init_action(void) { rgblight_init(); DDRC |= (1<<6); - if (BLE51_PowerState >= 4) display_connection_status_check_times = 1; } void hook_keyboard_loop() { + if (BLE51_PowerState > 1) return; static uint16_t rgb_update_timer = 0; - if (rgblight_timer_enabled && timer_elapsed(rgb_update_timer) > 40) { + static uint8_t steps = 0; + if (timer_elapsed(rgb_update_timer) > 40) { rgb_update_timer = timer_read(); - if (!low_battery) { - if (!display_connection_status_check_times || !ble51_boot_on) rgblight_task(); + //if (!display_connection_status_check_times || !ble51_boot_on) rgblight_task(); + rgblight_task(); + + if ((steps++ & 0b11)) return; + //led_status_task(); + // run every 4*40 = 160ms + PORTE &= ~(1<<6); // led1 off + PORTF &= ~(1<<0); // led2 off + PORTB &= ~(1<<2); // led3 off + // low_battery and display_connection_status when ble51_on + if (ble51_boot_on && (low_battery || display_connection_status_check_times)) { + rgblight_timer_enable(); + // 320ms on,320ms off. bt connected: 320ms*3 on, 320ms off. + if (low_battery) { + backlight_disable(); + rgblight_timer_disable(); + if (steps & 0b1000) PORTB |= (1<<2); + } else { + if (bt_connected) { + if (steps & 0b11000) { PORTB |= (1<<2); PORTF |= (1<<0); } + } else { + if (steps & 0b1000) { PORTB |= (1<<2); } + } + } + } + // capslock + if (host_keyboard_leds() & (1< 150) { - battery_timer = timer_read(); - ble51_task_steps++; - if (low_battery) { - if (ble51_task_steps > 3) { - ble51_task_steps = (low_battery == 1)? 3:0; //value 1 is extremely low battery - if (PINC & (1<<7)) { //not charging - backlight_disable(); - rgblight_timer_disable(); - PORTB ^= (1<<2); - } else { - low_battery = 0; - suspend_wakeup_init_action(); - } - } - } else if (display_connection_status_check_times) { - if (ble51_task_steps == 1) { - PORTB &= ~(1<<2); - PORTF &= ~(1<<0); - } - if (ble51_task_steps == 3) { - PORTB |= (1<<2); - if (bt_connected) { - PORTF |= (1<<0); - } - } - if ((!bt_connected && ble51_task_steps >= 5) || ble51_task_steps >= 11) { - ble51_task_steps = 0; - } - } - } + return; } diff --git a/keyboards/ydkb/louise/rules.mk b/keyboards/ydkb/louise/rules.mk index 0190456ae21..28ec28834c9 100644 --- a/keyboards/ydkb/louise/rules.mk +++ b/keyboards/ydkb/louise/rules.mk @@ -6,27 +6,31 @@ F_CPU = 8000000 # Bootloader selection BOOTLOADER = lufa-ms -BOOTLOADER_SIZE = 6144 +BOOTLOADER_SIZE = 6400 # Build Options # change yes to no to disable # CUSTOM_MATRIX = yes # Custom matrix file -UNICODE_ENABLE = yes # Unicode -BOOTMAGIC_ENABLE = yes # Enable Bootmagic Lite -MOUSEKEY_ENABLE = yes # Mouse keys -EXTRAKEY_ENABLE = yes # Audio control and System control -CONSOLE_ENABLE = no # Console for debug -COMMAND_ENABLE = yes # Commands for debug and configuration -NKRO_ENABLE = yes # Enable N-Key Rollover -BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality -RGBLIGHT_ENABLE = no -LTO_ENABLE = yes +BOOTMAGIC_ENABLE = no # Enable Bootmagic Lite +MOUSEKEY_ENABLE ?= yes # Mouse keys +EXTRAKEY_ENABLE ?= yes # Audio control and System control +CONSOLE_ENABLE ?= no # Console for debug +COMMAND_ENABLE = yes # Commands for debug and configuration +# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE +#SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend +# if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work +NKRO_ENABLE = yes # USB Nkey Rollover +BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality +RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow +LTO_ENABLE = yes # Enable Link Time Optimization +BACKLIGHT_DRIVER = custom -# project specific files -SRC ?= matrix.c \ +SRC += matrix.c \ led_fn.c \ light_ws2812.c \ + backlight_user.c \ rgblight.c + include $(TMK_DIR)/protocol/ble51.mk diff --git a/keyboards/ydkb/pearly_v1/backlight_user.c b/keyboards/ydkb/pearly_v1/backlight_user.c new file mode 100644 index 00000000000..948dc2116b7 --- /dev/null +++ b/keyboards/ydkb/pearly_v1/backlight_user.c @@ -0,0 +1,52 @@ +/* +Copyright 2020 YANG + +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 . +*/ + +#include +#include +#include +#include "backlight.h" +#include "timer.h" + + +void backlight_init_ports(void) { + DDRB |= (1<<7); + //PORTB |= (1<<7); +} + +void backlight_task(void) { + return; +} + +void backlight_user_enable(void) +{ + + //DDRB |= (1<<7); + PORTB &= ~(1<<7); +} + +void backlight_user_disable(void) +{ + //DDRB &= ~(1<<7); + PORTB |= (1<<7); +} + +void backlight_set(uint8_t level) +{ + if (level) backlight_user_enable(); + else backlight_user_disable(); +} + diff --git a/keyboards/ydkb/pearly_v1/config.h b/keyboards/ydkb/pearly_v1/config.h index 83747fff950..5fc04c88d53 100644 --- a/keyboards/ydkb/pearly_v1/config.h +++ b/keyboards/ydkb/pearly_v1/config.h @@ -3,14 +3,19 @@ #include "config_common.h" /* USB Device descriptor parameter */ -#define FW_VER QMK_DMCN -#define FW_VER_VIA VIA_DMCN -#define FW_VER_VIAL VIAL_DMCN +#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 VENDOR_ID 0x9D5B #define PRODUCT_ID 0x2040 #define DEVICE_VER 0x0001 #define MANUFACTURER YDKB +#if CONSOLE_ENABLE +#define PRODUCT Pearly Debug (FW_VER) +#else #define PRODUCT Pearly (FW_VER) +#endif #define MATRIX_ROWS 4 @@ -21,7 +26,7 @@ #define TAPPING_TOGGLE 2 #define BACKLIGHT_PIN B7 -#define BACKLIGHT_LEVELS 6 +#define BACKLIGHT_LEVELS 1 #define BACKLIGHT_ON_STATE 0 /* key combination for command */ @@ -34,6 +39,7 @@ #define ws2812_DDRREG DDRD #define ws2812_pin PD0 #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 @@ -67,15 +73,18 @@ #endif /* BT Power Control */ #define BT_POWERED (~PORTD & (1<<5)) -#define bt_power_init() do { DDRD |= (1<<5); PORTD &= ~(1<<5);} while(0) -#define turn_off_bt() do { PORTD |= (1<<5); UCSR1B &= ~(1<. #include "ble51.h" #include "rgblight.h" - +#if 0 void led_set_user(uint8_t usb_led) { if (usb_led & (1<event.pressed) { - switch (keycode) { - case USER00: - command_extra(KC_U); - break; - case USER01: //RESET - command_extra(KC_B); - break; - case USER02: //BATTERY LEVEL - command_extra(KC_V); - break; - case USER03: //LOCK MODE - command_extra(KC_L); - break; - case USER04 ... USER12: - rgblight_action(keycode - USER04); - break; + static const uint8_t userx_to_command[4] = { + KC_U, // 0 Host Switch + KC_B, // 1 Reset + KC_V, // 2 Output Battery Value + KC_L // 3 Lock Mode + }; + 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_v1/matrix.c b/keyboards/ydkb/pearly_v1/matrix.c index 73d42c4dba3..e689569b3ae 100644 --- a/keyboards/ydkb/pearly_v1/matrix.c +++ b/keyboards/ydkb/pearly_v1/matrix.c @@ -13,6 +13,7 @@ #include "keyboard.h" #include "timer.h" #include "matrix.h" +#include "debounce_pk.h" #include "suspend.h" #include "lufa.h" #include "rgblight.h" @@ -20,10 +21,6 @@ #include "ble51_task.h" #include "backlight.h" -#define DEBOUNCE_DN_MASK (uint8_t)(~(0x80 >> 5)) -#define DEBOUNCE_UP_MASK (uint8_t)(0x80 >> 5) - -bool is_ble_version = 1; extern rgblight_config_t rgblight_config; extern backlight_config_t backlight_config; @@ -41,54 +38,48 @@ static void select_row(uint8_t row); static uint8_t get_key(uint8_t col); static void init_cols(void); -__attribute__ ((weak)) -void matrix_scan_user(void) {} +void matrix_scan_user(void) {} __attribute__ ((weak)) void matrix_scan_kb(void) { matrix_scan_user(); hook_keyboard_loop(); } -inline -uint8_t matrix_rows(void) +inline uint8_t matrix_rows(void) { return MATRIX_ROWS; } -inline -uint8_t matrix_cols(void) +inline uint8_t matrix_cols(void) { return MATRIX_COLS; } void hook_early_init() { - if (pgm_read_byte(0x7e72) == 0x4c) { + // PD1 for BLE Reset, PB3 for BT_SW + DDRD &= ~(1<<1); + PORTD |= (1<<1); + DDRB &= ~(1<<3); + PORTB |= (1<<3); + WAIT_MS(6); + if ( (pgm_read_byte(0x7e72) == 0x4c) || (~PINB & (1<<3))) { // USB Only version - is_ble_version = 0; ble51_boot_on = 0; } else { - // PD1 for BLE Reset, PB3 for BT_SW - DDRD &= ~(1<<1); - PORTD |= (1<<1); - DDRB &= ~(1<<3); - PORTB |= (1<<3); - _delay_ms(2); - if ((~PINB & (1<<3))) ble51_boot_on = 0; //BLE Reset if (ble_reset_key == 0xBBAA) { - if (ble51_boot_on) { - // PE6 for BLE Reset - DDRD |= (1<<1); - PORTD &= ~(1<<1); - bt_power_init(); - // light CapsLED - DDRB |= (1<<2); - PORTB |= (1<<2); - _delay_ms(5000); - bootloader_jump(); - } + ble_reset_key = 0; + // PE6 for BLE Reset + DDRD |= (1<<1); + PORTD &= ~(1<<1); + bt_power_init(); + // light CapsLED + DDRB |= (1<<2); + PORTB |= (1<<2); + _delay_ms(5000); + bootloader_jump(); } } } @@ -105,19 +96,19 @@ uint8_t matrix_scan(void) { uint16_t time_check = timer_read(); - if (matrix_scan_timestamp == time_check) return 1; + if (matrix_scan_timestamp == time_check) return 0; matrix_scan_timestamp = time_check; + uint8_t matrix_keys_down = 0; uint8_t *debounce = &matrix_debouncing[0][0]; - for (uint8_t row=0; row> 1) | key; - - if (BLE51_PowerState >= 2 && BLE51_PowerState < 10 && key == 0x80) return 100; + //if ((*debounce > 0) && (*debounce < 255)) { if (1) { matrix_row_t *p_row = &matrix[row]; @@ -128,16 +119,18 @@ uint8_t matrix_scan(void) *p_row &= ~col_mask; } } + if (*debounce) matrix_keys_down++; } - if (matrix[row] > 0) { - if (!rgblight_config.enable && !backlight_config.enable) kb_idle_times = 12; - else kb_idle_times = 0; - } - //unselect_rows(); } - + + if (matrix_keys_down) { + //if (BLE_LIGHT_ON == 0) kb_idle_times = 12; + //else kb_idle_times = 0; + kb_idle_times = 0; + } + matrix_scan_quantum(); - return 1; + return matrix_keys_down; } @@ -163,14 +156,6 @@ void matrix_print(void) } } -uint8_t matrix_key_count(void) -{ - uint8_t count = 0; - for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - count += bitpop16(matrix[i]); - } - return count; -} static const struct AVR_PINS col_PIN[] = { PD(6), PD(7), PB(4), PB(5), PB(6), PC(6), PC(7), PE(2), PF(7), PF(6), PB(0), PF(5) }; static const struct AVR_PINS row_PIN[] = {PF(4), PF(1), PF(0), PE(6)}; @@ -230,7 +215,7 @@ static uint8_t get_key(uint8_t col) * row: 0 1 2 3 * pin: F4 F1 F0 E6 */ -void unselect_rows(void) +static void unselect_rows(void) { } @@ -242,34 +227,33 @@ static void select_row(uint8_t row) } } + bool suspend_wakeup_condition(void) { - if (BLE51_PowerState >= 10) { - matrix_scan(); - // K14 F, K17 J - uint8_t *debounce = &matrix_debouncing[0][0]; - uint8_t matrix_keys_down = 0; - for (uint8_t i=0; i< MATRIX_ROWS * MATRIX_COLS; i++, *debounce++) { - if (*debounce > 0) { - if (i == KP(1,4) || i == KP(1,7)) matrix_keys_down += 100; - else matrix_keys_down++; - } - } - if (matrix_keys_down == 200) { - return true; - } else if (!ble51_boot_on && matrix_keys_down) return true; - } else { - //check encoder - select_row(3); - _delay_us(6); - uint8_t encoder_state_check[2][2]; - encoder_state_check[0][1] = PIND&(1<= 10) {//lock mode + if (matrix_keys_down == 2) { + // K14 F, K17 J + if (matrix_debouncing[1][4] == 0xff && matrix_debouncing[1][7] == 0xff) return true; } + if (!ble51_boot_on) return true; + return false; } - return false; + + return matrix_keys_down; +} + +void bootmagic_lite(void) +{ + //do nothing + return; } + +void hook_nkro_change(void) +{ + return; + uint8_t kbd_nkro = keymap_config.nkro; + type_num(kbd_nkro?6:0); +} \ No newline at end of file diff --git a/keyboards/ydkb/pearly_v1/rgblight.c b/keyboards/ydkb/pearly_v1/rgblight.c index 423a62edd52..1c3f517a995 100644 --- a/keyboards/ydkb/pearly_v1/rgblight.c +++ b/keyboards/ydkb/pearly_v1/rgblight.c @@ -9,7 +9,6 @@ #include "lufa.h" #include "ble51.h" #include "ble51_task.h" -#include "backlight.h" #include "led.h" #include "quantum.h" @@ -17,37 +16,79 @@ #define rgblight_timer_enable() do { PORTD &= ~(1<<4);} while(0) #define rgblight_timer_disable() do { PORTD |= (1<<4);} while(0) #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]; + //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}; -//battery -extern bool no_rgblight; -extern bool is_ble_version; - rgblight_config_t rgblight_config = {.enable = 1, .mode = 8,.hue = 640, .sat = 255, .val = 255}; -struct cRGB led[RGBLED_NUM+1]; +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/ + use 8bit hue, hue16 = hue*3 to reach 0 to 767. + */ + uint8_t r, g, b; + uint8_t temp[5]; + 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[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); +} + +#if 0 // my old code. the new way use 8bit hue saves about 134B +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) { /* 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. */ uint8_t r, g, b; uint8_t temp[5]; - uint8_t n = (hue >> 8) % 3; + // 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; uint8_t s = ((256 - saturation) * brightness) >> 8; +#if 0 //temp[n-1] b g r temp[0] = temp[3] = s; temp[1] = temp[4] = x + s; temp[2] = brightness - x; - r = temp[n + 2]; - g = temp[n + 1]; - b = temp[n]; + r = temp[n + 1]; + g = temp[n]; + b = temp[n - 1]; setrgb(r,g,b, led1); +#else //temp[n-1] 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-1], 3); +#endif } +#endif void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) { @@ -86,7 +127,7 @@ void eeconfig_debug_rgblight(void) { void rgblight_init(void) { dprintf("rgblight_init start!\n"); -#if 0 +#if 1 if (!eeconfig_is_enabled()) { dprintf("rgblight_init eeconfig is not enabled.\n"); eeconfig_init(); @@ -103,25 +144,33 @@ void rgblight_init(void) rgblight_mode(rgblight_config.mode); } +uint8_t limit_value_0to255(int16_t value) { + if (value > 255) return 255; + else if (value < 0) return 0; + else return value; +} void rgblight_mode(int8_t mode) { - // rgb off + // rgb off, new way to save about 60B if (!rgblight_config.enable) { //rgblight_clear(); //rgblight_set(); rgblight_timer_disable(); } else { // rgb on + #if 1 //less than 0 can always be 0. save 8B if (mode < 0) mode = RGBLIGHT_MODES - 1; - else if (mode >= RGBLIGHT_MODES) mode = 0; + else + #endif + if (mode >= RGBLIGHT_MODES) mode = 0; rgblight_config.mode = mode; dprintf("rgblight mode: %u\n", rgblight_config.mode); rgblight_timer_enable(); } - // save config - eeconfig_write_rgblight(rgblight_config.raw); + // save config. rgblight_sethsv() will save config. + //eeconfig_write_rgblight(rgblight_config.raw); rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } @@ -142,9 +191,9 @@ void rgblight_action(uint8_t action) 5 sat- 6 sat+ 7 val- 8 val+ */ - uint16_t hue = rgblight_config.hue; - int16_t sat = rgblight_config.sat; - int16_t val = rgblight_config.val; + 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; if (get_mods() & MOD_BIT(KC_LSHIFT)) { @@ -160,19 +209,15 @@ void rgblight_action(uint8_t action) break; case 3: case 4: - hue = (rgblight_config.hue + 768 + RGBLIGHT_HUE_STEP * increament) % 768; + hue = rgblight_config.hue + RGBLIGHT_HUE_STEP * increament; break; case 5: case 6: - sat = rgblight_config.sat + RGBLIGHT_SAT_STEP * increament; - if (sat > 255) sat = 255; - if (sat < 0) sat = 0; + sat = limit_value_0to255(rgblight_config.sat + RGBLIGHT_SAT_STEP * increament); break; case 7: case 8: - val = rgblight_config.val + RGBLIGHT_VAL_STEP * increament; - if (val > 255) val = 255; - if (val < 0) val = 0; + val = limit_value_0to255(rgblight_config.val + RGBLIGHT_VAL_STEP * increament); break; default: break; @@ -180,58 +225,62 @@ 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]); for (uint8_t i=0; i< RGBLED_NUM; i++) { led[i] = led[RGBLED_TEMP]; } - rgblight_set(); + //rgblight_set(); } } -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); if (rgblight_config.enable) { + #if 0 // will do rgblight_sethsv_noeeprom() in rgblight_task if (rgblight_config.mode == 1) { // same static color rgblight_sethsv_noeeprom(hue, sat, val); } + #endif rgblight_config.hue = hue; rgblight_config.sat = sat; rgblight_config.val = val; - eeconfig_write_rgblight(rgblight_config.raw); dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } + // when rgblight_config.enable == 0, just save config.raw. + eeconfig_write_rgblight(rgblight_config.raw); } void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { - for (uint8_t i=0;i 15 case 15 ... 20: - rgblight_effect_snake(rgblight_config.mode-13); + rgblight_effect_snake(rgblight_config.mode-14); // 0 to 5 (0 to 2) break; #endif #if RGBLIGHT_MODES > 21 @@ -257,6 +306,7 @@ void rgblight_task(void) break; #endif } + rgblight_set(); } else { rgblight_timer_disable(); } @@ -276,29 +326,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 = (current_hue + interval * 3) % 768; + 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; - for (i=0; i 15 @@ -314,12 +359,15 @@ void rgblight_effect_snake(uint8_t interval) rgblight_clear(); if (interval%2) increament = -1; for (i=0; i= RGBLED_NUM) target_led -= 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; else if (pos < 0 ) pos = RGBLED_NUM; - rgblight_set(); + //rgblight_set(); } } #endif @@ -329,7 +377,6 @@ 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 int8_t increament = 1; if (++sled_step > interval) { @@ -337,17 +384,17 @@ void rgblight_effect_knight(uint8_t interval) sled_step = 0; rgblight_clear(); for (i=0; i= 0){ + int8_t target_col = pos+i; + if (target_col < RGBLED_NUM && target_col >= 0){ need_update = 1; - uint8_t tmp_col = pos+i; - led[tmp_col%RGBLED_NUM] = led[RGBLED_TEMP]; + led[target_col] = led[RGBLED_TEMP]; } } if (need_update) rgblight_set(); //Keep the first or last col on when increament changes. pos += increament; if (pos <= 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH || pos >= RGBLED_NUM) { increament *= -1; - current_hue = (current_hue + 40) % 768; + current_hue = current_hue + 16; sethsv(current_hue, rgblight_config.sat, rgblight_config.val, &led[RGBLED_TEMP]); } } @@ -358,7 +405,6 @@ void suspend_power_down_action(void) { PORTB &= ~(1<<2 | 1<<1); // PB2 Caps, PB1 LED2 DDRB &= ~(1<= 4) display_connection_status_check_times = 1; } void hook_keyboard_loop() { + if (BLE51_PowerState > 1) return; static uint16_t rgb_update_timer = 0; - if (rgblight_timer_enabled && timer_elapsed(rgb_update_timer) > 40) { + static uint8_t steps = 0; + if (timer_elapsed(rgb_update_timer) > 40) { rgb_update_timer = timer_read(); - if (!display_connection_status_check_times || !ble51_boot_on) rgblight_task(); - } -} + if (!display_connection_status_check_times) rgblight_task(); -void ble51_task_user(void) -{ - static uint8_t ble51_task_steps = 0; - static uint16_t battery_timer = 0; - if (timer_elapsed(battery_timer) > 150) { - battery_timer = timer_read(); - ble51_task_steps++; - if (low_battery) { - if (ble51_task_steps > 3) { - ble51_task_steps = (low_battery == 1)? 3:0; //value 1 is extremely low battery - if (USB_DeviceState != DEVICE_STATE_Configured) { //not charging - backlight_disable(); - rgblight_timer_disable(); - PORTB ^= (1<<2); + if ((steps++ & 0b11)) return; + //led_status_task(); + // run every 4*40 = 160ms + PORTB &= ~(1<<2 | 1<<1); //led off + if (ble51_boot_on && (low_battery || display_connection_status_check_times)) { + rgblight_timer_enable(); + rgblight_clear(); + // 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 | 1<<2); + rgblight_setrgb(0,128,0); //green + } } else { - low_battery = 0; - suspend_wakeup_init_action(); + if (steps & 0b1000) { + PORTB |= (1<<1); + rgblight_setrgb(0,0,128); //blue + } } } - } else if (display_connection_status_check_times) { - rgblight_timer_enable(); - if (ble51_task_steps == 1) { - PORTB &= ~(1<<2 | 1<<1); - rgblight_clear(); - rgblight_set(); - } else if (ble51_task_steps == 3) { - PORTB |= (1<<1); - if (bt_connected) PORTB |= (1<<2); - uint8_t g_color = bt_connected? 128:0; - uint8_t b_color = bt_connected? 0:128; - rgblight_setrgb(0,g_color,b_color); - } - if ((!bt_connected && ble51_task_steps >= 5) || ble51_task_steps >= 11) { - ble51_task_steps = 0; - } + ws2812_setleds(rgbled); + } + // capslock + else if (host_keyboard_leds() & (1<. #include "ble51.h" #include "rgblight.h" - +#if 0 void led_set_user(uint8_t usb_led) { if (usb_led & (1<event.pressed) { - switch (keycode) { - case USER00: - command_extra(KC_U); - break; - case USER01: //RESET - command_extra(KC_B); - break; - case USER02: //BATTERY LEVEL - command_extra(KC_V); - break; - case USER03: //LOCK MODE - command_extra(KC_L); - break; - case USER04 ... USER12: - rgblight_action(keycode - USER04); - break; + static const uint8_t userx_to_command[4] = { + KC_U, // 0 Host Switch + KC_B, // 1 Reset + KC_V, // 2 Output Battery Value + KC_L // 3 Lock Mode + }; + 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/light_ws2812.c b/keyboards/ydkb/pearly_v2/light_ws2812.c index 739c9ed8776..19122dda669 100644 --- a/keyboards/ydkb/pearly_v2/light_ws2812.c +++ b/keyboards/ydkb/pearly_v2/light_ws2812.c @@ -21,7 +21,6 @@ void ws2812_setleds(struct cRGB *ledarray) //_delay_us(50); } - // Timing in ns #define w_zeropulse 350 #define w_onepulse 900 diff --git a/keyboards/ydkb/pearly_v2/matrix.c b/keyboards/ydkb/pearly_v2/matrix.c index 4cf24bbc0ee..6c95107d27e 100644 --- a/keyboards/ydkb/pearly_v2/matrix.c +++ b/keyboards/ydkb/pearly_v2/matrix.c @@ -1,30 +1,37 @@ -#include -#include -#include -#include -#include -#include -#include "avr_config.h" +/* +Copyright 2023 YANG + +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 . +*/ + +/* + * scan matrix + */ +#include "wait.h" +#include "action_layer.h" #include "print.h" #include "debug.h" #include "util.h" -#include "action.h" -#include "command.h" -#include "keyboard.h" #include "timer.h" #include "matrix.h" +#include "debounce_pk.h" #include "suspend.h" -#include "lufa.h" #include "rgblight.h" #include "ble51.h" -#include "ble51_task.h" -#define DEBOUNCE_DN_MASK (uint8_t)(~(0x80 >> 5)) -#define DEBOUNCE_UP_MASK (uint8_t)(0x80 >> 5) - -bool is_ble_version = 1; -extern rgblight_config_t rgblight_config; +/* matrix state(1:on, 0:off) */ static matrix_row_t matrix[MATRIX_ROWS] = {0}; static uint16_t matrix_scan_timestamp = 0; @@ -47,17 +54,6 @@ void matrix_scan_kb(void) { hook_keyboard_loop(); } -inline -uint8_t matrix_rows(void) -{ - return MATRIX_ROWS; -} - -inline -uint8_t matrix_cols(void) -{ - return MATRIX_COLS; -} void hook_early_init() { @@ -71,6 +67,7 @@ void hook_early_init() if ((~PINB & (1<<3))) ble51_boot_on = 0; //BLE Reset if (ble_reset_key == 0xBBAA) { + ble_reset_key = 0; if (ble51_boot_on) { // PD3,TX_MCU to RX_51 DDRD |= (1<<3 ); @@ -80,7 +77,7 @@ void hook_early_init() DDRB |= (1<<2); PORTB |= (1<<2); _delay_ms(5000); - bootloader_jump(); + bootloader_jump(); } } } @@ -89,26 +86,27 @@ void hook_early_init() void matrix_init(void) { DDRB |= (1<<2); - PORTB &= ~(1<<2); + //PORTB &= ~(1<<2); init_cols(); rgblight_init(); } uint8_t matrix_scan(void) { - uint16_t time_check = timer_read(); if (matrix_scan_timestamp == time_check) return 1; matrix_scan_timestamp = time_check; + uint8_t matrix_keys_down = 0; + uint8_t *debounce = &matrix_debouncing[0][0]; - for (uint8_t row=0; row> 1) | key; - + //if ((*debounce > 0) && (*debounce < 255)) { if (1) { matrix_row_t *p_row = &matrix[row]; @@ -119,16 +117,18 @@ uint8_t matrix_scan(void) *p_row &= ~col_mask; } } + if (*debounce) matrix_keys_down++; } - if (matrix[row] > 0) { - if (!rgblight_config.enable) kb_idle_times = 12; - else kb_idle_times = 0; - } - //unselect_rows(); } - + + if (matrix_keys_down) { + if (BLE_LIGHT_ON == 0) kb_idle_times = 12; + else kb_idle_times = 0; + } + //unselect_rows(); + matrix_scan_quantum(); - return 1; + return matrix_keys_down; } @@ -146,10 +146,10 @@ matrix_row_t matrix_get_row(uint8_t row) void matrix_print(void) { - print("\nr/c 01234567\n"); + print("\nr/c 0123456789ABCDEF\n"); for (uint8_t row = 0; row < MATRIX_ROWS; row++) { print_hex8(row); print(": "); - print_bin_reverse8(matrix_get_row(row)); + print_bin_reverse16(matrix_get_row(row)); print("\n"); } } @@ -172,7 +172,7 @@ static const struct AVR_PINS row_PIN[] = {PF(4), PF(1), PF(0), PE(6)}; */ static void init_cols(void) { - for (uint8_t col=0; col= 10) { - matrix_scan(); - // K14 F, K17 J - uint8_t *debounce = &matrix_debouncing[0][0]; - uint8_t matrix_keys_down = 0; - for (uint8_t i=0; i< MATRIX_ROWS * MATRIX_COLS; i++, *debounce++) { - if (*debounce > 0) { - if (i == KP(1,4) || i == KP(1,7)) matrix_keys_down += 100; - else matrix_keys_down++; - } + 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; } - if (matrix_keys_down == 200) { - return true; - } else if (!ble51_boot_on && matrix_keys_down) return true; + if (!ble51_boot_on) return true; + return false; + } else { select_all_rows(); _delay_us(6); @@ -231,3 +226,18 @@ bool suspend_wakeup_condition(void) } return false; } + +void bootmagic_lite(void) +{ + //do nothing + return; + +} + +void hook_nkro_change(void) +{ + return; + uint8_t kbd_nkro = keymap_config.nkro; + type_num(kbd_nkro?6:0); +} + diff --git a/keyboards/ydkb/pearly_v2/rgblight.c b/keyboards/ydkb/pearly_v2/rgblight.c index c332a35503b..bcfddb63e74 100644 --- a/keyboards/ydkb/pearly_v2/rgblight.c +++ b/keyboards/ydkb/pearly_v2/rgblight.c @@ -17,33 +17,48 @@ #define rgblight_timer_disable() do { PORTD |= (1<<4);} while(0) #define rgblight_timer_enabled (~PORTD & (1<<4)) #define RGBLED_TEMP RGBLED_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}; +#define INDICATOR_NUM 0 +struct cRGB rgbled[RGBLED_NUM+INDICATOR_NUM+1]; +struct cRGB *led = &rgbled[INDICATOR_NUM]; -//battery -extern bool no_rgblight; -extern bool is_ble_version; +//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}; -struct cRGB led[RGBLED_NUM+1]; +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) { /* 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. */ uint8_t r, g, b; uint8_t temp[5]; - uint8_t n = (hue >> 8) % 3; + // 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; 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 + 2]; - g = temp[n + 1]; - b = temp[n]; + r = temp[n + 1]; + g = temp[n]; + b = temp[n - 1]; setrgb(r,g,b, led1); } @@ -84,7 +99,7 @@ void eeconfig_debug_rgblight(void) { void rgblight_init(void) { dprintf("rgblight_init start!\n"); -#if 0 +#if 1 if (!eeconfig_is_enabled()) { dprintf("rgblight_init eeconfig is not enabled.\n"); eeconfig_init(); @@ -101,14 +116,20 @@ void rgblight_init(void) rgblight_mode(rgblight_config.mode); } +uint8_t limit_value_0to255(int16_t value) { + if (value > 255) return 255; + else if (value < 0) return 0; + else return value; +} void rgblight_mode(int8_t mode) { - // rgb off + // rgb off, new way to save about 60B if (!rgblight_config.enable) { //rgblight_clear(); //rgblight_set(); rgblight_timer_disable(); + rgb_fading_index = 0; } else { // rgb on if (mode < 0) mode = RGBLIGHT_MODES - 1; @@ -118,8 +139,8 @@ void rgblight_mode(int8_t mode) rgblight_timer_enable(); } - // save config - eeconfig_write_rgblight(rgblight_config.raw); + // save config. rgblight_sethsv() will save config. + //eeconfig_write_rgblight(rgblight_config.raw); rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } @@ -141,8 +162,8 @@ void rgblight_action(uint8_t action) 7 val- 8 val+ */ uint16_t hue = rgblight_config.hue; - int16_t sat = rgblight_config.sat; - int16_t val = rgblight_config.val; + uint8_t sat = rgblight_config.sat; + uint8_t val = rgblight_config.val; int8_t increament = 1; if (action & 1) increament = -1; if (get_mods() & MOD_BIT(KC_LSHIFT)) { @@ -158,19 +179,15 @@ void rgblight_action(uint8_t action) break; case 3: case 4: - hue = (rgblight_config.hue + 768 + RGBLIGHT_HUE_STEP * increament) % 768; + hue = rgblight_config.hue + RGBLIGHT_HUE_STEP * increament; break; case 5: case 6: - sat = rgblight_config.sat + RGBLIGHT_SAT_STEP * increament; - if (sat > 255) sat = 255; - if (sat < 0) sat = 0; + sat = limit_value_0to255(rgblight_config.sat + RGBLIGHT_SAT_STEP * increament); break; case 7: case 8: - val = rgblight_config.val + RGBLIGHT_VAL_STEP * increament; - if (val > 255) val = 255; - if (val < 0) val = 0; + val = limit_value_0to255(rgblight_config.val + RGBLIGHT_VAL_STEP * increament); break; default: break; @@ -191,45 +208,60 @@ 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) { + hue = hue_fix(hue); if (rgblight_config.enable) { + #if 0 // will do rgblight_sethsv_noeeprom() in rgblight_task if (rgblight_config.mode == 1) { // same static color rgblight_sethsv_noeeprom(hue, sat, val); } + #endif rgblight_config.hue = hue; rgblight_config.sat = sat; rgblight_config.val = val; - eeconfig_write_rgblight(rgblight_config.raw); dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } + // when rgblight_config.enable == 0, just save config.raw. + eeconfig_write_rgblight(rgblight_config.raw); } void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { - for (uint8_t i=0;i= 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 15 case 15 ... 20: - rgblight_effect_snake(rgblight_config.mode-13); + 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); break; + #endif } } else { rgblight_timer_disable(); @@ -264,7 +300,8 @@ void rgblight_effect_breathing(uint8_t interval) static int8_t increament = 1; rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos])); pos = pos + interval*increament; - if (pos < interval || pos+interval > 126) { + if (pos < interval || pos+interval > 62) { + //if (pos < interval || pos+interval > 126) { increament *= -1; } } @@ -273,7 +310,7 @@ 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 = (current_hue + interval * 3) % 768; + current_hue = hue_fix(current_hue + interval * 3); } void rgblight_effect_rainbow_swirl(uint8_t interval) @@ -282,17 +319,16 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) uint16_t hue; uint8_t i; uint8_t interval2 = interval/2; + if (interval & 1) interval2 *= -1; for (i=0; i 15 void rgblight_effect_snake(uint8_t interval) { static int8_t pos = 0 - RGBLIGHT_EFFECT_SNAKE_LENGTH; @@ -305,7 +341,11 @@ void rgblight_effect_snake(uint8_t interval) 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]); } pos += increament; if (pos > RGBLED_NUM) pos = 0; @@ -313,7 +353,9 @@ void rgblight_effect_snake(uint8_t interval) rgblight_set(); } } +#endif +#if RGBLIGHT_MODES > 21 void rgblight_effect_knight(uint8_t interval) { static int8_t pos = RGBLED_NUM - 1; @@ -326,76 +368,79 @@ void rgblight_effect_knight(uint8_t interval) sled_step = 0; rgblight_clear(); for (i=0; i= 0){ + int8_t target_col = pos+i; + if (target_col < RGBLED_NUM && target_col >= 0){ need_update = 1; - uint8_t tmp_col = pos+i; - led[tmp_col%RGBLED_NUM] = led[RGBLED_TEMP]; + led[target_col] = led[RGBLED_TEMP]; } } if (need_update) rgblight_set(); //Keep the first or last col on when increament changes. pos += increament; if (pos <= 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH || pos >= RGBLED_NUM) { increament *= -1; - current_hue = (current_hue + 40) % 768; + current_hue = hue_fix(current_hue + 40); sethsv(current_hue, rgblight_config.sat, rgblight_config.val, &led[RGBLED_TEMP]); } } } +#endif void suspend_power_down_action(void) { PORTB &= ~(1<<2); rgblight_timer_disable(); //RGB_VCC off + rgb_fading_index = 0; } void suspend_wakeup_init_action(void) { rgblight_init(); - if (BLE51_PowerState >= 4) display_connection_status_check_times = 1; } void hook_keyboard_loop() { + if (BLE51_PowerState > 1) return; static uint16_t rgb_update_timer = 0; - if (rgblight_timer_enabled && timer_elapsed(rgb_update_timer) > 40) { + static uint8_t steps = 0; + if (timer_elapsed(rgb_update_timer) > 40) { rgb_update_timer = timer_read(); - if (!display_connection_status_check_times || !ble51_boot_on) rgblight_task(); - } -} + if (!display_connection_status_check_times) rgblight_task(); -void ble51_task_user(void) -{ - static uint8_t ble51_task_steps = 0; - static uint16_t battery_timer = 0; - if (timer_elapsed(battery_timer) > 150) { - battery_timer = timer_read(); - ble51_task_steps++; - if (low_battery) { - if (ble51_task_steps > 3) { - ble51_task_steps = (low_battery == 1)? 3:0; //value 1 is extremely low battery - if (PINB & (1<<1)) { //not charging - rgblight_timer_disable(); - PORTB ^= (1<<2); + 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 { - low_battery = 0; - suspend_wakeup_init_action(); + if (steps & 0b1000) { + PORTB |= (1<<2); + rgblight_setrgb(0,0,128); //blue + } } } - } else if (display_connection_status_check_times) { - rgblight_timer_enable(); - if (ble51_task_steps == 1) { - PORTB &= ~(1<<2); - rgblight_clear(); - rgblight_set(); - } else if (ble51_task_steps == 3) { - PORTB |= (1<<2); - uint8_t g_color = bt_connected? 128:0; - uint8_t b_color = bt_connected? 0:128; - rgblight_setrgb(0,g_color,b_color); - } - if ((!bt_connected && ble51_task_steps >= 5) || ble51_task_steps >= 11) { - ble51_task_steps = 0; - } + ws2812_setleds(rgbled); + } + // capslock + else if (host_keyboard_leds() & (1<= 4) display_connection_status_check_times = 1; } void hook_keyboard_loop() diff --git a/quantum/command.c b/quantum/command.c index f7b82c3f105..fcab51be1f4 100644 --- a/quantum/command.c +++ b/quantum/command.c @@ -66,6 +66,10 @@ static void switch_default_layer(uint8_t layer); command_state_t command_state = ONESHOT; bool command_proc(uint8_t code) { +#ifdef NO_DEFAULT_COMMAND //32B + if (!IS_COMMAND()) return false; + return command_extra(code); +#endif switch (command_state) { case ONESHOT: if (!IS_COMMAND()) return false; @@ -324,6 +328,9 @@ static void print_eeconfig(void) { #endif /* !NO_PRINT && !USER_PRINT */ static bool command_common(uint8_t code) { +#ifdef NO_DEFAULT_COMMAND + return true; +#endif #ifdef KEYBOARD_LOCK_ENABLE static host_driver_t *host_driver = 0; #endif @@ -554,6 +561,9 @@ static void command_console_help(void) { } static bool command_console(uint8_t code) { +#ifdef NO_DEFAULT_COMMAND + return true; +#endif switch (code) { case KC_H: case KC_SLASH: /* ? */ @@ -636,6 +646,9 @@ static void mousekey_console_help(void) { * any doubt: we return `false` to return to the main console, * which differs from the `bool` that `command_proc()` returns. */ bool mousekey_console(uint8_t code) { +#ifdef NO_DEFAULT_COMMAND + return true; +#endif static uint8_t param = 0; static uint8_t *pp = NULL; static char * desc = NULL; diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index f7f9e073a74..79d50df0604 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -274,11 +274,17 @@ void dynamic_keymap_reset(void) { for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) { for (int row = 0; row < MATRIX_ROWS; row++) { for (int column = 0; column < MATRIX_COLS; column++) { +#ifdef FLASH_KEYMAP8_COUNT + dynamic_keymap_set_keycode(layer, row, column, (layer < FLASH_KEYMAP8_COUNT)? pgm_read_byte(&keymaps8[layer][row][column]):1); +#elif defined(FLASH_KEYMAP_COUNT) + dynamic_keymap_set_keycode(layer, row, column, (layer < FLASH_KEYMAP_COUNT)? pgm_read_word(&keymaps[layer][row][column]):1); +#else if (layer < keymap_layer_count()) { dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column])); } else { dynamic_keymap_set_keycode(layer, row, column, KC_TRANSPARENT); } +#endif } } #ifdef ENCODER_MAP_ENABLE diff --git a/quantum/eeconfig.c b/quantum/eeconfig.c index 0ff9996ca41..43493af5466 100644 --- a/quantum/eeconfig.c +++ b/quantum/eeconfig.c @@ -41,6 +41,13 @@ void eeconfig_init_quantum(void) { #if defined(EEPROM_DRIVER) eeprom_driver_erase(); #endif +#ifdef RECORE //save 116B + eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); + for (uint8_t i=2; i <= 34; i++) { + eeprom_update_byte((uint8_t *)i,0); + } + default_layer_state = 0; +#else eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); eeprom_update_byte(EECONFIG_DEBUG, 0); eeprom_update_byte(EECONFIG_DEFAULT_LAYER, 0); @@ -56,6 +63,7 @@ void eeconfig_init_quantum(void) { eeprom_update_byte(EECONFIG_VELOCIKEY, 0); eeprom_update_dword(EECONFIG_RGB_MATRIX, 0); eeprom_update_word(EECONFIG_RGB_MATRIX_EXTENDED, 0); +#endif // TODO: Remove once ARM has a way to configure EECONFIG_HANDEDNESS // within the emulated eeprom via dfu-util or another tool @@ -73,8 +81,10 @@ void eeconfig_init_quantum(void) { // this is used in case haptic is disabled, but we still want sane defaults // in the haptic configuration eeprom. All zero will trigger a haptic_reset // when a haptic-enabled firmware is loaded onto the keyboard. +#ifndef RECORE //14B eeprom_update_dword(EECONFIG_HAPTIC, 0); #endif +#endif #if defined(VIA_ENABLE) // Invalidate VIA eeprom config, and then reset. // Just in case if power is lost mid init, this makes sure that it pets diff --git a/quantum/keymap.h b/quantum/keymap.h index edff4841290..14e887fa06a 100644 --- a/quantum/keymap.h +++ b/quantum/keymap.h @@ -55,6 +55,7 @@ along with this program. If not, see . uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key); extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; +extern const uint8_t keymaps8[][MATRIX_ROWS][MATRIX_COLS]; #ifdef ENCODER_MAP_ENABLE // Ensure we have a forward declaration for the encoder map diff --git a/quantum/keymap_common.c b/quantum/keymap_common.c index c41bf8f8bd5..82cf30159c8 100644 --- a/quantum/keymap_common.c +++ b/quantum/keymap_common.c @@ -43,8 +43,10 @@ action_t action_for_key(uint8_t layer, keypos_t key) { }; action_t action_for_keycode(uint16_t keycode) { +#ifndef RECORE //disable for BLE51 // keycode remapping keycode = keycode_config(keycode); +#endif action_t action = {}; uint8_t action_layer, mod; diff --git a/quantum/keymap_introspection.c b/quantum/keymap_introspection.c index 179b5eb0376..a1fe2d86b1a 100644 --- a/quantum/keymap_introspection.c +++ b/quantum/keymap_introspection.c @@ -11,7 +11,11 @@ #include "keymap_introspection.h" +#ifdef FLASH_KEYMAP8_COUNT +#define NUM_KEYMAP_LAYERS ((uint8_t)(sizeof(keymaps8) / ((MATRIX_ROWS) * (MATRIX_COLS)))) +#else #define NUM_KEYMAP_LAYERS ((uint8_t)(sizeof(keymaps) / ((MATRIX_ROWS) * (MATRIX_COLS) * sizeof(uint16_t)))) +#endif uint8_t keymap_layer_count(void) { return NUM_KEYMAP_LAYERS; diff --git a/quantum/mousekey.c b/quantum/mousekey.c index b7aaac57443..2d8e62c56e8 100644 --- a/quantum/mousekey.c +++ b/quantum/mousekey.c @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - +#ifndef RECORE #include #include #include "keycode.h" @@ -537,3 +537,4 @@ report_mouse_t mousekey_get_report(void) { bool should_mousekey_report_send(report_mouse_t *mouse_report) { return mouse_report->x || mouse_report->y || mouse_report->v || mouse_report->h; } +#endif \ No newline at end of file diff --git a/quantum/via.c b/quantum/via.c index e00d092ab9e..0003bfcad99 100644 --- a/quantum/via.c +++ b/quantum/via.c @@ -85,6 +85,11 @@ void eeconfig_update_rgb_matrix(void); // EEPROM is invalid and use/save defaults. bool via_eeprom_is_valid(void) { #ifdef VIAL_ENABLE + #ifdef RECORE + // build_id.py: print("#define BUILD_ID ((uint32_t)0x{:08X})".format(random.randrange(0, 2 ** 24 - 1))) + // use 16bit,and it should not be 0xffff. + return (eeprom_read_word((void *)VIA_EEPROM_MAGIC_ADDR) == (BUILD_ID & 0xfffe)); + #endif uint8_t magic0 = BUILD_ID & 0xFF; uint8_t magic1 = (BUILD_ID >> 8) & 0xFF; uint8_t magic2 = (BUILD_ID >> 16) & 0xFF; @@ -102,6 +107,10 @@ bool via_eeprom_is_valid(void) { // Keyboard level code (eg. via_init_kb()) should not call this void via_eeprom_set_valid(bool valid) { #ifdef VIAL_ENABLE + #ifdef RECORE + eeprom_update_word((void *)VIA_EEPROM_MAGIC_ADDR, valid ? (BUILD_ID & 0xfffe) : 0xffff); + return; + #endif uint8_t magic0 = BUILD_ID & 0xFF; uint8_t magic1 = (BUILD_ID >> 8) & 0xFF; uint8_t magic2 = (BUILD_ID >> 16) & 0xFF; @@ -140,8 +149,13 @@ void via_init(void) { } void eeconfig_init_via(void) { +#ifdef __AVR__ + cli(); +#endif // set the magic number to false, in case this gets interrupted + #ifndef RECORE via_eeprom_set_valid(false); + #endif // This resets the layout options via_set_layout_options(VIA_EEPROM_LAYOUT_OPTIONS_DEFAULT); // This resets the keymaps in EEPROM to what is in flash. @@ -150,6 +164,9 @@ void eeconfig_init_via(void) { dynamic_keymap_macro_reset(); // Save the magic number last, in case saving was interrupted via_eeprom_set_valid(true); +#ifdef __AVR__ + sei(); +#endif } // This is generalized so the layout options EEPROM usage can be @@ -193,6 +210,7 @@ bool process_record_via(uint16_t keycode, keyrecord_t *record) { // TODO: ideally this would be generalized and refactored into // QMK core as advanced keycodes, until then, the simple case // can be available here to keyboards using VIA + #ifndef RECORE switch (keycode) { case FN_MO13: if (record->event.pressed) { @@ -215,6 +233,7 @@ bool process_record_via(uint16_t keycode, keyrecord_t *record) { return false; break; } + #endif return true; } @@ -238,6 +257,7 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { uint8_t *command_data = &(data[1]); #ifdef VIAL_ENABLE + #ifndef VIAL_INSECURE /* When unlock is in progress, we can only react to a subset of commands */ if (vial_unlock_in_progress) { if (data[0] != id_vial_prefix) @@ -246,6 +266,7 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { if (cmd != vial_get_keyboard_id && cmd != vial_get_size && cmd != vial_get_def && cmd != vial_get_unlock_status && cmd != vial_unlock_start && cmd != vial_unlock_poll) goto skip; } + #endif #endif switch (*command_id) { @@ -274,9 +295,11 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { } case id_switch_matrix_state: { #ifdef VIAL_ENABLE + #ifndef VIAL_INSECURE /* Disable wannabe keylogger unless unlocked */ if (!vial_unlocked) goto skip; + #endif #endif #if ((MATRIX_COLS / 8 + 1) * MATRIX_ROWS <= 28) @@ -424,9 +447,11 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { } case id_dynamic_keymap_macro_set_buffer: { #ifdef VIAL_ENABLE + #ifndef VIAL_INSECURE /* Until keyboard is unlocked, don't allow changing macros */ if (!vial_unlocked) goto skip; + #endif #endif uint16_t offset = (command_data[0] << 8) | command_data[1]; uint16_t size = command_data[2]; // size <= 28 diff --git a/quantum/vial.c b/quantum/vial.c index 93bb15e4e94..5df35d375a2 100644 --- a/quantum/vial.c +++ b/quantum/vial.c @@ -137,6 +137,7 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) { } #endif case vial_get_unlock_status: { +#ifndef RECORE /* Reset message to all FF's */ memset(msg, 0xFF, length); /* First byte of message contains the status: whether board is unlocked */ @@ -151,12 +152,15 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) { } #endif break; +#endif } case vial_unlock_start: { +#if !defined(VIAL_INSECURE) && !defined(RECORE) vial_unlock_in_progress = 1; vial_unlock_counter = VIAL_UNLOCK_COUNTER_MAX; vial_unlock_timer = timer_read(); break; +#endif } case vial_unlock_poll: { #ifndef VIAL_INSECURE @@ -179,9 +183,15 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) { } } #endif +#ifndef RECORE msg[0] = vial_unlocked; msg[1] = vial_unlock_in_progress; msg[2] = vial_unlock_counter; +#else + msg[0] = 1;//vial_unlocked; + msg[1] = 0;//vial_unlock_in_progress; + msg[2] = 0;//vial_unlock_counter; +#endif break; } case vial_lock: { @@ -238,12 +248,22 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) { uint8_t idx = msg[3]; vial_tap_dance_entry_t td; memcpy(&td, &msg[4], sizeof(td)); +#ifndef RECORE td.on_tap = vial_keycode_firewall(td.on_tap); td.on_hold = vial_keycode_firewall(td.on_hold); td.on_double_tap = vial_keycode_firewall(td.on_double_tap); td.on_tap_hold = vial_keycode_firewall(td.on_tap_hold); msg[0] = dynamic_keymap_set_tap_dance(idx, &td); reload_tap_dance(); + +#else + //td.on_tap = vial_keycode_firewall(td.on_tap); + //td.on_hold = vial_keycode_firewall(td.on_hold); + //td.on_double_tap = vial_keycode_firewall(td.on_double_tap); + //td.on_tap_hold = vial_keycode_firewall(td.on_tap_hold); + msg[0] = dynamic_keymap_set_tap_dance(idx, &td); + vial_init(); +#endif break; } #endif @@ -259,9 +279,15 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) { uint8_t idx = msg[3]; vial_combo_entry_t entry; memcpy(&entry, &msg[4], sizeof(entry)); +#ifndef RECORE entry.output = vial_keycode_firewall(entry.output); msg[0] = dynamic_keymap_set_combo(idx, &entry); reload_combo(); +#else + //entry.output = vial_keycode_firewall(entry.output); + msg[0] = dynamic_keymap_set_combo(idx, &entry); + vial_init(); +#endif break; } #endif diff --git a/tmk_core/protocol/ble51.mk b/tmk_core/protocol/ble51.mk index 69e791a4b3c..907df20488f 100644 --- a/tmk_core/protocol/ble51.mk +++ b/tmk_core/protocol/ble51.mk @@ -1,9 +1,10 @@ BLE51_DIR = protocol/ble51 SRC += $(BLE51_DIR)/recore/serial_uart.c \ - $(BLE51_DIR)/recore/action.c \ + $(BLE51_DIR)/recore/recore.c \ $(BLE51_DIR)/recore/bootloader.c \ $(BLE51_DIR)/recore/suspend.c \ + $(BLE51_DIR)/recore/mousekey.c \ $(BLE51_DIR)/ble51.c \ $(BLE51_DIR)/ble51_task.c \ $(BLE51_DIR)/main.c @@ -14,3 +15,5 @@ OPT_DEFS += -DRECORE VPATH += $(TMK_DIR)/$(BLE51_DIR) VPATH += $(TMK_DIR)/$(BLE51_DIR)/recore + +ALLOW_WARNINGS = yes \ No newline at end of file diff --git a/tmk_core/protocol/ble51/ble51.c b/tmk_core/protocol/ble51/ble51.c index c71fcf6a4d3..0783b7e43ed 100644 --- a/tmk_core/protocol/ble51/ble51.c +++ b/tmk_core/protocol/ble51/ble51.c @@ -25,11 +25,20 @@ along with this program. If not, see . #include "debug.h" #include "timer.h" #include "wait.h" - +#ifndef NOT_BLE #ifndef BLE51_CONSUMER_ON_DELAY -#define BLE51_CONSUMER_ON_DELAY 633 +#define BLE51_CONSUMER_ON_DELAY 255 //633 works for normal kbd with 8M avr #endif +#if (SERIAL_UART_BAUD == 76800) && !defined(SERIAL_UART_BAUD_STR) +#define SERIAL_UART_BAUD_STR "76800" +#endif +/* +Starting with firmware version 0.6.7, +when the TX FIFO buffer is full a 200ms blocking delay will be used to see +if any free space becomes available in the FIFO before returning ERROR. +*/ + /* Host driver */ static uint8_t keyboard_leds(void); static void send_keyboard(report_keyboard_t *report); @@ -47,38 +56,64 @@ host_driver_t ble51_driver = { uint8_t ble51_stop_sending = 0; bool report_error = false; -static uint16_t consumer_ble51_on = 0; +//Confirm the result when all keys pop up +uint8_t g_u8_kbd_report_clear_failed_count = 0; +static uint8_t ble51_recv2_finished = 0; // 0: false; other: true +static uint8_t consumer_ble51_on = 0; static uint16_t consumer_ble51_data; -char ble51_buf[30]; +/* ble51_buf */ +char ble51_buf[32]; static inline void serial_init_1st(void) { +#if 0 cli(); UBRR1L = ((F_CPU/(8.0*9600)-1+0.5)); /* baud rate 9600*/ - UBRR1H = 103>>8; /* baud rate */ + //UBRR1H = 103>>8; /* baud rate H. 9600bps with 8M/16M will not exceed 0xff */ UCSR1B |= (1<mods, - report->keys[0], - report->keys[1], - report->keys[2], - report->keys[3], - report->keys[4], - report->keys[5]); - dprint("sending keyboard..."); - if (memcmp(ble51_cmd(ble51_buf), "ERROR", 5) == 0) { - report_error = 1; + + #if 0 //may save some flash + sprintf(ble51_buf, "%02X-00-%02X-%02X-%02X-%02X-%02X-%02X\n", + report->mods, + report->keys[0], + report->keys[1], + report->keys[2], + report->keys[3], + report->keys[4], + report->keys[5]); + #else + uint8_t *p_report = &report->mods; + for (uint8_t i=0; i<8; i++) { + sprintf(ble51_buf+i*3, "%02X-", *p_report++); + } + ble51_buf[23] = '\n'; // the last '-' to '\n' + #endif + dprint("sending keyboard..."); +#ifndef DEBUG_BLE51 // no need to get the result of keyboard command + ble51_puts(ble51_buf); + + if (report->mods == 0 && report->keys[0] == 0) { + const char *result = ble51_gets(); + dprintf("rpt_Clear %X: %s\n", timer_read(), result); + //result is 'O'K + if (result[0] == 'O') { + g_u8_kbd_report_clear_failed_count = 0; } else { - report_error = 0; - dprint("end\n"); - } -#ifndef NO_BLE51_LED - uint8_t key = report->keys[0]; - if (report->mods == 0 ) { - uint8_t key_led = 0; - if (key == KC_NLCK) key_led = 16; - else if (key == KC_CAPS) key_led = 17; - else if (key == KC_SLCK) key_led = 18; - if (key_led) ble51_led_set(key_led); + g_u8_kbd_report_clear_failed_count++; + //kb_idle_times = 0; } + } + _delay_us(15); +#else + if (memcmp(ble51_cmd(ble51_buf), "ERROR", 5) == 0) { + report_error = 1; + } else { + report_error = 0; + dprint("end\n"); + } #endif +#ifndef NO_BLE51_LED + uint8_t key = report->keys[0]; + if (report->mods == 0 ) { + uint8_t key_led = 0; + if (key == KC_NLCK) key_led = 16; + else if (key == KC_CAPS) key_led = 17; + else if (key == KC_SLCK) key_led = 18; + if (key_led) ble51_led_set(key_led); } +#endif } } @@ -226,6 +347,9 @@ static void send_mouse(report_mouse_t *report) report->y, report->v, report->h); +#ifndef DEBUG_BLE51 // no need to get the result of keyboard command + ble51_puts(ble51_buf); +#else if (memcmp(ble51_cmd(ble51_buf), "OK", 2) == 0) { report_error = 0; dprint("end\n"); @@ -233,6 +357,7 @@ static void send_mouse(report_mouse_t *report) else { report_error = 1; } +#endif } #ifndef MOUSEBUTTON_NO_CHECK @@ -245,6 +370,9 @@ static void send_mouse(report_mouse_t *report) dprint("sending mouse button..."); //sprintf(ble51_buf, "AT+BLEHIDMOUSEBUTTON=%d\n", report->buttons); sprintf(ble51_buf+strlen("AT+BLEHIDMOUSE"), "BUTTON=%d\n", report->buttons); +#ifndef DEBUG_BLE51 // no need to get the result of keyboard command + ble51_puts(ble51_buf); +#else if (memcmp(ble51_cmd(ble51_buf), "OK", 2) == 0) { report_error = 0; dprint("end\n"); @@ -252,6 +380,7 @@ static void send_mouse(report_mouse_t *report) else { report_error = 1; } +#endif } } @@ -270,12 +399,16 @@ static void send_consumer(uint16_t data) if (!report_error) { dprintf("sending consumer...%X...", data); sprintf(ble51_buf, "AT+BLEHIDCONTROLKEY=%d\n",data); +#ifndef DEBUG_BLE51 // no need to get the result of keyboard command + ble51_puts(ble51_buf); +#else if (memcmp(ble51_cmd(ble51_buf), "OK", 2) == 0) { report_error = 0; dprint("end\n"); } else { report_error = 1; } +#endif } } @@ -285,9 +418,11 @@ void ble51_consumer_task(void) if (consumer_ble51_on > 1 ) { consumer_ble51_on--; } else { - static uint8_t sending_consumer_step = 0; //controll repeat speed to be similar to USB - if (++sending_consumer_step >= (0xF3 - consumer_ble51_data)) { - sending_consumer_step = 0; + static uint16_t sending_consumer_timer = 0; //controll repeat speed to be similar to USB + //if (++sending_consumer_step >= (0xF3 - consumer_ble51_data)) { + // ʹ ble51_puts() ble51_cmd()ʱ졣ԷͼҪ//0xe9 volup + if (timer_elapsed(sending_consumer_timer) > 40) { + sending_consumer_timer = timer_read(); send_consumer(consumer_ble51_data); } } @@ -310,6 +445,7 @@ void ble51_set_connectable(uint8_t mode) void ble51_set_blehiden(uint8_t mode) { + //save space if (mode == '0') ble51_init_cmd("AT+BLEHIDEN=0\n"); else ble51_init_cmd("AT+BLEHIDEN=1\n"); } @@ -331,3 +467,5 @@ void ble51_del_bonds(void) { ble51_init_cmd("AT+GAPDELBONDS\n"); } + +#endif \ No newline at end of file diff --git a/tmk_core/protocol/ble51/ble51.h b/tmk_core/protocol/ble51/ble51.h index 2df823a3d5f..de1c1bceb20 100644 --- a/tmk_core/protocol/ble51/ble51.h +++ b/tmk_core/protocol/ble51/ble51.h @@ -2,18 +2,15 @@ #define BLE51_H #include -#include #include "host_driver.h" #include "ble51_task.h" #include "recore.h" -#define TIMEOUT 100 - host_driver_t ble51_driver; void ble51_init(void); int16_t ble51_getc(void); -const char *ble51_gets(uint16_t timeout); +const char *ble51_gets(void); void ble51_putc(uint8_t c); void ble51_puts(char *s); void ble51_consumer_task(void); @@ -21,7 +18,10 @@ void ble51_led_set(uint8_t key_led); const char *ble51_cmd(char *s); void ble51_init_cmd(char *s); +void ble51_clear_recv2(void); void ble51_clear_keys(void); +void ble51_stop_sending_end_action(void); + void ble51_init_blename(void); void ble51_factory_reset(void); @@ -32,5 +32,7 @@ uint8_t ble51_get_connection_status(void); void ble51_del_bonds(void); extern uint8_t ble51_stop_sending; +//Confirm the result when all keys pop up +extern uint8_t g_u8_kbd_report_clear_failed_count; #endif diff --git a/tmk_core/protocol/ble51/ble51_task.c b/tmk_core/protocol/ble51/ble51_task.c index d1d6b09921a..850608d94ba 100644 --- a/tmk_core/protocol/ble51/ble51_task.c +++ b/tmk_core/protocol/ble51/ble51_task.c @@ -43,9 +43,10 @@ along with this program. If not, see . #define BT_POWER_OFF_TIME 9000000 #endif -static void update_battery(uint8_t mode); -static void update_battery_value(uint8_t value); +//static void update_battery(uint8_t mode); +static void update_battery_value(void); static void ble51_reset(uint8_t pressed_mods); +//static void ble51_device_switching(uint8_t mode); //device_switching costs about 212B uint16_t ble_reset_key __attribute__ ((section (".noinit"))); extern debug_config_t debug_config; @@ -62,13 +63,16 @@ uint16_t kb_idle_times = 0; uint8_t usb_connected = 4; +bool usb_connected_once = 0; bool bt_connected = 0; +//bool display_connection_status = true; uint8_t display_connection_status_check_times = 1; -#define U8V(x) (x/16 - 1024/16) //convert voltage to uint8_t (0-255) -static const uint8_t battery_level_value[9] = {U8V(4050), U8V(3950), U8V(3900), U8V(3800), U8V(3750), U8V(3700), U8V(3650), U8V(3600), U8V(3550)}; +//#define U8V(x) (x/16 - 1024/16) //convert voltage to uint8_t (0-255) +#define U8V(x) (x/8 - (330-25)) //adc_u8 +static const uint8_t battery_level_value[9] = {U8V(4070), U8V(3970), U8V(3900), U8V(3800), U8V(3750), U8V(3700), U8V(3650), U8V(3600), U8V(3550)}; uint8_t battery_calc_u8 = 0; uint16_t battery_voltage = 3699; uint8_t battery_check_timer = 0; @@ -87,7 +91,11 @@ uint8_t low_battery = 0; bool is_charging = 0; #endif +// BLE51_PowerState: work 1 | dozing 2 | update battery 3(delete) | sleeping 4 | lock mode 10 +// BLE51_PowerDozing_EN: default 1 | no_sleep 0 +// ble51_off and no usb, 12 uint8_t BLE51_PowerState = 1; +uint8_t BLE51_PowerDozing_EN = 1; void ble51_task_init(void) @@ -95,16 +103,14 @@ void ble51_task_init(void) #ifdef CHARGING_STATE_INIT CHARGING_STATE_INIT(); #endif - update_battery(1); + update_battery(1); //update battery value when keyboard boots. } __attribute__ ((weak)) void ble51_task_user(void){} -static uint8_t usb_nkro; - -void ble51_task(void) +void usb_bt_state_task(void) { // update USB states usb_connected = (USB_DeviceState == DEVICE_STATE_Configured)? 1 : 0; @@ -112,25 +118,57 @@ void ble51_task(void) /* Bluetooth mode | USB mode */ if (keyboard_protocol & (1<<7)) { if (host_get_driver() != &ble51_driver) { + //usb_connected = 0; //set it to 0 to reconfirm USB_DeviceState print("Bluetooth\n"); - usb_nkro = keymap_config.nkro; + //ble51_usb_nkro = keymap_config.nkro; host_set_driver(&ble51_driver); - } - keymap_config.nkro = 0; // always disable NKRO for BT mode + //function_led_state &= ~(1<<1); //Bluetooth Mode, LED off + //keyboard_function_led_set(); + } + if (!usb_connected_once && usb_connected) { + keyboard_protocol &= ~(1<<7); + } } else { - if (host_get_driver() != &lufa_driver) { - print("USB\n"); - keymap_config.nkro = usb_nkro; - host_set_driver(&lufa_driver); - } else if (!usb_connected) { + if (!usb_connected) { + // no usb, switch to bt if ble51_boot_on if (ble51_boot_on) keyboard_protocol |= (1<<7); else BLE51_PowerState = 12; + } else if (host_get_driver() != &lufa_driver) { + print("USB\n"); + host_set_driver(&lufa_driver); + //function_led_state |= (1<<1); //USB Mode, LED On + //keyboard_function_led_set(); + usb_connected_once = 1; } } - - if (!ble51_boot_on) return; +} + +void ble51_task(void) +{ + //if (!ble51_boot_on) return; if (BT_POWERED) { +#ifndef DEBUG_BLE51 + //get the result(OK or ERROR) of keyboard_cmd ble51_puts() with no waiting. + ble51_clear_recv2(); + // to confirm report_clear(key up action) is executed + if (g_u8_kbd_report_clear_failed_count && bt_connected) { + ble51_clear_keys(); + // After many failures, try to restart. + xprintf("clear failed times: %d\n", g_u8_kbd_report_clear_failed_count); + if (g_u8_kbd_report_clear_failed_count == 16) { + #ifdef bt_power_reset() + //run only once when the count is specified. + bt_connected = 0; + bt_power_reset(); + print("bt power reset\n"); + #else + for (;;); + #endif + } + } +#else + /* if error */ if (report_error) { print("\nerroring..."); if (memcmp(ble51_cmd("AT\n"), "OK", 2) == 0) { @@ -148,16 +186,10 @@ void ble51_task(void) need_clear = 0; } } +#endif - - if (BLE51_PowerState == 2) { - update_battery(1); - BLE51_PowerState = 3; - } - - /* check every 1s.*/ - if (!need_clear && timer_elapsed(check_timer) > 1000) { + if (!need_clear && timer_elapsed(check_timer) >= 1000) { check_timer = timer_read(); static uint8_t check_connection_times = 0; /* if bt_connected, check every 4s */ @@ -174,28 +206,38 @@ void ble51_task(void) if (bt_connected) print("BT disconnected\n"); bt_connected = 0; disconnect_s++; - /* if disconnected for more than 90s, turn off bt */ - if (!usb_connected && BLE51_PowerState && disconnect_s >= 90) { + /* if usb_connected == 0 and disconnected for more than 90s, turn off bt */ + //if (!usb_connected && BLE51_PowerState && disconnect_s >= 90) { + if (!usb_connected && disconnect_s >= 90) { print("90s now. BT is off. Sleeping.\n"); - //if (USB_DeviceState != DEVICE_STATE_Configured) { - turn_off_bt(); - BLE51_PowerState = 4; - //} + turn_off_bt(); + //BLE51_PowerState = 4; disconnect_s = 0; } - } else { - //xprintf("%s\n", result); + } + #ifdef DEBUG_BLE51 + else { + xprintf("%s\n", result); need_clear = 1; } + #endif } /* Display connect status*/ if (display_connection_status_check_times) { - if (display_connection_status_check_times == 100) { + if (++display_connection_status_check_times > 20) { display_connection_status_check_times = 0; + // qmk led_wakeup(); - } else if (++display_connection_status_check_times >= 20 || bt_connected == 1) { - display_connection_status_check_times = 100; + } else { + //display_connection_status_check_times++; + if (bt_connected == 1) { + // After a 20s timeout or bt_connected, the next loop ends. + // So there is 1s to indicate that it is connected. + display_connection_status_check_times = 100; + // 重置check_connection_times,确保已连接指示有4s左右(闪三次) + check_connection_times = 0; + } } } } @@ -212,37 +254,48 @@ void ble51_task(void) #endif } - if (!usb_connected && !display_connection_status_check_times) { + if (!usb_connected && !display_connection_status_check_times && BLE51_PowerDozing_EN) { + // count idle time kb_idle_times++; dprintf("idle times: %d, PowerState: %d\n", kb_idle_times, BLE51_PowerState); - - if (BLE51_PowerState == 1 && (kb_idle_times >= (BT_POWER_SAVE_TIME/1000))) { - print("dozing\n"); - ble51_clear_keys(); - BLE51_PowerState = 2; - } - - if (BLE51_PowerState == 3 && (kb_idle_times >= (BT_POWER_OFF_TIME/1000))) { - print("BT is idle for a long time. Turn off. \n"); + if (kb_idle_times >= (BT_POWER_OFF_TIME/1000)) { + // 暂时保留 BLE51_PowerState 4,HHKB BLE和BLE660C等有使用到 BLE51_PowerState = 4; turn_off_bt(); + } else + // always set State to 2, when BT_POWERED and kb_idle_times for POWER_SAVE_TIME. + if (kb_idle_times >= (BT_POWER_SAVE_TIME/1000)) { + BLE51_PowerState = 2; } } } - + /* ble set once + *AT+GAPSETADVDATA=02-01-04/n Discoverable OFF KC_O + *AT+GAPSETADVDATA=02-01-06/n Discoverable ON KC_I + *AT+GAPDELBONDS\n Pair Clear KC_R + */ if (ble_set_code > 0) { + //watchdog_off(); if (ble_set_code == KC_I) { - ble51_cmd("AT+GAPSETADVDATA=02-01-06\n"); + ble51_puts("AT+GAPSETADVDATA=02-01-0"); + ble51_cmd("6\n"); } else if (ble_set_code == KC_O) { - ble51_cmd("AT+GAPSETADVDATA=02-01-04\n"); + ble51_puts("AT+GAPSETADVDATA=02-01-0"); + ble51_cmd("4\n"); + //watchdog_on(); } else if (ble_set_code == KC_R) { + //ble51_set_connectable('0'); + //ble51_cmd("AT+GAPDISCONNECT\n"); + //ble51_cmd("AT+GATTCLEAR\n"); //clear battery service. ble51_del_bonds(); ble51_set_blehiden('0'); ble51_set_blehiden('1'); + //ble51_set_connectable('1'); ble51_cmd("ATZ\n"); - ble51_cmd("AT+GAPSETADVDATA=02-01-06\n"); + ble51_puts("AT+GAPSETADVDATA=02-01-0"); + ble51_cmd("6\n"); } ble_set_code = 0; } @@ -250,13 +303,9 @@ void ble51_task(void) /* hold consumer key */ ble51_consumer_task(); } - - - - if (BLE51_PowerState <= 1) ble51_task_user(); } -static void update_battery(uint8_t mode) { +void update_battery(uint8_t mode) { /* mode 0: not update bat_value, only get voltage and calc low_battery. */ /*only update when light is off.*/ @@ -269,30 +318,59 @@ static void update_battery(uint8_t mode) { #endif const char *result = ble51_cmd("AT+HWADC=6\n"); + /* simple calculate batt level + * Char "0"->"9" = 0x30->0x39 Char "O"=0x4F. atol() needs more space. + * When no battery, ADC value is 32 and voltage measured by multimeter is 54mv. + * So result[2] will be "_" not 0. + */ +#ifdef DEBUG_BLE51 if (result[1] < '0' || result[1] > '9') need_clear = 1; else if(result[2] > '9') print("No battery or wrong!\n"); else { - uint16_t battery_voltage_get = ((result[0]-48)*100 + (result[1]-48)*10 + result[2]- 48 -32) * 8 + 54; - battery_voltage = (battery_voltage_get + ((battery_voltage == 3699)?battery_voltage_get:battery_voltage)) / 2; - dprintf(" Battery Voltage: %dmv, ADC Value:%s\n", battery_voltage, result); - -#ifdef UPDATE_BATTERY_WHEN_CHARGING - if (BATTERY_CHARGING) { - is_charging = 1; - battery_voltage -= CHARGING_FIX_VALUE; - } else { - is_charging = 0; - } +#else + // confirm the result is voltage + if (result[0] >= '3' && result[2] >= '0' && result[2] <= '9') { #endif - battery_calc_u8 = U8V(battery_voltage); - if (battery_calc_u8 > U8V(3600)) low_battery = 0; - else if (battery_calc_u8 < U8V(3420)) low_battery = 1; - else if (battery_calc_u8 < U8V(3550)) low_battery = 5; + //uint16_t battery_voltage_get = ((result[0]-48)*100 + (result[1]-48)*10 + result[2]- 48 -32) * 8 + 54; + // adc - 330, to unit8_t, max voltage = 4480 + uint8_t bat_adc_u8 = result[0]*100 + result[1]*10 + result[2]- 48*100 - 48*10 - 48 - 330; + + // Control the effective range of adc_u8, and update only within this range. + if (bat_adc_u8 > U8V(3350)) { + #ifndef BLE51_NO_BATTERY_VOLTAGE //save space + uint16_t battery_voltage_get = bat_adc_u8 * 8 + (330-25)*8; + battery_voltage = (battery_voltage_get + ((battery_voltage == 3699)?battery_voltage_get:battery_voltage)) / 2; + dprintf(" BAT Volt: %dmv, ADC:%s\n", battery_voltage, result); + battery_calc_u8 = U8V(battery_voltage); + #else + battery_calc_u8 = bat_adc_u8; + #endif + #ifdef UPDATE_BATTERY_WHEN_CHARGING + if (BATTERY_CHARGING) { + is_charging = 1; + //battery_voltage -= CHARGING_FIX_VALUE; + //Directly testing the battery voltage during charging is not accurate. + if (battery_calc_u8 < U8V(4208)) battery_calc_u8 -= ((CHARGING_FIX_VALUE+2) / 4); + else battery_calc_u8 -= ((CHARGING_FIX_VALUE+4) / 8); + dprintf(" CHG Calc: %dmv\n", battery_calc_u8*8+(330-25)*8); + } else { + is_charging = 0; + } + #endif + #ifdef BLE51_NO_ULTRA_LOW_BATTERY + low_battery = (battery_calc_u8 < U8V(3550))? 5:0; + #else + if (battery_calc_u8 > U8V(3600)) low_battery = 0; + else if (battery_calc_u8 < U8V(3420)) low_battery = 1; //Ultra low battery + else if (battery_calc_u8 < U8V(3550)) low_battery = 5; + #endif + } } #ifdef BLE_BATTERY_SERVICE if (mode == 1) { + /* Calculate battery level */ uint8_t battery_level = 44; uint8_t i = 0; for (uint8_t j = 90; j > 0; j -= 10) { @@ -313,16 +391,21 @@ static void update_battery(uint8_t mode) { #else if (battery_calc_u8 >= U8V(4140)) battery_level = 100; #endif - update_battery_value(battery_level); + battery_percentage = battery_level; + update_battery_value(); } #endif } -static void update_battery_value(uint8_t value) { - battery_percentage = value; - ble51_puts("AT+GATTCHAR=1,"); - sprintf(ble51_buf, "%d\n", value); + +static void update_battery_value(void) { + #ifdef BLE51_USE_BLEBATT // Two ways for battery service. Only use one of them. + sprintf(ble51_buf, "AT+BLEBATTVAL=%d\n", battery_percentage); + #else + sprintf(ble51_buf, "AT+GATTCHAR=1,%d\n", battery_percentage); + #endif ble51_cmd(ble51_buf); + ble51_cmd(ble51_buf); // run it one more time to properly refresh on macOS. } #define MODS_SHIFT_MASK (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) @@ -338,12 +421,12 @@ static void ble51_reset(uint8_t pressed_mods) { } } - bool command_extra(uint8_t code) { uint8_t pressed_mods = get_mods(); clear_keyboard(); switch (code) { +#ifndef NOT_BLE case KC_U: if (ble51_boot_on) keyboard_protocol ^= (1<<7); return true; @@ -360,21 +443,29 @@ bool command_extra(uint8_t code) return false; } case KC_S: + // 1: start to display + // 100: end display and restore ledmapu. display_connection_status_check_times = display_connection_status_check_times? 100:1; return true; case KC_P: - BLE51_PowerState ^= 1; - type_num(BLE51_PowerState); - return true; - case KC_B: - ble51_reset(pressed_mods); + BLE51_PowerDozing_EN ^= 1; + type_num(BLE51_PowerDozing_EN); return true; - case KC_V: + case KC_W: +#if !defined(HARDWARE_BT_SWITCH) && !defined(NO_BT_SWITCH) + // EECONFIG_USER (uint32_t *)19, (uint8_t)0xBD mean off + eeprom_update_byte(EECONFIG_USER, (pressed_mods&MOD_BIT(KC_LCTRL))?0xBD:1); + for (;;); +#endif + case KC_V: // output battery and bt connection type_numbers(battery_percentage); + #ifndef BLE51_NO_BATTERY_VOLTAGE + //display voltage with LCtrl pressed. 30Byte if (pressed_mods & MOD_BIT(KC_LCTRL)) { tap_code(KC_MINS); type_numbers(battery_voltage); } + #endif tap_code(KC_MINS); type_num(bt_connected); return true; @@ -386,9 +477,18 @@ bool command_extra(uint8_t code) layer_clear(); } return true; +#endif // USB only, less + case KC_B: // decide what (LCtrl+)LRShift+B by user action_function + ble51_reset(pressed_mods); + return true; + case KC_N: // for hook; + hook_nkro_change(); + //ble51_usb_nkro = !ble51_usb_nkro; + #ifdef NO_DEFAULT_COMMAND + keymap_config.nkro = !keymap_config.nkro; + #endif + return true; default: - return false; + return false; // yield to default command } - return true; } - diff --git a/tmk_core/protocol/ble51/ble51_task.h b/tmk_core/protocol/ble51/ble51_task.h index 301b6a2d5fa..1aa2da1de17 100644 --- a/tmk_core/protocol/ble51/ble51_task.h +++ b/tmk_core/protocol/ble51/ble51_task.h @@ -4,18 +4,23 @@ #include #include "ble51.h" -extern uint8_t BLE51_PowerState; +extern uint8_t BLE51_PowerState; //no_sleep 0, work 1,dozing 2, check battery once when dozing 3, sleeping 4, lock mode 10 +extern uint8_t BLE51_PowerDozing_EN; // extern bool ble51_boot_on; extern uint16_t kb_idle_times; extern uint8_t display_connection_status_check_times; extern bool bt_connected; extern uint8_t usb_connected; +extern bool usb_connected_once; extern uint8_t low_battery; extern uint16_t battery_voltage; +extern uint8_t ble51_usb_nkro; +void usb_bt_state_task(void); void ble51_task_init(void); void ble51_task(void); void ble51_task_user(void); +void update_battery(uint8_t mode); extern uint16_t ble_reset_key; #endif diff --git a/tmk_core/protocol/ble51/config_ble51.h b/tmk_core/protocol/ble51/config_ble51.h new file mode 100644 index 00000000000..5deb76c7666 --- /dev/null +++ b/tmk_core/protocol/ble51/config_ble51.h @@ -0,0 +1,53 @@ +#pragma once + +#define CONTACT(x,y) x##y //https://blog.csdn.net/aiynmimi/article/details/123486956 +#define CONTACT2(x,y) CONTACT(x,y) +#if CONSOLE_ENABLE +#define FW_VER CONTACT2(DEBUG_VIAL_, FW_VER_DATE) +#else +#define FW_VER CONTACT2(VIAL_, FW_VER_DATE) +#endif + +/* command. LShift+RShift+X and LShift+RShift+LCtrl+X for YDKB*/ +#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))) \ +) + +/* disable command for default layer */ +#define MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS 0 +#define MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS 0 + +/* UART Config */ +#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 "timer.h" #include "suspend.h" #include "ble51.h" -#include "ble51_task.h" - void protocol_post_task(void); +/* hooks */ +__attribute__((weak)) +void hook_early_init(void) {} + +static int8_t sendchar_func(uint8_t c) +{ + //xmit(c); // SUART + sendchar(c); // LUFA + return 0; +} + static void SetupHardware(void) { /* Disable clock division */ @@ -51,38 +60,58 @@ static void SetupHardware(void) } int main(void) __attribute__ ((weak)); + +#ifndef NOT_BLE // BLE Ver + int main(void) { SetupHardware(); sei(); +#if !defined(HARDWARE_BT_SWITCH) && !defined(NO_BT_SWITCH) + if (eeprom_read_byte(EECONFIG_USER) == 0xBD) ble51_boot_on = 0; +#endif + + // BLE Power on + if (ble51_boot_on) { + ble51_init(); + ble51_task_init(); + } +#if 1 //def CONSOLE_ENABLE /* wait for USB startup to get ready for debug output */ uint8_t timeout = 255; while (timeout-- && USB_DeviceState != DEVICE_STATE_Configured) { - _delay_ms(6); + WAIT_MS(12); // wait for USB about 3s +#ifdef PS2_USE_INT // BLE980M may need more time. + WAIT_MS(16); +#endif #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); #endif } print("\nUSB init\n"); - - if (ble51_boot_on) { - ble51_init(); - ble51_task_init(); - } +#endif /* init modules */ keyboard_init(); print("Keyboard start\n"); +#ifdef DEFAULT_6KRO + keymap_config.nkro = 0; +#else + keymap_config.nkro = 1; // default force nkro on for ble series +#endif watchdog_on(); while (1) { - if (BLE51_PowerState < 4){ + //if (BT_POWERED){ + usb_bt_state_task(); + if (BLE51_PowerState < 10){ wdt_reset(); if (BLE51_PowerState <= 1) { keyboard_task(); protocol_post_task(); + if (ble51_boot_on) ble51_task_user(); } ble51_task(); } @@ -91,14 +120,59 @@ int main(void) watchdog_on(); if (suspend_wakeup_condition()) { kb_idle_times = 0; + if (ble51_boot_on) { + BLE51_PowerState = 1; + if (BT_POWERED) { + update_battery(1); + } else { + display_connection_status_check_times = 1; + turn_on_bt(); + } + } suspend_wakeup_init_action(); - BLE51_PowerState = 1; - if (ble51_boot_on) { - turn_on_bt(); + if (usb_connected_once && USB_DeviceState == DEVICE_STATE_Suspended) { + USB_Device_SendRemoteWakeup(); } - else if (USB_DeviceState == DEVICE_STATE_Suspended) USB_Device_SendRemoteWakeup(); } } } } +#else // Not BLE +int main(void) +{ + SetupHardware(); + sei(); + + /* wait for USB startup & debug output */ + while (USB_DeviceState != DEVICE_STATE_Configured) { +#if !defined(INTERRUPT_CONTROL_ENDPOINT) + USB_USBTask(); +#endif + } + + print("\nUSB init\n"); + /* init modules */ + keyboard_init(); + host_set_driver(&lufa_driver); + usb_connected = 1; + print("Keyboard start\n"); + watchdog_on(); + while (1) { + // usb suspend + while (USB_DeviceState == DEVICE_STATE_Suspended) { + suspend_power_down(); + watchdog_on(); + if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { + USB_Device_SendRemoteWakeup(); + //suspend_wakeup_init_action(); + for (;;); //restart keyboard + } + } + wdt_reset(); + keyboard_task(); + protocol_post_task(); // for via? + } +} + +#endif \ No newline at end of file diff --git a/tmk_core/protocol/ble51/recore.h b/tmk_core/protocol/ble51/recore.h index 00e397ad439..c33331a248e 100644 --- a/tmk_core/protocol/ble51/recore.h +++ b/tmk_core/protocol/ble51/recore.h @@ -6,23 +6,27 @@ #include "ble51.h" #include "bootloader.h" #include "quantum.h" -#include "send_string.h" //qmk +//#include "send_string.h" //qmk #include "keycode_config.h" //qmk +#ifdef NOT_BLE +#define BT_POWERED 0 +#endif // for lock mode, *debounce + x #define KP(row,col) (row * MATRIX_COLS + col) -#define type_num(x) send_nibble(x) +#define WAIT_MS(x) _delay_ms(x) +//#define type_num(x) send_nibble(x) +#define type_num(x) type_numbers(x) //save 40B void type_numbers(uint16_t value); - void suspend_power_down_action(void); void suspend_wakeup_init_action(void); void watchdog_on(void); -__attribute__ ((weak)) void hook_early_init(void); +void hook_nkro_change(void) ; #endif diff --git a/tmk_core/protocol/ble51/recore/action.c b/tmk_core/protocol/ble51/recore/action.c deleted file mode 100644 index 62a6c74bd74..00000000000 --- a/tmk_core/protocol/ble51/recore/action.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include -#include "matrix.h" -#include "action.h" -#include "suspend.h" -#include "timer.h" -#include "send_string.h" -#include "recore.h" - -void type_numbers(uint16_t value) -{ - for (uint16_t i=10000; i>=1; i=i/10) { - uint8_t this_num = value/i % 10; - if (value/i) { - send_nibble(this_num); - } - } -} \ No newline at end of file diff --git a/tmk_core/protocol/ble51/recore/bootloader.c b/tmk_core/protocol/ble51/recore/bootloader.c index 9582aee6f35..07956dcee82 100644 --- a/tmk_core/protocol/ble51/recore/bootloader.c +++ b/tmk_core/protocol/ble51/recore/bootloader.c @@ -35,6 +35,7 @@ void bootloader_jump(void) { if (BLE51_PowerState <= 1) { _delay_ms(10); USB_Disable(); + // When BLE51_PowerState > 1, watchdog already on watchdog_on(); } for (;;); diff --git a/tmk_core/protocol/ble51/recore/mousekey.c b/tmk_core/protocol/ble51/recore/mousekey.c new file mode 100644 index 00000000000..956de12373c --- /dev/null +++ b/tmk_core/protocol/ble51/recore/mousekey.c @@ -0,0 +1,178 @@ +/* +Copyright 2019 YANG + +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 . +*/ + +#include +#include "keycode.h" +#include "host.h" +#include "timer.h" +#include "print.h" +#include "debug.h" +#include "mousekey.h" + +#include "action_util.h" + + + +report_mouse_t mouse_report = {}; + +static uint8_t mouse_repeat_start = 0; +static uint8_t mouse_wheel_timer = 0; + +static uint8_t mousekey_accel = 0; + +static void mousekey_debug(void); + +// rapidfire key + +/* + * Mouse keys acceleration algorithm + * http://en.wikipedia.org/wiki/Mouse_keys + * + * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000) + */ +/* milliseconds between the initial key press and first repeated motion event (0-2550) */ +//uint8_t mk_delay = MOUSEKEY_DELAY/10; +/* milliseconds between repeated motion events (0-255) */ +//uint8_t mk_interval = MOUSEKEY_INTERVAL; +/* steady speed (in action_delta units) applied each event (0-255) */ +//uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED; +/* number of events (count) accelerating to steady speed (0-255) */ +//uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX; +/* ramp used to reach maximum pointer speed (NOT SUPPORTED) */ +//int8_t mk_curve = 0; +/* wheel params */ +//uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED; +//uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX; + + +static uint16_t last_timer = 0; + + + +void mousekey_task(void) +{ + + if (timer_elapsed(last_timer) < (mouse_repeat_start == 2 ? 5 : 40)) //delay about 40ms to start //(mouse_repeat ? mk_interval : mk_delay*10)) + return; + + //int8_t *p = &mouse_report.x; + //if ((*p++ | *p++ | *p++ | *p++) == 0) { + if ((mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h) == 0) { + //if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) + mouse_repeat_start = 0; + return; + } else if (mouse_repeat_start < 2) { + mouse_repeat_start++; + return; + } + //control wheel speed + int8_t mouse_v, mouse_h; + mouse_v = mouse_report.v; + mouse_h = mouse_report.h; + if ((++mouse_wheel_timer) & 0b1111) { + mouse_report.v = 0; + mouse_report.h = 0; + } + mousekey_send(); + mouse_report.v = mouse_v; + mouse_report.h = mouse_h; + + static uint8_t timer1 = 0; + if (++timer1 > MOUSEKEY_REPEAT_UPDATE_INTERVAL) { //control mouse speedup +#if defined(BLE_NAME) || defined(BLE_NAME_VARIABLE) + #if (SERIAL_UART_BAUD == 19200) + //默认REPEAT_INTERVAL是12,蓝牙5到12是7,使用19200时改到10 + timer1 = (keyboard_protocol & (1<<7))? 10:0; + #else + timer1 = (keyboard_protocol & (1<<7))? 5:0; + #endif +#else + timer1 = 0; +#endif + int8_t *p_report = &mouse_report.x; + for (uint8_t i=0; i<4; i++, *p_report++) { + if (*p_report == 0) continue; + if (mousekey_accel) { // 定速移动 + *p_report = (MOUSEKEY_MOVE_REPEAT_MAX / 4) * mousekey_accel * (*p_report > 0? 1: -1); // mousekey_accel 1 2 4 + } else if (*p_report > 0) { + if (++(*p_report) > MOUSEKEY_MOVE_REPEAT_MAX) { + *p_report = MOUSEKEY_MOVE_REPEAT_MAX; + } + } else if (--(*p_report) < -MOUSEKEY_MOVE_REPEAT_MAX) { + *p_report = -MOUSEKEY_MOVE_REPEAT_MAX; + } + } + } + +} + +void mousekey_on(uint8_t code) +{ + if (code < KC_MS_UP) return; + if (code == KC_MS_UP) mouse_report.y = -1; + else if (code == KC_MS_DOWN) mouse_report.y = 1; + else if (code == KC_MS_LEFT) mouse_report.x = -1; + else if (code == KC_MS_RIGHT) mouse_report.x = 1; + else if (code <= KC_MS_BTN5) mouse_report.buttons |= (1<<(code-KC_MS_BTN1)); //0xF4 - 0xF8 + else if (code == KC_MS_WH_UP) mouse_report.v = 1; + else if (code == KC_MS_WH_DOWN) mouse_report.v = -1; + else if (code == KC_MS_WH_LEFT) mouse_report.h = -1; + else if (code == KC_MS_WH_RIGHT) mouse_report.h = 1; + else if (code <= KC_MS_ACCEL2) mousekey_accel |= (1<<(code-KC_MS_ACCEL0)); //0xFD - 0xFF + if (code >= KC_MS_WH_UP && code <= KC_MS_WH_RIGHT) mouse_wheel_timer = 0; +} + +void mousekey_off(uint8_t code) +{ + if (code < KC_MS_UP) return; + if (code == KC_MS_UP ) { if (mouse_report.y < 0) mouse_report.y = 0; } + else if (code == KC_MS_DOWN ) { if (mouse_report.y > 0) mouse_report.y = 0; } + else if (code == KC_MS_LEFT ) { if (mouse_report.x < 0) mouse_report.x = 0; } + else if (code == KC_MS_RIGHT ) { if (mouse_report.x > 0) mouse_report.x = 0; } + else if (code <= KC_MS_BTN5 ) { mouse_report.buttons &= ~(1<<(code-KC_MS_BTN1)); }//0xF4 - 0xF8 + else if (code == KC_MS_WH_UP ) { if (mouse_report.v > 0) mouse_report.v = 0; } + else if (code == KC_MS_WH_DOWN ) { if (mouse_report.v < 0) mouse_report.v = 0; } + else if (code == KC_MS_WH_LEFT ) { if (mouse_report.h < 0) mouse_report.h = 0; } + else if (code == KC_MS_WH_RIGHT) { if (mouse_report.h > 0) mouse_report.h = 0; } + else if (code <= KC_MS_ACCEL2 ) { mousekey_accel &= ~(1<<(code-KC_MS_ACCEL0)); } //0xFD - 0xFF + +} + +void mousekey_send(void) +{ + mousekey_debug(); + host_mouse_send(&mouse_report); + last_timer = timer_read(); +} + +void mousekey_clear(void) +{ + mouse_report = (report_mouse_t){}; + mousekey_accel = 0; +} + +static void mousekey_debug(void) +{ + if (!debug_mouse) return; + print("mousekey [btn|x y v h](rep/acl): ["); + print_hex8(mouse_report.buttons); print("|"); + print_decs(mouse_report.x); print(" "); + print_decs(mouse_report.y); print(" "); + print_decs(mouse_report.v); print(" "); + print_decs(mouse_report.h); print("]("); + print_dec(mousekey_accel); print(")\n"); +} diff --git a/tmk_core/protocol/ble51/recore/mousekey.h b/tmk_core/protocol/ble51/recore/mousekey.h new file mode 100644 index 00000000000..4b3d1ecb787 --- /dev/null +++ b/tmk_core/protocol/ble51/recore/mousekey.h @@ -0,0 +1,55 @@ +/* +Copyright 2022 YANG + +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 . +*/ + +#ifndef MOUSEKEY_H +#define MOUSEKEY_H + +#include +#include "host.h" + + +#define MOUSEKEY_MOVE_REPEAT_MAX 20 // 4的倍数 +#ifndef MOUSEKEY_REPEAT_UPDATE_INTERVAL +#define MOUSEKEY_REPEAT_UPDATE_INTERVAL 12 // for 8M +#endif + + +#ifdef __cplusplus +extern "C" { +#endif + +extern uint8_t mk_delay; +extern uint8_t mk_interval; +extern uint8_t mk_max_speed; +extern uint8_t mk_time_to_max; +extern uint8_t mk_wheel_max_speed; +extern uint8_t mk_wheel_time_to_max; + +extern bool rapidfire_key[]; +extern bool rapidfire_mode; + +void mousekey_task(void); +void mousekey_on(uint8_t code); +void mousekey_off(uint8_t code); +void mousekey_clear(void); +void mousekey_send(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/tmk_core/protocol/ble51/recore/recore.c b/tmk_core/protocol/ble51/recore/recore.c new file mode 100644 index 00000000000..306c78b390e --- /dev/null +++ b/tmk_core/protocol/ble51/recore/recore.c @@ -0,0 +1,89 @@ +/* +Copyright 2023 YANG + +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 . +*/ +#include +#include +#include +#include +#include "matrix.h" +#include "action.h" +#include "suspend.h" +#include "timer.h" +#include "send_string.h" +#include "recore.h" + +// 使用 send_string 里的 send_nibble(), 进行数字输出。 +void type_numbers(uint16_t value) +{ +#if 1 //save 24B + static char numbers[5] = {0}; + sprintf(numbers,"%d",value); // char 0 to 9, 48 to 57. but key code 1 to 9 to 0, 30 to 39. + for (uint8_t i=0; i<5; i++) { // strlen(str) needs 14B more + uint8_t code = numbers[i]; + if (code == '\0') break; + else { + if (code == 48) code = 58; + tap_code(code - 19); + } + } +#else + for (uint16_t i=10000; i>=1; i=i/10) { + uint8_t this_num = value/i % 10; + if (value/i) { + send_nibble(this_num); + } + } +#endif +} + +void keyboard_post_init_kb(void) +{ +#ifdef VIAL_ENABLE + vial_init(); +#endif +} + +//22B +void tap_code(uint8_t code) { + register_code(code); + if (code == KC_CAPS) wait_ms(100); + unregister_code(code); +} + +// 4B ever if (mods) +void register_mods(uint8_t mods) { + add_mods(mods); + send_keyboard_report(); +} + +void unregister_mods(uint8_t mods) { + del_mods(mods); + send_keyboard_report(); +} + +void register_weak_mods(uint8_t mods) { + add_weak_mods(mods); + send_keyboard_report(); +} + +void unregister_weak_mods(uint8_t mods) { + del_weak_mods(mods); + send_keyboard_report(); +} +// if (mods) end. 4B*4 = 16B + +__attribute__((weak)) +void hook_nkro_change(void) {} diff --git a/tmk_core/protocol/ble51/recore/serial.h b/tmk_core/protocol/ble51/recore/serial.h new file mode 100644 index 00000000000..0204b84a921 --- /dev/null +++ b/tmk_core/protocol/ble51/recore/serial.h @@ -0,0 +1,46 @@ +/* +Copyright 2012 Jun WAKO + +This software is licensed with a Modified BSD License. +All of this is supposed to be Free Software, Open Source, DFSG-free, +GPL-compatible, and OK to use in both free and proprietary applications. +Additions and corrections to this file are welcome. + + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + +* Neither the name of the copyright holders nor the names of + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ + +#pragma once + +#define SERIAL_UART_DATA UDR1 + +/* host role */ +void serial_init(void); +uint8_t serial_recv(void); +int16_t serial_recv2(void); +void serial_send(uint8_t data); diff --git a/tmk_core/protocol/ble51/recore/serial_uart.c b/tmk_core/protocol/ble51/recore/serial_uart.c index 3316304b10c..ec9162893a3 100644 --- a/tmk_core/protocol/ble51/recore/serial_uart.c +++ b/tmk_core/protocol/ble51/recore/serial_uart.c @@ -38,8 +38,9 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include "protocol/serial.h" +#include "serial.h" +#ifndef NOT_BLE #if defined(SERIAL_UART_RTS_LO) && defined(SERIAL_UART_RTS_HI) // Buffer state @@ -76,7 +77,8 @@ uint8_t serial_recv(void) } data = rbuf[rbuf_tail]; - rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; + //rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; + rbuf_tail = (rbuf_tail + 1) & 0xff; rbuf_check_rts_lo(); return data; } @@ -89,7 +91,8 @@ int16_t serial_recv2(void) } data = rbuf[rbuf_tail]; - rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; + //rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; + rbuf_tail = (rbuf_tail + 1) & 0xff; rbuf_check_rts_lo(); return data; } @@ -103,10 +106,13 @@ void serial_send(uint8_t data) // USART RX complete interrupt ISR(SERIAL_UART_RXD_VECT) { - uint8_t next = (rbuf_head + 1) % RBUF_SIZE; + //uint8_t next = (rbuf_head + 1) % RBUF_SIZE; + uint8_t next = (rbuf_head + 1) & 0xff; if (next != rbuf_tail) { rbuf[rbuf_head] = SERIAL_UART_DATA; rbuf_head = next; } rbuf_check_rts_hi(); } + +#endif \ No newline at end of file diff --git a/tmk_core/protocol/ble51/recore/suspend.c b/tmk_core/protocol/ble51/recore/suspend.c index 82605998ea4..0287212d558 100644 --- a/tmk_core/protocol/ble51/recore/suspend.c +++ b/tmk_core/protocol/ble51/recore/suspend.c @@ -58,6 +58,9 @@ static void power_down(uint8_t wdto) void suspend_power_down(void) { +#ifdef WDTO_EXTRA_15MS + power_down(WDTO_15MS); +#endif power_down(WDTO_30MS); } @@ -71,7 +74,11 @@ void suspend_wakeup_init(void) ISR(WDT_vect) { + // default 30ms instead of 15ms if (wdt_timeout == WDTO_30MS) { timer_count += 30 + 2; } +#ifdef WDTO_EXTRA_15MS + else if (wdt_timeout == WDTO_15MS) { timer_count += 15 + 2;} //from observation +#endif } diff --git a/tmk_core/protocol/host.c b/tmk_core/protocol/host.c index 3d8604d5414..7e83e548e5d 100644 --- a/tmk_core/protocol/host.c +++ b/tmk_core/protocol/host.c @@ -65,7 +65,11 @@ led_t host_keyboard_led_state(void) { void host_keyboard_send(report_keyboard_t *report) { if (!driver) return; #if defined(NKRO_ENABLE) && defined(NKRO_SHARED_EP) +#ifndef RECORE if (keyboard_protocol && keymap_config.nkro) { +#else + if (IS_NKRO()) { +#endif /* The callers of this function assume that report->mods is where mods go in. * But report->nkro.mods can be at a different offset if core keyboard does not have a report ID. */ diff --git a/tmk_core/protocol/host.h b/tmk_core/protocol/host.h index 6b15f0d0c10..fcaec9132ba 100644 --- a/tmk_core/protocol/host.h +++ b/tmk_core/protocol/host.h @@ -33,6 +33,12 @@ along with this program. If not, see . extern "C" { #endif +#ifdef RECORE +#define IS_NKRO() (keyboard_protocol == 1 && keymap_config.nkro) +#else +#define IS_NKRO() (keyboard_protocol && keymap_config.nkro) +#endif + extern uint8_t keyboard_idle; extern uint8_t keyboard_protocol; diff --git a/tmk_core/protocol/report.c b/tmk_core/protocol/report.c index 5755098c60f..f50c1d1c19e 100644 --- a/tmk_core/protocol/report.c +++ b/tmk_core/protocol/report.c @@ -36,11 +36,24 @@ static int8_t cb_count = 0; * FIXME: Needs doc */ uint8_t has_anykey(report_keyboard_t* keyboard_report) { +#ifdef RECORE //26B, + // Only check if key, no need to get how many keys. + uint8_t *pr = keyboard_report->keys; + uint8_t lpr = sizeof(keyboard_report->nkro.bits); + while (lpr--) { + if (*pr++) return 1; + } + return 0; +#endif uint8_t cnt = 0; uint8_t* p = keyboard_report->keys; uint8_t lp = sizeof(keyboard_report->keys); #ifdef NKRO_ENABLE +#ifndef RECORE if (keyboard_protocol && keymap_config.nkro) { +#else + if (IS_NKRO()) { +#endif p = keyboard_report->nkro.bits; lp = sizeof(keyboard_report->nkro.bits); } @@ -57,7 +70,11 @@ uint8_t has_anykey(report_keyboard_t* keyboard_report) { */ uint8_t get_first_key(report_keyboard_t* keyboard_report) { #ifdef NKRO_ENABLE +#ifndef RECORE if (keyboard_protocol && keymap_config.nkro) { +#else + if (IS_NKRO()) { +#endif uint8_t i = 0; for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++) ; @@ -84,11 +101,21 @@ uint8_t get_first_key(report_keyboard_t* keyboard_report) { * Note: The function doesn't support modifers currently, and it returns false for KC_NO */ bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) { +#ifdef RECORE + //https://github.com/qmk/qmk_firmware/issues/1708 + // disable with return, save 110B(false, and not work) + // save 90B (true). Then always del_key before add + return true; +#endif if (key == KC_NO) { return false; } #ifdef NKRO_ENABLE +#ifndef RECORE if (keyboard_protocol && keymap_config.nkro) { +#else + if (IS_NKRO()) { +#endif if ((key >> 3) < KEYBOARD_REPORT_BITS) { return keyboard_report->nkro.bits[key >> 3] & 1 << (key & 7); } else { @@ -109,6 +136,23 @@ bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) { * FIXME: Needs doc */ void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) { +#ifdef RECORE +#define KEYBOARD_REPORT_6KRO_BUFFER 6 //(KEYBOARD_REPORT_KEYS - 1) + //save 16B + int8_t target_pos = -1; + /* find the code pos, or when finding the first 0, the code is not in report. */ + for (uint8_t i=0; i < KEYBOARD_REPORT_6KRO_BUFFER; i++) { + if (keyboard_report->keys[i] == code || keyboard_report->keys[i] == 0) { + target_pos = i; + break; + } + } + + if (target_pos != -1) { + keyboard_report->keys[target_pos] = code; + } + return; +#endif #ifdef RING_BUFFERED_6KRO_REPORT_ENABLE int8_t i = cb_head; int8_t empty = -1; @@ -242,7 +286,11 @@ void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { */ void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) { #ifdef NKRO_ENABLE +#ifndef RECORE if (keyboard_protocol && keymap_config.nkro) { +#else + if (IS_NKRO()) { +#endif add_key_bit(keyboard_report, key); return; } @@ -256,7 +304,11 @@ void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) { */ void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) { #ifdef NKRO_ENABLE +#ifndef RECORE if (keyboard_protocol && keymap_config.nkro) { +#else + if (IS_NKRO()) { +#endif del_key_bit(keyboard_report, key); return; } @@ -269,9 +321,17 @@ void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) { * FIXME: Needs doc */ void clear_keys_from_report(report_keyboard_t* keyboard_report) { +#ifdef RECORE //always clear all. save 20B + memset(keyboard_report->nkro.bits, 0, KEYBOARD_REPORT_BITS); + return; +#endif // not clear mods #ifdef NKRO_ENABLE +#ifndef RECORE if (keyboard_protocol && keymap_config.nkro) { +#else + if (IS_NKRO()) { +#endif memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits)); return; }