void Touch_Tests(void)
{
	int firstx ;
	int firsty ;
	int x ;
	int y ;
	int state ;
	gfx_Cls() ;
	putstr("Touch Tests\n") ;
	printf("Touch Tests.\n") ;
	putstr("Please ensure Touch is only\ndetected in the Blue area") ;
	printf("Detecting touch in Region\n") ;
	touch_Set(TOUCH_ENABLE) ;
	touch_DetectRegion(100,100, 200, 200) ;
	gfx_RectangleFilled(100,100, 200, 200, BLUE) ;
	do {} while (touch_Get(TOUCH_STATUS) != TOUCH_PRESSED);
	touch_Set(TOUCH_REGIONDEFAULT) ;
	gfx_Cls() ;
	putstr("Draw.. Drawing stops\nwhen touch released\n") ;
	printf("Drawing\n") ;

	while(touch_Get(TOUCH_STATUS) != TOUCH_PRESSED)
	{      // we"ll wait for a touch
	}
	firstx = touch_Get(TOUCH_GETX);                          // so we can get the first point
	firsty = touch_Get(TOUCH_GETY);
	while(state != TOUCH_RELEASED)
	{
		state = touch_Get(TOUCH_STATUS);                       // look for any touch activity
		x = touch_Get(TOUCH_GETX);                             // grab the x
		y = touch_Get(TOUCH_GETY);                             // and the y coordinates of the touch
		if (state == TOUCH_PRESSED)                               // if there"s a press
		{
			firstx = x;
			firsty = y;
		}

		if (state == TOUCH_MOVING)                                // if there"s movement
		{
			gfx_Line(firstx, firsty, x, y, BLUE);                 // but lines are much better
			firstx = x;
			firsty = y;
		}
	}
	putstr("Done!\n") ;
	touch_Set(TOUCH_DISABLE) ;
}
void IMG_Tests(void)
{
	WORD handle ;
	int i ;
	int j ;
	int k ;
	gfx_Cls() ;
	txt_MoveCursor(0, 5) ;
	putstr("IMG Tests") ;
	printf("IMG Tests") ;
	handle = file_LoadImageControl("gfx2demo.dat", "gfx2demo.gci", 1) ;
	printf("%d\n", handle) ;
	for (i = 0; i <= 4; i++)  // 4 is "default", same as no dark/light ening
	{
		gfx_BevelShadow(i) ;
		img_Darken(handle, 0) ; // bug, darkens atm
		img_Show(handle, 0) ;
		sleep(250) ;
	}
	for (i = 3; i >= 0; i--)
	{
		gfx_BevelShadow(i) ;
		img_Lighten(handle, 0) ;
		img_Show(handle, 0) ;
		sleep(250) ;
	}
	gfx_BevelShadow(3) ; // back to default

	img_SetPosition(handle, 0, 0, 50) ; // move to a different position
	img_Show(handle, 0) ;

	j = img_GetWord(handle, 0, IMAGE_FRAMES) ;
	for (i = 0; i < j; i++)
	{
		img_SetWord(handle, 0, IMAGE_INDEX, i) ;
		img_Show(handle, 0) ;
		sleep(500) ;
	}

	img_Disable(handle, ALL) ;
	j = 0 ;
	k = 0 ;
	for (i = 36; i <= 39; i++)
	{
		img_SetPosition(handle, i, j, k) ; // move to a different position
		if (j == 119)
		{
			k = 149 ;
			j = 0 ;
		}
		else
			j = 119 ;
		img_Enable(handle, i) ;
	}
	img_Show(handle,ALL) ;
	//  img_ClearAttributes(handle, index, value) ;
	//  img_SetAttributes(handle, index, value) ;
	if (ftouchtests)
	{
		touch_Set(TOUCH_ENABLE) ;
		printf("Please Touch an Image\n") ;
		i = -1 ;
		do
		{
			j = touch_Get(TOUCH_STATUS) ;
			if (j == TOUCH_PRESSED)
				i = img_Touched(handle, ALL) ;
		} while (i == -1) ;
		printf("You touched Image Index %d\n", i) ;
	}
	mem_Free(handle) ;
}
int main() 
{
    int rc;
    int firstx ;
    int firsty ;
    int x ;
    int y ;
    int state ;
    
    TimeLimit4D = 2000;
    Callback4D = errCallback;
    // abort on detected 4D Serial error
    Error_Abort4D = 1 ;
    comspeed = 9600;
    //WORD *x = 0x001A;
    //char *s = sys_GetModel(&x);
    //x  = &0x001A;
    rc = OpenComm(SERIALPORT, comspeed);
    if (rc != 0)
    {
        printf("Error %d Opening: %s - %s\n", errno, SERIALPORT, strerror(errno));
        exit(EXIT_FAILURE);
    }

	gfx_Cls() ;
	putStr("Touch Tests\n") ;
	printf("Touch Tests.\n") ;
	putStr("Please ensure Touch is only\ndetected in the Blue area") ;
	printf("Detecting touch in Region\n") ;
	touch_Set(TOUCH_ENABLE) ;
	touch_DetectRegion(100,100, 200, 200) ;
	gfx_RectangleFilled(100,100, 200, 200, BLUE) ;
	do {} while (touch_Get(TOUCH_STATUS) != TOUCH_PRESSED);
	touch_Set(TOUCH_REGIONDEFAULT) ;
	gfx_Cls() ;
	putStr("Draw.. Drawing stops\nwhen touch released\n") ;
	printf("Drawing\n") ;

	while(touch_Get(TOUCH_STATUS) != TOUCH_PRESSED)
	{      // we"ll wait for a touch
	}
	firstx = touch_Get(TOUCH_GETX);                          // so we can get the first point
	firsty = touch_Get(TOUCH_GETY);
	while(state != TOUCH_RELEASED)
	{
		state = touch_Get(TOUCH_STATUS);                       // look for any touch activity
		x = touch_Get(TOUCH_GETX);                             // grab the x
		y = touch_Get(TOUCH_GETY);                             // and the y coordinates of the touch
		if (state == TOUCH_PRESSED)                               // if there"s a press
		{
			firstx = x;
			firsty = y;
		}
		if (state == TOUCH_MOVING)                                // if there"s movement
		{
			gfx_Line(firstx, firsty, x, y, BLUE);                 // but lines are much better
			firstx = x;
			firsty = y;
		}
	}
	putStr("Done!\n") ;
	touch_Set(TOUCH_DISABLE);
    	
    return (EXIT_SUCCESS);
}