diff options
| -rw-r--r-- | host.c | 47 | ||||
| -rw-r--r-- | host.h | 3 | ||||
| -rw-r--r--[-rwxr-xr-x] | layer.c | 44 |
3 files changed, 75 insertions, 19 deletions
| @@ -35,7 +35,9 @@ report_keyboard_t *keyboard_report_prev = &report1; | |||
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | static inline void add_key_byte(uint8_t code); | 37 | static inline void add_key_byte(uint8_t code); |
| 38 | static inline void del_key_byte(uint8_t code); | ||
| 38 | static inline void add_key_bit(uint8_t code); | 39 | static inline void add_key_bit(uint8_t code); |
| 40 | static inline void del_key_bit(uint8_t code); | ||
| 39 | 41 | ||
| 40 | 42 | ||
| 41 | void host_set_driver(host_driver_t *d) | 43 | void host_set_driver(host_driver_t *d) |
| @@ -66,11 +68,27 @@ void host_add_key(uint8_t key) | |||
| 66 | add_key_byte(key); | 68 | add_key_byte(key); |
| 67 | } | 69 | } |
| 68 | 70 | ||
| 71 | void host_del_key(uint8_t key) | ||
| 72 | { | ||
| 73 | #ifdef NKRO_ENABLE | ||
| 74 | if (keyboard_nkro) { | ||
| 75 | del_key_bit(key); | ||
| 76 | return; | ||
| 77 | } | ||
| 78 | #endif | ||
| 79 | del_key_byte(key); | ||
| 80 | } | ||
| 81 | |||
| 69 | void host_add_mod_bit(uint8_t mod) | 82 | void host_add_mod_bit(uint8_t mod) |
| 70 | { | 83 | { |
| 71 | keyboard_report->mods |= mod; | 84 | keyboard_report->mods |= mod; |
| 72 | } | 85 | } |
| 73 | 86 | ||
| 87 | void host_del_mod_bit(uint8_t mod) | ||
| 88 | { | ||
| 89 | keyboard_report->mods &= ~mod; | ||
| 90 | } | ||
| 91 | |||
| 74 | void host_set_mods(uint8_t mods) | 92 | void host_set_mods(uint8_t mods) |
| 75 | { | 93 | { |
| 76 | keyboard_report->mods = mods; | 94 | keyboard_report->mods = mods; |
| @@ -85,6 +103,15 @@ void host_add_code(uint8_t code) | |||
| 85 | } | 103 | } |
| 86 | } | 104 | } |
| 87 | 105 | ||
| 106 | void host_del_code(uint8_t code) | ||
| 107 | { | ||
| 108 | if (IS_MOD(code)) { | ||
| 109 | host_del_mod_bit(MOD_BIT(code)); | ||
| 110 | } else { | ||
| 111 | host_del_key(code); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 88 | void host_swap_keyboard_report(void) | 115 | void host_swap_keyboard_report(void) |
| 89 | { | 116 | { |
| 90 | uint8_t sreg = SREG; | 117 | uint8_t sreg = SREG; |
| @@ -180,6 +207,17 @@ static inline void add_key_byte(uint8_t code) | |||
| 180 | } | 207 | } |
| 181 | } | 208 | } |
| 182 | 209 | ||
| 210 | static inline void del_key_byte(uint8_t code) | ||
| 211 | { | ||
| 212 | int i = 0; | ||
| 213 | for (; i < REPORT_KEYS; i++) { | ||
| 214 | if (keyboard_report->keys[i] == code) { | ||
| 215 | keyboard_report->keys[i] = 0; | ||
| 216 | break; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 183 | static inline void add_key_bit(uint8_t code) | 221 | static inline void add_key_bit(uint8_t code) |
| 184 | { | 222 | { |
| 185 | if ((code>>3) < REPORT_KEYS) { | 223 | if ((code>>3) < REPORT_KEYS) { |
| @@ -188,3 +226,12 @@ static inline void add_key_bit(uint8_t code) | |||
| 188 | debug("add_key_bit: can't add: "); phex(code); debug("\n"); | 226 | debug("add_key_bit: can't add: "); phex(code); debug("\n"); |
| 189 | } | 227 | } |
| 190 | } | 228 | } |
| 229 | |||
| 230 | static inline void del_key_bit(uint8_t code) | ||
| 231 | { | ||
| 232 | if ((code>>3) < REPORT_KEYS) { | ||
| 233 | keyboard_report->keys[code>>3] &= ~(1<<(code&7)); | ||
| 234 | } else { | ||
| 235 | debug("del_key_bit: can't del: "); phex(code); debug("\n"); | ||
| 236 | } | ||
| 237 | } | ||
| @@ -37,9 +37,12 @@ uint8_t host_keyboard_leds(void); | |||
| 37 | 37 | ||
| 38 | /* keyboard report operations */ | 38 | /* keyboard report operations */ |
| 39 | void host_add_key(uint8_t key); | 39 | void host_add_key(uint8_t key); |
| 40 | void host_del_key(uint8_t key); | ||
| 40 | void host_add_mod_bit(uint8_t mod); | 41 | void host_add_mod_bit(uint8_t mod); |
| 42 | void host_del_mod_bit(uint8_t mod); | ||
| 41 | void host_set_mods(uint8_t mods); | 43 | void host_set_mods(uint8_t mods); |
| 42 | void host_add_code(uint8_t code); | 44 | void host_add_code(uint8_t code); |
| 45 | void host_del_code(uint8_t code); | ||
| 43 | void host_swap_keyboard_report(void); | 46 | void host_swap_keyboard_report(void); |
| 44 | void host_clear_keyboard_report(void); | 47 | void host_clear_keyboard_report(void); |
| 45 | uint8_t host_has_anykey(void); | 48 | uint8_t host_has_anykey(void); |
| @@ -25,7 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 25 | 25 | ||
| 26 | /* | 26 | /* |
| 27 | * Parameters: | 27 | * Parameters: |
| 28 | * ENTER_DELAY |=======| | 28 | * SWITCH_DELAY |=======| |
| 29 | * SEND_FN_TERM |================| | 29 | * SEND_FN_TERM |================| |
| 30 | * | 30 | * |
| 31 | * Fn key processing cases: | 31 | * Fn key processing cases: |
| @@ -49,7 +49,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 49 | * other key press _____________|~~|__________ | 49 | * other key press _____________|~~|__________ |
| 50 | * other key send _____________|~~|__________ | 50 | * other key send _____________|~~|__________ |
| 51 | * | 51 | * |
| 52 | * 4. press other key during ENTER_DELAY. | 52 | * 4. press other key during SWITCH_DELAY. |
| 53 | * Layer sw ___________________________ | 53 | * Layer sw ___________________________ |
| 54 | * Fn key press ___|~~~~~~~~~|_____________ | 54 | * Fn key press ___|~~~~~~~~~|_____________ |
| 55 | * Fn key send ______|~~~~~~|_____________ | 55 | * Fn key send ______|~~~~~~|_____________ |
| @@ -69,11 +69,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 69 | * Fn key send _____|~|__|~~~~~~~~~~~~~~~~ | 69 | * Fn key send _____|~|__|~~~~~~~~~~~~~~~~ |
| 70 | */ | 70 | */ |
| 71 | 71 | ||
| 72 | // LAYER_ENTER_DELAY: prevent from moving new layer | 72 | // LAYER_SWITCH_DELAY: prevent from moving to new layer |
| 73 | #define LAYER_ENTER_DELAY 150 | 73 | #ifndef LAYER_SWITCH_DELAY |
| 74 | # define LAYER_SWITCH_DELAY 150 | ||
| 75 | #endif | ||
| 74 | 76 | ||
| 75 | // LAYER_SEND_FN_TERM: send keycode if release key in this term | 77 | // LAYER_SEND_FN_TERM: send keycode if release key in this term |
| 76 | #define LAYER_SEND_FN_TERM 500 | 78 | #ifndef LAYER_SEND_FN_TERM |
| 79 | # define LAYER_SEND_FN_TERM 500 | ||
| 80 | #endif | ||
| 77 | 81 | ||
| 78 | 82 | ||
| 79 | uint8_t default_layer = 0; | 83 | uint8_t default_layer = 0; |
| @@ -107,10 +111,11 @@ void layer_switching(uint8_t fn_bits) | |||
| 107 | if (fn_bits == 0) { | 111 | if (fn_bits == 0) { |
| 108 | // do nothing | 112 | // do nothing |
| 109 | } else { | 113 | } else { |
| 110 | if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) { | 114 | if (!keymap_fn_keycode(BIT_SUBST(fn_bits, sent_fn)) || |
| 115 | timer_elapsed(last_timer) > LAYER_SWITCH_DELAY) { | ||
| 111 | uint8_t _layer_to_switch = new_layer(BIT_SUBST(fn_bits, sent_fn)); | 116 | uint8_t _layer_to_switch = new_layer(BIT_SUBST(fn_bits, sent_fn)); |
| 112 | if (current_layer != _layer_to_switch) { // not switch layer yet | 117 | if (current_layer != _layer_to_switch) { // not switch layer yet |
| 113 | debug("Fn case: 1,2,3(LAYER_ENTER_DELAY passed)\n"); | 118 | debug("Fn case: 1,2,3(LAYER_SWITCH_DELAY passed)\n"); |
| 114 | debug("Switch Layer: "); debug_hex(current_layer); | 119 | debug("Switch Layer: "); debug_hex(current_layer); |
| 115 | current_layer = _layer_to_switch; | 120 | current_layer = _layer_to_switch; |
| 116 | layer_used = false; | 121 | layer_used = false; |
| @@ -120,14 +125,14 @@ void layer_switching(uint8_t fn_bits) | |||
| 120 | if (host_has_anykey()) { // other keys is pressed | 125 | if (host_has_anykey()) { // other keys is pressed |
| 121 | uint8_t _fn_to_send = BIT_SUBST(fn_bits, sent_fn); | 126 | uint8_t _fn_to_send = BIT_SUBST(fn_bits, sent_fn); |
| 122 | if (_fn_to_send) { | 127 | if (_fn_to_send) { |
| 123 | debug("Fn case: 4(send Fn before other key pressed)\n"); | 128 | debug("Fn case: 4(press other key during SWITCH_DELAY.)\n"); |
| 124 | // send only Fn key first | 129 | // send only Fn key first |
| 125 | host_swap_keyboard_report(); | 130 | uint8_t tmp_mods = keyboard_report->mods; |
| 126 | host_clear_keyboard_report(); | 131 | host_add_code(keymap_fn_keycode(_fn_to_send)); |
| 127 | host_set_mods(last_mods); | 132 | host_set_mods(last_mods); |
| 128 | host_add_code(keymap_fn_keycode(_fn_to_send)); // TODO: do all Fn keys | ||
| 129 | host_send_keyboard_report(); | 133 | host_send_keyboard_report(); |
| 130 | host_swap_keyboard_report(); | 134 | host_set_mods(tmp_mods); |
| 135 | host_del_code(keymap_fn_keycode(_fn_to_send)); | ||
| 131 | sent_fn |= _fn_to_send; | 136 | sent_fn |= _fn_to_send; |
| 132 | } | 137 | } |
| 133 | } | 138 | } |
| @@ -143,15 +148,16 @@ void layer_switching(uint8_t fn_bits) | |||
| 143 | debug("last_fn: "); debug_bin(last_fn); debug("\n"); | 148 | debug("last_fn: "); debug_bin(last_fn); debug("\n"); |
| 144 | debug("last_mods: "); debug_hex(last_mods); debug("\n"); | 149 | debug("last_mods: "); debug_hex(last_mods); debug("\n"); |
| 145 | debug("last_timer: "); debug_hex16(last_timer); debug("\n"); | 150 | debug("last_timer: "); debug_hex16(last_timer); debug("\n"); |
| 151 | debug("timer_count: "); debug_hex16(timer_count); debug("\n"); | ||
| 146 | 152 | ||
| 147 | // pressed Fn | 153 | // pressed Fn |
| 148 | if ((fn_changed = BIT_SUBST(fn_bits, last_fn))) { | 154 | if ((fn_changed = BIT_SUBST(fn_bits, last_fn))) { |
| 149 | debug("fn_changed: "); debug_bin(fn_changed); debug("\n"); | 155 | debug("fn_changed: "); debug_bin(fn_changed); debug("\n"); |
| 150 | if (host_has_anykey()) { | 156 | if (host_has_anykey()) { |
| 151 | debug("Fn case: 5(pressed Fn with other key)\n"); | 157 | debug("Fn case: 5(pressed Fn with other key)\n"); |
| 152 | sent_fn |= fn_changed; | 158 | sent_fn |= fn_changed; |
| 153 | } else if (fn_changed & sent_fn) { // pressed same Fn in a row | 159 | } else if (fn_changed & sent_fn) { // pressed same Fn in a row |
| 154 | if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) { | 160 | if (timer_elapsed(last_timer) > LAYER_SEND_FN_TERM) { |
| 155 | debug("Fn case: 6(not repeat)\n"); | 161 | debug("Fn case: 6(not repeat)\n"); |
| 156 | // time passed: not repeate | 162 | // time passed: not repeate |
| 157 | sent_fn &= ~fn_changed; | 163 | sent_fn &= ~fn_changed; |
| @@ -162,17 +168,17 @@ void layer_switching(uint8_t fn_bits) | |||
| 162 | } | 168 | } |
| 163 | // released Fn | 169 | // released Fn |
| 164 | if ((fn_changed = BIT_SUBST(last_fn, fn_bits))) { | 170 | if ((fn_changed = BIT_SUBST(last_fn, fn_bits))) { |
| 165 | debug("fn_changed: "); debug_bin(fn_changed); debug("\n"); | 171 | debug("fn_changed: "); debug_bin(fn_changed); debug("\n"); |
| 166 | if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) { | 172 | if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) { |
| 167 | if (!layer_used && BIT_SUBST(fn_changed, sent_fn)) { | 173 | if (!layer_used && BIT_SUBST(fn_changed, sent_fn)) { |
| 168 | debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n"); | 174 | debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n"); |
| 169 | // send only Fn key first | 175 | // send only Fn key first |
| 170 | host_swap_keyboard_report(); | 176 | uint8_t tmp_mods = keyboard_report->mods; |
| 171 | host_clear_keyboard_report(); | 177 | host_add_code(keymap_fn_keycode(fn_changed)); |
| 172 | host_set_mods(last_mods); | 178 | host_set_mods(last_mods); |
| 173 | host_add_code(keymap_fn_keycode(fn_changed)); // TODO: do all Fn keys | ||
| 174 | host_send_keyboard_report(); | 179 | host_send_keyboard_report(); |
| 175 | host_swap_keyboard_report(); | 180 | host_set_mods(tmp_mods); |
| 181 | host_del_code(keymap_fn_keycode(fn_changed)); | ||
| 176 | sent_fn |= fn_changed; | 182 | sent_fn |= fn_changed; |
| 177 | } | 183 | } |
| 178 | } | 184 | } |
