 //
// USBeeEX2App.c : Defines the entry point for the console application.
//
// This console application demonstrates how to use the USBee EX2 interface 
// library routines.
//

#include "stdio.h"
#include "conio.h"
#include <time.h>
#include "usbeeex2.h"
#include "windows.h"
#include <string.h>

#define TX_BYTES   16384
#define RX_BYTES   16384
//#define TX_BYTES 65536
//#define RX_BYTES 65536

// Mode definitions
#define FAST_ONEWAY_DATA		1
#define SLOW_TWOWAY_DATA		0

#define DATA_CHANGES_ON_RISING_EDGE			2
#define DATA_CHANGES_ON_FALLING_EDGE		0
#define DATA_IS_SAMPLED_ON_RISING_EDGE		0
#define DATA_IS_SAMPLED_ON_FALLING_EDGE		2

#define _24MHz		(0 << 2)
#define _12MHz		(1 << 2)
#define _6MHz		(2 << 2)
#define _3MHz		(3 << 2)
#define _1MHz		(4 << 2)


int x = TRUE;

int main(int argc, char* argv[])
{
	unsigned char DataInBuffer[RX_BYTES];
	unsigned char DataOutBuffer[TX_BYTES];
	int ReturnVal;
	unsigned long x;
	unsigned char ch = 0;
	unsigned int PodNumber;		
	char filename[100];/* = "C:\test.txt";*/
	FILE *fp, *fp_read;
	long size,result, length;
	unsigned char buffer[5116];
	char pcCommPort[5]="COM3";
	HANDLE hCom;
	BOOL fSuccess;
	DCB dcb;
    static DWORD iBytesRead = -1;
	char read_buffer[5112];
	//char COM_NUMBER = '4';
		// USBee EX2 I/O Functions
    strcpy(filename, "C:\test.txt");
	printf("Sample USBee EX2 Toolbuilder application in C\n");
	
	PodNumber = 6105;		// Change this to your pod number

	for(x=2;x<TX_BYTES;x++) DataOutBuffer[x]= (char)x;
	
	printf("Initializing the Pod\n");
	ReturnVal = InitializeEX2Pod(PodNumber);
	if (ReturnVal != 1) {
		printf("Failure Initializing the Pod\n");
		getch();
		return 0;
	}

	printf("Setting the Mode to fast mode\n");
	ReturnVal = SetMode(PodNumber, FAST_ONEWAY_DATA | DATA_CHANGES_ON_FALLING_EDGE | _1MHz );	
	printf("Press any key to continue\n");
	getch();
	//ReturnVal = SetMode(PodNumber, 4 );		
#if 1
	if (ReturnVal != 1) {
		printf("Failure setting the mode\n");
		getch();
		return 0;
	}
#endif
	printf("Opening file\n");
	fp = fopen("test.txt","rb");
	printf("file Opened %d\n", fp);

    // obtain file size:
    fseek (fp , 0 , SEEK_END);
    size = ftell (fp);
    rewind (fp);

    printf("size = %d\n", size);
    length = size;

       printf("----");

       result = fread (buffer,1,length,fp);
	

	printf("Sending %d bytes out the pod\n",size);
	SetSignals (PodNumber, 0xFF /* Don't Care */, size, buffer);
	printf(" Data is successfully written to External RAM\n");

	printf("Initialising the UART to read the data from External RAM\n");
   //strcat(pcCommPort,COM_NUMBER);
   hCom = CreateFile( pcCommPort,
                      GENERIC_READ | GENERIC_WRITE,
                      0, // must be opened with exclusive-access
                      NULL, // no security attributes
                      OPEN_EXISTING, // must use OPEN_EXISTING
                      0, // not overlapped I/O
                      NULL // hTemplate must be NULL for comm devices
                     );

   if (hCom == INVALID_HANDLE_VALUE)
   {
     // Handle the error.
       printf ("Cann't open UART port, Please close if the port is open by other application %d.\n", GetLastError());
       return (1);
   }

// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.

   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess)
   {
       // Handle the error.
       printf ("Cann't open UART port, Please close if the port is open by other application %d.\n", GetLastError());
       return (2);
   }

// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.

   dcb.BaudRate = CBR_57600; // set the baud rate
   dcb.ByteSize = 8; // data size, xmit, and rcv
   dcb.Parity = NOPARITY; // no parity bit
   dcb.StopBits = ONESTOPBIT; // one stop bit

   fSuccess = SetCommState(hCom, &dcb);

   if (!fSuccess)
   {
     // Handle the error.
       printf ("Cann't set the UART port Settings %d.\n", GetLastError());
        return (3);
   }

    printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
	ReadFile(hCom, (char *)&read_buffer, size,&iBytesRead,NULL);
    printf ("reading data from eSRAM is DONE\n");

	fp_read = fopen("read_file.txt","w+");
    printf ("Opening file is done %d\n",fp_read);

//	fwrite(read_buffer,2048,4096,fp_read);
	fwrite(read_buffer,1,size,fp_read);
    printf ("writing to file is done\n");

	fclose(fp_read);
	printf("Done\n");
	printf("Press any key to colse the application\n");
	getch();
	return(0);

}
