Project

General

Profile

Statistics
| Revision:

root / proj / libs / kbc / src / keyboard.c @ 328

History | View | Annotate | Download (1.73 KB)

1 145 up20180655
#include <lcom/lcf.h>
2
3
#include "keyboard.h"
4
5
#include "kbc.h"
6
#include "utils.h"
7
#include "errors.h"
8 167 up20180655
#include "proj_func.h"
9 145 up20180655
10
int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
11
    if (interrupt_id == NULL) return NULL_PTR;
12
    *interrupt_id = interrupt_bit;
13
    if(sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
14
    return SUCCESS;
15
}
16
17 321 up20180642
static uint8_t scancode[2];
18
static int done = 1;
19
static int sz = 1;
20
static int got_error_keyboard = SUCCESS;
21
const uint8_t* keyboard_get_scancode(void){ return scancode; }
22
int keyboard_get_done(void){ return done; }
23
int keyboard_get_size(void){ return sz; }
24
int keyboard_get_error(void){return got_error_keyboard; }
25 145 up20180655
26 321 up20180642
27 145 up20180655
void (kbc_ih)(void) {
28 171 up20180655
    if(done) { sz = 1; }
29 145 up20180655
    else     sz++;
30
    uint8_t status = 0;
31
    got_error_keyboard = SUCCESS;
32
    if ((got_error_keyboard = util_sys_inb(STATUS_REG, &status))) return;
33
    if (status & (TIME_OUT_REC | PARITY_ERROR)) {
34
        got_error_keyboard = 1;
35
        return;
36
    }
37
    if ((status & OUT_BUF_FUL) == 0 || (status & AUX_MOUSE) != 0) {
38
        got_error_keyboard = READ_ERROR;
39
        return;
40
    }
41
    uint8_t byte = 0;
42
    if ((got_error_keyboard = util_sys_inb(OUTPUT_BUF, &byte))) return;
43
44
    scancode[sz-1] = byte;
45
    done = !(TWO_BYTE_CODE == byte);
46 167 up20180655
47 184 up20180655
    if (done) update_key_presses();
48 167 up20180655
49 145 up20180655
}
50
51
int (keyboard_poll)(uint8_t bytes[], uint8_t *size){
52
    int ret = 0;
53
    if(bytes == NULL || size == NULL) return NULL_PTR;
54
    uint8_t c;
55
    if((ret = kbc_read_byte(&c))) return ret;
56
    if(c == TWO_BYTE_CODE){
57
        if((ret = kbc_read_byte(&bytes[1]))) return ret;
58
        bytes[0] = c;
59
        *size = 2;
60
    }else{
61
        bytes[1] = 0;
62
        bytes[0] = c;
63
        *size = 1;
64
    }
65
    return SUCCESS;
66
}