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