1 /* Copyright 2017 Fred Sundvik
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "keycode_config.h"
23 uint8_t has_anykey(report_keyboard_t* keyboard_report)
26 for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
27 if (keyboard_report->raw[i])
33 uint8_t get_first_key(report_keyboard_t* keyboard_report)
36 if (keyboard_protocol && keymap_config.nkro) {
38 for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
40 return i<<3 | biton(keyboard_report->nkro.bits[i]);
43 #ifdef USB_6KRO_ENABLE
46 if (keyboard_report->keys[i] != 0) {
50 } while (i != cb_tail);
51 return keyboard_report->keys[i];
53 return keyboard_report->keys[0];
57 void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code)
59 #ifdef USB_6KRO_ENABLE
64 if (keyboard_report->keys[i] == code) {
67 if (empty == -1 && keyboard_report->keys[i] == 0) {
71 } while (i != cb_tail);
73 if (cb_tail == cb_head) {
76 // pop head when has no empty space
77 cb_head = RO_INC(cb_head);
81 // left shift when has empty space
85 if (keyboard_report->keys[i] != 0) {
86 keyboard_report->keys[empty] = keyboard_report->keys[i];
87 keyboard_report->keys[i] = 0;
88 empty = RO_INC(empty);
94 } while (i != cb_tail);
95 cb_tail = RO_SUB(cb_tail, offset);
101 keyboard_report->keys[cb_tail] = code;
102 cb_tail = RO_INC(cb_tail);
107 for (; i < KEYBOARD_REPORT_KEYS; i++) {
108 if (keyboard_report->keys[i] == code) {
111 if (empty == -1 && keyboard_report->keys[i] == 0) {
115 if (i == KEYBOARD_REPORT_KEYS) {
117 keyboard_report->keys[empty] = code;
123 void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code)
125 #ifdef USB_6KRO_ENABLE
129 if (keyboard_report->keys[i] == code) {
130 keyboard_report->keys[i] = 0;
133 // reset head and tail
134 cb_tail = cb_head = 0;
136 if (i == RO_DEC(cb_tail)) {
137 // left shift when next to tail
139 cb_tail = RO_DEC(cb_tail);
140 if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
143 } while (cb_tail != cb_head);
148 } while (i != cb_tail);
151 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
152 if (keyboard_report->keys[i] == code) {
153 keyboard_report->keys[i] = 0;
160 void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code)
162 if ((code>>3) < KEYBOARD_REPORT_BITS) {
163 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
165 dprintf("add_key_bit: can't add: %02X\n", code);
169 void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code)
171 if ((code>>3) < KEYBOARD_REPORT_BITS) {
172 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
174 dprintf("del_key_bit: can't del: %02X\n", code);
179 void add_key_to_report(report_keyboard_t* keyboard_report, int8_t key)
182 if (keyboard_protocol && keymap_config.nkro) {
183 add_key_bit(keyboard_report, key);
187 add_key_byte(keyboard_report, key);
190 void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key)
193 if (keyboard_protocol && keymap_config.nkro) {
194 del_key_bit(keyboard_report, key);
198 del_key_byte(keyboard_report, key);
201 void clear_keys_from_report(report_keyboard_t* keyboard_report)
204 for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
205 keyboard_report->raw[i] = 0;