Project

General

Profile

Statistics
| Revision:

root / lab5 / lab5.c @ 109

History | View | Annotate | Download (6.03 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 104 up20180642
///lcom_run lab5 "rectangle 105 3 3 30 30 12 -t 1"
79 102 up20180642
int(video_test_rectangle)(uint16_t mode, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t color) {
80
    int r;
81
    if ((r = get_permissions_first_mbyte()))
82 109 up20180655
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
83 93 up20180655
84 109 up20180655
    if (vbe_get_mode_information(mode)) {
85
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
86
        if (vg_exit())
87
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
88
        return 1;
89
    }
90
91
    map_vram(); // if function fails it aborts program
92
93 102 up20180642
    if (set_graphics_mode(mode)) {
94
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
95
        if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
96
        return 1;
97
    };
98
99 109 up20180655
    if (vg_draw_rectangle(x, y, width, height, color)) {
100
        if (vg_exit()) {
101
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
102
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
103
        }
104
        return 1;
105
    }
106
107
    /// loop stuff
108
    int ipc_status;
109
    message msg;
110
    /// Keyboard interrupt handling
111
    uint8_t kbc_irq_bit = KBC_IRQ;
112
    int kbc_id = 0;
113
    int kbc_irq = BIT(kbc_irq_bit);
114
    if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
115
        if (vg_exit()) {
116
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
117
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
118
        }
119
        return 1;
120
    }
121
    /// cycle
122
    int good = 1;
123
    while (good) {
124
        /* Get a request message. */
125
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
126
            printf("driver_receive failed with %d", r);
127
            continue;
128
        }
129
        if (is_ipc_notify(ipc_status)) { /* received notification */
130
            switch (_ENDPOINT_P(msg.m_source)) {
131
                case HARDWARE: /* hardware interrupt notification */
132
                    if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */
133
                        kbc_ih();
134
                        if (scancode[0] == ESC_BREAK_CODE) good = 0;
135
                    }
136
                    break;
137
                default:
138
                    break; /* no other notifications expected: do nothing */
139
            }
140
        } else { /* received standart message, not a notification */
141
            /* no standart message expected: do nothing */
142
        }
143
    }
144
145
    if (unsubscribe_interrupt(&kbc_id)) {
146
        if (vg_exit()) {
147
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
148
            if (free_memory()) printf("%s: lm_free failed\n", __func__);
149
        }
150
        return 1;
151
    };
152
153 102 up20180642
    if (vg_exit()) {
154
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
155
        if (free_memory()) printf("%s: lm_free failed\n", __func__);
156
        return 1;
157
    }
158 109 up20180655
159
    if (free_memory()) {
160
        printf("%s: lm_free failed\n", __func__);
161
        return 1;
162
    }
163
164 102 up20180642
    return 0;
165 93 up20180655
}
166
167
int(video_test_pattern)(uint16_t mode, uint8_t no_rectangles, uint32_t first, uint8_t step) {
168 103 up20180655
  /* To be completed */
169
  printf("%s(0x%03x, %u, 0x%08x, %d): under construction\n", __func__,
170
         mode, no_rectangles, first, step);
171
172
  return 1;
173 93 up20180655
}
174
175
int(video_test_xpm)(xpm_map_t xpm, uint16_t x, uint16_t y) {
176 102 up20180642
    /* To be completed */
177
    printf("%s(%8p, %u, %u): under construction\n", __func__, xpm, x, y);
178 93 up20180655
179 102 up20180642
    return 1;
180 93 up20180655
}
181
182 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) {
183
    /* To be completed */
184
    printf("%s(%8p, %u, %u, %u, %u, %d, %u): under construction\n",
185
    __func__, xpm, xi, yi, xf, yf, speed, fr_rate);
186 93 up20180655
187 102 up20180642
    return 1;
188 93 up20180655
}
189
190
int(video_test_controller)() {
191 102 up20180642
    /* To be completed */
192
    printf("%s(): under construction\n", __func__);
193 93 up20180655
194 102 up20180642
    return 1;
195 93 up20180655
}