root / lab5 / lab5.c @ 122
History | View | Annotate | Download (12.1 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include <lcom/lab5.h> |
4 |
|
5 |
#include <stdint.h> |
6 |
#include <stdio.h> |
7 |
|
8 |
#include "graphics.h" |
9 |
#include "graphics_macros.h" |
10 |
#include "keyboard.h" |
11 |
#include "kbc.h" |
12 |
#include "kbc_macros.h" |
13 |
|
14 |
// Any header files included below this line should have been created by you
|
15 |
|
16 |
int main(int argc, char *argv[]) { |
17 |
// sets the language of LCF messages (can be either EN-US or PT-PT)
|
18 |
lcf_set_language("EN-US");
|
19 |
|
20 |
// 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 |
|
24 |
// 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 |
|
28 |
// handles control over to LCF
|
29 |
// [LCF handles command line arguments and invokes the right function]
|
30 |
if (lcf_start(argc, argv))
|
31 |
return 1; |
32 |
|
33 |
// LCF clean up tasks
|
34 |
// [must be the last statement before return]
|
35 |
lcf_cleanup(); |
36 |
|
37 |
return 0; |
38 |
} |
39 |
|
40 |
int(video_test_init)(uint16_t mode, uint8_t delay) {
|
41 |
//int r;
|
42 |
//if ((r = get_permissions_first_mbyte()))
|
43 |
// panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
44 |
|
45 |
if (vbe_get_mode_information(mode)) {
|
46 |
printf("%s: failed to get information for mode %x.\n", __func__, mode);
|
47 |
if (vg_exit())
|
48 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
49 |
return 1; |
50 |
} |
51 |
|
52 |
map_vram(); // if function fails it aborts program
|
53 |
|
54 |
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 |
tickdelay(micros_to_ticks(delay*1e6));
|
62 |
|
63 |
if (vg_exit()) {
|
64 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
65 |
if (free_memory())
|
66 |
printf("%s: lm_free failed\n", __func__);
|
67 |
return 1; |
68 |
} |
69 |
|
70 |
if (free_memory()) {
|
71 |
printf("%s: lm_free failed\n", __func__);
|
72 |
return 1; |
73 |
} |
74 |
|
75 |
return 0; |
76 |
} |
77 |
|
78 |
// lcom_run lab5 "rectangle 105 100 100 100 100 1"
|
79 |
// lcom_run lab5 "rectangle 115 100 100 100 100 FF0000"
|
80 |
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 |
// panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
84 |
|
85 |
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 |
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 |
if (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 |
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 |
|
160 |
if (free_memory()) {
|
161 |
printf("%s: lm_free failed\n", __func__);
|
162 |
return 1; |
163 |
} |
164 |
|
165 |
return 0; |
166 |
} |
167 |
|
168 |
int(video_test_pattern)(uint16_t mode, uint8_t no_rectangles, uint32_t first, uint8_t step) {
|
169 |
int r;
|
170 |
//if ((r = get_permissions_first_mbyte()))
|
171 |
// panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
172 |
|
173 |
if (vbe_get_mode_information(mode)) {
|
174 |
printf("%s: failed to get information for mode %x.\n", __func__, mode);
|
175 |
if (vg_exit())
|
176 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
177 |
return 1; |
178 |
} |
179 |
|
180 |
map_vram(); // if function fails it aborts program
|
181 |
|
182 |
if (set_graphics_mode(mode)) {
|
183 |
printf("%s: failed to set graphic mode %x.\n", __func__, mode);
|
184 |
if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__); |
185 |
return 1; |
186 |
}; |
187 |
|
188 |
uint16_t W = get_XRes()/no_rectangles; |
189 |
uint16_t H = get_YRes()/no_rectangles; |
190 |
|
191 |
uint32_t color, R, G, B; |
192 |
for(uint8_t row = 0; row < no_rectangles; ++row){ |
193 |
for(uint8_t col = 0; col < no_rectangles; ++col){ |
194 |
if(get_bytes_pixel() == 1){ |
195 |
color = (first + (row * no_rectangles + col) * step) % (1 << get_bits_pixel());
|
196 |
}else{
|
197 |
R = (GET_RED(first) + col*step) % (1 << get_RedMaskSize());
|
198 |
G = (GET_GRE(first) + row*step) % (1 << get_GreenMaskSize());
|
199 |
B = (GET_BLU(first) + (col+row)*step) % (1 << get_BlueMaskSize());
|
200 |
color = SET_COLOR(R,G,B); |
201 |
} |
202 |
if (draw_rectangle(col*W,row*H,W,H,color)) {
|
203 |
if (vg_exit()) {
|
204 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
205 |
if (free_memory()) printf("%s: lm_free failed\n", __func__); |
206 |
} |
207 |
return 1; |
208 |
} |
209 |
} |
210 |
} |
211 |
/// loop stuff
|
212 |
int ipc_status;
|
213 |
message msg; |
214 |
/// Keyboard interrupt handling
|
215 |
uint8_t kbc_irq_bit = KBC_IRQ; |
216 |
int kbc_id = 0; |
217 |
int kbc_irq = BIT(kbc_irq_bit);
|
218 |
if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
|
219 |
if (vg_exit()) {
|
220 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
221 |
if (free_memory()) printf("%s: lm_free failed\n", __func__); |
222 |
} |
223 |
return 1; |
224 |
} |
225 |
/// cycle
|
226 |
int good = 1; |
227 |
while (good) {
|
228 |
/* Get a request message. */
|
229 |
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
230 |
printf("driver_receive failed with %d", r);
|
231 |
continue;
|
232 |
} |
233 |
if (is_ipc_notify(ipc_status)) { /* received notification */ |
234 |
switch (_ENDPOINT_P(msg.m_source)) {
|
235 |
case HARDWARE: /* hardware interrupt notification */ |
236 |
if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */ |
237 |
kbc_ih(); |
238 |
if (scancode[0] == ESC_BREAK_CODE) good = 0; |
239 |
} |
240 |
break;
|
241 |
default:
|
242 |
break; /* no other notifications expected: do nothing */ |
243 |
} |
244 |
} else { /* received standart message, not a notification */ |
245 |
/* no standart message expected: do nothing */
|
246 |
} |
247 |
} |
248 |
|
249 |
if (unsubscribe_interrupt(&kbc_id)) {
|
250 |
if (vg_exit()) {
|
251 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
252 |
if (free_memory()) printf("%s: lm_free failed\n", __func__); |
253 |
} |
254 |
return 1; |
255 |
}; |
256 |
|
257 |
if (vg_exit()) {
|
258 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
259 |
if (free_memory()) printf("%s: lm_free failed\n", __func__); |
260 |
return 1; |
261 |
} |
262 |
|
263 |
if (free_memory()) {
|
264 |
printf("%s: lm_free failed\n", __func__);
|
265 |
return 1; |
266 |
} |
267 |
|
268 |
return 0; |
269 |
} |
270 |
|
271 |
int(video_test_xpm)(xpm_map_t xpm, uint16_t x, uint16_t y) {
|
272 |
int r;
|
273 |
if ((r = get_permissions_first_mbyte()))
|
274 |
panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
275 |
|
276 |
if (vbe_get_mode_information(INDEXED_1024_768)) {
|
277 |
printf("%s: failed to get information for mode %x.\n", __func__, INDEXED_1024_768);
|
278 |
if (vg_exit())
|
279 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
280 |
return 1; |
281 |
} |
282 |
|
283 |
map_vram(); // if function fails it aborts program
|
284 |
|
285 |
if (set_graphics_mode(INDEXED_1024_768)) {
|
286 |
printf("%s: failed to set graphic mode %x.\n", __func__, INDEXED_1024_768);
|
287 |
if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__); |
288 |
return 1; |
289 |
}; |
290 |
|
291 |
enum xpm_image_type type = XPM_INDEXED;
|
292 |
xpm_image_t img; |
293 |
|
294 |
uint8_t *map = xpm_load(xpm, type, &img); |
295 |
|
296 |
for (int i = 0; i < img.width; i++) { |
297 |
for (int j = 0; j < img.height; j++) { |
298 |
set_pixel(x + i, y + j, map[i + j * img.width]); |
299 |
} |
300 |
} |
301 |
|
302 |
/// loop stuff
|
303 |
int ipc_status;
|
304 |
message msg; |
305 |
/// Keyboard interrupt handling
|
306 |
uint8_t kbc_irq_bit = KBC_IRQ; |
307 |
int kbc_id = 0; |
308 |
int kbc_irq = BIT(kbc_irq_bit);
|
309 |
if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
|
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 |
/// cycle
|
317 |
int good = 1; |
318 |
while (good) {
|
319 |
/* Get a request message. */
|
320 |
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
321 |
printf("driver_receive failed with %d", r);
|
322 |
continue;
|
323 |
} |
324 |
if (is_ipc_notify(ipc_status)) { /* received notification */ |
325 |
switch (_ENDPOINT_P(msg.m_source)) {
|
326 |
case HARDWARE: /* hardware interrupt notification */ |
327 |
if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */ |
328 |
kbc_ih(); |
329 |
if (scancode[0] == ESC_BREAK_CODE) good = 0; |
330 |
} |
331 |
break;
|
332 |
default:
|
333 |
break; /* no other notifications expected: do nothing */ |
334 |
} |
335 |
} else { /* received standart message, not a notification */ |
336 |
/* no standart message expected: do nothing */
|
337 |
} |
338 |
} |
339 |
|
340 |
if (unsubscribe_interrupt(&kbc_id)) {
|
341 |
if (vg_exit()) {
|
342 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
343 |
if (free_memory()) printf("%s: lm_free failed\n", __func__); |
344 |
} |
345 |
return 1; |
346 |
}; |
347 |
|
348 |
if (vg_exit()) {
|
349 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
350 |
if (free_memory()) printf("%s: lm_free failed\n", __func__); |
351 |
return 1; |
352 |
} |
353 |
|
354 |
if (free_memory()) {
|
355 |
printf("%s: lm_free failed\n", __func__);
|
356 |
return 1; |
357 |
} |
358 |
|
359 |
return 0; |
360 |
} |
361 |
|
362 |
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) {
|
363 |
|
364 |
return 1; |
365 |
} |
366 |
|
367 |
int(video_test_controller)() {
|
368 |
|
369 |
|
370 |
|
371 |
return 1; |
372 |
} |