/*
 *  led.c
 *
 *  v 0.0 2021.5.31
 *    Write/Read 16K byte data for LP-9x81(i.MX8).
 */

#include "msw.h"
#include "armplat.h"
 
#define VERSION "1.3(20220614)"

void help(int pac_id)
{
	printf("function : led\n");

	if(pac_id == ALP9X81)
	{
		printf("ALP-9x81 set RUN(0), L1(1), L2(2) display ON/OFF \n");	
		printf("Example :led -l2 -o1\n");
		printf("Set L2 ON\n");
		printf("Example :led -l2 -o0 \n");
		printf("Set L2 OFF\n");
	}
	else if(pac_id == LP9X81)
	{
		printf("LP-9x81 set RUN(0), L1(1), L2(2) display ON/OFF \n");	
		printf("Example :led -l2 -o1\n");
		printf("Set L2 ON\n");
		printf("Example :led -l2 -o0 \n");
		printf("Set L2 OFF\n");
	}
	else 
	{
		printf("LP-2841 set RUN(0), L1(1), L2(2), L3(3) display ON/OFF \n");
		printf("Example :led -l1 -o1\n");
		printf("Set L1 ON\n");
		printf("Example :led -l1 -o0 \n");
		printf("Set L1 OFF\n");
	}
}

int main(int argc,char **argv)
{
	int c;
	int led = -1;
	int on = 0;
	int show_ver = 0;
	int result = 0;
	unsigned int pac_id = 1;

	result = linpac_read_pacid(&pac_id);

	if(result)
	{
		printf("pacid(%d) : LinPAC ID read error!\n",result);
		return FAILURE;
	}

	if (argc < 2)
        {
		help(pac_id);
		return FAILURE;
	}


	while((c=getopt(argc, argv, "hil:o:v")) != -1)
	{
		switch(c)
		{
			case 'h':
				help(pac_id);
				break;

			case 'l':
				led = atoi(optarg);

				if(pac_id == LP9X81 || pac_id == ALP9X81)
				{
					if(led != 0 && led != 1 && led != 2)
					{
						printf("command(l) out of range\n");
						return FAILURE;
					}
				}
				else
				{
					if(led != 0 && led != 1 && led != 2 && led != 3)
					{
						printf("command(l) out of range\n");
						return FAILURE;
					}
				}
				break;

			case 'o':
				on = atoi(optarg);

				if(on != 1 && on != 0)

				{
					printf("command(g) out of range\n");
					return FAILURE;
				}
				break;

			case 'v':
				printf("led version : %s\n", VERSION);
				return NOERROR;

			case '?':
				help(pac_id);
				return FAILURE;

			default:
				help(pac_id);
				return NOERROR;
				
		}
	}

	///printf("led = %d ... on = %d\n", led, on);
        SetLED(led, on);
	return NOERROR;
}

