git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/examples/megaduck/laptop_keyboard/src/main.c

      1 #include <gbdk/platform.h>
      2 #include <gbdk/console.h>
      3 #include <gb/isr.h>
      4 
      5 #include <stdint.h>
      6 #include <stdbool.h>
      7 #include <stdio.h>
      8 
      9 #include <duck/laptop_io.h>
     10 #include <duck/model.h>
     11 
     12 #include "megaduck_keyboard.h"
     13 
     14 uint8_t cursor_x, cursor_y;
     15 bool keyboard_read_ok;
     16 
     17 
     18 duck_keyboard_data_t keydata;
     19 
     20 // A dashed underscore cursor sprite
     21 const uint8_t cursor_tile[16] = {
     22     0b00000000, 0b00000000,
     23     0b00000000, 0b00000000,
     24     0b00000000, 0b00000000,
     25     0b00000000, 0b00000000,
     26     0b00000000, 0b00000000,
     27     0b00000000, 0b00000000,
     28     0b01010101, 0b01010101,
     29     0b10101010, 0b10101010,
     30 };
     31 
     32 #define SPR_CURSOR 0u
     33 
     34 static void update_edit_cursor(int8_t delta_x, int8_t delta_y);
     35 static void use_key_data(char key);
     36 static void gfx_init(void);
     37 
     38 
     39 // Moves sprite based cursor and changes console.h current text position
     40 static void update_edit_cursor(int8_t delta_x, int8_t delta_y) {
     41     gotoxy(posx() + delta_x, posy() + delta_y);
     42     move_sprite(SPR_CURSOR, (posx() * 8) + DEVICE_SPRITE_PX_OFFSET_X, (posy() * 8) + DEVICE_SPRITE_PX_OFFSET_Y);
     43 }
     44 
     45 
     46 // Example of typing and moving a cursor around the screen with arrow keys
     47 static void use_key_data(char key) {
     48 
     49     switch (key) {
     50 
     51         case NO_KEY: break;
     52 
     53         case KEY_ARROW_UP:    update_edit_cursor( 0, -1); break;
     54         case KEY_ARROW_DOWN:  update_edit_cursor( 0,  1); break;
     55         case KEY_ARROW_LEFT:  update_edit_cursor(-1,  0); break;
     56         case KEY_ARROW_RIGHT: update_edit_cursor( 1,  0); break;
     57 
     58         // Clears the screen
     59         case KEY_HELP:
     60             cls();
     61             update_edit_cursor(1, 0);
     62             break;
     63 
     64         case KEY_ENTER:
     65             gotoxy(0, posy() + 1);
     66             update_edit_cursor(1, 0);
     67             break;
     68 
     69         case KEY_BACKSPACE:
     70             gotoxy(posx() - 1, posy());
     71             putchar(' ');
     72             update_edit_cursor(-1, 0);
     73             break;
     74 
     75         // All other keys
     76         default:
     77             putchar(key);
     78             update_edit_cursor(0, 0);
     79             break;
     80     }
     81 }
     82 
     83 
     84 static void gfx_init(void) {
     85 
     86     // Set up sprite cursor
     87     set_sprite_data(0,1,cursor_tile);
     88     set_sprite_tile(SPR_CURSOR,0);
     89     hide_sprite(SPR_CURSOR);
     90 
     91     SPRITES_8x8;
     92     SHOW_SPRITES;
     93     SHOW_BKG;
     94 }
     95 
     96 
     97 void main(void) {
     98 
     99     uint8_t megaduck_model = duck_check_model(); // This must be called before any vram tiles are loaded or cleared
    100     bool megaduck_laptop_init_ok = duck_io_laptop_init();
    101 
    102     gfx_init();
    103     printf("Initializing..\n");
    104 
    105     if (!megaduck_laptop_init_ok) {
    106         // If laptop hardware is not present then there isn't anything
    107         // for this program to do, so just idle in a loop
    108         printf("Laptop not detected\n"
    109                "or Failed to Initialize");
    110         while(1) {
    111             vsync();
    112         }
    113     }
    114 
    115 
    116     // Otherwise laptop init succeeded
    117     printf("Laptop Detected! >%hu<\n", megaduck_model);
    118 
    119     // This may not work in emulators which don't simulate
    120     // the preloaded Laptop System ROM font tiles in VRAM
    121     if (megaduck_model == MEGADUCK_LAPTOP_SPANISH)
    122 		printf("Spanish model\n");
    123     else if (megaduck_model == MEGADUCK_LAPTOP_GERMAN)
    124         printf("German model\n");
    125 
    126     // Put the editing cursor in a starting location
    127     update_edit_cursor(1,1);
    128 
    129 
    130 	while(1) {
    131 	    vsync();
    132 
    133         // Laptop serial command intervals below 20 msec may cause laptop hardware lockup
    134         // Poll for keyboard every other frame (~32 msec)
    135 	    if (sys_time & 0x01u) {
    136 
    137 	        if (duck_io_poll_keyboard(&keydata)) {
    138 	            // Convert from key scancodes to ascii and apply key repeat
    139 	            char current_key = duck_io_process_key_data(&keydata, megaduck_model);
    140 
    141 	            use_key_data(current_key);
    142 	        }
    143 	    }
    144 	}
    145 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.