/*
    Shinobi AICA RTC Sample

    COPYRIGHT (C) SEGA ENTERSRISES,LTD.
    written by SekiM2

    Date:
    1998/4/3

    Discription:
    AICA RTC Sample for SET4
    This sample works to display RTC date and time.
    If you push A button on 1st port control-pad, RTC server is stopped and then displayed date is stopped too.Pushing C Button again, running displayed date again.
    If you push B button on 1st port control-pad, RTC register is set to 1950/01/01 00:00:00.
    In njUserInit(), RTC register is set to now,
    If you push X button on 1st port control-pad, RTC library will finish.
    If you push Y button on 1st port control-pad, RTC library will init again.
*/

#include <Ninja.h>
#if 0
#include <sg_syrtc.h>
#else
#include "sg_syrtc.h"
#include <shinobi.h>
#endif

void njUserInit(void)
{
    sbInitSystem(NJD_RESOLUTION_640x480_NTSCI, NJD_FRAMEBUFFER_MODE_RGB565, 1);
    njInitVertexBuffer(100000, 0, 100000, 0, 0);
    njInitPrint(NULL, 0, 0);
    njSetBackColor(0x00000000, 0x00000000, 0x000000FF);

    syRtcInit();
    {
        SYS_RTC_DATE nowDate;
        Uint32 nowCount;
        nowDate.year   = 1998;
        nowDate.month  = 6;
        nowDate.day    = 17;
        nowDate.hour   = 17;
        nowDate.minute = 30;
        nowDate.second = 25;
        /* to set day of week and age of the moon */
        syRtcDateToCount( &nowDate, &nowCount );
        syRtcCountToDate( nowCount, &nowDate );
        /* set now */
        syRtcSetDate( &nowDate );
    }
}

void usrDisplayDate( Sint32 x, Sint32 y, SYS_RTC_DATE *date )
{
    njPrintD( NJM_LOCATION(x,y), date->year, 4 );
    x+=4;
    njPrintC( NJM_LOCATION(x++,y), "/" );
    njPrintD( NJM_LOCATION(x,y), date->month, 2 );
    x+=2;
    njPrintC( NJM_LOCATION(x++,y), "/" );
    njPrintD( NJM_LOCATION(x,y), date->day, 2 );
    x+=2;
    njPrintC( NJM_LOCATION(x++,y), " " );
    njPrintD( NJM_LOCATION(x,y), date->hour, 2);
    x+=2;
    njPrintC(NJM_LOCATION(x++,y), ":");
    njPrintD( NJM_LOCATION(x,y), date->minute, 2);
    x+=2;
    njPrintC(NJM_LOCATION(x++,y), ":");
    njPrintD( NJM_LOCATION(x,y), date->second, 2);
}

extern volatile Uint32 sygRtcCount;

Sint32 njUserMain(void)
{
    Sint32 y=0,x = 0;
    NJS_PERIPHERAL *pad;
    static Sint32 i;
    njPrintC(NJM_LOCATION(10,10), "RTC TEST");
    njPrintH(NJM_LOCATION(10,20),i++, 8);
    njPrintPeripheralInfo(NJM_LOCATION(40, 20), NJD_PORT_A0);
    pad = njGetPeripheral( NJD_PORT_A0 );
    switch( pad->press ) {
#if 1 /* 19980828 */
    case PDD_DGT_TX:
        syRtcFinish();
        break;
    case PDD_DGT_TY:
        syRtcInit();
        break;
#endif /* 19980828 */
    case 0x04: /* A button pressed */
        if( syRtcGetStat() == SYD_RTC_STAT_ACTIVE ) {
            syRtcSetServerMode( SYD_RTC_STAT_PASSIVE );
        } else {
            syRtcSetServerMode( SYD_RTC_STAT_ACTIVE );
        }
        break;
    case 0x02: /* B button pressed */
        {
            SYS_RTC_DATE setDate;
            Uint32 setCount;
            setCount = 0x11121112;
            syRtcCountToDate( setCount, &setDate );
            syRtcSetDate( &setDate );
        }
    default:
        break;
    }
#if 0
    syRtcExecServer();
#endif
    {
        SYS_RTC_DATE date;
        syRtcGetDate( &date );
        usrDisplayDate( 10, 21, &date );
        njPrintH( NJM_LOCATION(10, 22), sygRtcCount, 8 );
    }
    njPrintH(NJM_LOCATION(10,30),i++, 8);
    return NJD_USER_CONTINUE;
}

void njUserExit(void)
{
    syRtcFinish();
    njExitPrint();
    sbExitSystem();
}
