/*
 * version : 0.0.1
 */

#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>


#include "plcm_ioctl.h"


int main(void)
{
	struct plcm_io plcm;
	int error, fd;
	int i = 0;
	unsigned char LCM_Message1[40] = "Lanner@Taiwan";
	unsigned char Ret_Message1[40] = "";
	unsigned char LCM_Message2[40] = "2010-05-25";
	unsigned char Ret_Message2[40] = "";
	unsigned char LCM_Message3[40] = "";
	unsigned char Keypad_Value = 0, Pre_Value = 0, Counter = 0;
	unsigned char Keypad_Message[40] = "";
	unsigned char Up[]    = "Up   ";
	unsigned char Down[]  = "Down ";
	unsigned char Left[]  = "Left ";
	unsigned char Right[] = "Right"; 
	int MaxTestTime = 15 * 10;
	for(i = 0; i < 5; i++)
	{
		LCM_Message3[i*8 + 0] = 0;
                LCM_Message3[i*8 + 1] = 1;
                LCM_Message3[i*8 + 2] = 2;
                LCM_Message3[i*8 + 3] = 3;
                LCM_Message3[i*8 + 4] = 4;
                LCM_Message3[i*8 + 5] = 5;
                LCM_Message3[i*8 + 6] = 6;
                LCM_Message3[i*8 + 7] = 7;
	}
	
	printf("Lanner Parallel LCM Test Program\n");
	fd = open("/dev/plcm", O_WRONLY);
	if (fd == -1)
	{
		err(1, "open");
	}
	printf("  PLCM_IOCTL_STOP_THREAD Test \n");
	plcm.value=0;
	error=ioctl(fd, PLCM_IOCTL_STOP_THREAD, &plcm);
	if (error == -1)
			err(1, "ioctl");
	sleep(2);
	printf("  Backlight Control - Off\n");
	plcm.value=1;
	error=ioctl(fd, PLCM_IOCTL_BACKLIGHT, &plcm);
	if (error == -1)
			err(1, "ioctl");
	sleep(2);
	printf("  Backlight Control - On\n");
	plcm.value=0;
	error=ioctl(fd, PLCM_IOCTL_BACKLIGHT, &plcm);
	if (error == -1)
			err(1, "ioctl");
	sleep(2);
	printf("  Display Control - Display Off\n"); 
	plcm.value=0;
	error=ioctl(fd, PLCM_IOCTL_DISPLAY_D, &plcm);
	if (error == -1)
			err(1, "ioctl");
	sleep(2);
	printf("  Display Control - Display On\n"); 
	plcm.value=1;
	error=ioctl(fd, PLCM_IOCTL_DISPLAY_D, &plcm);
	if (error == -1)
			err(1, "ioctl");
	sleep(2);
	printf("  Display Control - Cursor Off\n"); 
	plcm.value=0;
	error=ioctl(fd, PLCM_IOCTL_DISPLAY_C, &plcm);
	if (error == -1)
			err(1, "ioctl");
	sleep(2);
	printf("  Display Control - Cursor On\n");
	plcm.value=1;
	error=ioctl(fd, PLCM_IOCTL_DISPLAY_C, &plcm);
	if (error == -1)
			err(1, "ioctl");
	sleep(2);
	printf("  Display Control - Blinking Off\n");
	plcm.value=0;
	error=ioctl(fd, PLCM_IOCTL_DISPLAY_B, &plcm);
	if (error == -1)
			err(1, "ioctl");
	sleep(2);
	printf("  Display Control - Blinking On\n"); 
	plcm.value=1;
	ioctl(fd, PLCM_IOCTL_DISPLAY_B, &plcm);
	sleep(2);	
	printf("Write and Read Testing...\n");
	//write
	plcm.value=1;
	ioctl(fd, PLCM_IOCTL_SET_LINE, &plcm);
	strcpy(plcm.msg,LCM_Message1);
	plcm.value=strlen(LCM_Message1);
	error=ioctl(fd,PLCM_IOCTL_WRITE,&plcm);
	if (error == -1)
			err(1, "write");
	printf("  Write: %s\n", LCM_Message1);
	sleep(2);
	//read
	ioctl(fd, PLCM_IOCTL_SET_LINE, &plcm);
	error=ioctl(fd,PLCM_IOCTL_READ,&plcm);
	if (error == -1)
			err(1, "read");
	printf("  Read: %s\n", plcm.msg);
	sleep(2);	
	plcm.value=2;
	ioctl(fd, PLCM_IOCTL_SET_LINE, &plcm);
	//write
	strcpy(plcm.msg,LCM_Message2);
	plcm.value=strlen(LCM_Message2);
	error=ioctl(fd,PLCM_IOCTL_WRITE,&plcm);
	if (error == -1)
			err(1, "write");
	printf("  Write: %s\n", LCM_Message2);
	sleep(2);
	//read
	ioctl(fd, PLCM_IOCTL_SET_LINE, &plcm);
	error=ioctl(fd,PLCM_IOCTL_READ,&plcm);
	printf("  Read: %s\n", plcm.msg);
	sleep(2);
	plcm.value=1;
    ioctl(fd, PLCM_IOCTL_SET_LINE, &plcm);
	printf("CGRAM Test...\n");
	strcpy(plcm.msg,LCM_Message3);
	plcm.value=strlen(LCM_Message3);
	error=ioctl(fd,PLCM_IOCTL_WRITE,&plcm);
	if (error == -1)
			err(1, "write");
	sleep(5);
	printf("Keypad Testing....\n");
	printf("  You only have 15 second to test it.\n");
	printf("  Or press Ctrl+C to exit.\n");
	plcm.value=1;
	ioctl(fd, PLCM_IOCTL_SET_LINE, &plcm);
	do{
		plcm.value=0;
		Keypad_Value = ioctl(fd, PLCM_IOCTL_GET_KEYPAD, &plcm);
		if(Pre_Value != Keypad_Value)
		{
			plcm.value=0;
			ioctl(fd, PLCM_IOCTL_CLEARDISPLAY, &plcm);
			plcm.value=0;
			ioctl(fd, PLCM_IOCTL_RETURNHOME, &plcm);
			Counter = 0;
		}
		else
		{
			Counter++;
		}
		switch(Keypad_Value)
		{
			case 0xC7:
				strcpy(Keypad_Message, Up);
				break;
			case 0xE7:
				strcpy(Keypad_Message, Left);
				break;
			case 0xEF:
				strcpy(Keypad_Message, Right);
				break;
			case 0xCF:
				strcpy(Keypad_Message, Down);
				break;
		}
		if(Counter > 5)
		{
			//write(devfd, Keypad_Message, 40);
			plcm.value=40;
			strcpy(plcm.msg,Keypad_Message);
			error=ioctl(fd,PLCM_IOCTL_WRITE,&plcm);
			if (error == -1)
					err(1, "write");
		}
		Pre_Value = Keypad_Value;
		usleep(100000); // 100 msec
		MaxTestTime--;
		if(MaxTestTime == 0)
		{
			printf("Keypad Testing Finished.\n");
			break;
		}
	}while(1);
	close(fd);

	return 0;
}