Project

General

Profile

Statistics
| Revision:

root / lab5 / lab5.c @ 109

History | View | Annotate | Download (6.03 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 3 3 30 30 12 -t 1"
79
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
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
83

    
84
    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
    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
    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
    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

    
159
    if (free_memory()) {
160
        printf("%s: lm_free failed\n", __func__);
161
        return 1;
162
    }
163

    
164
    return 0;
165
}
166

    
167
int(video_test_pattern)(uint16_t mode, uint8_t no_rectangles, uint32_t first, uint8_t step) {
168
  /* 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
}
174

    
175
int(video_test_xpm)(xpm_map_t xpm, uint16_t x, uint16_t y) {
176
    /* To be completed */
177
    printf("%s(%8p, %u, %u): under construction\n", __func__, xpm, x, y);
178

    
179
    return 1;
180
}
181

    
182
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

    
187
    return 1;
188
}
189

    
190
int(video_test_controller)() {
191
    /* To be completed */
192
    printf("%s(): under construction\n", __func__);
193

    
194
    return 1;
195
}