root / lab4 / lab4.c @ 79
History | View | Annotate | Download (8.15 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 |
extern uint8_t packet[3]; |
39 |
extern int counter; |
40 |
|
41 |
int (mouse_test_packet)(uint32_t cnt) {
|
42 |
int ret = 0; |
43 |
/// loop stuff
|
44 |
int ipc_status, r;
|
45 |
message msg; |
46 |
/// Mouse interrupt handling
|
47 |
uint8_t mouse_irq_bit = 12;
|
48 |
int mouse_id = 0; |
49 |
int mouse_irq = BIT(mouse_irq_bit);
|
50 |
if ((ret = mouse_set_data_report(true))) return ret; |
51 |
//if ((ret = mouse_enable_data_reporting())) return ret;
|
52 |
|
53 |
if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; |
54 |
/// cycle
|
55 |
int good = 1; |
56 |
uint32_t cnt_now = 0;
|
57 |
while (good) {
|
58 |
/* Get a request message. */
|
59 |
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
60 |
printf("driver_receive failed with %d", r);
|
61 |
continue;
|
62 |
} |
63 |
if (is_ipc_notify(ipc_status)) { /* received notification */ |
64 |
switch (_ENDPOINT_P(msg.m_source)) {
|
65 |
case HARDWARE: /* hardware interrupt notification */ |
66 |
if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */ |
67 |
mouse_ih(); |
68 |
if(counter >= 3){ |
69 |
struct packet pp = mouse_parse_packet(packet);
|
70 |
mouse_print_packet(&pp); |
71 |
cnt_now++; |
72 |
if(cnt == cnt_now) good = 0; |
73 |
} |
74 |
} |
75 |
break;
|
76 |
default:
|
77 |
break; /* no other notifications expected: do nothing */ |
78 |
} |
79 |
} else { /* received standart message, not a notification */ |
80 |
/* no standart message expected: do nothing */
|
81 |
} |
82 |
} |
83 |
if (unsubscribe_interrupt(&mouse_id)) return 1; |
84 |
if (mouse_set_data_report(false)) return 1; |
85 |
return 0; |
86 |
} |
87 |
|
88 |
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
|
89 |
|
90 |
// Mouse packets data
|
91 |
uint8_t packet[3];
|
92 |
int sz = 0; |
93 |
uint8_t data = 0;
|
94 |
// Cycle
|
95 |
int packetCounter = 0; |
96 |
int good = 1; |
97 |
// return value
|
98 |
int ret;
|
99 |
|
100 |
if ((ret = mouse_issue_cmd(SET_REMOTE_MD))) return ret; |
101 |
|
102 |
if ((ret = mouse_set_data_report(true))) return ret; |
103 |
|
104 |
while (good) {
|
105 |
|
106 |
if ((ret = mouse_read_data(&data))) return ret; |
107 |
|
108 |
if ((data & FIRST_BYTE_ID) || sz) {
|
109 |
packet[sz] = data; |
110 |
sz++; |
111 |
} |
112 |
if (sz == 3) { |
113 |
struct packet pp = mouse_parse_packet(packet);
|
114 |
mouse_print_packet(&pp); |
115 |
packetCounter++; |
116 |
sz = 0;
|
117 |
if (packetCounter == cnt) good = 0; |
118 |
} |
119 |
tickdelay(micros_to_ticks(period*1e3));
|
120 |
} |
121 |
|
122 |
// Set Stream mode
|
123 |
if ((ret = mouse_issue_cmd(SET_STREAM_MD))) return ret; |
124 |
// Disable data reporting
|
125 |
if ((ret = mouse_set_data_report(false))) return ret; |
126 |
uint8_t cmd_byte = minix_get_dflt_kbc_cmd_byte(); |
127 |
if ((ret = kbc_change_cmd(cmd_byte))) return ret; |
128 |
|
129 |
return SUCCESS;
|
130 |
} |
131 |
|
132 |
int (mouse_test_async)(uint8_t idle_time) {
|
133 |
/// loop stuff
|
134 |
int ipc_status, r;
|
135 |
message msg; |
136 |
/// Timer interrupt handling
|
137 |
const uint32_t frequency = sys_hz(); // Frequency asummed at 60Hz |
138 |
uint8_t timer_irq_bit = 0;
|
139 |
int timer_id = 0; |
140 |
int timer_irq = BIT(timer_irq_bit);
|
141 |
if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) return 1; |
142 |
|
143 |
no_interrupts = 0;
|
144 |
int time = 0; |
145 |
/// Mouse interrupt handling
|
146 |
uint8_t mouse_irq_bit = 12;
|
147 |
int mouse_id = 0; |
148 |
int mouse_irq = BIT(mouse_irq_bit);
|
149 |
if (mouse_set_data_report(true)) return 1; |
150 |
|
151 |
if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; |
152 |
/// cycle
|
153 |
int good = 1; |
154 |
while (good) {
|
155 |
/* Get a request message. */
|
156 |
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
157 |
printf("driver_receive failed with %d", r);
|
158 |
continue;
|
159 |
} |
160 |
if (is_ipc_notify(ipc_status)) { /* received notification */ |
161 |
switch (_ENDPOINT_P(msg.m_source)) {
|
162 |
case HARDWARE: /* hardware interrupt notification */ |
163 |
if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */ |
164 |
timer_int_handler(); |
165 |
if (no_interrupts%frequency == 0) time++; |
166 |
if(time >= idle_time) good = 0; |
167 |
} |
168 |
if (msg.m_notify.interrupts & mouse_irq) { /// subscribed interrupt |
169 |
mouse_ih(); |
170 |
if(counter >= 3){ |
171 |
struct packet pp = mouse_parse_packet(packet);
|
172 |
mouse_print_packet(&pp); |
173 |
time = 0;
|
174 |
no_interrupts = 0;
|
175 |
} |
176 |
} |
177 |
break;
|
178 |
default:
|
179 |
break; /* no other notifications expected: do nothing */ |
180 |
} |
181 |
} else { /* received standart message, not a notification */ |
182 |
/* no standart message expected: do nothing */
|
183 |
} |
184 |
} |
185 |
|
186 |
if (unsubscribe_interrupt(&mouse_id)) return 1; |
187 |
if (mouse_set_data_report(false)) return 1; |
188 |
|
189 |
if (unsubscribe_interrupt(&timer_id)) return 1; |
190 |
|
191 |
return 0; |
192 |
} |
193 |
|
194 |
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
|
195 |
int ret;
|
196 |
/// loop stuff
|
197 |
int ipc_status, r;
|
198 |
message msg; |
199 |
/// Mouse interrupt handling
|
200 |
uint8_t mouse_irq_bit = 12;
|
201 |
int mouse_id = 0; |
202 |
int mouse_irq = BIT(mouse_irq_bit);
|
203 |
if ((ret = mouse_set_data_report(true))) return ret; |
204 |
|
205 |
if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; |
206 |
/// cycle
|
207 |
int good = 1; |
208 |
|
209 |
// mouse event gesture
|
210 |
struct mouse_ev *event;
|
211 |
struct packet pp;
|
212 |
int response;
|
213 |
while (good) {
|
214 |
/* Get a request message. */
|
215 |
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
216 |
printf("driver_receive failed with %d", r);
|
217 |
continue;
|
218 |
} |
219 |
if (is_ipc_notify(ipc_status)) { /* received notification */ |
220 |
switch (_ENDPOINT_P(msg.m_source)) {
|
221 |
case HARDWARE: /* hardware interrupt notification */ |
222 |
if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */ |
223 |
mouse_ih(); |
224 |
if(counter >= 3) { |
225 |
pp = mouse_parse_packet(packet); |
226 |
mouse_print_packet(&pp); |
227 |
event = mouse_get_event(&pp); |
228 |
|
229 |
response = state_machine(event, x_len, tolerance); |
230 |
|
231 |
if (response == SUCCESS)
|
232 |
good = 0;
|
233 |
else if (response == INVALID_STATE) |
234 |
return response;
|
235 |
} |
236 |
} |
237 |
break;
|
238 |
default:
|
239 |
break; /* no other notifications expected: do nothing */ |
240 |
} |
241 |
} else { /* received standart message, not a notification */ |
242 |
/* no standart message expected: do nothing */
|
243 |
} |
244 |
} |
245 |
if (unsubscribe_interrupt(&mouse_id)) return 1; |
246 |
if (mouse_set_data_report(false)) return 1; |
247 |
|
248 |
return 0; |
249 |
} |