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"
28 uint8_t has_anykey(report_keyboard_t
* keyboard_report
)
31 uint8_t *p
= keyboard_report
->keys
;
32 uint8_t lp
= sizeof(keyboard_report
->keys
);
34 if (keyboard_protocol
&& keymap_config
.nkro
) {
35 p
= keyboard_report
->nkro
.bits
;
36 lp
= sizeof(keyboard_report
->nkro
.bits
);
46 /** \brief get_first_key
50 uint8_t get_first_key(report_keyboard_t
* keyboard_report
)
53 if (keyboard_protocol
&& keymap_config
.nkro
) {
55 for (; i
< KEYBOARD_REPORT_BITS
&& !keyboard_report
->nkro
.bits
[i
]; i
++)
57 return i
<<3 | biton(keyboard_report
->nkro
.bits
[i
]);
60 #ifdef USB_6KRO_ENABLE
63 if (keyboard_report
->keys
[i
] != 0) {
67 } while (i
!= cb_tail
);
68 return keyboard_report
->keys
[i
];
70 return keyboard_report
->keys
[0];
74 /** \brief add key byte
78 void add_key_byte(report_keyboard_t
* keyboard_report
, uint8_t code
)
80 #ifdef USB_6KRO_ENABLE
85 if (keyboard_report
->keys
[i
] == code
) {
88 if (empty
== -1 && keyboard_report
->keys
[i
] == 0) {
92 } while (i
!= cb_tail
);
94 if (cb_tail
== cb_head
) {
97 // pop head when has no empty space
98 cb_head
= RO_INC(cb_head
);
102 // left shift when has empty space
106 if (keyboard_report
->keys
[i
] != 0) {
107 keyboard_report
->keys
[empty
] = keyboard_report
->keys
[i
];
108 keyboard_report
->keys
[i
] = 0;
109 empty
= RO_INC(empty
);
115 } while (i
!= cb_tail
);
116 cb_tail
= RO_SUB(cb_tail
, offset
);
122 keyboard_report
->keys
[cb_tail
] = code
;
123 cb_tail
= RO_INC(cb_tail
);
128 for (; i
< KEYBOARD_REPORT_KEYS
; i
++) {
129 if (keyboard_report
->keys
[i
] == code
) {
132 if (empty
== -1 && keyboard_report
->keys
[i
] == 0) {
136 if (i
== KEYBOARD_REPORT_KEYS
) {
138 keyboard_report
->keys
[empty
] = code
;
144 /** \brief del key byte
148 void del_key_byte(report_keyboard_t
* keyboard_report
, uint8_t code
)
150 #ifdef USB_6KRO_ENABLE
154 if (keyboard_report
->keys
[i
] == code
) {
155 keyboard_report
->keys
[i
] = 0;
158 // reset head and tail
159 cb_tail
= cb_head
= 0;
161 if (i
== RO_DEC(cb_tail
)) {
162 // left shift when next to tail
164 cb_tail
= RO_DEC(cb_tail
);
165 if (keyboard_report
->keys
[RO_DEC(cb_tail
)] != 0) {
168 } while (cb_tail
!= cb_head
);
173 } while (i
!= cb_tail
);
176 for (uint8_t i
= 0; i
< KEYBOARD_REPORT_KEYS
; i
++) {
177 if (keyboard_report
->keys
[i
] == code
) {
178 keyboard_report
->keys
[i
] = 0;
185 /** \brief add key bit
189 void add_key_bit(report_keyboard_t
* keyboard_report
, uint8_t code
)
191 if ((code
>>3) < KEYBOARD_REPORT_BITS
) {
192 keyboard_report
->nkro
.bits
[code
>>3] |= 1<<(code
&7);
194 dprintf("add_key_bit: can't add: %02X\n", code
);
198 /** \brief del key bit
202 void del_key_bit(report_keyboard_t
* keyboard_report
, uint8_t code
)
204 if ((code
>>3) < KEYBOARD_REPORT_BITS
) {
205 keyboard_report
->nkro
.bits
[code
>>3] &= ~(1<<(code
&7));
207 dprintf("del_key_bit: can't del: %02X\n", code
);
212 /** \brief add key to report
216 void add_key_to_report(report_keyboard_t
* keyboard_report
, uint8_t key
)
219 if (keyboard_protocol
&& keymap_config
.nkro
) {
220 add_key_bit(keyboard_report
, key
);
224 add_key_byte(keyboard_report
, key
);
227 /** \brief del key from report
231 void del_key_from_report(report_keyboard_t
* keyboard_report
, uint8_t key
)
234 if (keyboard_protocol
&& keymap_config
.nkro
) {
235 del_key_bit(keyboard_report
, key
);
239 del_key_byte(keyboard_report
, key
);
242 /** \brief clear key from report
246 void clear_keys_from_report(report_keyboard_t
* keyboard_report
)
250 if (keyboard_protocol
&& keymap_config
.nkro
) {
251 memset(keyboard_report
->nkro
.bits
, 0, sizeof(keyboard_report
->nkro
.bits
));
255 memset(keyboard_report
->keys
, 0, sizeof(keyboard_report
->keys
));