Project

General

Profile

Statistics
| Revision:

root / lab4 / mouse.c @ 66

History | View | Annotate | Download (1.85 KB)

1 59 up20180642
#include <lcom/lcf.h>
2
3
#include "mouse.h"
4
5
#include "errors.h"
6
#include "kbc_macros.h"
7
#include "mouse_macros.h"
8
#include "kbc.h"
9
10
int (subscribe_mouse_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(MOUSE_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
14
    return SUCCESS;
15
}
16
17
int got_error_mouse_ih = 0;
18
uint8_t packet[3];
19
int counter = 0;
20
21
void (mouse_ih)(void) {
22
    uint8_t status = 0;
23
    got_error_mouse_ih = 0;
24
    if(counter >= 3) counter = 0;
25
26
    if (util_sys_inb(STATUS_REG, &status)) {
27
        got_error_mouse_ih = 1;
28
        return;
29
    }
30
31
    if (status & (TIME_OUT_REC | PARITY_ERROR)) {
32
        got_error_mouse_ih = 1;
33
        return;
34
    }
35
36
    uint8_t byte = 0;
37
38
    if (util_sys_inb(OUTPUT_BUF, &byte)) {
39
        got_error_mouse_ih = 1;
40
        return;
41
    }
42
43
    /// This does not run if: I was expecting the first one but what I get is definitely not the first byte
44
    if((byte & FIRST_BYTE_ID)  || counter){
45
        packet[counter] = byte;
46
        counter++;
47
    }
48
}
49
50
struct packet (mouse_parse_packet)(const uint8_t *packet_bytes){
51
    struct packet pp;
52
    pp.bytes[0] = packet_bytes[0];
53
    pp.bytes[1] = packet_bytes[1];
54
    pp.bytes[2] = packet_bytes[2];
55
    pp.rb       = pp.bytes[0] & RIGHT_BUTTON;
56
    pp.mb       = pp.bytes[0] & MIDDLE_BUTTON;
57
    pp.lb       = pp.bytes[0] & LEFT_BUTTON;
58
    pp.delta_x  = pp.bytes[1];
59
    pp.delta_y  = pp.bytes[2];
60
    pp.x_ov     = pp.bytes[0] & X_OVERFLOW;
61
    pp.y_ov     = pp.bytes[0] & Y_OVERFLOW;
62
    return pp;
63
}
64
65
int (mouse_set_data_report)(int on){
66
    int ret = 0;
67
    if((ret = kbc_issue_cmd(MOUSE_WRITE_B))) return ret;
68
    if(sys_outb(KBC_CMD_ARG, DIS_DATA_REP)) return WRITE_ERROR;
69
    return SUCCESS;
70
}