|
| 1 | +#include "fb.h" |
| 2 | +#include "de.h" |
| 3 | +#include "hdmi.h" |
| 4 | +#include "malloc.h" |
| 5 | +#include "printf.h" |
| 6 | +#include "uart.h" |
| 7 | +#include "strings.h" |
| 8 | + |
| 9 | +// module-level variables |
| 10 | +static struct { |
| 11 | + int width; // count of horizontal pixels |
| 12 | + int height; // count of vertical pixels |
| 13 | + int depth; // num bytes per pixel |
| 14 | + void *framebuffer; // address of framebuffer memory |
| 15 | +} module; |
| 16 | + |
| 17 | + |
| 18 | +void fb_init(int width, int height, fb_mode_t mode) { |
| 19 | + module.width = width; |
| 20 | + module.height = height; |
| 21 | + module.depth = 4; // our pixels always 32-bit |
| 22 | + int nbytes = module.width * module.height * module.depth; |
| 23 | + module.framebuffer = malloc(nbytes); |
| 24 | + // what will be contents of newly malloc'ed memory block? |
| 25 | + |
| 26 | + hdmi_resolution_id_t id = hdmi_best_match(width, height); // choose from available screen resolutions |
| 27 | + hdmi_init(id); |
| 28 | + de_init(width, height, hdmi_get_screen_width(), hdmi_get_screen_height()); |
| 29 | + de_set_active_framebuffer(module.framebuffer); |
| 30 | +} |
| 31 | + |
| 32 | +static void clear_char_by_char(void) { |
| 33 | + uint8_t *im = module.framebuffer; |
| 34 | + int total_bytes = module.width * module.height * module.depth; |
| 35 | + // write each byte in the framebuffer |
| 36 | + for (int i = 0; i < total_bytes; i++) { |
| 37 | + *im++ = 0xff; // white |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +static void clear_int_by_int_green(void) { |
| 42 | + unsigned int *im = module.framebuffer; |
| 43 | + int npixels = module.width * module.height; |
| 44 | + for (int i = 0; i < npixels; i++) { |
| 45 | + *im++ = 0xff00ff00; // green |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +static void clear_2d_purple(void) { |
| 50 | + unsigned int (*im)[module.width] = module.framebuffer; |
| 51 | + for (int y = 0; y < module.height; y++) { |
| 52 | + for (int x = 0; x < module.width; x++) { |
| 53 | + im[y][x] = 0xffff00ff; // purple |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +static bool confirm(const char *msg) { |
| 59 | + printf("Next: %s. Hit any key when ready (q to quit): ", msg); |
| 60 | + int ch = uart_getchar(); |
| 61 | + printf("%c\n", ch); |
| 62 | + return ch != 'q'; |
| 63 | +} |
| 64 | + |
| 65 | +void main(void) { |
| 66 | + uart_init(); |
| 67 | + |
| 68 | + fb_init(1600, 900, FB_SINGLEBUFFER); |
| 69 | + |
| 70 | + printf("Screen size %d x %d\n", hdmi_get_screen_width(), hdmi_get_screen_height()); |
| 71 | + printf("Framebuffer size %d x %d\n", module.width, module.height); |
| 72 | + clear_char_by_char(); |
| 73 | + while (1) { |
| 74 | + if (!confirm("clear to white, char by char")) break; |
| 75 | + clear_char_by_char(); |
| 76 | + |
| 77 | + if (!confirm("clear to green, int by int")) break; |
| 78 | + clear_int_by_int_green(); |
| 79 | + |
| 80 | + if (!confirm("clear to purple, 2-D of int")) break; |
| 81 | + clear_2d_purple(); |
| 82 | + } |
| 83 | + printf("Completed %s\n", __FILE__); |
| 84 | +} |
0 commit comments