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_rtc/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 
     11 #include "megaduck_rtc.h"
     12 
     13 static void use_rtc_data(duck_rtc_data_t * p_rtc);
     14 static void send_some_rtc_data(duck_rtc_data_t * p_rtc);
     15 
     16 duck_rtc_data_t rtc;
     17 
     18 
     19 // Example of displaying RTC date info
     20 static void use_rtc_data(duck_rtc_data_t * p_rtc) {
     21 
     22     const char * ampm_str[] = {"am", "pm"};
     23     const char * dow_str[]  = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
     24 
     25     gotoxy(0,6);
     26 
     27     printf("Year:  %d  \n"
     28            "Month: %d  \n"
     29            "Day:   %d  \n"
     30            "DoW:   %s  \n"
     31            "Time:  %d:%d:%d %s   \n",
     32         (uint16_t)p_rtc->year,
     33         (uint16_t)p_rtc->mon,
     34         (uint16_t)p_rtc->day,
     35         (uint16_t)dow_str[p_rtc->weekday],
     36 
     37         (uint16_t)p_rtc->hour,
     38         (uint16_t)p_rtc->min,
     39         (uint16_t)p_rtc->sec,
     40         ampm_str[p_rtc->ampm] );
     41 }
     42 
     43 
     44 // This date and time used is just for example, they could be different values
     45 static void send_some_rtc_data(duck_rtc_data_t * p_rtc) {
     46 
     47     // For this example set the first power-on defaults
     48     // to those used by the Spanish model laptop
     49     p_rtc->year    = 1993u;
     50     p_rtc->mon     = 6u; // June
     51     p_rtc->day     = 1u; // 1st
     52     p_rtc->weekday = 2u; // Tuesday
     53 
     54     p_rtc->ampm    = 0u; // AM
     55     p_rtc->hour    = 0u;
     56     p_rtc->min     = 0u;
     57     p_rtc->sec     = 0u;
     58 
     59     gotoxy(0,12);
     60 
     61     if (duck_io_set_rtc(p_rtc)) {
     62         printf("Send RTC: Success");
     63     }
     64     else {
     65         printf("Send RTC: Failed!");
     66     }
     67 }
     68 
     69 
     70 void main(void) {
     71 
     72     uint8_t gamepad;
     73 
     74     bool megaduck_laptop_init_ok = duck_io_laptop_init();
     75 
     76     SPRITES_8x8;
     77     SHOW_SPRITES;
     78     SHOW_BKG;
     79     printf("Initializing..\n");
     80 
     81 	if (!megaduck_laptop_init_ok) {
     82         // If laptop hardware is not present then there isn't anything
     83         // for this program to do, so just idle in a loop
     84 	    printf("Laptop not detected\n"
     85                "or Failed to Initialize");
     86         while(1) {
     87             vsync();
     88         }
     89 	}
     90 
     91     // Otherwise laptop init succeeded
     92     printf("Laptop Detected!\n");
     93 
     94     printf("\n*SELECT to Send RTC\n Sys rom defaults");
     95 
     96 	while(1) {
     97 	    vsync();
     98         gamepad = joypad();
     99 
    100 	    // Laptop serial command intervals below 20 msec may cause laptop hardware lockup
    101 	    // Poll for RTC every other frame (~32 msec)
    102 	    if (sys_time & 0x01u) {
    103 
    104             // Send RTC data to device if SELECT is pressed
    105             // otherwise Read RTC data
    106             if (gamepad & J_SELECT) {
    107                 send_some_rtc_data(&rtc);
    108 
    109                 // Wait until SELECT is released before continuing
    110                 waitpadup();
    111             }
    112             else {
    113 	            if (duck_io_get_rtc(&rtc)) {
    114 	                use_rtc_data(&rtc);
    115                 }
    116 	        }
    117 	    }
    118 	}
    119 }

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