Personal tools
Document Actions

Visual Laser cpp

by aullmj — last modified 2009-02-10 11:51

main source code for visual laser, also requires laser code from Tutorials > lesson 8

Click here to get the file

Size 4.9 kB - File type text/x-c++src

File contents

#include <windows.h>
#include <iostream>
#include <fstream>
using namespace std;
#include <time.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include "resource.h"
#include "sicklms2.h"
//-----------------------------------------------------------------------------
// Function-prototypes
//-----------------------------------------------------------------------------
INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
//long WINAPI ThreadFunction(double lParam[]);
void OnTimer(void);
void RePaint(HWND hDlg);

fstream out;
int param=0;
int doPaint=1;
char str[256];

SickLMS lms1;
time_t Now=1000,Then=0,PThen=0;
//HANDLE hThread;
//double data[2];

/*struct test{
	int scandata[180];
	void readData(void){for (int ii=1;ii<180;scandata[ii]=(3*(ii+param))%256,ii++);}
};
test lms1;*/

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point for the application.  Since we use a simple dialog for 
//       user interaction we don't need to pump messages.
//-----------------------------------------------------------------------------
int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, int )
{
			out.open("lms.txt",ios::out);
			out <<0;//write file header
			for (int ii=0;ii<180;out<<'\t'<<ii,ii++);
			out<<endl;
    // Display the main dialog box.
    DialogBox( hInst, MAKEINTRESOURCE( IDD_DIALOG1 ), NULL, MainDlgProc );
    return 0;
}

//-----------------------------------------------------------------------------
// Name: MainDialogProc
// Desc: Handles dialog messages
//-----------------------------------------------------------------------------
INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
    UNREFERENCED_PARAMETER( lParam );

	Now=clock();
	if (Now-Then>=300)
	{//run laser
		Then=Now;
		OnTimer();
		InvalidateRect (hDlg, NULL, TRUE);

		UpdateWindow(hDlg);
		//SendMessage(hDlg,WM_PAINT,0,0);
		//UpdateWindow(GetDlgItem(hDlg,IDC_IMAGE1));
	}
    switch( msg )
    {
        case WM_INITDIALOG:
            // Set a timer to go off 30 times a second. At every timer message
            // the input device will be read
            //SetTimer( hDlg, 1, 1000 , NULL );
            return TRUE;

/*		case WM_TIMER:
			// Update the input device every timer message
			OnTimer( );
			return TRUE;*/

		case WM_PAINT:
			RePaint(hDlg);
			return true;

        case WM_COMMAND:
            switch( LOWORD( wParam ) )
            {
                case IDC_BUTTON1:
                    MessageBoxA(hDlg,"world","hello",MB_OK);
                    return TRUE;
                case IDCANCEL:
                    EndDialog( hDlg, 0 );
                    return TRUE;
                case IDOK:
                    EndDialog( hDlg, 0 );
                    return TRUE;
            }

        case WM_DESTROY:
            // Cleanup everything
			out.close();
            KillTimer( hDlg, 0 );
            return TRUE;
    }

    return FALSE; // Message not handled 
}

void OnTimer(void)
{
	lms1.readData();
	out <<Now;//write data
	for (int ii=0;ii<180;out<<'\t'<<lms1.scandata[ii],ii++);
	out<<endl;
	param+=10;
}
void RePaint(HWND hDlg)
{
	PAINTSTRUCT ps;
	RECT rect;
	//Now=clock();
	if (doPaint&&Now-PThen>=100)
	{
		//write numbers
		sprintf(str,"%d",Now-Then);
		SetWindowTextA(GetDlgItem(hDlg,IDC_TIME),str);
		sprintf(str,"%d",param);
		SetWindowTextA(GetDlgItem(hDlg,IDC_PARAM),str);
		doPaint=0;//don't run again
		PThen=Now;

		//startup
		HWND hImg=GetDlgItem(hDlg,IDC_IMAGE1);
		HDC hDC= BeginPaint(hImg, &ps);
		HPEN hPen=CreatePen(PS_SOLID, 1, RGB(0,255,0));
		HBRUSH hBrush= CreateSolidBrush(RGB(255, 255, 255));
		GetClientRect(hImg, &rect);

		//draw
		FillRect(hDC, &rect, hBrush);
		SelectObject(hDC, hPen);

		int x=lms1.scandata[0],y=0;
		for (int ii=1;ii<180;ii++)
		{
			//MoveToEx(hDC, ii-1, lms1.scandata[ii-1]/10, NULL);
			MoveToEx(hDC, x, y, NULL);
			x=lms1.scandata[ii-1]/50*cos(ii*M_PI/180)+100;
			y=250-lms1.scandata[ii-1]/50*sin(ii*M_PI/180);
			LineTo(hDC, x, y);
			//LineTo(hDC, ii, lms1.scandata[ii]/10);
		}

		//cleanup
		DeleteObject(hPen);
		DeleteObject(hBrush);
		EndPaint(hImg, &ps);

		doPaint=1;//done
	}
}

/*data[0]=1;
unsigned long dwID; 
hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunction,LPVOID(&data),0,&dwID);*/

/*data[0]=0;
WaitForSingleObject(hThread,1000);
CloseHandle(hThread);*/
/*
long WINAPI ThreadFunction(double lParam[])
{
	fstream out("lms.txt",ios::out);
	out <<0;//write file header
	for (int ii=0;ii<180;out<<'\t'<<ii,ii++);
	out<<endl;
	while (lParam[0])
	{
		Then=Now;
		Now=time(NULL);
		lms1.readData();
		out <<Now;//write data
		for (int ii=0;ii<180;out<<'\t'<<lms1.scandata[ii],ii++);
		out<<endl;
		Sleep(300);
	}
	return 0;
} */