root / lab5 / lab5.c @ 114
History | View | Annotate | Download (12.9 KB)
1 | 93 | up20180655 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | #include <lcom/lab5.h> |
||
4 | |||
5 | #include <stdint.h> |
||
6 | #include <stdio.h> |
||
7 | |||
8 | 94 | up20180655 | #include "graphics.h" |
9 | #include "graphics_macros.h" |
||
10 | 109 | up20180655 | #include "keyboard.h" |
11 | #include "kbc.h" |
||
12 | #include "kbc_macros.h" |
||
13 | 94 | up20180655 | |
14 | 93 | up20180655 | // Any header files included below this line should have been created by you
|
15 | |||
16 | int main(int argc, char *argv[]) { |
||
17 | 102 | up20180642 | // sets the language of LCF messages (can be either EN-US or PT-PT)
|
18 | lcf_set_language("EN-US");
|
||
19 | 93 | up20180655 | |
20 | 102 | up20180642 | // enables to log function invocations that are being "wrapped" by LCF
|
21 | // [comment this out if you don't want/need it]
|
||
22 | lcf_trace_calls("/home/lcom/labs/lab5/trace.txt");
|
||
23 | 93 | up20180655 | |
24 | 102 | up20180642 | // enables to save the output of printf function calls on a file
|
25 | // [comment this out if you don't want/need it]
|
||
26 | lcf_log_output("/home/lcom/labs/lab5/output.txt");
|
||
27 | 93 | up20180655 | |
28 | 102 | up20180642 | // handles control over to LCF
|
29 | // [LCF handles command line arguments and invokes the right function]
|
||
30 | if (lcf_start(argc, argv))
|
||
31 | 93 | up20180655 | return 1; |
32 | |||
33 | 102 | up20180642 | // LCF clean up tasks
|
34 | // [must be the last statement before return]
|
||
35 | lcf_cleanup(); |
||
36 | 93 | up20180655 | |
37 | 102 | up20180642 | return 0; |
38 | 93 | up20180655 | } |
39 | |||
40 | int(video_test_init)(uint16_t mode, uint8_t delay) {
|
||
41 | 99 | up20180655 | int r;
|
42 | if ((r = get_permissions_first_mbyte()))
|
||
43 | 103 | up20180655 | panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
44 | 93 | up20180655 | |
45 | 103 | up20180655 | if (vbe_get_mode_information(mode)) {
|
46 | 99 | up20180655 | printf("%s: failed to get information for mode %x.\n", __func__, mode);
|
47 | if (vg_exit())
|
||
48 | 103 | up20180655 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
49 | 99 | up20180655 | return 1; |
50 | } |
||
51 | |||
52 | 103 | up20180655 | map_vram(); // if function fails it aborts program
|
53 | 99 | up20180655 | |
54 | 109 | up20180655 | if (set_graphics_mode(mode)) {
|
55 | printf("%s: failed to set graphic mode %x.\n", __func__, mode);
|
||
56 | if (vg_exit())
|
||
57 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
58 | return 1; |
||
59 | }; |
||
60 | |||
61 | 96 | up20180655 | tickdelay(micros_to_ticks(delay*1e6));
|
62 | 94 | up20180655 | |
63 | 99 | up20180655 | if (vg_exit()) {
|
64 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
65 | 102 | up20180642 | if (free_memory())
|
66 | 103 | up20180655 | printf("%s: lm_free failed\n", __func__);
|
67 | 99 | up20180655 | return 1; |
68 | } |
||
69 | 102 | up20180642 | |
70 | 99 | up20180655 | if (free_memory()) {
|
71 | printf("%s: lm_free failed\n", __func__);
|
||
72 | return 1; |
||
73 | 102 | up20180642 | } |
74 | 103 | up20180655 | |
75 | 94 | up20180655 | return 0; |
76 | 93 | up20180655 | } |
77 | |||
78 | 111 | up20180642 | // lcom_run lab5 "rectangle 105 100 100 100 100 1"
|
79 | // lcom_run lab5 "rectangle 115 100 100 100 100 FF0000"
|
||
80 | 102 | up20180642 | int(video_test_rectangle)(uint16_t mode, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t color) {
|
81 | int r;
|
||
82 | if ((r = get_permissions_first_mbyte()))
|
||
83 | 109 | up20180655 | panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
84 | 93 | up20180655 | |
85 | 109 | up20180655 | if (vbe_get_mode_information(mode)) {
|
86 | printf("%s: failed to get information for mode %x.\n", __func__, mode);
|
||
87 | if (vg_exit())
|
||
88 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
89 | return 1; |
||
90 | } |
||
91 | |||
92 | map_vram(); // if function fails it aborts program
|
||
93 | |||
94 | 102 | up20180642 | if (set_graphics_mode(mode)) {
|
95 | printf("%s: failed to set graphic mode %x.\n", __func__, mode);
|
||
96 | if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__); |
||
97 | return 1; |
||
98 | }; |
||
99 | |||
100 | 109 | up20180655 | if (vg_draw_rectangle(x, y, width, height, color)) {
|
101 | if (vg_exit()) {
|
||
102 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
103 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
104 | } |
||
105 | return 1; |
||
106 | } |
||
107 | |||
108 | /// loop stuff
|
||
109 | int ipc_status;
|
||
110 | message msg; |
||
111 | /// Keyboard interrupt handling
|
||
112 | uint8_t kbc_irq_bit = KBC_IRQ; |
||
113 | int kbc_id = 0; |
||
114 | int kbc_irq = BIT(kbc_irq_bit);
|
||
115 | if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
|
||
116 | if (vg_exit()) {
|
||
117 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
118 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
119 | } |
||
120 | return 1; |
||
121 | } |
||
122 | /// cycle
|
||
123 | int good = 1; |
||
124 | while (good) {
|
||
125 | /* Get a request message. */
|
||
126 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
127 | printf("driver_receive failed with %d", r);
|
||
128 | continue;
|
||
129 | } |
||
130 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
131 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
132 | case HARDWARE: /* hardware interrupt notification */ |
||
133 | if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */ |
||
134 | kbc_ih(); |
||
135 | if (scancode[0] == ESC_BREAK_CODE) good = 0; |
||
136 | } |
||
137 | break;
|
||
138 | default:
|
||
139 | break; /* no other notifications expected: do nothing */ |
||
140 | } |
||
141 | } else { /* received standart message, not a notification */ |
||
142 | /* no standart message expected: do nothing */
|
||
143 | } |
||
144 | } |
||
145 | |||
146 | if (unsubscribe_interrupt(&kbc_id)) {
|
||
147 | if (vg_exit()) {
|
||
148 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
149 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
150 | } |
||
151 | return 1; |
||
152 | }; |
||
153 | |||
154 | 102 | up20180642 | if (vg_exit()) {
|
155 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
156 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
157 | return 1; |
||
158 | } |
||
159 | 109 | up20180655 | |
160 | if (free_memory()) {
|
||
161 | printf("%s: lm_free failed\n", __func__);
|
||
162 | return 1; |
||
163 | } |
||
164 | |||
165 | 102 | up20180642 | return 0; |
166 | 93 | up20180655 | } |
167 | |||
168 | int(video_test_pattern)(uint16_t mode, uint8_t no_rectangles, uint32_t first, uint8_t step) {
|
||
169 | 111 | up20180642 | int r;
|
170 | if ((r = get_permissions_first_mbyte()))
|
||
171 | panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
||
172 | 103 | up20180655 | |
173 | 111 | up20180642 | if (vbe_get_mode_information(mode)) {
|
174 | printf("%s: failed to get information for mode %x.\n", __func__, mode);
|
||
175 | 114 | up20180642 | if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__); |
176 | 111 | up20180642 | return 1; |
177 | } |
||
178 | |||
179 | map_vram(); // if function fails it aborts program
|
||
180 | |||
181 | if (set_graphics_mode(mode)) {
|
||
182 | printf("%s: failed to set graphic mode %x.\n", __func__, mode);
|
||
183 | if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__); |
||
184 | return 1; |
||
185 | } |
||
186 | 112 | up20180642 | |
187 | 111 | up20180642 | uint16_t W = get_XRes()/no_rectangles; |
188 | uint16_t H = get_YRes()/no_rectangles; |
||
189 | uint32_t color, R, G, B; |
||
190 | for(uint8_t row = 0; row < no_rectangles; ++row){ |
||
191 | for(uint8_t col = 0; col < no_rectangles; ++col){ |
||
192 | 112 | up20180642 | if(get_bytes_pixel() == 1){ |
193 | 111 | up20180642 | color = (first + (row * no_rectangles + col) * step) % (1 << get_bits_pixel());
|
194 | }else{
|
||
195 | 113 | up20180642 | R = (GET_RED(first) + col*step) % (1 << get_RedMaskSize());
|
196 | G = (GET_GRE(first) + row*step) % (1 << get_GreenMaskSize());
|
||
197 | B = (GET_BLU(first) + (col+row)*step) % (1 << get_BlueMaskSize());
|
||
198 | color = SET_COLOR(R,G,B); |
||
199 | 111 | up20180642 | } |
200 | if (vg_draw_rectangle(col*W,row*H,W,H,color)) {
|
||
201 | if (vg_exit()) {
|
||
202 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
203 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
204 | } |
||
205 | return 1; |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /// loop stuff
|
||
211 | int ipc_status;
|
||
212 | message msg; |
||
213 | /// Keyboard interrupt handling
|
||
214 | uint8_t kbc_irq_bit = KBC_IRQ; |
||
215 | int kbc_id = 0; |
||
216 | int kbc_irq = BIT(kbc_irq_bit);
|
||
217 | if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
|
||
218 | if (vg_exit()) {
|
||
219 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
220 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
221 | } |
||
222 | return 1; |
||
223 | } |
||
224 | /// cycle
|
||
225 | int good = 1; |
||
226 | while (good) {
|
||
227 | /* Get a request message. */
|
||
228 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
229 | printf("driver_receive failed with %d", r);
|
||
230 | continue;
|
||
231 | } |
||
232 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
233 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
234 | case HARDWARE: /* hardware interrupt notification */ |
||
235 | if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */ |
||
236 | kbc_ih(); |
||
237 | if (scancode[0] == ESC_BREAK_CODE) good = 0; |
||
238 | } |
||
239 | break;
|
||
240 | default:
|
||
241 | break; /* no other notifications expected: do nothing */ |
||
242 | } |
||
243 | } else { /* received standart message, not a notification */ |
||
244 | /* no standart message expected: do nothing */
|
||
245 | } |
||
246 | } |
||
247 | |||
248 | if (unsubscribe_interrupt(&kbc_id)) {
|
||
249 | if (vg_exit()) {
|
||
250 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
251 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
252 | } |
||
253 | return 1; |
||
254 | }; |
||
255 | |||
256 | if (vg_exit()) {
|
||
257 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
258 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
259 | return 1; |
||
260 | } |
||
261 | |||
262 | if (free_memory()) {
|
||
263 | printf("%s: lm_free failed\n", __func__);
|
||
264 | return 1; |
||
265 | } |
||
266 | |||
267 | return 0; |
||
268 | 93 | up20180655 | } |
269 | |||
270 | int(video_test_xpm)(xpm_map_t xpm, uint16_t x, uint16_t y) {
|
||
271 | 102 | up20180642 | /* To be completed */
|
272 | printf("%s(%8p, %u, %u): under construction\n", __func__, xpm, x, y);
|
||
273 | 93 | up20180655 | |
274 | 102 | up20180642 | return 1; |
275 | 93 | up20180655 | } |
276 | |||
277 | 102 | up20180642 | int(video_test_move)(xpm_map_t xpm, uint16_t xi, uint16_t yi, uint16_t xf, uint16_t yf, int16_t speed, uint8_t fr_rate) {
|
278 | 114 | up20180642 | int r;
|
279 | if ((r = get_permissions_first_mbyte()))
|
||
280 | panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
||
281 | 93 | up20180655 | |
282 | 114 | up20180642 | if (vbe_get_mode_information(mode)) {
|
283 | printf("%s: failed to get information for mode %x.\n", __func__, mode);
|
||
284 | if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__); |
||
285 | return 1; |
||
286 | } |
||
287 | |||
288 | map_vram(); // if function fails it aborts program
|
||
289 | |||
290 | if (set_graphics_mode(mode)) {
|
||
291 | printf("%s: failed to set graphic mode %x.\n", __func__, mode);
|
||
292 | if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__); |
||
293 | return 1; |
||
294 | } |
||
295 | |||
296 | uint16_t W = get_XRes()/no_rectangles; |
||
297 | uint16_t H = get_YRes()/no_rectangles; |
||
298 | uint32_t color, R, G, B; |
||
299 | for(uint8_t row = 0; row < no_rectangles; ++row){ |
||
300 | for(uint8_t col = 0; col < no_rectangles; ++col){ |
||
301 | if(get_bytes_pixel() == 1){ |
||
302 | color = (first + (row * no_rectangles + col) * step) % (1 << get_bits_pixel());
|
||
303 | }else{
|
||
304 | R = (GET_RED(first) + col*step) % (1 << get_RedMaskSize());
|
||
305 | G = (GET_GRE(first) + row*step) % (1 << get_GreenMaskSize());
|
||
306 | B = (GET_BLU(first) + (col+row)*step) % (1 << get_BlueMaskSize());
|
||
307 | color = SET_COLOR(R,G,B); |
||
308 | } |
||
309 | if (vg_draw_rectangle(col*W,row*H,W,H,color)) {
|
||
310 | if (vg_exit()) {
|
||
311 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
312 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
313 | } |
||
314 | return 1; |
||
315 | } |
||
316 | } |
||
317 | } |
||
318 | |||
319 | /// loop stuff
|
||
320 | int ipc_status;
|
||
321 | message msg; |
||
322 | /// Keyboard interrupt handling
|
||
323 | uint8_t kbc_irq_bit = KBC_IRQ; |
||
324 | int kbc_id = 0; |
||
325 | int kbc_irq = BIT(kbc_irq_bit);
|
||
326 | if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
|
||
327 | if (vg_exit()) {
|
||
328 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
329 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
330 | } |
||
331 | return 1; |
||
332 | } |
||
333 | /// cycle
|
||
334 | int good = 1; |
||
335 | while (good) {
|
||
336 | /* Get a request message. */
|
||
337 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
338 | printf("driver_receive failed with %d", r);
|
||
339 | continue;
|
||
340 | } |
||
341 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
342 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
343 | case HARDWARE: /* hardware interrupt notification */ |
||
344 | if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */ |
||
345 | kbc_ih(); |
||
346 | if (scancode[0] == ESC_BREAK_CODE) good = 0; |
||
347 | } |
||
348 | break;
|
||
349 | default:
|
||
350 | break; /* no other notifications expected: do nothing */ |
||
351 | } |
||
352 | } else { /* received standart message, not a notification */ |
||
353 | /* no standart message expected: do nothing */
|
||
354 | } |
||
355 | } |
||
356 | |||
357 | if (unsubscribe_interrupt(&kbc_id)) {
|
||
358 | if (vg_exit()) {
|
||
359 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
360 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
361 | } |
||
362 | return 1; |
||
363 | }; |
||
364 | |||
365 | if (vg_exit()) {
|
||
366 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
367 | if (free_memory()) printf("%s: lm_free failed\n", __func__); |
||
368 | return 1; |
||
369 | } |
||
370 | |||
371 | if (free_memory()) {
|
||
372 | printf("%s: lm_free failed\n", __func__);
|
||
373 | return 1; |
||
374 | } |
||
375 | |||
376 | return 0; |
||
377 | 93 | up20180655 | } |
378 | |||
379 | int(video_test_controller)() {
|
||
380 | 102 | up20180642 | /* To be completed */
|
381 | printf("%s(): under construction\n", __func__);
|
||
382 | 93 | up20180655 | |
383 | 102 | up20180642 | return 1; |
384 | 93 | up20180655 | } |