root / lab4 / lab4.c @ 76
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 (mouse_set_data_report(true)) return 1; |
48 |
|
49 |
if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; |
50 |
/// cycle
|
51 |
int good = 1; |
52 |
uint32_t cnt_now = 0;
|
53 |
while (good) {
|
54 |
/* Get a request message. */
|
55 |
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
56 |
printf("driver_receive failed with %d", r);
|
57 |
continue;
|
58 |
} |
59 |
if (is_ipc_notify(ipc_status)) { /* received notification */ |
60 |
switch (_ENDPOINT_P(msg.m_source)) {
|
61 |
case HARDWARE: /* hardware interrupt notification */ |
62 |
if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */ |
63 |
mouse_ih(); |
64 |
if(counter >= 3){ |
65 |
struct packet pp = mouse_parse_packet(packet);
|
66 |
mouse_print_packet(&pp); |
67 |
cnt_now++; |
68 |
if(cnt == cnt_now) good = 0; |
69 |
} |
70 |
} |
71 |
break;
|
72 |
default:
|
73 |
break; /* no other notifications expected: do nothing */ |
74 |
} |
75 |
} else { /* received standart message, not a notification */ |
76 |
/* no standart message expected: do nothing */
|
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 (mouse_set_data_report(true)) return 1; |
146 |
|
147 |
if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; |
148 |
/// cycle
|
149 |
int good = 1; |
150 |
while (good) {
|
151 |
/* Get a request message. */
|
152 |
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
153 |
printf("driver_receive failed with %d", r);
|
154 |
continue;
|
155 |
} |
156 |
if (is_ipc_notify(ipc_status)) { /* received notification */ |
157 |
switch (_ENDPOINT_P(msg.m_source)) {
|
158 |
case HARDWARE: /* hardware interrupt notification */ |
159 |
if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */ |
160 |
timer_int_handler(); |
161 |
if (no_interrupts%frequency == 0) time++; |
162 |
if(time >= idle_time) good = 0; |
163 |
} |
164 |
if (msg.m_notify.interrupts & mouse_irq) { /// subscribed interrupt |
165 |
mouse_ih(); |
166 |
if(counter >= 3){ |
167 |
struct packet pp = mouse_parse_packet(packet);
|
168 |
mouse_print_packet(&pp); |
169 |
time = 0;
|
170 |
no_interrupts = 0;
|
171 |
} |
172 |
} |
173 |
break;
|
174 |
default:
|
175 |
break; /* no other notifications expected: do nothing */ |
176 |
} |
177 |
} else { /* received standart message, not a notification */ |
178 |
/* no standart message expected: do nothing */
|
179 |
} |
180 |
} |
181 |
|
182 |
if (unsubscribe_interrupt(&mouse_id)) return 1; |
183 |
if (mouse_set_data_report(false)) return 1; |
184 |
|
185 |
if (unsubscribe_interrupt(&timer_id)) return 1; |
186 |
|
187 |
return 0; |
188 |
} |
189 |
|
190 |
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
|
191 |
/* To be completed */
|
192 |
printf("%s: under construction\n", __func__);
|
193 |
return 1; |
194 |
} |