Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- LIBS --
- local win = libs.win;
- local keyboard = libs.keyboard;
- local log = require("log");
- local ffi = require("ffi");
- local tm = require("timer");
- local fs = require("fs");
- -- SETTINGS --
- local sProcessName = "CNCUSBController.exe";
- local sCommandText = "";
- local nMainWindowHandler = 0;
- local nMainWindowScanInterval = 1000;
- local uMainWindowScanTimerID = nil;
- local nAxisPosChangeInterval = 250;
- local uAxisPosChangeTimerID = nil;
- local nEditWindowsCount = 13;
- local tEditWindowHandler = { _parent = 0 };
- -- HOT KEYS, see program's "Settings->User inteface->Keys"
- local tKeys = {
- move = {
- down = {
- x = {"rightcontrol","left"},
- y = {"rightcontrol","down"},
- z = {"rightcontrol","pagedown"},
- a = {"rightcontrol","end"},
- b = {"rightcontrol"},
- c = {"rightcontrol"},
- u = {"rightcontrol"},
- v = {"rightcontrol"},
- w = {"rightcontrol"},
- step = {
- x = {"rightcontrol","rightshift","left"},
- y = {"rightcontrol","rightshift","down"},
- z = {"rightcontrol","rightshift","pagedown"},
- a = {"rightcontrol","rightshift","end"},
- b = {"rightcontrol"},
- c = {"rightcontrol"},
- u = {"rightcontrol"},
- v = {"rightcontrol"},
- w = {"rightcontrol"}
- }
- },
- up = {
- x = {"rightcontrol","right"},
- y = {"rightcontrol","up"},
- z = {"rightcontrol","pageup"},
- a = {"rightcontrol","home"},
- b = {"rightcontrol"},
- c = {"rightcontrol"},
- u = {"rightcontrol"},
- v = {"rightcontrol"},
- w = {"rightcontrol"},
- step = {
- x = {"rightcontrol","rightshift","right"},
- y = {"rightcontrol","rightshift","up"},
- z = {"rightcontrol","rightshift","pageup"},
- a = {"rightcontrol","rightshift","home"},
- b = {"rightcontrol"},
- c = {"rightcontrol"},
- u = {"rightcontrol"},
- v = {"rightcontrol"},
- w = {"rightcontrol"}
- }
- }
- }
- };
- -- MDI commands
- local tCommands = {
- spindle = {
- startCW = "M3",
- startCCW = "M4",
- stop = "M5",
- changeToCW = "/426",
- changeToCCW = "/427",
- speedUp = "/424",
- speedDown = "/425",
- mist = "/421",
- flood = "/422"
- },
- machine = {
- estop = "/400",
- play = "/401",
- pause = "/407",
- stop = "/406"
- },
- zero = {
- x = "/44101",
- y = "/44102",
- z = "/44103",
- a = "/44104",
- b = "/44105",
- c = "/44106",
- u = "/44107",
- v = "/44108",
- w = "/44109"
- },
- home = {
- all = "/431",
- xy = "/432",
- x = "x0",
- y = "y0",
- z = "z0",
- a = "a0",
- b = "b0",
- c = "c0",
- u = "u0",
- v = "v0",
- w = "w0"
- }
- };
- -- USEFUL C FUNCTIONS --
- ffi.cdef[[
- int SendMessageA( int hWnd, int Msg, int wParam, void* lParam );
- ]]
- local WM_GETTEXT = 0x000D;
- local WM_GETTEXTLENGTH = 0x000E;
- local WM_SETTEXT = 0x000C;
- local WM_SETFOCUS = 0x0007;
- function fGetWindowText ( nWindowHandler )
- local cdataText = ffi.new( "char[20]" );
- local nTextLength = ffi.C.SendMessageA( nWindowHandler, WM_GETTEXTLENGTH, 0, cdataText );
- if nTextLength > 19 then nTextLength = 19 end
- ffi.C.SendMessageA( nWindowHandler, WM_GETTEXT, nTextLength + 1, cdataText );
- return ffi.string( cdataText, nTextLength );
- end
- function fSetWindowFocus ( nWindowHandler )
- ffi.C.SendMessageA( nWindowHandler, WM_SETFOCUS, 0, ffi.new("int*") );
- end
- -- TOOLS --
- function fFindAllWindowsHandlers()
- if nMainWindowHandler < 1 then
- tEditWindowHandler._parent = 0;
- fShowInfo("Process "..sProcessName.." isn't running");
- return false;
- end
- -- find all childs
- local tAllChildWindows = win.findall( nMainWindowHandler, nil, nil, true );
- -- find all EDITs (must be 13)
- local tChildEditWindows = {};
- for k,v in pairs(tAllChildWindows) do
- if string.find( win.class(v), "EDIT", 1, true) then
- table.insert( tChildEditWindows, v );
- end
- end
- if not (#tChildEditWindows == nEditWindowsCount) then
- tEditWindowHandler._parent = 0;
- fShowInfo("Number of EDIT fields must be "..nEditWindowsCount.." (found "..#tChildEditWindows..")");
- return false;
- end
- tEditWindowHandler.JOG = tChildEditWindows[1];
- tEditWindowHandler.OVRD = tChildEditWindows[2];
- tEditWindowHandler.SPD = tChildEditWindows[3];
- tEditWindowHandler.W = tChildEditWindows[4];
- tEditWindowHandler.V = tChildEditWindows[5];
- tEditWindowHandler.U = tChildEditWindows[6];
- tEditWindowHandler.Y = tChildEditWindows[7];
- tEditWindowHandler.C = tChildEditWindows[8];
- tEditWindowHandler.X = tChildEditWindows[9];
- tEditWindowHandler.Z = tChildEditWindows[10];
- tEditWindowHandler.A = tChildEditWindows[11];
- tEditWindowHandler.B = tChildEditWindows[12];
- tEditWindowHandler.MDI = tChildEditWindows[13];
- tEditWindowHandler._parent = nMainWindowHandler;
- fShowInfo("Remote works fine");
- return true;
- end
- function fFocusMainWindow ()
- win.switchtowait(sProcessName);
- end
- function fShowInfo ( sInfo )
- if type(sInfo) ~= 'string' then sInfo = tostring(sInfo) end
- if layout.infoLabel ~= nil then layout.infoLabel.text = sInfo; end
- end
- function fRunCommand ( sCommand )
- if nMainWindowHandler < 1 then
- fShowInfo("Process "..sProcessName.." isn't running");
- return false;
- end
- fFocusMainWindow();
- fSetWindowFocus(tEditWindowHandler.MDI);
- keyboard.stroke("down");
- keyboard.text(sCommand.."\r\n");
- end
- -- ACTIONS --
- actions.inputChanged = function ( sText )
- sCommandText = sText;
- end
- actions.cmdRunTap = function()
- if nMainWindowHandler < 1 then return end
- fRunCommand(sCommandText);
- layout.commandText.text = '';
- fShowInfo( "Last command: "..string.upper(sCommandText) );
- end
- -- EVENTS --
- events.action = function ( sName, tExtras )
- local sCommand = string.lower(sName);
- -- find and execute commands
- if string.find(sCommand,'cmd_',1,true) then
- fFocusMainWindow();
- -- zero axis
- if string.find(sCommand,'zero',1,true) then
- local sAxis = string.sub(sCommand,-1);
- fShowInfo("zero - "..sAxis);
- fRunCommand( tCommands["zero"][sAxis] );
- -- home axes
- elseif string.find(sCommand,'home',1,true) then
- local sAxes = string.match( sCommand, '_(%a+)$' );
- fShowInfo("home - "..sAxes);
- fRunCommand( tCommands["home"][sAxes] );
- -- machine cmd (eStop,play,pause,stop)
- elseif string.find(sCommand,'machine',1,true) then
- local sCMD = string.match( sCommand, '_(%a+)$' );
- fShowInfo("machine - "..sCMD);
- fRunCommand( tCommands["machine"][sCMD] );
- end
- end
- -- find and do MOVE actions
- if string.find(sCommand,'move_',1,true) then
- fFocusMainWindow();
- local sAxis = string.sub(sCommand,-1);
- local sDir = "down";
- if string.find(sCommand,'up',1,true) then sDir = "up"; end
- -- step move
- if string.find(sCommand,'step',1,true) then
- keyboard.stroke( unpack( tKeys["move"][sDir]["step"][sAxis] ) );
- -- continious move
- else
- -- move start
- if string.find(sCommand,'start',1,true) then
- keyboard.down( unpack( tKeys["move"][sDir][sAxis] ) );
- -- move end
- else
- keyboard.up( unpack( tKeys["move"][sDir][sAxis] ) );
- end
- end
- end
- end
- -- INIT --
- do
- nMainWindowHandler = win.window(sProcessName);
- fFindAllWindowsHandlers();
- uMainWindowScanTimerID = tm.interval(
- function()
- local nHandler = win.window(sProcessName);
- if nHandler < 1 then
- fShowInfo("Process "..sProcessName.." isn't running");
- nMainWindowHandler = nHandler;
- elseif (nHandler ~= nMainWindowHandler) or
- (nMainWindowHandler < 1) or
- (tEditWindowHandler._parent < 1)
- then
- nMainWindowHandler = nHandler;
- fFindAllWindowsHandlers();
- end
- end,
- nMainWindowScanInterval
- );
- uAxisPosChangeTimerID = tm.interval(
- function()
- if nMainWindowHandler < 1 or tEditWindowHandler.X < 1 then return end
- layout.xPos.text = fGetWindowText( tEditWindowHandler.X );
- layout.yPos.text = fGetWindowText( tEditWindowHandler.Y );
- layout.zPos.text = fGetWindowText( tEditWindowHandler.Z );
- layout.aPos.text = fGetWindowText( tEditWindowHandler.A );
- layout.bPos.text = fGetWindowText( tEditWindowHandler.B );
- layout.cPos.text = fGetWindowText( tEditWindowHandler.C );
- layout.spd.text = fGetWindowText( tEditWindowHandler.SPD );
- layout.jog.text = fGetWindowText( tEditWindowHandler.JOG );
- end,
- nAxisPosChangeInterval
- );
- fShowInfo("Remote started and works fine");
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement