// Copyright  1999 Production Robots Eng Ltd
//
// PROGRAM: Lc4hs_C.c Test and demonstrate Lc4hs card under windows NT
// using explicit linking

#define APPNAME "LC4HSTEST"
#define ICONNAME "MAINICON"

#include <windows.h>
#include <stdio.h>

#include "resource.h"

// Lc4hs.dll must be on your dll search path at
// runtime. Proper location for lc4hs.dll is C:\Winnt\System32

// this file is actually in ..\include\ see Options|Project|Directories
#include "Lc4hs.h"

// Global Variables:

HINSTANCE hInst;      // current instance
char szAppName[] = APPNAME; // Name of the app

HANDLE  DriverHandle = INVALID_HANDLE_VALUE;
HWND  hWindow;

static void IDInit(void) {
	HWND hStatus = GetDlgItem(hWindow, IDC_STATUS);
	SetWindowText(hStatus, "Initialising...");
	UpdateWindow(hStatus);
	if (Lc4hsInit(DriverHandle)) {
		MessageBox(hWindow, "Initialised OK", "Information", MB_OK | MB_ICONINFORMATION);
	} else {
		MessageBox(hWindow, "Could not initialise", 
			"Error", MB_OK | MB_ICONEXCLAMATION);
	}
	SetWindowText(hStatus, "");
}

static void IDFinish(void) {
	if (Lc4hsFinish(DriverHandle))
		MessageBox(hWindow, "Finished OK", "Information", MB_OK | MB_ICONINFORMATION);
	else 
		MessageBox(hWindow, "Could not Finish", "Error", MB_OK | MB_ICONEXCLAMATION);
}


static void IDRead(void) {
	FLOAT Val[4];
	char MsgBuf[0x100];
	if (Lc4hsGetCurrentWeight4(DriverHandle, Val)) {
		sprintf(MsgBuf, "%.3f %.3f %.3f %.3f", Val[0],Val[1],Val[2],Val[3]);
	} else {
		strcpy(MsgBuf, "read error");
	}
	SetWindowText(GetDlgItem(hWindow, IDC_STATUS), MsgBuf);
}

LRESULT CALLBACK WindowProc( HWND hWindow,
                        UINT iMsg,
						WPARAM wParam,
						LPARAM lParam) {
	switch (iMsg) {
    case WM_COMMAND:
		switch LOWORD(wParam) {
        case IDC_INIT:
 			IDInit();
			return 0;
        case IDC_FINISH: 
			IDFinish();
			return 0;
        case IDC_READ: 
			IDRead();
			return 0;
        case IDCANCEL:
			SendMessage(hWindow, WM_CLOSE, 0, 0);
			return 0;
		}

	case WM_DESTROY: 
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hWindow, iMsg, wParam, lParam);
}

int WINAPI WinMain (
	HINSTANCE hInstance, 
	HINSTANCE hPrevInstance,
	PSTR szCommandLine,
	int iCmdShow) {
	MSG msg;
	WNDCLASSEX wc;
	wc.cbSize = sizeof(wc);
   wc.style = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc = WindowProc;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = DLGWINDOWEXTRA;
   wc.hInstance = hInstance;
   wc.hIcon = LoadIcon(hInstance, ICONNAME);
   wc.hCursor = LoadCursor(0, IDC_ARROW);
   wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
   wc.lpszMenuName = NULL;
   wc.lpszClassName = APPNAME;
   wc.hIconSm = LoadIcon(hInstance, ICONNAME);
   RegisterClassEx(&wc);
   hInst = hInstance;
   hWindow = CreateDialog( hInstance, szAppName, 0, NULL);
   ShowWindow(hWindow, iCmdShow);
	DriverHandle = Lc4hsOpen("4HS1");
	if (DriverHandle == INVALID_HANDLE_VALUE) {
		MessageBox(hWindow, "Could not Open driver",
			"Error", MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}
    while (GetMessage (&msg, NULL, 0, 0)) {
        if( !IsDialogMessage( hWindow, &msg )) {
            TranslateMessage (&msg) ;
            DispatchMessage (&msg) ;
       }
    }
	CloseHandle(DriverHandle);
    return msg.wParam;
}

