//	M020 : Locate & print
option unsigned;

define	fStart = _FALSE;
define	fEnd   = _FALSE;

//------------------------------------------------------------------------------//
//	Locate & print sample														//
//																				//
//	Return	:	None															//
//------------------------------------------------------------------------------//
function main()
{
	define	i, t, n;
	define	tm[8];

	_SRand(_CurTime());
	_EnableAutoScroll(_FALSE);						//	No auto scroll
	_BtnOpen(0, "Start");  _BtnEnable(0, _TRUE);	//	Start button display
	_BtnOpen(1, "Stop" );  _BtnEnable(1, _TRUE);	//	Stop  button display

	_SetVramSize(100, 40);

	_Cls();
	_Print("The current date and time is displayed in the following positions.\n");
	_Print("@Line position  = remainder obtained by dividing minute by 30\n");
	_Print("@Digit position = second\n");
	_Print("Set display text color and background color at random.\n");
	_Print("Press the 'Start' button to start the display and exit with the 'Stop' button.\n");

	//	Wait for pressing start / end button
	_Stop();

	//	Clear screen
	_Cls();

	//	Line position indication
	for (i = 0; i < 30; i++) {
		_Locate(0, i);
		_PrintF("%2d:", i);
	}

	//	Display loop
	while (!fEnd) {						//	Loop until end button pressed
		n = _Rand();					//		n = display color (random value)
		tm[] = _GetDateAndTime();		//		Get current date and time

		_Locate(tm[5] + 5, tm[4] % 30);	//		Cursor position setting (digit = second, line = minute% 30)
		_SetTextColor(n % 8);			//		Text drawing color setting
		_SetBackColor((n + 2) % 8);		//		Text background color setting
		_Print($_DateAndTime());		//		Display date and time

		_msWait(1000);					//		One second wait

		_Locate(tm[5] + 5, tm[4] % 30);	//		Clear the date and time display
		_ResetColor();					//		.
		_Print("                   ");	//		.
	}

	_Cls();
	// Button erase (1 second wait so that erase of button 0 can be viewed)
	_BtnClose(0); _msWait(1000);
	_BtnClose(1);
}
//------------------------------------------------------------------------------//
//	Processing when button is pressed											//
//																				//
//	Argument:	BtnNo - Button ID		 										//
//																				//
//	Return	:	None															//
//------------------------------------------------------------------------------//
function _OnButton(BtnNo)
{
	if		(BtnNo == 0) {				//	'Start' button?
		fStart = _TRUE;					//		Notice that the start button is pressed, flag setting
		_BtnEnable(0, _FALSE);			//		Start button disabled
	}
	else if (BtnNo == 1) {				//	'Stop' button?
		_ResetColor();
		fEnd   = _TRUE;					//		Notice that the 'Stop' button is pressed, flag setting
	}
	_Restart();
}
