summaryrefslogtreecommitdiff
path: root/ps2_vusb/keyboard.c
blob: c480908f847cb1c33e0c754553b96377ed862b35 (plain)
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
#include "usb_keycodes.h"
#include "host.h"
#include "ps2.h"
#include "usb.h"
#include "keyboard.h"
#include "print.h"

static report_keyboard_t report0;
static report_keyboard_t report1;
static report_keyboard_t *report = &report0;
static report_keyboard_t *report_prev = &report1;


void keyboard_set_led(uint8_t usb_led)
{
    uint8_t ps2_led = 0;
    if (usb_led & (1<<USB_LED_SCROLL_LOCK))
        ps2_led |= (1<<PS2_LED_SCROLL_LOCK);
    if (usb_led & (1<<USB_LED_NUM_LOCK))
        ps2_led |= (1<<PS2_LED_NUM_LOCK);
    if (usb_led & (1<<USB_LED_CAPS_LOCK))
        ps2_led |= (1<<PS2_LED_CAPS_LOCK);
    print("ps2_led: "); phex(ps2_led); print("\n");

    ps2_host_set_led(ps2_led);
}

void keyboard_send(void)
{
    host_keyboard_send(report);
}

bool keyboard_has_key(void)
{
    for (int i = 0; i < REPORT_KEYS; i++) {
        if (report->keys[i])
            return true;
    }
    return false;
}

void keyboard_add_mod(uint8_t mod)
{
    report->mods |= mod;
}

void keyboard_add_key(uint8_t code)
{
    int8_t i = 0;
    int8_t empty = -1;
    for (; i < REPORT_KEYS; i++) {
        if (report_prev->keys[i] == code) {
            report->keys[i] = code;
            break;
        }
        if (empty == -1 && report_prev->keys[i] == KB_NO && report->keys[i] == KB_NO) {
            empty = i;
        }
    }
    if (i == REPORT_KEYS && empty != -1) {
        report->keys[empty] = code;
    }
}

void keyboard_add_code(uint8_t code)
{
    if (IS_MOD(code)) {
        keyboard_add_mod(code);
    } else {
        keyboard_add_key(code);
    }
}

void keyboard_swap_report(void)
{
    report_keyboard_t *tmp = report_prev;
    report_prev = report;
    report = tmp;
}

void keyboard_clear_report(void)
{
    report->mods = 0;
    for (int8_t i = 0; i < REPORT_KEYS; i++) {
        report->keys[i] = 0;
    }
}

report_keyboard_t *keyboard_report(void)
{
    return report;
}

report_keyboard_t *keyboard_report_prev(void)
{
    return report_prev;
}