/*
	MGRTC.C
	
	BL14xx/MicroG or BL15xx/MicroG2

	Sample program to read and write the real-time-clock (RTC) of the MicroG.
*/

void showtime(struct tm thistime);
void loadtime(struct tm *thistime, int hour, int min, int sec,
					int mon, int mday,int year, int wday);
main()
{
struct tm thistime;
struct tm thattime;
int oldsec;

	oldsec = 0;
	tm_rd(&thistime);
	showtime(thistime);

	if(mktime(&thistime)==0L)
	{
		printf("Real Time Clock has not been running.\n");
		printf("Loading new time of 8 a.m. on 4-1-95.\n");
		loadtime(&thistime, 8, 0, 0, 4, 1, 95, 6);
		tm_wr(&thistime);
	}
	
	for(;;)
	{
		hitwd();
		runwatch();
		tm_rd(&thistime);
		if(oldsec != thistime.tm_sec)
		{
			showtime(thistime);
			oldsec = thistime.tm_sec;
		}
	}
}

void showtime(struct tm thistime)
{
	printf("%02d:%02d:%02d  %02d-%02d-%02d-%1d\n",
				thistime.tm_hour,
				thistime.tm_min,
				thistime.tm_sec,
				thistime.tm_mon,
				thistime.tm_mday,
				thistime.tm_year,
				thistime.tm_wday);
}

void loadtime(struct tm *thistime, int hour, int min, int sec,
					int mon, int mday,int year, int wday)
{
	thistime->tm_hour = hour;
	thistime->tm_min = min;
	thistime->tm_sec = sec;
	thistime->tm_mon = mon;
	thistime->tm_mday = mday;
	thistime->tm_year = year;
	thistime->tm_wday = wday;
}