I'm writing plugin for MACH3 with external generation step
Screen Diagnostic (Alt+F7) and general screen show operate sensor (limit and home x,y,z), but MACH do not reaction to it
//=====================================================================
//
// Plugin.cpp - the optional custom part of the plugin
//
// this source file can be filled with the actual custom code that
// makes the plugin work. it is the choice of the developer to enable
// which functions will be used from the available MachDevice calls.
//
// if this is a mixed mode dll each function can be declared as either
// an unmanaged or managed function.
//
// please see the documentation in Plugin.h for the #define statements
// that control each functions compilation.
//
// if this is a mixed mode dll and you need to keep global managed
// reference please see the MG class in ManagedGlobal.h
//
// please read the notes and comments in MachDevice.cpp for general
// information and disclaimers.
//
//=====================================================================
#include "stdafx.h"
#include "MachDevice.h"
#include "ConfigDialog.h"
#include "PlugInControlDialog.h"
#include "XYZDemoDialog.h"
#include "XMLNetProfile.h"
#include "ManagedGlobal.h"
#include <stdlib.h>
#include "./include/lusb0_usb.h" // this is libusb
#pragma comment(lib,"./lib/msvc/libusb.lib")
//---------------------------------------------------------------------
// the default namespace for managed functions and classes
//---------------------------------------------------------------------
using namespace XYZTutor;
//---------------------------------------------------------------------
// data area
//---------------------------------------------------------------------
unsigned int adr_buf = 4;
unsigned short read_input, count = 0;
int handle;
int menuStart; // the starting menu id
double pr_PosX = 0, pr_PosY = 0, pr_PosZ = 0;
char cusb0;
char cusb1;
char cusb2;
char cusb3;
char cusb4;
char cusb5;
// Enables this example to work with a device running the
// libusb-win32 PIC Benchmark Firmware.
#define BENCHMARK_DEVICE
//////////////////////////////////////////////////////////////////////////////
// TEST SETUP (User configurable)
// Issues a Set configuration request
#define TEST_SET_CONFIGURATION
// Issues a claim interface request
#define TEST_CLAIM_INTERFACE
// Use the libusb-win32 async transfer functions. see
// transfer_bulk_async() below.
#define TEST_ASYNC
// Attempts one bulk read.
#define TEST_BULK_READ
// Attempts one bulk write.
// #define TEST_BULK_WRITE
//////////////////////////////////////////////////////////////////////////////
// DEVICE SETUP (User configurable)
// Device vendor and product id.
#define MY_VID 0x0483
#define MY_PID 0xfff0
// Device configuration and interface id.
#define MY_CONFIG 1
#define MY_INTF 0
// Device endpoint(s)
#define EP_IN 0x81
#define EP_OUT 0x02
// Device of bytes to transfer.
#define BUF_SIZE 64
usb_dev_handle *dev = NULL; /* the device handle */
char buf_tx[BUF_SIZE];
char buf_rx[BUF_SIZE];
int ret, i;
usb_dev_handle *open_dev(void);
static int transfer_bulk_async(usb_dev_handle *dev,
int ep,
char *bytes,
int size,
int timeout);
usb_dev_handle *open_dev(void)
{
struct usb_bus *bus;
struct usb_device *dev;
for (bus = usb_get_busses(); bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
if (dev->descriptor.idVendor == MY_VID
&& dev->descriptor.idProduct == MY_PID)
{
return usb_open(dev);
}
}
}
return NULL;
}
bool piInitControl()
{
menuStart = GetMenuRange(1);
MachView->m_PrinterOn = false;
MainPlanner->ExternalType = EX_VMS;
MainPlanner->ExBufferHi = 60; // Quantity points for 10ms
MainPlanner->ExTime = 0.0018;
for (int i = 0; i < 7; i++ )
MainPlanner->ExternalPulseRates[i] = 100000;
return true;
}
char* piSetProName(LPCSTR name)
{
XYZDemoConfig^ xyzDemoConfig = gcnew XYZDemoConfig;
XMLNetProfile^ profile = gcnew XMLNetProfile(gcnew String(ProfileName), "XYZTutorPlugin", true);
profile->Load();
xyzDemoConfig->enableDlg = profile->ReadBool("Dlg" , false);
xyzDemoConfig->enableX = profile->ReadBool("Axis/X", true);
xyzDemoConfig->enableY = profile->ReadBool("Axis/Y", true);
xyzDemoConfig->enableZ = profile->ReadBool("Axis/Z", true);
MG::xyzDemoConfig = xyzDemoConfig;
return "XYZTutor-Author-v1.0.0.0";
}
void piPostInitControl()
{
// usb_dev_handle *dev = NULL; /* the device handle */
HMENU hMachMenu = GetMenu(MachView->MachFrame->m_hWnd);
HMENU hPluginMenu = 0;
int machMenuCnt = GetMenuItemCount(hMachMenu);
MENUITEMINFO mii;
LPTSTR txt;
for (int i = 0; i < machMenuCnt; i++)
{
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_SUBMENU | MIIM_STRING;
mii.dwTypeData = NULL;
if (GetMenuItemInfo(hMachMenu, i, true, &mii))
{
txt = (LPTSTR) malloc(++mii.cch);
mii.dwTypeData = txt;
if (GetMenuItemInfo(hMachMenu, i, true, &mii))
{
if (strcmp(txt, "PlugIn Control") == 0)
{
hPluginMenu = mii.hSubMenu;
i = machMenuCnt;
}
}
free(txt);
}
if (hPluginMenu)
{
InsertMenu(hPluginMenu, -1, MF_BYPOSITION, menuStart , "XYZTutor");
}
}
XYZDemoDialog^ xyzDemoDialog = gcnew XYZDemoDialog();
xyzDemoDialog->labelX->Visible = MG::xyzDemoConfig->enableX;
xyzDemoDialog->labelY->Visible = MG::xyzDemoConfig->enableY;
xyzDemoDialog->labelZ->Visible = MG::xyzDemoConfig->enableZ;
xyzDemoDialog->textBoxX->Visible = MG::xyzDemoConfig->enableX;
xyzDemoDialog->textBoxY->Visible = MG::xyzDemoConfig->enableY;
xyzDemoDialog->textBoxZ->Visible = MG::xyzDemoConfig->enableZ;
xyzDemoDialog->Visible = MG::xyzDemoConfig->enableDlg;
MG::xyzDemoDialog = xyzDemoDialog;
usb_init(); // initialize the library
usb_find_busses(); // find all busses
usb_find_devices(); // find all connected devices
dev = open_dev();
ret = usb_set_configuration(dev, MY_CONFIG);
ret = usb_claim_interface(dev, 0);
ret = usb_bulk_write(dev, EP_OUT, buf_tx, 2, 10);
if (ret < 0)
xyzDemoDialog->textBox8->Text = "Error";
else
xyzDemoDialog->textBox8->Text = "Succes";
Engine->Axis[0].Jogging = true;
Engine->Axis[1].Jogging = true;
Engine->Axis[2].Jogging = true;
}
void piConfig()
{
ConfigDialog^ configDialog = gcnew ConfigDialog();
configDialog->ShowDialog();
}
void piStopPlug()
{
XYZDemoConfig^ xyzDemoConfig = MG::xyzDemoConfig;
XMLNetProfile^ profile = gcnew XMLNetProfile(gcnew String(ProfileName), "XYZTutorPlugin", true);
if (profile->Load())
{
if (MG::xyzDemoDialog)
xyzDemoConfig->enableDlg = MG::xyzDemoDialog->Visible;
profile->WriteBool("Dlg" ,xyzDemoConfig->enableDlg);
profile->WriteBool("Axis/X",xyzDemoConfig->enableX);
profile->WriteBool("Axis/Y",xyzDemoConfig->enableY);
profile->WriteBool("Axis/Z",xyzDemoConfig->enableZ);
profile->Save();
}
usb_close(dev);
}
void piUpdate()
{
unsigned int mask;
//double value_DRO;
if (MG::xyzDemoDialog)
{
XYZDemoDialog^ xyzDemoDialog = MG::xyzDemoDialog;
xyzDemoDialog->textBoxX->Text = GetDRO(800).ToString("F4");
xyzDemoDialog->textBoxY->Text = GetDRO(801).ToString("F4");
xyzDemoDialog->textBoxZ->Text = GetDRO(802).ToString("F4");
if (Engine->TrajHead != Engine->TrajIndex)
{
xyzDemoDialog->textBox7->Text = "";
while (Engine->TrajHead != Engine->TrajIndex)
{
Engine->DisplayLine = Engine->Trajectories[Engine->TrajIndex].ID;
GMoves move = MainPlanner->Movements[Engine->TrajIndex];
xyzDemoDialog->textBox1->Text = move.ex.ToString("F0");
xyzDemoDialog->textBox2->Text = move.ey.ToString("F0");
xyzDemoDialog->textBox3->Text = move.ez.ToString("F0");
Engine->Axis[0].Index += (int)move.ex;
Engine->Axis[1].Index += (int)move.ey;
Engine->Axis[2].Index += (int)move.ez;
buf_tx[adr_buf] = (int)move.ex;
buf_tx[adr_buf + 1] = (int)move.ey;
buf_tx[adr_buf + 2] = (int)move.ez;
if (adr_buf < 59)
adr_buf += 3;
else
{
xyzDemoDialog->textBox7->Text = xyzDemoDialog->textBox7->Text + (adr_buf - 1).ToString("F0") + ",";
buf_tx[0] = adr_buf - 1; // Size block
adr_buf = 4;
// usb_bulk_write(dev, EP_OUT, buf_tx, 64, 10);
}
// Increment adr bufer
Engine->TrajIndex ++;
Engine->TrajIndex &= 0xfff;
}
if (adr_buf != 4)
{
xyzDemoDialog->textBox7->Text = xyzDemoDialog->textBox7->Text + (adr_buf - 1).ToString("F0") + ",";
buf_tx[0] = adr_buf - 1; // Size block
adr_buf = 4;
usb_bulk_write(dev, EP_OUT, buf_tx, 64, 10);
}
if ((Engine->TrajHead == Engine->TrajIndex) && MainPlanner->BufferEnding)
{
MainPlanner->ExternalStill = true;
adr_buf = 4;
}
xyzDemoDialog->textBox4->Text = Engine->TrajIndex.ToString("F0");
xyzDemoDialog->textBox5->Text = Engine->TrajHead.ToString("F0");
}
else
adr_buf = 4;
xyzDemoDialog->textBox6->Text = read_input.ToString("F0");
// }
buf_tx[0] = 0; // Size block
usb_bulk_write(dev, EP_OUT, buf_tx, 64, 10);
// Read key zero, limit
usb_bulk_read(dev, EP_IN, buf_rx, 2, 10);
read_input = (buf_rx[0] | (buf_rx[1] << 8));
mask = 1;
for (i = 0; i < 7; i ++)
{
if ((read_input & mask) == 0)
Engine->InSigs[i].Activated = false;
else
Engine->InSigs[i].Activated = true;
mask <<= 1;
}
mask <<= 1;
for (i = 7; i < 9; i ++)
{
if ((read_input & mask) == 0)
Engine->InSigs[i].Activated = false;
else
Engine->InSigs[i].Activated = true;
mask <<= 1;
}
if ((read_input & 0x1000) == 0)
Engine->InSigs[22].Activated = false;
else
Engine->InSigs[22].Activated = true;
}
}
void piNotify(int id)
{
if (id == menuStart)
{
PlugInControlDialog^ pluginControlDialog = gcnew PlugInControlDialog();
XYZDemoConfig^ xyzDemoConfig = MG::xyzDemoConfig;
xyzDemoConfig->enableDlg = MG::xyzDemoDialog->Visible;
pluginControlDialog->checkBoxDlg->Checked = xyzDemoConfig->enableDlg;
pluginControlDialog->checkBoxX->Checked = xyzDemoConfig->enableX;
pluginControlDialog->checkBoxY->Checked = xyzDemoConfig->enableY;
pluginControlDialog->checkBoxZ->Checked = xyzDemoConfig->enableZ;
if (pluginControlDialog->ShowDialog() == DialogResult::OK)
{
xyzDemoConfig->enableDlg = pluginControlDialog->checkBoxDlg->Checked;
xyzDemoConfig->enableX = pluginControlDialog->checkBoxX->Checked;
xyzDemoConfig->enableY = pluginControlDialog->checkBoxY->Checked;
xyzDemoConfig->enableZ = pluginControlDialog->checkBoxZ->Checked;
XYZDemoDialog^ xyzDemoDialog = MG::xyzDemoDialog;
xyzDemoDialog->labelX->Visible = xyzDemoConfig->enableX;
xyzDemoDialog->labelY->Visible = xyzDemoConfig->enableY;
xyzDemoDialog->labelZ->Visible = xyzDemoConfig->enableZ;
xyzDemoDialog->textBoxX->Visible = xyzDemoConfig->enableX;
xyzDemoDialog->textBoxY->Visible = xyzDemoConfig->enableY;
xyzDemoDialog->textBoxZ->Visible = xyzDemoConfig->enableZ;
xyzDemoDialog->Visible = xyzDemoConfig->enableDlg;
}
}
}
void piDoDwell(double time)
{
}
void piReset()
{
}
void piJogOn(short axis, short dir, double speed)
{
/*
XYZDemoDialog^ xyzDemoDialog = MG::xyzDemoDialog;
double JogFeed = (MainPlanner->Velocities[axis]); //rapid steps per second
if (Engine->TrajHead != Engine->TrajIndex)
return;
if (speed != 0)
JogFeed = speed;
// Keyboard Shift key is on?
// If shift is not on, then JogPercent is the speed to use.
if (!(*MachView->m_ShiftState) && (speed == 0))
JogFeed = (JogFeed * _setup->JogPercent / 100); //JogSteps/Sec..
if (dir == 0)
Engine->Axis[axis].Index += (int)JogFeed;
else
Engine->Axis[axis].Index -= (int)JogFeed;
}
void piJogOff(short axis)
{
int check = 0;
Engine->Axis[axis].Dec = true;
Engine->Axis[axis].Jogging = false;
for(int x = 0; x < 6; x++)
{
if(!Engine->Axis[x].Jogging) check ++;
}
if( check >= 6) //no axis jogging, lets do a sync..
{
Engine->Jogging = false;
MainPlanner->Jogging = false;
Engine->Sync = true;
}
}
void piPurge(short flags)
{
}
void piProbe()
{
}
void piHome(short axis)
{
}
Help me please ...