root / lab4 / lab4.c @ 74
History | View | Annotate | Download (6.05 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 "timer.h" |
11 |
|
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 |
extern uint8_t packet[3]; |
37 |
extern int counter; |
38 |
|
39 |
int (mouse_test_packet)(uint32_t cnt) {
|
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 (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; |
48 |
if (mouse_set_data_report(true)) return 1; |
49 |
/// cycle
|
50 |
int good = 1; |
51 |
uint32_t cnt_now = 0;
|
52 |
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 |
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 |
} |
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 |
if (mouse_set_data_report(false)) return 1; |
81 |
|
82 |
return 0; |
83 |
} |
84 |
|
85 |
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
|
86 |
|
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 |
if ((ret = mouse_set_data_report(false))) return ret; |
121 |
|
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 |
} |
127 |
|
128 |
int (mouse_test_async)(uint8_t idle_time) {
|
129 |
/// 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 |
} |
188 |
|
189 |
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
|
190 |
/* To be completed */
|
191 |
printf("%s: under construction\n", __func__);
|
192 |
return 1; |
193 |
} |