Project

General

Profile

Statistics
| Revision:

root / lab5 / lab5.c @ 126

History | View | Annotate | Download (11.5 KB)

1
#include <lcom/lcf.h>
2

    
3
#include <lcom/lab5.h>
4

    
5
#include <stdint.h>
6
#include <stdio.h>
7

    
8
#include "sprite.h"
9
#include "graphics.h"
10
#include "graphics_macros.h"
11
#include "keyboard.h"
12
#include "kbc.h"
13
#include "kbc_macros.h"
14

    
15
// Any header files included below this line should have been created by you
16

    
17
int main(int argc, char *argv[]) {
18
    // sets the language of LCF messages (can be either EN-US or PT-PT)
19
    lcf_set_language("EN-US");
20

    
21
    // enables to log function invocations that are being "wrapped" by LCF
22
    // [comment this out if you don't want/need it]
23
    lcf_trace_calls("/home/lcom/labs/lab5/trace.txt");
24

    
25
    // enables to save the output of printf function calls on a file
26
    // [comment this out if you don't want/need it]
27
    lcf_log_output("/home/lcom/labs/lab5/output.txt");
28

    
29
    // handles control over to LCF
30
    // [LCF handles command line arguments and invokes the right function]
31
    if (lcf_start(argc, argv))
32
    return 1;
33

    
34
    // LCF clean up tasks
35
    // [must be the last statement before return]
36
    lcf_cleanup();
37

    
38
    return 0;
39
}
40

    
41
int(video_test_init)(uint16_t mode, uint8_t delay) {
42

    
43
    if (vbe_get_mode_information(mode)) {
44
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
45
        if (vg_exit())
46
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
47
        return 1;
48
    }
49

    
50
    map_vram(); // if function fails it aborts program
51

    
52
    if (set_graphics_mode(mode)) {
53
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
54
        if (vg_exit())
55
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
56
        return 1;
57
    };
58

    
59
    tickdelay(micros_to_ticks(delay*1e6));
60

    
61
    if (vg_exit()) {
62
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
63
        if (free_memory())
64
            printf("%s: lm_free failed\n", __func__);
65
        return 1;
66
    }
67

    
68
    if (free_memory()) {
69
        printf("%s: lm_free failed\n", __func__);
70
        return 1;
71
    }
72

    
73
    return 0;
74
}
75

    
76
// lcom_run lab5 "rectangle 105 100 100 100 100 1"
77
// lcom_run lab5 "rectangle 115 100 100 100 100 FF0000"
78
int(video_test_rectangle)(uint16_t mode, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t color) {
79
    int r;
80

    
81
    if (vbe_get_mode_information(mode)) {
82
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
83
        if (vg_exit())
84
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
85
        return 1;
86
    }
87

    
88
    map_vram(); // if function fails it aborts program
89

    
90
    if (set_graphics_mode(mode)) {
91
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
92
        if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
93
        return 1;
94
    };
95

    
96
    if (draw_rectangle(x, y, width, height, color)) {
97
        if (vg_exit()) {
98
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
99
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
100
        }
101
        return 1;
102
    }
103

    
104
    /// loop stuff
105
    int ipc_status;
106
    message msg;
107
    /// Keyboard interrupt handling
108
    uint8_t kbc_irq_bit = KBC_IRQ;
109
    int kbc_id = 0;
110
    int kbc_irq = BIT(kbc_irq_bit);
111
    if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
112
        if (vg_exit()) {
113
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
114
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
115
        }
116
        return 1;
117
    }
118
    /// cycle
119
    int good = 1;
120
    while (good) {
121
        /* Get a request message. */
122
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
123
            printf("driver_receive failed with %d", r);
124
            continue;
125
        }
126
        if (is_ipc_notify(ipc_status)) { /* received notification */
127
            switch (_ENDPOINT_P(msg.m_source)) {
128
                case HARDWARE: /* hardware interrupt notification */
129
                    if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */
130
                        kbc_ih();
131
                        if (scancode[0] == ESC_BREAK_CODE) good = 0;
132
                    }
133
                    break;
134
                default:
135
                    break; /* no other notifications expected: do nothing */
136
            }
137
        } else { /* received standart message, not a notification */
138
            /* no standart message expected: do nothing */
139
        }
140
    }
141

    
142
    if (unsubscribe_interrupt(&kbc_id)) {
143
        if (vg_exit()) {
144
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
145
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
146
        }
147
        return 1;
148
    };
149

    
150
    if (vg_exit()) {
151
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
152
        if (free_memory()) printf("%s: lm_free failed\n", __func__);
153
        return 1;
154
    }
155

    
156
    if (free_memory()) {
157
        printf("%s: lm_free failed\n", __func__);
158
        return 1;
159
    }
160

    
161
    return 0;
162
}
163

    
164
int(video_test_pattern)(uint16_t mode, uint8_t no_rectangles, uint32_t first, uint8_t step) {
165
    int r;
166

    
167
    if (vbe_get_mode_information(mode)) {
168
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
169
        if (vg_exit())
170
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
171
        return 1;
172
    }
173

    
174
    map_vram(); // if function fails it aborts program
175

    
176
    if (set_graphics_mode(mode)) {
177
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
178
        if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
179
        return 1;
180
    };
181

    
182
    uint16_t W = get_XRes()/no_rectangles;
183
    uint16_t H = get_YRes()/no_rectangles;
184

    
185
    uint32_t color, R, G, B;
186
    for(uint8_t row = 0; row < no_rectangles; ++row){
187
        for(uint8_t col = 0; col < no_rectangles; ++col){
188
            if(get_bytes_pixel() == 1){
189
                color = (first + (row * no_rectangles + col) * step) % (1 << get_bits_pixel());
190
            }else{
191
                R = (GET_RED(first) + col*step) % (1 << get_RedMaskSize());
192
                G = (GET_GRE(first) + row*step) % (1 << get_GreenMaskSize());
193
                B = (GET_BLU(first) + (col+row)*step) % (1 << get_BlueMaskSize());
194
                color = SET_COLOR(R,G,B);
195
            }
196
            if (draw_rectangle(col*W,row*H,W,H,color)) {
197
                if (vg_exit()) {
198
                    printf("%s: vg_exit failed to exit to text mode.\n", __func__);
199
                    if (free_memory()) printf("%s: lm_free failed\n", __func__);
200
                }
201
                return 1;
202
            }
203
        }
204
    }
205
    /// loop stuff
206
    int ipc_status;
207
    message msg;
208
    /// Keyboard interrupt handling
209
    uint8_t kbc_irq_bit = KBC_IRQ;
210
    int kbc_id = 0;
211
    int kbc_irq = BIT(kbc_irq_bit);
212
    if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
213
        if (vg_exit()) {
214
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
215
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
216
        }
217
        return 1;
218
    }
219
    /// cycle
220
    int good = 1;
221
    while (good) {
222
        /* Get a request message. */
223
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
224
            printf("driver_receive failed with %d", r);
225
            continue;
226
        }
227
        if (is_ipc_notify(ipc_status)) { /* received notification */
228
            switch (_ENDPOINT_P(msg.m_source)) {
229
                case HARDWARE: /* hardware interrupt notification */
230
                    if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */
231
                        kbc_ih();
232
                        if (scancode[0] == ESC_BREAK_CODE) good = 0;
233
                    }
234
                    break;
235
                default:
236
                    break; /* no other notifications expected: do nothing */
237
            }
238
        } else { /* received standart message, not a notification */
239
            /* no standart message expected: do nothing */
240
        }
241
    }
242

    
243
    if (unsubscribe_interrupt(&kbc_id)) {
244
        if (vg_exit()) {
245
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
246
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
247
        }
248
        return 1;
249
    };
250

    
251
    if (vg_exit()) {
252
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
253
        if (free_memory()) printf("%s: lm_free failed\n", __func__);
254
        return 1;
255
    }
256

    
257
    if (free_memory()) {
258
        printf("%s: lm_free failed\n", __func__);
259
        return 1;
260
    }
261

    
262
    return 0;
263
}
264

    
265
int(video_test_xpm)(xpm_map_t xpm, uint16_t x, uint16_t y) {
266
    int r;
267

    
268
    if (vbe_get_mode_information(INDEXED_1024_768)) {
269
        printf("%s: failed to get information for mode %x.\n", __func__, INDEXED_1024_768);
270
        if (vg_exit())
271
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
272
        return 1;
273
    }
274

    
275
    map_vram(); // if function fails it aborts program
276

    
277
    if (set_graphics_mode(INDEXED_1024_768)) {
278
        printf("%s: failed to set graphic mode %x.\n", __func__, INDEXED_1024_768);
279
        if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
280
        return 1;
281
    };
282

    
283
    sprite_t *sp = sprite_ctor(xpm);
284
    sprite_set_pos(sp, x, y);
285
    sprite_draw(sp);
286

    
287
    /// loop stuff
288
    int ipc_status;
289
    message msg;
290
    /// Keyboard interrupt handling
291
    uint8_t kbc_irq_bit = KBC_IRQ;
292
    int kbc_id = 0;
293
    int kbc_irq = BIT(kbc_irq_bit);
294
    if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
295
        if (vg_exit()) {
296
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
297
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
298
        }
299
        return 1;
300
    }
301
    /// cycle
302
    int good = 1;
303
    while (good) {
304
        /* Get a request message. */
305
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
306
            printf("driver_receive failed with %d", r);
307
            continue;
308
        }
309
        if (is_ipc_notify(ipc_status)) { /* received notification */
310
            switch (_ENDPOINT_P(msg.m_source)) {
311
                case HARDWARE: /* hardware interrupt notification */
312
                    if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */
313
                        kbc_ih();
314
                        if (scancode[0] == ESC_BREAK_CODE) good = 0;
315
                    }
316
                    break;
317
                default:
318
                    break; /* no other notifications expected: do nothing */
319
            }
320
        } else { /* received standart message, not a notification */
321
            /* no standart message expected: do nothing */
322
        }
323
    }
324

    
325
    if (unsubscribe_interrupt(&kbc_id)) {
326
        if (vg_exit()) {
327
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
328
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
329
        }
330
        return 1;
331
    };
332

    
333
    if (vg_exit()) {
334
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
335
        if (free_memory()) printf("%s: lm_free failed\n", __func__);
336
        return 1;
337
    }
338

    
339
    if (free_memory()) {
340
        printf("%s: lm_free failed\n", __func__);
341
        return 1;
342
    }
343

    
344
    return 0;
345
}
346

    
347
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) {
348

    
349
    return 1;
350
}
351

    
352
int(video_test_controller)() {
353

    
354

    
355

    
356
    return 1;
357
}