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