This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lab4.c
290 lines (245 loc) · 6.77 KB
/
lab4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// IMPORTANT: you must include the following line in all your C files
#include <lcom/lcf.h>
#include <stdint.h>
#include <stdio.h>
#include "mouse.h"
#include "i8042.h"
#include "i8254.h"
#include "timer.h"
#include "utils.h"
uint16_t byteArray[3];
struct packet pp;
uint32_t data;
uint8_t counter = 0;
extern int contador;
extern bool canPrint;
// Any header files included below this line should have been created by you
int main(int argc, char *argv[])
{
// sets the language of LCF messages (can be either EN-US or PT-PT)
lcf_set_language("EN-US");
// enables to log function invocations that are being "wrapped" by LCF
// [comment this out if you don't want/need/ it]
lcf_trace_calls("/home/lcom/labs/lab4/trace.txt");
// enables to save the output of printf function calls on a file
// [comment this out if you don't want/need it]
lcf_log_output("/home/lcom/labs/lab4/output.txt");
// handles control over to LCF
// [LCF handles command line arguments and invokes the right function]
if (lcf_start(argc, argv))
return 1;
// LCF clean up tasks
// [must be the last statement before return]
lcf_cleanup();
return 0;
}
int(mouse_test_packet)(uint32_t cnt)
{
int r;
int irq_set;
int ipc_status;
message msg;
if (issueCommand(REP_ENABLE) != 0) return 1;
if (mouse_subscribe_int(&irq_set) != 0) return 1;
while (cnt > 0)
{
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0)
{
printf("driver_receive failed with: %d\n", r);
continue;
}
if (is_ipc_notify(ipc_status))
{
switch (_ENDPOINT_P(msg.m_source))
{
case HARDWARE:
if (msg.m_notify.interrupts & irq_set)
{
mouse_ih();
handleSync();
if(counter == 3){
counter = 0;
cnt--;
parsePacket();
mouse_print_packet(&pp);
}
}
break;
default:
break;
}
}
else
{
}
}
if (mouse_unsubscribe_int() != 0) return 1;
if (issueCommand(REP_DISABLE) != 0) return 1;
return 0;
}
//remote mode
int(mouse_test_remote)(uint16_t period, uint8_t cnt)
{
uint8_t counter_1 = 0;
while(counter_1 < cnt){
mouse_ih();
if(issueCommand(READ_DATA) != OK) return 1;
handleSync();
if(counter == 3){
counter_1++;
parsePacket();
mouse_print_packet(&pp);
counter++;
counter = 0;
tickdelay(micros_to_ticks(period*1000));
}
}
if(issueCommand(SET_STREAM_MODE) != OK) return 1;
if(issueCommand(REP_DISABLE) != OK) return 1;
return resetKBC();
}
int(mouse_test_async)(uint8_t idle_time)
{
int r;
int irq_set;
int ipc_status;
uint8_t irq_set_timer0;
message msg;
int idle =idle_time* 60;
if (issueCommand(REP_ENABLE) != 0) return 1;
if (mouse_subscribe_int(&irq_set) != 0) return 1;
if (timer_subscribe_int(&irq_set_timer0)!= 0) return 1;
while (contador < idle)
{
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0)
{
printf("driver_receive failed with: %d\n", r);
continue;
}
if (is_ipc_notify(ipc_status))
{
switch (_ENDPOINT_P(msg.m_source))
{
case HARDWARE:
if (msg.m_notify.interrupts & irq_set_timer0){
timer_int_handler(); //increment the contador
}
if (msg.m_notify.interrupts & irq_set)
{
mouse_ih();
handleSync();
if(counter == 3){
counter = 0;
parsePacket();
mouse_print_packet(&pp);
}
contador = 0;
}
break;
default:
break;
}
}
else
{
}
}
if (timer_unsubscribe_int() != 0) return 1;
if (mouse_unsubscribe_int() != 0) return 1;
if (issueCommand(REP_DISABLE) != 0) return 1;
return 0;
}
int(mouse_test_gesture)(uint8_t x_len, uint8_t tolerance)
{
int r;
int irq_set;
int ipc_status;
message msg;
states state = INIT;
gesture machine;
machine.prevS = 0;
machine.prevX = 0.0;
machine.prevY = 0.0;
events event;
int8_t dX,dY;
int32_t total_x = 0, total_y = 0;
uint16_t displacement = 0;
if (issueCommand(REP_ENABLE) != 0) return 1;
if (mouse_subscribe_int(&irq_set) != 0) return 1;
while (state != FINAL)
{
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0)
{
printf("driver_receive failed with: %d\n", r);
continue;
}
if (is_ipc_notify(ipc_status))
{
switch (_ENDPOINT_P(msg.m_source))
{
case HARDWARE:
if (msg.m_notify.interrupts & irq_set)
{
mouse_ih();
handleSync();
if(counter == 3){
counter = 0;
parsePacket();
mouse_print_packet(&pp);
mouse_event(&event);
dX = pp.delta_x;
dY = pp.delta_y;
total_x += abs(dX);
total_y += abs(dY);
switch(state){
case INIT:
if(event == LB_DN){
displacement = 0;
state = FIRSTL;
}
break;
case FIRSTL:
displacement+=abs(dX);
if(event == LB_UP && displacement>=x_len) state = VERTEX;
else if(event == LB_UP && displacement < x_len) state = INIT;
else if(event == RB_UP || event == BUTTON_E) state = INIT;
else if((dX < 0 && abs(dX) > tolerance) || (dY < 0 && abs(dY) > tolerance) || (abs(total_y) < abs(total_x))) state = INIT;
break;
case VERTEX:
if(event == RB_DN){
displacement = 0;
state = SECONDL;
}
else if(event == LB_DN){
displacement = 0;
state = FIRSTL;
}
else if((event == MOUSE_MV) && (( abs(dX) > tolerance) || (abs(dY) > tolerance)))
state = INIT;
else if(event == BUTTON_E) state = INIT;
break;
case SECONDL:
displacement += abs(dX);
if(event == RB_UP && displacement >= x_len) state = FINAL;
else if(event == RB_UP && displacement < x_len) state = INIT;
else if(event == BUTTON_E) state = INIT;
else if((dX < 0 && abs(dX) > tolerance) || (dY < 0 && abs(dY) > tolerance) || abs(total_y) < abs(total_x)) state = INIT;
break;
default:
break;
}
}
}
break;
default:
break;
}
}
else
{
}
}
if (mouse_unsubscribe_int() != 0) return 1;
if (issueCommand(REP_DISABLE) != 0) return 1;
return 0;
}