Source file: /~heha/basteln/NC/Mach3Usb/Mach3Usb.zip/MachDevice.cpp

// MachDevice.cpp : Defines the initialization routines for the DLL.
//
// Revision history
// 9 June John Prentice - this version separates the user supplied code into another file or files with functions
//		called as externals. 
//		It should be safe to replace the source of the SDK framework
//		as upgrades are made with no more user effort than providing dummy entries for
//		any new unused functionality and recompiling the plugin.
//		The linker will advise of this need so it should be quite crash-resistant

// =======================================================================================================

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlwapi.h>

#include "MachDevice.h"
#include "Engine.h"
#include "rs274ngc.h"
//#include "TrajectoryControl.h"

// This is a Generalized Device file
// The actual device is implemented in the file with these entries. 
// Conventionally this is MachDevImplementation.cpp

extern void myPostInitControl();		// called when mach fully set up
extern void myUpdate ();			// 10Hz update loop
extern void myCleanUp();                        //Destrucion routine for clenaup.

//System Varibles
DoButton_t	DoButton;     // void DoButton( code )
GetDRO_t	GetDRO;       // Double GetDRO( code )
SetDRO_t	SetDRO;       // void   SetDRO( short code, double value);
GetLED_t	GetLED;       // bool   GetLED( short code );
SetLED_t	SetLED;       //SetLED Fucntion
GetProName_t	GetProName;   // CString GetProName()
Code_t		Code;         // void    Code( "G0X10Y10" );

// High spped thread data block
//bool  KickTimer = false; //this will be a special case variable. See Update loop;
//bool  TimerOn = false;   // this tells us the timer is not yet running. (See Update Loop );

 
char *ProfileName;

// Mach3 defines of External Variables. These var's are directly usable in this dll as they are all 
// instantiated prior to this dll being opened. Most work in Mach can be done by attaching to these variables 
// You cannot, however, call functions in these classes, they are not bound to the plugin. You may only call 
// the functions instantiated in thsi dll. All variables and structures , however, may be used.
// Note the way this example uses various objects.

//TrajectoryControl *MainPlanner; //used for most planner funcitons and program control 
HWND hwndMach;		//used for most framework and configuration calls. 
TrajBuffer *Engine;	//Ring0 memory for printer port control and other device syncronisation
setup *_setup;          //Trajectory planners setup block. Always in effect

// Get the Callbacks to the Mach3 System
//*******************************************************
EXTERN_C __declspec(dllexport) void SetDoButton(DoButton_t pFunc) { DoButton = pFunc; }
EXTERN_C __declspec(dllexport) void SetSetDRO(SetDRO_t pFunc) { SetDRO = pFunc; }
EXTERN_C __declspec(dllexport) void SetGetDRO(GetDRO_t pFunc) { GetDRO = pFunc; }
EXTERN_C __declspec(dllexport) void SetGetLED(GetLED_t pFunc) {GetLED = pFunc;}
EXTERN_C __declspec(dllexport) void SetSetLED(SetLED_t pFunc) { SetLED = pFunc; }
EXTERN_C __declspec(dllexport) void SetCode(Code_t pFunc) { Code = pFunc; }

EXTERN_C IMAGE_DOS_HEADER __ImageBase;

EXTERN_C __declspec(dllexport) PCTSTR SetProName(char*XmlFileName) {          // CString  GetProfName();
 ProfileName=XmlFileName;
 char buf[100];
 wnsprintf(buf,100,"SetProName(\"%s\")\n",XmlFileName);
 OutputDebugString(buf);
 char DllName[100];
/* MSVC6:
 MEMORY_BASIC_INFORMATION mbi;
 VirtualQuery(&SetProName,&mbi,sizeof(mbi));
*/
 GetModuleFileName((HMODULE)&__ImageBase,DllName,sizeof(DllName));
 char *n=PathFindFileName(DllName);
 *PathFindExtension(n)=0;
 static char ret[100];
 wnsprintf(ret,sizeof(ret),"%s Digitizing PlugIn - A.Fenerty Ver 1.0a",n);
 return ret;
// MUSS genau so heißen, sonst verreckt das Plugin irgendwo!
}

// Calls into the DLL ////////////////////////////////////////////
//*****************************************************
//
// Mach3 Calls to the Plugin Follow
//
// Tells Mach3 whether to start the printer driver in Ring 0 or not. Return false to run a different device.
// Also gives access to parameter blocks and varibles form the 4 main classes of Mach3. 

//This is a timer loop set for 25ms to keep latency low. We do need to disable it though
//if the callback loop is not running. 

EXTERN_C __declspec(dllexport) void StopPlug(void)           // bool  GetLED( short code );  
{
//  AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
//used for destrcution of variables prior to exit..  Called as MAch3 shuts down. 
 OutputDebugString("StopPlug\n");
}

EXTERN_C __declspec(dllexport) void DoDwell(double time)           // bool  GetLED( short code );  
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
 OutputDebugString("DoDwell\n");
}

void CALLBACK MYProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime) {
 static int ticks = 0;
  //If the update loop doesnt turn this back on in 20 ticks. ( 1/2 second) then kill the timer, user 
  // has shut us down. 
 ticks++;
/*
 if( ticks > 20 && !KickTimer ) {
  KillTimer( hwnd, idEvent );
  TimerOn = false;
  return;
 }
  //Alternately , lets make sure the user has us selected, if so, keep the timer running
  // in an indefinite loop of 40hz. 
 if (KickTimer) {
  KickTimer = false;
  ticks = 0; //reset the test
  TimerOn = true;
 }
*/
  //We get here at 40hz only if the user has selected us as ON, and the Mach3 programs update loops are runnign correctly
  //Any interruption of Mach3 will operate as a safety watchdog and we wont get here in that case.
 myUpdate();
// called at 40Hz. This is used for Higher Res timing needs. 
// only use as required, it can use alot of system reosurces. 
//not really required in the G100, so Ill leave it blank for now..
}

EXTERN_C __declspec(dllexport) void PostInitControl() {
// AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
//this routine is called after Mach3 has initialised.
// Use it as Init, BUT no usage of the XML files at all here. Only in  Init.
//this routine is handy for changing variables that Mach3 has loaded at startup.
// Usually, Mach3 will permanently save any var's you change here..

// called when mach fully set up so all data can be used but initialization  outcomes not affected

  //In the case of a MotionControl plugin, this rouine is only called to tell us to 
  // take over. We HAVE been selected and are now commanded to do whatever it takes 
  // to OWN the IO and movement of this run.
  // If you wish for example, to zero all IO at this point, your free to do so..

  //First, lets add a message tracking facility to handle message traffic output.
  //Output is in its own class because its important that we do not overrun the G100's
  //buffers or queues. We'll make the tracker object intelligent enough to be able to handle any errors, retransmissions
  // and trackign ack numbers and such..
//First lets open a Debug File. We'll only use it if instructed by Mach3's DebugThisRun selection in config/state
 SetTimer(hwndMach,1,25,MYProc);
 OutputDebugString("PostInitControl\n");
}


//This routine is for setting variosu pointers, and for shutting off the Movement Engines
// for an external device to use Mach3, is necessary. It can interrupt things from occuring.. 

EXTERN_C __declspec(dllexport) bool InitControl(TrajBuffer*oEngine , setup *oSetup , void *oMainPlanner, HWND*oView) {
// AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
 Engine = oEngine;
 _setup = oSetup;
 hwndMach = oView[7];
// MainPlanner = (TrajectoryControl *) oMainPlanner;
// TimerOn = false;
// KickTimer = false; //see update loop
// called during Mach initialisation. You can influence subsequent init by actions here
 OutputDebugString("InitControl\n");
 return false;	//<true> == start the printer port..
}	//  Use this for all devices that need the printer port. For now..thats all of them..

EXTERN_C __declspec(dllexport) void Config() {           // CString  GetProfName();  
// AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
/*
 DevProf = new CXMLProfile(); //start up the Profile class for XML usage. Same as Mach3's. 
	 //XML reading and writing can occur here..
// Called to configure the device
// Has read/write access to Mach XML profile to remember what it needs to.
 delete DevProf;
*/
 MessageBox(0,"Currently no config","Blank Plugin",MB_TASKMODAL);
 OutputDebugString("Config\n");
}


EXTERN_C __declspec(dllexport) void Reset() { // Called when Reset is pressed. 
// AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
	//Called when reset is pressed, at end of actual reset commend in Mach3. 
	//Check the Engine.Estop variable to see if we have reset or not..
 OutputDebugString("Reset\n");
}

EXTERN_C __declspec(dllexport) void JogOn(short axis, short dir, double speed ) { //Called when Reset is pressed. 
	//Called when Jog is commanded. 0 for speed is Jog% jog, otherwise it is a new jog%
// AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
 OutputDebugString("JogOn\n");
}

EXTERN_C __declspec(dllexport) void JogOff( short axis ) { // Called when Reset is pressed. 
	//Called when jog shoudl stop on a particular axis
// AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
 OutputDebugString("JogOff\n");
}

EXTERN_C __declspec(dllexport) void Purge( short flags ) { //Called when Reset is pressed. 
	//Called when jog shoudl stop on a particular axis
// AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
 OutputDebugString("Purge\n");
}


EXTERN_C __declspec(dllexport) void Probe() { // Called when Reset is pressed. 
//Called when jog shoudl stop on a particular axis

//This routine is called by Mach3 when a probe should occur. Relevent positions are stored in 
// the MainPlanner as ProbeX, ProbeY..ect..
 OutputDebugString("Probe\n");
}

EXTERN_C __declspec(dllexport) void Home(short axis) { // Called when Reset is pressed. 
//Called when jog shoudl stop on a particular axis

//This is called by Mach3 when homing is commanded. Relevent axis are in the Engine.Axis status
//word to indicate they should be homed..
 OutputDebugString("Home\n");
}

EXTERN_C __declspec(dllexport) void Update() { // called 10 times a second
    // This is your main update loop. Approx 10hz or so..
	// Since the Timer refresh is too low for ModIO at only 10hz, and we want smooth control at 40hz or so, 
	// we will use this loop only to shut down the main timing loop if the user disables this plugin. 
	// If the plugin gets enabled, the timer proceedure is kicked into life at 25ms update, or about 40hz.
// AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
// KickTimer = true;
//	if (!TimerOn) SetTimer(NULL,1,25,MYProc);
// myUpdate ();
}   // Update
Detected encoding: ANSI (CP1252)4
Wrong umlauts? - Assume file is ANSI (CP1252) encoded