]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard.c
6bf2b3a9ed867263ed471adb2e453f333eaabc48
[max/tmk_keyboard.git] / keyboard.c
1 #include "usb_keycodes.h"
2 #include "host.h"
3 #include "led.h"
4 #include "keyboard.h"
5 #include "print.h"
6
7 static report_keyboard_t report0;
8 static report_keyboard_t report1;
9 report_keyboard_t *keyboard_report = &report0;
10 report_keyboard_t *keyboard_report_prev = &report1;
11
12
13 void keyboard_set_led(uint8_t usb_led)
14 {
15     led_set(usb_led);
16 }
17
18 void keyboard_send(void)
19 {
20     host_keyboard_send(keyboard_report);
21 }
22
23 void keyboard_add_key(uint8_t code)
24 {
25     int8_t i = 0;
26     int8_t empty = -1;
27     for (; i < REPORT_KEYS; i++) {
28         if (keyboard_report_prev->keys[i] == code) {
29             keyboard_report->keys[i] = code;
30             break;
31         }
32         if (empty == -1 && keyboard_report_prev->keys[i] == KB_NO && keyboard_report->keys[i] == KB_NO) {
33             empty = i;
34         }
35     }
36     if (i == REPORT_KEYS && empty != -1) {
37         keyboard_report->keys[empty] = code;
38     }
39 }
40
41 void keyboard_add_mod_bit(uint8_t mod)
42 {
43     keyboard_report->mods |= mod;
44 }
45
46 void keyboard_set_mods(uint8_t mods)
47 {
48     keyboard_report->mods = mods;
49 }
50
51 void keyboard_add_code(uint8_t code)
52 {
53     if (IS_MOD(code)) {
54         keyboard_add_mod_bit(MOD_BIT(code));
55     } else {
56         keyboard_add_key(code);
57     }
58 }
59
60 void keyboard_swap_report(void)
61 {
62     report_keyboard_t *tmp = keyboard_report_prev;
63     keyboard_report_prev = keyboard_report;
64     keyboard_report = tmp;
65 }
66
67 void keyboard_clear_report(void)
68 {
69     keyboard_report->mods = 0;
70     for (int8_t i = 0; i < REPORT_KEYS; i++) {
71         keyboard_report->keys[i] = 0;
72     }
73 }
74
75 uint8_t keyboard_has_anykey(void)
76 {
77     uint8_t cnt = 0;
78     for (int i = 0; i < REPORT_KEYS; i++) {
79         if (keyboard_report->keys[i])
80             cnt++;
81     }
82     return cnt;
83 }
84
85 uint8_t *keyboard_get_keys(void)
86 {
87     return keyboard_report->keys;
88 }
89
90 uint8_t keyboard_get_mods(void)
91 {
92     return keyboard_report->mods;
93 }