
#include "stdafx.h"
#include "MultiZebrasDemo.h"
#include "MultiZebrasDemoDlg.h"
#include "FileOpenCombo.h"
#include "afxdialogex.h"

#include "AppI2cOperation.h"

LPTSTR lpAnsi2lpWChar(LPSTR lpstr);
LPSTR  lpWChar2lpAnsi(LPTSTR lptstr);

#ifndef ZEBRAZ4U3DLLWIN32_EXPORTS
#include "ZebraZ4U3DllWin32.h"
#else
int I2cRawDataRead2(__HZHANDLE__ hZhandle, UCHAR cfgByte, USHORT i2cAddr, UCHAR *pSubAddr, int nReadLen, UCHAR *pReadBuffer);
int I2cRawDataWrite2(__HZHANDLE__ hZhandle, UCHAR cfgByte, USHORT i2cAddr, UCHAR *pWrData, int WrDataLen);
#endif

void ZebraI2cOperationHandler(void *pThis, __HZHANDLE__ hZhandle, int myId, UCHAR cfgByte, USHORT i2cAddr, UCHAR *pSubAddr, int nRdWrLen, UCHAR *pRdWrBuffer)
{
	CMultiZebrasDemoDlg *pMain = (CMultiZebrasDemoDlg *)pThis;
	TCHAR szPrintBuffer[2048];	// larged for show read data

	if (cfgByte & _ReadI2cDevice)
	{
		int nRdBytes;
		switch (nRdBytes = I2cRawDataRead2(hZhandle, cfgByte, i2cAddr, pSubAddr, nRdWrLen, pRdWrBuffer))
		{
		case -1:		// USB can't write
			pMain->UpdateProgressMsg(myId, -1, L"USB can't write");
			break;
		case -2:		// I2c write error
			pMain->UpdateProgressMsg(myId, -1, L"I2c write error");
			break;
		case -3:		// not all i2c data been write
			pMain->UpdateProgressMsg(myId, -1, L"not all i2c data been write");
			break;
		case -4:		// USB can't read
			pMain->UpdateProgressMsg(myId, -1, L"USB can't read");
			break;

		default:
			if (nRdBytes <= 0)
			{
				swprintf(szPrintBuffer, L"to read %d bytes but only %d bytes data has read back", nRdWrLen, nRdBytes);
				nRdWrLen = 0;
			}
			else if (nRdBytes < nRdWrLen)
			{
				swprintf(szPrintBuffer, L"to read %d bytes but only %d bytes data has read back", nRdWrLen, nRdBytes);
			}
			else
			{
				swprintf(szPrintBuffer, L"to read %d bytes and %d bytes data has read back", nRdWrLen, nRdBytes);
			}
			pMain->UpdateProgressMsg(myId, -1, szPrintBuffer);

			// set read data to text editer box
			szPrintBuffer[0] = 0;
			for (int j, i = 0; i < nRdBytes; i++)
			{
				j = wcslen(szPrintBuffer);
				if (i == 0)
					swprintf(szPrintBuffer + j, L"0x%x", pRdWrBuffer[i]);
				else
					swprintf(szPrintBuffer + j, L",0x%x", pRdWrBuffer[i]);
			}
			pMain->SetDlgItemText(IDC_EDT_I2C_DATA, szPrintBuffer);
		}
	}
	else
	{
		switch (I2cRawDataWrite2(hZhandle, cfgByte, i2cAddr, pRdWrBuffer, nRdWrLen))
		{
			case 0:
				swprintf(szPrintBuffer, L"%d bytes data has write", nRdWrLen);
				pMain->UpdateProgressMsg(myId, -1, szPrintBuffer);
				break;
			case -1:		// USB can't write
				pMain->UpdateProgressMsg(myId, -1, L"USB can't write");
				break;
			case -2:		// I2c write error
				pMain->UpdateProgressMsg(myId, -1, L"I2c write error");
				break;
			case -3:		// not all i2c data been write
				pMain->UpdateProgressMsg(myId, -1, L"not all i2c data been write");
				break;
		}
	}
}


int fnDecodeHexStr2Values(char *pHexStr, unsigned char *HexVals, int SizHexVals, int needItems)
{
	int nItems = 0;
	char *ptr = pHexStr;

	if (SizHexVals < needItems) needItems = SizHexVals;

	while (nItems < needItems && strlen(ptr))
	{
		while (*ptr && *ptr <= 0x20) ptr++;
		if (*ptr == 0x0) break;

		if (*ptr == 0x30 && (*(ptr + 1) == 'x') || *(ptr + 1) == 'X')
		{
			// Hex
			*(ptr + 1) = 'x';
			sscanf(ptr, "0x%x", &HexVals[nItems++]);
		}
		else
		{
			// decimal
			sscanf(ptr, "%d", &HexVals[nItems++]);
		}
		// skip to next val
		while (*ptr > 0x20 && *ptr != ',') ptr++;
		if (*ptr == ',') ptr++;
	}

	return nItems;
}