#include <iostream>
#include "windows.h"
#include "stdio.h"
#include "conio.h"
#include "jeti_core.h"
#include "jeti_spectro_ex.h"

using namespace std;

DWORD_PTR dwDevice;
DWORD dwError;
INT32 dwDark[4096];
INT32 dwLight[4096];

int PerformMeasurement(void);

void main(void)
{
	int		ch;
	int		i;
	DWORD	dwNumDevices;
	DWORD	dwDeviceNum;
	char	dwSerial1;
	char	dwSerial2;
	char	dwSerial3;
	system("cls");

	JETI_GetNumDevices(&dwNumDevices);
	printf("\nNUMBER OF DEVICES %d", dwNumDevices);

	if (dwError != 0)
	{
		printf("Could not open device!\nError code 0x%08X\nPress any key to continue...", dwError);
		_getch();
		return;
	}
	
	if (dwNumDevices == 0)
	{
		printf("No matching device found!\nError code 0x%08X\nPress any key to continue...", dwError);
		dwError = JETI_OpenDevice(0, &dwDevice);
		if (dwError != 0)
		{
			printf("Could not open device!\nError code 0x%08X\nPress any key to continue...", dwError);
			_getch();
			return;
		}
		_getch();
		return;
	}
	else
	{
		if (dwNumDevices > 1)			// More than one device found
		{
			printf("More than one device found:\n");
			for (i = 0; i < dwNumDevices; i++)
			{
				JETI_GetSerialDevice(i, &dwSerial1, &dwSerial2, &dwSerial3);
				printf("device number: %d  serial number 1: %08d  serial number 2: %05d\n", i, dwSerial1, dwSerial2);
			}
			printf("Please enter the device number to open: ");
			ch = _getch();
			dwDeviceNum = ch - 48;
			if (dwDeviceNum >= dwNumDevices)
			{
				printf("\n\nInvalid device number!\nPress any key to continue...");
				_getch();
				return;
			}
			dwError = JETI_OpenDevice(dwDeviceNum, &dwDevice);
			if (dwError != 0)
			{
				printf("Could not open device!\nError code 0x%08X\nPress any key to continue...", dwError);
				_getch();
				return;
			}
		}
		else							// One device found
		{
			dwError = JETI_OpenDevice(0, &dwDevice);
			if (dwError != 0)
			{
				printf("Could not open device!\nError code 0x%08X\nPress any key to continue...", dwError);
				_getch();
				return;
			}
		}
	}
	do
	{
		system("cls");
		printf("Please select:\n\n");
		printf("m) perform spectrometer measurement...\n\n");
		printf("0) exit\n\n");
		ch = _getch();
		switch (ch)
		{
		case 'm':
			system("cls");
			// start measurement
			PerformMeasurement();
			_getch();
			break;
		default:
			system("cls");
			break;
		}
	} while (ch != '0');
	JETI_CloseDevice(dwDevice);
	return;
}


int PerformMeasurement(void)
{

	int	    ch;
	BOOL	boStatus;
	FLOAT	dwTint;
	WORD	wAver;


	// Set integration time to 10 msec
	dwTint = 10;
	// Set averaging to 1 (no averaging)
	wAver = 1;


	// Initiate Dark measurement
	dwError = JETI_StartDarkEx(dwDevice, dwTint, wAver);
	if (dwError != 0)
	{
		printf("Could not start dark measurement!\nError code 0x%08X\nPress any key to continue...", dwError);
		return -1;
	}
	printf("Measurement started. Please wait...\n");

	// Check if Dark measurement has finished
	do
	{
		dwError = JETI_MeasureStatusCore(dwDevice, &boStatus);
		if (dwError != 0)
		{
			printf("Could not determine measurement status!\nError code 0x%08X\nPress any key to continue...", dwError);
			return -1;
		}
		printf(".");
		Sleep(100);

	} while (boStatus);
	printf("\n\n");
	if (dwError)
		return -1;

	// Read Dark measurements for all pixels
	dwError = JETI_DarkPixEx(dwDevice, &dwDark[0]);
	if (dwError != 0)
	{
		printf("Could not read dark measurement!\nError code 0x%08X\nPress any key to continue...", dwError);
		return -1;
	}
	printf("%d\n", dwDark[0]);
	printf("%d\n", dwDark[1]);
	printf("%d\n", dwDark[2]);
	printf("%d\n", dwDark[3]);
	printf("Dark measurement done. \n");


	// Initiate Light measurement
	printf("Please turn on light source.\n\n");
	printf("hit any key to continue...\n\n");
	ch = _getch();

	dwError = JETI_StartLightEx(dwDevice, dwTint, wAver);
	if (dwError != 0)
	{
		printf("Could not start light measurement!\nError code 0x%08X\nPress any key to continue...", dwError);
		return -1;
	}
	printf("Measurement started. Please wait...\n");

	// Check if Light measurement has finished
	do
	{
		dwError = JETI_SpectroStatusEx(dwDevice, &boStatus);
		if (dwError != 0)
		{
			printf("Could not determine measurement status!\nError code 0x%08X\nPress any key to continue...", dwError);
			return -1;
		}
		printf(".");
		Sleep(100);

	} while (boStatus);
	printf("\n\n");
	if (dwError)
		return -1;

	// Read Light measurements for all pixels
	dwError = JETI_LightPixEx(dwDevice, &dwLight[0]);
	if (dwError != 0)
	{
		printf("Could not read light measurement!\nError code 0x%08X\nPress any key to continue...", dwError);
		return -1;
	}
	printf("%d\n", dwLight[0]);
	printf("%d\n", dwLight[1]);
	printf("%d\n", dwLight[2]);
	printf("%d\n", dwLight[3]);
	printf("Light measurement done. \n");

	

	return 0;
}
