// opencv_brunel.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "includes.h"

#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdlib.h>
#include <stdio.h>
#endif

#define NUMBER 100
#define DELAY 20
char wndname[] = "Automotive Data Logger";
char wndname_trackbars[] = "Data Logger User Interface";

CvScalar random_color(CvRNG* rng)
{
    int icolor = cvRandInt(rng);
    return CV_RGB(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
}

//Serial definitions
HANDLE initiate_serial(int argc, char* argv);
void write_read_serial(HANDLE hSerial,unsigned char tx_data[32]);
//-----------Configuration Start----------------
char *serial_com_port;
//-----------Configuration End----------------
void read_configuration(void);

//Global variables

#include "conversion_table.h"


#include "code/global_variables.cpp"
#include "code/software_timer.cpp"
#include "code/ni_initialization.cpp"
#include "code/trackbars.cpp"



int main( int argc, char** argv )
{
	ni_routine();
	#include "code/gui_start.cpp"	
	#include "adc_connections.h"

	//for(int i=0;i<huge_buffer_length;i++)
	//			huge_buffer[i]=NULL;
	//printf("done\n");
	
	
	experiment_name[0]='e';
	experiment_name[1]='x';
	experiment_name[2]='p';
	experiment_name[3]='e';
	experiment_name[4]='r';
	experiment_name[5]='i';
	experiment_name[6]='m';
	experiment_name[7]='e';
	experiment_name[8]='n';
	experiment_name[9]='t';
	experiment_name[10]='s';
	experiment_name[11]='/';

	// Initiliaze buffers
	for(int n=0;n<4;n++){
		front_left_rpm_buf[n]=0;
		front_right_rpm_buf[n]=0;
		rear_left_rpm_buf[n]=0;
		rear_right_rpm_buf[n]=0;
		engine_rpm_buf[n]=0;
		car_speed_buf[n]=0;	
		//temp_buf[n]=0;
	}
	for(int n=0;n<200;n++){
		log_buffer_1[n]=0;
		log_buffer_2[n]=0;
		log_buffer_3[n]=0;
	}
	//----------------Serial port Start------------------------------
	//unsigned char tx_data[32];
	serial_com_port= new char[10];
	read_configuration();
	//HANDLE hSerial;

	if(serial_com_port)
		hSerial=initiate_serial(2, serial_com_port);
	else
		hSerial=initiate_serial(1, NULL);
	//Reconstruct packet stage;
	int char_read=0;
	

	//Data read
	int fl_ticks=0,fr_ticks=0,rl_ticks=0,rr_ticks=0,rpm_ticks=0;
	int fl_rpm=0,fr_rpm=0,rl_rpm=0,rr_rpm=0,rpm=0;

	//unsigned char read_data_buffer[31];
	//bool	serial_packet_arrived=FALSE;	
	//bool	valid_packet=TRUE;
	
	int		bytes_read_till_now=0;
	int k=0;

	int iterations=0;

	// get ticks per second
    QueryPerformanceFrequency(&frequency);
    // start timer
    QueryPerformanceCounter(&t1);
    // stop timer
    QueryPerformanceCounter(&t2);
	QueryPerformanceCounter(&tstart);
	// Set timer callback 
	iTimerID = SetTimer(0, 0, text_sampling_period, timer_proc);

	while(1){
		cvCopy( image2, image, 0 );
		/*
		write_read_serial(hSerial,tx_data);
		if(dwBytesRead>1){
			//printf ("Bytes %d\n",dwBytesRead);
			k=0;
			while(k<dwBytesRead&&serial_packet_arrived==FALSE){
				if(szBuff[k]=='$'){
					bytes_read_till_now=0;
					if(k>1)
						printf("\n\nzeroed\n\n");
				}
				read_data_buffer[bytes_read_till_now]=szBuff[k];
				bytes_read_till_now++;
				k++;
				if(bytes_read_till_now==31){
					serial_packet_arrived=TRUE;
					bytes_read_till_now=0;		//zero counter
					valid_packet=TRUE;			//Check for validation right after
				}
				else 
					if(bytes_read_till_now>31){
						printf("error\n!!!");
						break;
					}
			}	
		}
		if(serial_packet_arrived){//Check for consistency
			#include "code/packet_consistency.cpp"
			if(valid_packet){
				#include "code/update.cpp"
			}
			else
				printf ("Invalid packet\n");
		}
		*/
		//if(serial_packet_arrived&&valid_packet){//Update image only when packet arrived
			#include "code/update_gauges.cpp"
			#include "code/update_inertial.cpp"
			#include "code/states.cpp"
			cvShowImage(wndname,image);
			cvWaitKey(2);	
		//}
		//Consume packet
		serial_packet_arrived=FALSE;

		//iterations+=20;
		//if(iterations>230)
			//iterations=0;
	}
	

	cvWaitKey();

    // Wait for a key stroke; the same function arranges events processing
    cvWaitKey(0);
    cvReleaseImage(&image);
	cvReleaseImage(&image_back);
    cvReleaseImage(&image2);
    cvDestroyWindow(wndname);

    return 0;
}

#ifdef _EiC
main(1,"drawing.c");
#endif

void read_configuration(void){
	using namespace std;
	string line;
	ifstream myfile ("conf/conf.txt");
	const char *buffer;
	char *temp_char=new char[20];
	char * pch;
	char *char_found;
	int i=1;
	if (myfile.is_open())
	{
		while (! myfile.eof() )
		{
			getline (myfile,line);
			buffer=line.data();
			strcpy(temp_char,buffer);
			pch=strrchr(temp_char,' ');// Locate last occurence of character in string
			if(pch!=NULL){
				char_found=&temp_char[(int)(pch-temp_char+1)];
			}
			else{
				printf("error reading file conf/conf.txt\n");
				break;
			}
			switch (i){
				case 1:
					strcpy(serial_com_port,char_found);
				break;
				default:
				break;
			}
			cout << line << endl;
			i++;
		}
		myfile.close();
	}
	i=1;
}






  
