Project

General

Profile

Statistics
| Revision:

root / lab4 / lab4.c @ 75

History | View | Annotate | Download (6.05 KB)

1 53 up20180655
#include <lcom/lcf.h>
2
3
#include <stdint.h>
4
#include <stdio.h>
5
6 58 up20180642
#include "mouse.h"
7
#include "kbc.h"
8 70 up20180655
#include "errors.h"
9
#include "mouse_macros.h"
10 74 up20180642
#include "timer.h"
11 53 up20180655
12
int main(int argc, char *argv[]) {
13
  // sets the language of LCF messages (can be either EN-US or PT-PT)
14
  lcf_set_language("EN-US");
15
16
  // enables to log function invocations that are being "wrapped" by LCF
17
  // [comment this out if you don't want/need/ it]
18
  lcf_trace_calls("/home/lcom/labs/lab4/trace.txt");
19
20
  // enables to save the output of printf function calls on a file
21
  // [comment this out if you don't want/need it]
22
  lcf_log_output("/home/lcom/labs/lab4/output.txt");
23
24
  // handles control over to LCF
25
  // [LCF handles command line arguments and invokes the right function]
26
  if (lcf_start(argc, argv))
27
    return 1;
28
29
  // LCF clean up tasks
30
  // [must be the last statement before return]
31
  lcf_cleanup();
32
33
  return 0;
34
}
35
36 56 up20180642
extern uint8_t packet[3];
37
extern int counter;
38 53 up20180655
39
int (mouse_test_packet)(uint32_t cnt) {
40 55 up20180642
    /// loop stuff
41
    int ipc_status, r;
42
    message msg;
43 74 up20180642
    /// Mouse interrupt handling
44 55 up20180642
    uint8_t mouse_irq_bit = 12;
45
    int mouse_id = 0;
46
    int mouse_irq = BIT(mouse_irq_bit);
47
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
48 73 up20180642
    if (mouse_set_data_report(true)) return 1;
49 55 up20180642
    /// cycle
50
    int good = 1;
51 56 up20180642
    uint32_t cnt_now = 0;
52 55 up20180642
    while (good) {
53
        /* Get a request message. */
54
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
55
            printf("driver_receive failed with %d", r);
56
            continue;
57
        }
58
        if (is_ipc_notify(ipc_status)) { /* received notification */
59
            switch (_ENDPOINT_P(msg.m_source)) {
60
                case HARDWARE: /* hardware interrupt notification */
61
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
62
                        mouse_ih();
63 56 up20180642
                        if(counter >= 3){
64
                            struct packet pp = mouse_parse_packet(packet);
65
                            mouse_print_packet(&pp);
66
                            cnt_now++;
67
                            if(cnt == cnt_now) good = 0;
68
                        }
69 55 up20180642
                    }
70
                    break;
71
                default:
72
                    break; /* no other notifications expected: do nothing */
73
            }
74
        } else { /* received standart message, not a notification */
75
            /* no standart message expected: do nothing */
76
        }
77
    }
78
79
    if (unsubscribe_interrupt(&mouse_id)) return 1;
80 73 up20180642
    if (mouse_set_data_report(false)) return 1;
81 72 up20180642
82 55 up20180642
    return 0;
83 53 up20180655
}
84
85
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
86 70 up20180655
87
    // Mouse packets data
88
    uint8_t packet[3];
89
    int sz = 0;
90
    uint8_t data = 0;
91
    // Cycle
92
    int packetCounter = 0;
93
    int good = 1;
94
    // return value
95
    int ret;
96
97
    while (good) {
98
99
        if ((ret = mouse_read_data(&data))) return ret;
100
101
        if ((data & FIRST_BYTE_ID) || sz) {
102
            packet[sz] = data;
103
            sz++;
104
        }
105
106
        if (sz == 3) {
107
            struct packet pp = mouse_parse_packet(packet);
108
            mouse_print_packet(&pp);
109
            packetCounter++;
110
            sz = 0;
111
            if (packetCounter == cnt) good = 0;
112
        }
113
114
        tickdelay(micros_to_ticks(period*1e3));
115
    }
116
117
    // Set Stream mode
118
    if ((ret = mouse_issue_cmd(SET_STREAM_MD))) return ret;
119
    // Disable data reporting
120 73 up20180642
    if ((ret = mouse_set_data_report(false))) return ret;
121 70 up20180655
122
    uint8_t cmd_byte = minix_get_dflt_kbc_cmd_byte();
123
    if ((ret = kbc_change_cmd(cmd_byte))) return ret;
124
125
    return SUCCESS;
126 53 up20180655
}
127
128
int (mouse_test_async)(uint8_t idle_time) {
129 74 up20180642
    /// loop stuff
130
    int ipc_status, r;
131
    message msg;
132
    /// Timer interrupt handling
133
    const uint32_t frequency = sys_hz(); // Frequency asummed at 60Hz
134
    uint8_t timer_irq_bit = 0;
135
    int timer_id = 0;
136
    int timer_irq = BIT(timer_irq_bit);
137
    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) return 1;
138
139
    no_interrupts = 0;
140
    int time = 0;
141
    /// Mouse interrupt handling
142
    uint8_t mouse_irq_bit = 12;
143
    int mouse_id = 0;
144
    int mouse_irq = BIT(mouse_irq_bit);
145
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
146
    if (mouse_set_data_report(true)) return 1;
147
    /// cycle
148
    int good = 1;
149
    while (good) {
150
        /* Get a request message. */
151
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
152
            printf("driver_receive failed with %d", r);
153
            continue;
154
        }
155
        if (is_ipc_notify(ipc_status)) { /* received notification */
156
            switch (_ENDPOINT_P(msg.m_source)) {
157
                case HARDWARE: /* hardware interrupt notification */
158
                    if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */
159
                        timer_int_handler();
160
                        if (no_interrupts%frequency == 0) time++;
161
                        if(time >= idle_time) good = 0;
162
                    }
163
                    if (msg.m_notify.interrupts & mouse_irq) { /// subscribed interrupt
164
                        mouse_ih();
165
                        if(counter >= 3){
166
                            struct packet pp = mouse_parse_packet(packet);
167
                            mouse_print_packet(&pp);
168
                            time = 0;
169
                            no_interrupts = 0;
170
                        }
171
                    }
172
                    break;
173
                default:
174
                    break; /* no other notifications expected: do nothing */
175
            }
176
        } else { /* received standart message, not a notification */
177
            /* no standart message expected: do nothing */
178
        }
179
    }
180
181
    if (unsubscribe_interrupt(&mouse_id)) return 1;
182
    if (mouse_set_data_report(false)) return 1;
183
184
    if (unsubscribe_interrupt(&timer_id)) return 1;
185
186
    return 0;
187 53 up20180655
}
188
189 54 up20180642
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
190 53 up20180655
    /* To be completed */
191
    printf("%s: under construction\n", __func__);
192
    return 1;
193
}