aboutsummaryrefslogtreecommitdiffstats
path: root/keyboard.c
blob: af170673500400975d013e8196fe014d053286c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "keyboard.h"
#include "host.h"
#include "layer.h"
#include "matrix.h"
#include "led.h"
#include "usb_keycodes.h"
#include "timer.h"
#include "print.h"
#include "debug.h"
#include "command.h"
#ifdef MOUSEKEY_ENABLE
#include "mousekey.h"
#endif
#ifdef USB_EXTRA_ENABLE
#include <util/delay.h>
#endif


static uint8_t last_leds = 0;


void keyboard_init(void)
{
    timer_init();
    matrix_init();
#ifdef PS2_MOUSE_ENABLE
    ps2_mouse_init();
#endif
}

void keyboard_proc(void)
{
    uint8_t fn_bits = 0;
#ifdef USB_EXTRA_ENABLE
    uint16_t consumer_code = 0;
#endif

    matrix_scan();

    if (matrix_is_modified()) {
        if (debug_matrix) matrix_print();
#ifdef DEBUG_LED
        // LED flash for debug
        DEBUG_LED_CONFIG;
        DEBUG_LED_ON;
#endif
    }

    if (matrix_has_ghost()) {
        // should send error?
        debug("matrix has ghost!!\n");
        return;
    }

    host_swap_keyboard_report();
    host_clear_keyboard_report();
    for (int row = 0; row < matrix_rows(); row++) {
        for (int col = 0; col < matrix_cols(); col++) {
            if (!matrix_is_on(row, col)) continue;

            uint8_t code = layer_get_keycode(row, col);
            if (code == KB_NO) {
                // do nothing
            } else if (IS_MOD(code)) {
                host_add_mod_bit(MOD_BIT(code));
            } else if (IS_FN(code)) {
                fn_bits |= FN_BIT(code);
            }
#ifdef USB_EXTRA_ENABLE
            // System Control
            else if (code == KB_SYSTEM_POWER) {
#ifdef HOST_PJRC
                if (suspend && remote_wakeup) {
                    usb_remote_wakeup();
                } else {
                    host_system_send(SYSTEM_POWER_DOWN);
                }
#else
                host_system_send(SYSTEM_POWER_DOWN);
#endif
                host_system_send(0);
                _delay_ms(500);
            } else if (code == KB_SYSTEM_SLEEP) {
                host_system_send(SYSTEM_SLEEP);
                host_system_send(0);
                _delay_ms(500);
            } else if (code == KB_SYSTEM_WAKE) {
                host_system_send(SYSTEM_WAKE_UP);
                host_system_send(0);
                _delay_ms(500);
            }
            // Consumer Page
            else if (code == KB_AUDIO_MUTE) {
                consumer_code = AUDIO_MUTE;
            } else if (code == KB_AUDIO_VOL_UP) {
                consumer_code = AUDIO_VOL_UP;
            } else if (code == KB_AUDIO_VOL_DOWN) {
                consumer_code = AUDIO_VOL_DOWN;
            }
            else if (code == KB_MEDIA_NEXT_TRACK) {
                consumer_code = TRANSPORT_NEXT_TRACK;
            } else if (code == KB_MEDIA_PREV_TRACK) {
                consumer_code = TRANSPORT_PREV_TRACK;
            } else if (code == KB_MEDIA_STOP) {
                consumer_code = TRANSPORT_STOP;
            } else if (code == KB_MEDIA_PLAY_PAUSE) {
                consumer_code = TRANSPORT_PLAY_PAUSE;
            } else if (code == KB_MEDIA_SELECT) {
                consumer_code = AL_CC_CONFIG;
            }
            else if (code == KB_MAIL) {
                consumer_code = AL_EMAIL;
            } else if (code == KB_CALCULATOR) {
                consumer_code = AL_CALCULATOR;
            } else if (code == KB_MY_COMPUTER) {
                consumer_code = AL_LOCAL_BROWSER;
            }
            else if (code == KB_WWW_SEARCH) {
                consumer_code = AC_SEARCH;
            } else if (code == KB_WWW_HOME) {
                consumer_code = AC_HOME;
            } else if (code == KB_WWW_BACK) {
                consumer_code = AC_BACK;
            } else if (code == KB_WWW_FORWARD) {
                consumer_code = AC_FORWARD;
            } else if (code == KB_WWW_STOP) {
                consumer_code = AC_STOP;
            } else if (code == KB_WWW_REFRESH) {
                consumer_code = AC_REFRESH;
            } else if (code == KB_WWW_FAVORITES) {
                consumer_code = AC_BOOKMARKS;
            }
#endif
            else if (IS_KEY(code)) {
                host_add_key(code);
            }
#ifdef MOUSEKEY_ENABLE
            else if (IS_MOUSEKEY(code)) {
                mousekey_decode(code);
            }
#endif
            else {
                debug("ignore keycode: "); debug_hex(code); debug("\n");
            }
        }
    }

    layer_switching(fn_bits);

    if (command_proc()) {
        return;
    }

    // TODO: should send only when changed from last report
    if (matrix_is_modified()) {
        host_send_keyboard_report();
#ifdef USB_EXTRA_ENABLE
        host_consumer_send(consumer_code);
#endif
#ifdef DEBUG_LED
        // LED flash for debug
        DEBUG_LED_CONFIG;
        DEBUG_LED_OFF;
#endif
    }

#ifdef MOUSEKEY_ENABLE
    mousekey_send();
#endif

#ifdef PS2_MOUSE_ENABLE
    // TODO: should comform new API
    if (ps2_mouse_read() == 0)
        ps2_mouse_usb_send();
#endif

    if (last_leds != host_keyboard_leds()) {
        keyboard_set_leds(host_keyboard_leds());
        last_leds = host_keyboard_leds();
    }
}

void keyboard_set_leds(uint8_t leds)
{
    led_set(leds);
}