//------------------------------------------------------------------------------
// Project: HID test software.
// File:    main.cpp
// Purpose: hidLIB test application.
// History: FB, 2001feb07, created.

// Adapted by Nathaniel Brown for CSE477
// 6/10/04
// A write will read hex valued image from "test.txt" and send over USB to FPGA
// A read will read back resized image from FPGA and write to "image.txt"
//------------------------------------------------------------------------------

#include <windows.h>
#include <stdio.h>
#include <conio.h>

#include "hidLIB.hpp"

//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{	unsigned short VID, PID;
	
	fprintf(stdout, "VID: 0x");
	fscanf(stdin, "%x", &VID);
	fprintf(stdout, "PID: 0x");
	fscanf(stdin, "%x", &PID);
	fprintf(stdout, "VID, PID: 0x%04X, 0x%04X\n", VID, PID);

	HANDLE hid= hidOpen(VID, PID);
	if (!hid) 
	{	fprintf(stderr, "device not found\n");
		return -1;
	}

#if 1
	int inreport, outreport;
	fprintf(stdout, "\n");
	hidCaps(hid, &inreport, &outreport);
	fprintf(stdout, "\n");
#endif

	int c; char line[80];
	do
	{	fprintf(stdout, "r, w, x: ");
		scanf("%s", line);
		c= line[0];

		unsigned char buftest[80];
		memset(buftest, 0, sizeof(buftest));

		unsigned char buf0[80];
		unsigned char buf[4];
		unsigned char buf2[4];
		unsigned char buf3[80];
		memset(buf0, 0, sizeof(buf));
		memset(buf, 0, sizeof(buf));
		memset(buf2, 0, sizeof(buf));
		memset(buf3, 0, sizeof(buf));
		switch(c)
		{	case 'r':
			case 'R':
			{	FILE * imageFile;
				imageFile = fopen("image.txt", "wt");
				
				int b0;
				char data0 [] = "00";
				sscanf(data0, "%x", &b0);
				buf0[1]= unsigned char(b0);
				buf0[2]= unsigned char(b0);

				int b3;
				char data3 [] = "FF";
				sscanf(data3, "%x", &b3);
				buf3[1]= unsigned char(b3);
				buf3[2]= unsigned char(b3);

				int b4;
				char data4 [] = "FF";
				sscanf(data4, "%x", &b4);
				buf2[1] = unsigned char(b4);

				bool keepread = true;
				int count = 0;
				int readCount = 0;
				while(keepread) {
					int q= hidRead(hid, buf, inreport);
					readCount++;
					if (q!= inreport) {	
						fprintf(stderr, "\n*** error\n");
					}else{
						if(!(buf2[1] == buf[1])||(readCount == 20)) {
							for (int j= 1; j< inreport; j++)
								fprintf(imageFile, "%02X ", buf[j]);
							fprintf(imageFile, "\n");
							int r= hidWrite(hid, buf3, outreport);
							if (r!= outreport) fprintf(stderr, "*** error\n");
							int r2= hidWrite(hid, buf0, outreport);
							if (r2!= outreport) fprintf(stderr, "*** error\n");
							count++;
							//fprintf(stdout, "%d", readCount);
							//fprintf(stdout, "\n");
							readCount = 0;
						}
						buf2[1] = buf[1];
						if(count == 1201) keepread = false;
					}
				}
				fclose(imageFile);
			}	break;

			case 'w':
			case 'W':
			{	FILE * imageFile;
				imageFile = fopen ("test.txt", "rt");

				int b4;
				char data4 [] = "00";
				sscanf(data4, "%x", &b4);
				buf2[1] = unsigned char(b4);
				buf2[2] = unsigned char(b4);

				while(!feof(imageFile)){
					int a;
					int a1;
					fscanf(imageFile, "%x", &a);
					fscanf(imageFile, "%x", &a1);
					if(a == 0) {
						a = 1;
						a1 = 1;
					}else if (a == 255) {
						a = 254;
						a1 = 254;
					}else if (a  == 34) {
						a = 35;
						a1 = 35;
					}
					buftest[1] = unsigned char(a);
					buftest[2] = unsigned char(a1);

					if(buf2[1] == buftest[1] && buf2[2] == buftest[2]) {
						a = a + 1;
						a1 = a1 + 1;
						if(a == 255) {
							a = 253;
							a1 = 253;
						}else if (a == 34) {
							a = 32;
							a1 = 32;
						}
						buftest[1] = unsigned char(a);
						buftest[2] = unsigned char(a1);

					}
					buf2[1] = buftest[1];
					buf2[2] = buftest[2];

					fprintf(stdout, "hidWrite: ");

					for (int j= 1; j< outreport; j++)
						fprintf(stdout, "%02X ", buftest[j]);
					fprintf(stdout, "\n");

					//int q= hidWrite(hid, buftest, outreport);
					//if (q!= outreport) fprintf(stderr, "*** error\n");
					while (outreport != hidWrite(hid, buftest, outreport));
				}

			}	break;

		}
	} while(c!= 'x' && c!= 'X');

	hidClose(hid);
	return 0;
}

//------------------------------------------------------------------------------
// end of file
