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"
27 uint8_t has_anykey(report_keyboard_t* keyboard_report)
30 for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
31 if (keyboard_report->raw[i])
37 /** \brief get_first_key
41 uint8_t get_first_key(report_keyboard_t* keyboard_report)
44 if (keyboard_protocol && keymap_config.nkro) {
46 for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
48 return i<<3 | biton(keyboard_report->nkro.bits[i]);
51 #ifdef USB_6KRO_ENABLE
54 if (keyboard_report->keys[i] != 0) {
58 } while (i != cb_tail);
59 return keyboard_report->keys[i];
61 return keyboard_report->keys[0];
65 /** \brief add key byte
69 void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code)
71 #ifdef USB_6KRO_ENABLE
76 if (keyboard_report->keys[i] == code) {
79 if (empty == -1 && keyboard_report->keys[i] == 0) {
83 } while (i != cb_tail);
85 if (cb_tail == cb_head) {
88 // pop head when has no empty space
89 cb_head = RO_INC(cb_head);
93 // left shift when has empty space
97 if (keyboard_report->keys[i] != 0) {
98 keyboard_report->keys[empty] = keyboard_report->keys[i];
99 keyboard_report->keys[i] = 0;
100 empty = RO_INC(empty);
106 } while (i != cb_tail);
107 cb_tail = RO_SUB(cb_tail, offset);
113 keyboard_report->keys[cb_tail] = code;
114 cb_tail = RO_INC(cb_tail);
119 for (; i < KEYBOARD_REPORT_KEYS; i++) {
120 if (keyboard_report->keys[i] == code) {
123 if (empty == -1 && keyboard_report->keys[i] == 0) {
127 if (i == KEYBOARD_REPORT_KEYS) {
129 keyboard_report->keys[empty] = code;
135 /** \brief del key byte
139 void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code)
141 #ifdef USB_6KRO_ENABLE
145 if (keyboard_report->keys[i] == code) {
146 keyboard_report->keys[i] = 0;
149 // reset head and tail
150 cb_tail = cb_head = 0;
152 if (i == RO_DEC(cb_tail)) {
153 // left shift when next to tail
155 cb_tail = RO_DEC(cb_tail);
156 if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
159 } while (cb_tail != cb_head);
164 } while (i != cb_tail);
167 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
168 if (keyboard_report->keys[i] == code) {
169 keyboard_report->keys[i] = 0;
176 /** \brief add key bit
180 void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code)
182 if ((code>>3) < KEYBOARD_REPORT_BITS) {
183 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
185 dprintf("add_key_bit: can't add: %02X\n", code);
189 /** \brief del key bit
193 void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code)
195 if ((code>>3) < KEYBOARD_REPORT_BITS) {
196 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
198 dprintf("del_key_bit: can't del: %02X\n", code);
203 /** \brief add key to report
207 void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key)
210 if (keyboard_protocol && keymap_config.nkro) {
211 add_key_bit(keyboard_report, key);
215 add_key_byte(keyboard_report, key);
218 /** \brief del key from report
222 void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key)
225 if (keyboard_protocol && keymap_config.nkro) {
226 del_key_bit(keyboard_report, key);
230 del_key_byte(keyboard_report, key);
233 /** \brief clear key from report
237 void clear_keys_from_report(report_keyboard_t* keyboard_report)
240 for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
241 keyboard_report->raw[i] = 0;