#include "stdafx.h"
#include "my_functions.h"

#include <wtypes.h>
#include <windef.h>  
#include <setupapi.h>
#include <WinBase.h>

extern "C"{
#include "hidsdi.h"
#include "hidusage.h"
#include "Hidpi.h"
}


//--------------------------HID promenne----------------------------------
	 HIDD_ATTRIBUTES						Attributes;
	 SP_DEVICE_INTERFACE_DATA				devInfoData;
	 bool									LastDevice = FALSE;
	 int									MemberIndex = 0;
	 LONG									Result;	
	 ULONG									Length = 0;
	 PSP_DEVICE_INTERFACE_DETAIL_DATA		detailData = NULL;
	 HANDLE									DeviceHandle=NULL;
	 GUID									HidGuid;
	 HANDLE									hDevInfo;
	 ULONG									Required;
	 bool									MyDeviceDetected = FALSE; 
	 HANDLE									WriteHandle;
	 HANDLE									ReadHandle;
	 
// -------------------------Detekuj zarizeni------------------------------
bool PripojeniZarizeni()
		{
		if (ReadHandle == NULL) CloseHandle(ReadHandle);
		if (WriteHandle == NULL) CloseHandle(WriteHandle);
		MemberIndex=0;
		LastDevice=FALSE;

		HidD_GetHidGuid(&HidGuid);

		hDevInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
		devInfoData.cbSize = sizeof(devInfoData);
		do 
		{
			Result = SetupDiEnumDeviceInterfaces(hDevInfo,0,&HidGuid,MemberIndex,&devInfoData);
			if (Result != 0) 
			{
				Result = SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,NULL,0,&Length,NULL);
				detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
				detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
				Result = SetupDiGetDeviceInterfaceDetail (hDevInfo,&devInfoData,detailData,Length,&Required,NULL);
				DeviceHandle = CreateFile (detailData->DevicePath, 0,FILE_SHARE_READ|FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING, 0, NULL);
				Attributes.Size = sizeof(Attributes);
				Result = HidD_GetAttributes(DeviceHandle,&Attributes);
				MyDeviceDetected = FALSE;
				if (Attributes.VendorID == 0x0483)			//VendorID - STM
				{
					if (Attributes.ProductID == 0x5750)		//ProducktID - STM
					{
						WriteHandle=CreateFile(detailData->DevicePath, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING, NULL, NULL);
						ReadHandle=CreateFile(detailData->DevicePath, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,(LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, NULL, NULL);
						MyDeviceDetected = TRUE;
						return TRUE;
					}
					else CloseHandle(DeviceHandle);
				}
				else CloseHandle(DeviceHandle);
					free(detailData);
			}
			else LastDevice=TRUE;
				MemberIndex = MemberIndex + 1;
		}
		while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE));
		

		if (MyDeviceDetected == FALSE) 
		{
			return FALSE;
		}
		return TRUE;
}


// -------------------------Detekuj pripojeni------------------------------
bool DetekcePripojeni()
		{
		MemberIndex=0;
		LastDevice=FALSE;

		HidD_GetHidGuid(&HidGuid);

		hDevInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
		devInfoData.cbSize = sizeof(devInfoData);
		do 
		{
			Result = SetupDiEnumDeviceInterfaces(hDevInfo,0,&HidGuid,MemberIndex,&devInfoData);
			if (Result != 0) 
			{
				Result = SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,NULL,0,&Length,NULL);
				detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
				detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
				Result = SetupDiGetDeviceInterfaceDetail (hDevInfo,&devInfoData,detailData,Length,&Required,NULL);
				DeviceHandle = CreateFile (detailData->DevicePath, 0,FILE_SHARE_READ|FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING, 0, NULL);
				Attributes.Size = sizeof(Attributes);
				Result = HidD_GetAttributes(DeviceHandle,&Attributes);
				MyDeviceDetected = FALSE;
				if (Attributes.VendorID == 0x0483)		
				{
					if (Attributes.ProductID == 0x5750)	
					{
						MyDeviceDetected = TRUE;
						return TRUE;
					}
					else CloseHandle(DeviceHandle);
				}
				else CloseHandle(DeviceHandle);
					free(detailData);
			}
			else LastDevice=TRUE;
				MemberIndex = MemberIndex + 1;
		}
		while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE));
		

		if (MyDeviceDetected == FALSE) 
		{
			return FALSE;
		}
		return TRUE;

}
//-------------------------------------------------------------------------
//-------------------odeslani 1 BYTE -------------------------------------- 
void PosliData(char sData)
{
	BYTE data[10];
	DWORD cnt;

	data[0] = 0x03;
    data[1] = sData;
	data[2] = sData+1;
	int result = WriteFile(WriteHandle,data,5,&cnt,NULL);
	DWORD res = GetLastError();
};
	
// -------------------------Zapis 29bunek od adresy------------------------
void WriteToMemory(unsigned long AdresaPocatecniBunky,unsigned short *Datas, int delka)
{
	unsigned short temp;
	unsigned short temp2 = 0;
	BYTE data[64];
	DWORD cnt;
	int i;
	int	j= 0;

		data[0] = 0x03;
		data[1] = 0x01;
		data[2] = (char)AdresaPocatecniBunky;
		AdresaPocatecniBunky = (AdresaPocatecniBunky >> 8);
		data[3] = (char)AdresaPocatecniBunky;
		AdresaPocatecniBunky = (AdresaPocatecniBunky >> 8);
		data[4] = (char)AdresaPocatecniBunky;

	
		for(i=0;i<delka;i++)
			{
				temp = Datas[i];//*(Datas +i);
				data[(i*2)+5] = (char)temp;
				temp   = (temp >> 8);
				data[(i*2)+6] = (char)temp;
				
			};
	int result = WriteFile(WriteHandle,data,64,&cnt,NULL);
};
// ------------------------------------------------------------------------
// -------------------- Kontrolní ovládací report -------------------------
void Report01_control(unsigned char subID, bool EnableState)
{
		BYTE data[64];
		DWORD cnt;
		data[0] = 0x01;
		data[1] = subID;
		if(EnableState == true)
		{
			data[2] = 0x01;
		}
		else
		{
			data[2] = 0x00;
		}
		int result = WriteFile(WriteHandle,data,64,&cnt,NULL);

}
// ------------------------------------------------------------------------
// -------------------- Nastavení hodnot citace ---------------------------
void TimerSetting(unsigned short TimerPeriode, unsigned short TimerPrescaler)
{
		BYTE data[64];
		DWORD cnt;
		data[0] = 0x03;
		data[1] = 0x03;
		
		data[2] = (char)TimerPeriode;
		TimerPeriode = (TimerPeriode >> 8);
		data[3] = (char)TimerPeriode;

		data[4] = (char)TimerPrescaler;
		TimerPrescaler = (TimerPrescaler >> 8);
		data[5] = (char)TimerPrescaler;
		int result = WriteFile(WriteHandle,data,64,&cnt,NULL);
}
//-------------------------------------------------------------------------------
//---------------------- Nastavení hranicnich adres generovani ------------------
void StartStopAddressSetting(unsigned long AOUT1_StartAddress,
							unsigned long AOUT1_StopAddress,
							unsigned long AOUT2_StartAddress,
							unsigned long AOUT2_StopAddress,
							unsigned long DOUT_StartAddress,
							unsigned long DOUT_StopAddress)
{
	unsigned short temp2 = 0;
	BYTE data[64];
	DWORD cnt;
	int	j= 0;

		data[0] = 0x03;										// ReportID
		data[1] = 0x04;										// ReportSubID

		data[2] = (char)AOUT1_StartAddress;					// Analog OUT1 starting and stop address
		AOUT1_StartAddress = (AOUT1_StartAddress >> 8);
		data[3] = (char)AOUT1_StartAddress;
		AOUT1_StartAddress = (AOUT1_StartAddress >> 8);
		data[4] = (char)AOUT1_StartAddress;
		
		data[5] = (char)AOUT1_StopAddress;
		AOUT1_StopAddress = (AOUT1_StopAddress >> 8);
		data[6] = (char)AOUT1_StopAddress;
		AOUT1_StopAddress = (AOUT1_StopAddress >> 8);
		data[7] = (char)AOUT1_StopAddress;

		data[8] = (char)AOUT2_StartAddress;					// Analog OUT2 starting and stop address
		AOUT2_StartAddress = (AOUT2_StartAddress >> 8);
		data[9] = (char)AOUT2_StartAddress;
		AOUT2_StartAddress = (AOUT2_StartAddress >> 8);
		data[10] = (char)AOUT2_StartAddress;
		
		data[11] = (char)AOUT2_StopAddress;
		AOUT2_StopAddress = (AOUT2_StopAddress >> 8);
		data[12] = (char)AOUT2_StopAddress;
		AOUT2_StopAddress = (AOUT2_StopAddress >> 8);
		data[13] = (char)AOUT2_StopAddress;

		data[14] = (char)DOUT_StartAddress;					// Digital OUT starting and stop address
		DOUT_StartAddress = (DOUT_StartAddress >> 8);
		data[15] = (char)DOUT_StartAddress;
		DOUT_StartAddress = (DOUT_StartAddress >> 8);
		data[16] = (char)DOUT_StartAddress;
		
		data[17] = (char)DOUT_StopAddress;
		DOUT_StopAddress = (DOUT_StopAddress >> 8);
		data[18] = (char)DOUT_StopAddress;
		DOUT_StopAddress = (DOUT_StopAddress >> 8);
		data[19] = (char)DOUT_StopAddress;

		int result = WriteFile(WriteHandle,data,64,&cnt,NULL);
}
