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