Advertisement
MX_Master

UnifiedRemote - CNC USB Controller - remote.lua

Jun 30th, 2015
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.92 KB | None | 0 0
  1. -- LIBS --
  2. local win       = libs.win;
  3. local keyboard  = libs.keyboard;
  4. local log       = require("log");
  5. local ffi       = require("ffi");
  6. local tm        = require("timer");
  7. local fs        = require("fs");
  8.  
  9.  
  10.  
  11.  
  12.  
  13. -- SETTINGS --
  14.  
  15. local sProcessName              = "CNCUSBController.exe";
  16. local sCommandText              = "";
  17.  
  18. local nMainWindowHandler        = 0;
  19. local nMainWindowScanInterval   = 1000;
  20. local uMainWindowScanTimerID    = nil;
  21.  
  22. local nAxisPosChangeInterval    = 250;
  23. local uAxisPosChangeTimerID     = nil;
  24.  
  25. local nEditWindowsCount         = 13;
  26. local tEditWindowHandler        = { _parent = 0 };
  27.  
  28. -- HOT KEYS, see program's "Settings->User inteface->Keys"
  29. local tKeys = {
  30.     move = {
  31.         down = {
  32.             x = {"rightcontrol","left"},
  33.             y = {"rightcontrol","down"},
  34.             z = {"rightcontrol","pagedown"},
  35.             a = {"rightcontrol","end"},
  36.             b = {"rightcontrol"},
  37.             c = {"rightcontrol"},
  38.             u = {"rightcontrol"},
  39.             v = {"rightcontrol"},
  40.             w = {"rightcontrol"},
  41.  
  42.             step = {
  43.                 x = {"rightcontrol","rightshift","left"},
  44.                 y = {"rightcontrol","rightshift","down"},
  45.                 z = {"rightcontrol","rightshift","pagedown"},
  46.                 a = {"rightcontrol","rightshift","end"},
  47.                 b = {"rightcontrol"},
  48.                 c = {"rightcontrol"},
  49.                 u = {"rightcontrol"},
  50.                 v = {"rightcontrol"},
  51.                 w = {"rightcontrol"}
  52.             }
  53.         },
  54.  
  55.         up = {
  56.             x = {"rightcontrol","right"},
  57.             y = {"rightcontrol","up"},
  58.             z = {"rightcontrol","pageup"},
  59.             a = {"rightcontrol","home"},
  60.             b = {"rightcontrol"},
  61.             c = {"rightcontrol"},
  62.             u = {"rightcontrol"},
  63.             v = {"rightcontrol"},
  64.             w = {"rightcontrol"},
  65.  
  66.             step = {
  67.                 x = {"rightcontrol","rightshift","right"},
  68.                 y = {"rightcontrol","rightshift","up"},
  69.                 z = {"rightcontrol","rightshift","pageup"},
  70.                 a = {"rightcontrol","rightshift","home"},
  71.                 b = {"rightcontrol"},
  72.                 c = {"rightcontrol"},
  73.                 u = {"rightcontrol"},
  74.                 v = {"rightcontrol"},
  75.                 w = {"rightcontrol"}
  76.             }
  77.         }
  78.     }
  79. };
  80.  
  81. -- MDI commands
  82. local tCommands = {
  83.     spindle = {
  84.         startCW     = "M3",
  85.         startCCW    = "M4",
  86.         stop        = "M5",
  87.  
  88.         changeToCW  = "/426",
  89.         changeToCCW = "/427",
  90.  
  91.         speedUp     = "/424",
  92.         speedDown   = "/425",
  93.        
  94.         mist        = "/421",
  95.         flood       = "/422"
  96.     },
  97.    
  98.     machine = {
  99.         estop   = "/400",
  100.         play    = "/401",
  101.         pause   = "/407",
  102.         stop    = "/406"
  103.     },
  104.    
  105.     zero = {
  106.         x = "/44101",
  107.         y = "/44102",
  108.         z = "/44103",
  109.         a = "/44104",
  110.         b = "/44105",
  111.         c = "/44106",
  112.         u = "/44107",
  113.         v = "/44108",
  114.         w = "/44109"
  115.     },
  116.    
  117.     home = {
  118.         all = "/431",
  119.         xy  = "/432",
  120.         x = "x0",
  121.         y = "y0",
  122.         z = "z0",
  123.         a = "a0",
  124.         b = "b0",
  125.         c = "c0",
  126.         u = "u0",
  127.         v = "v0",
  128.         w = "w0"
  129.     }
  130. };
  131.  
  132.  
  133.    
  134.  
  135.  
  136. -- USEFUL C FUNCTIONS --
  137.  
  138. ffi.cdef[[
  139. int SendMessageA( int hWnd, int Msg, int wParam, void* lParam );
  140. ]]
  141.  
  142. local WM_GETTEXT        = 0x000D;
  143. local WM_GETTEXTLENGTH  = 0x000E;
  144. local WM_SETTEXT        = 0x000C;
  145. local WM_SETFOCUS       = 0x0007;
  146.  
  147. function fGetWindowText ( nWindowHandler )
  148.     local cdataText     = ffi.new( "char[20]" );
  149.     local nTextLength   = ffi.C.SendMessageA( nWindowHandler, WM_GETTEXTLENGTH, 0, cdataText );
  150.    
  151.     if nTextLength > 19 then nTextLength = 19 end
  152.  
  153.     ffi.C.SendMessageA( nWindowHandler, WM_GETTEXT, nTextLength + 1, cdataText );
  154.  
  155.     return ffi.string( cdataText, nTextLength );
  156. end
  157.  
  158. function fSetWindowFocus ( nWindowHandler )
  159.     ffi.C.SendMessageA( nWindowHandler, WM_SETFOCUS, 0, ffi.new("int*") );
  160. end
  161.  
  162.  
  163.  
  164.  
  165. -- TOOLS --
  166.  
  167. function fFindAllWindowsHandlers()
  168.     if nMainWindowHandler < 1 then
  169.         tEditWindowHandler._parent = 0;
  170.         fShowInfo("Process "..sProcessName.." isn't running");
  171.         return false;
  172.     end
  173.    
  174.     -- find all childs
  175.     local tAllChildWindows = win.findall( nMainWindowHandler, nil, nil, true );
  176.  
  177.     -- find all EDITs (must be 13)
  178.     local tChildEditWindows = {};
  179.    
  180.     for k,v in pairs(tAllChildWindows) do
  181.         if string.find( win.class(v), "EDIT", 1, true) then
  182.             table.insert( tChildEditWindows, v );
  183.         end
  184.     end
  185.  
  186.     if not (#tChildEditWindows == nEditWindowsCount) then
  187.         tEditWindowHandler._parent = 0;
  188.         fShowInfo("Number of EDIT fields must be "..nEditWindowsCount.." (found "..#tChildEditWindows..")");
  189.         return false;
  190.     end
  191.  
  192.     tEditWindowHandler.JOG   = tChildEditWindows[1];
  193.     tEditWindowHandler.OVRD  = tChildEditWindows[2];
  194.     tEditWindowHandler.SPD   = tChildEditWindows[3];
  195.     tEditWindowHandler.W     = tChildEditWindows[4];
  196.     tEditWindowHandler.V     = tChildEditWindows[5];
  197.     tEditWindowHandler.U     = tChildEditWindows[6];
  198.     tEditWindowHandler.Y     = tChildEditWindows[7];
  199.     tEditWindowHandler.C     = tChildEditWindows[8];
  200.     tEditWindowHandler.X     = tChildEditWindows[9];
  201.     tEditWindowHandler.Z     = tChildEditWindows[10];
  202.     tEditWindowHandler.A     = tChildEditWindows[11];
  203.     tEditWindowHandler.B     = tChildEditWindows[12];
  204.     tEditWindowHandler.MDI   = tChildEditWindows[13];
  205.  
  206.     tEditWindowHandler._parent = nMainWindowHandler;
  207.  
  208.     fShowInfo("Remote works fine");
  209.  
  210.     return true;
  211. end
  212.  
  213.  
  214. function fFocusMainWindow ()
  215.     win.switchtowait(sProcessName);
  216. end
  217.  
  218.  
  219. function fShowInfo ( sInfo )
  220.     if type(sInfo) ~= 'string' then sInfo = tostring(sInfo) end
  221.    
  222.     if layout.infoLabel ~= nil then layout.infoLabel.text = sInfo; end
  223. end
  224.  
  225.  
  226. function fRunCommand ( sCommand )
  227.     if nMainWindowHandler < 1 then
  228.         fShowInfo("Process "..sProcessName.." isn't running");
  229.         return false;
  230.     end
  231.  
  232.     fFocusMainWindow();
  233.     fSetWindowFocus(tEditWindowHandler.MDI);
  234.  
  235.     keyboard.stroke("down");    
  236.     keyboard.text(sCommand.."\r\n");
  237. end
  238.  
  239.  
  240.  
  241.  
  242.  
  243. -- ACTIONS --
  244.  
  245. actions.inputChanged = function ( sText )
  246.     sCommandText = sText;
  247. end
  248.  
  249.  
  250. actions.cmdRunTap = function()
  251.     if nMainWindowHandler < 1 then return end
  252.  
  253.     fRunCommand(sCommandText);
  254.  
  255.     layout.commandText.text = '';
  256.  
  257.     fShowInfo( "Last command: "..string.upper(sCommandText) );
  258. end
  259.  
  260.  
  261.  
  262.  
  263.  
  264. -- EVENTS --
  265.  
  266. events.action = function ( sName, tExtras )
  267.     local sCommand = string.lower(sName);
  268.  
  269.     -- find and execute commands
  270.     if string.find(sCommand,'cmd_',1,true) then
  271.         fFocusMainWindow();
  272.        
  273.         -- zero axis
  274.         if string.find(sCommand,'zero',1,true) then
  275.             local sAxis = string.sub(sCommand,-1);
  276.             fShowInfo("zero - "..sAxis);
  277.             fRunCommand( tCommands["zero"][sAxis] );
  278.         -- home axes
  279.         elseif string.find(sCommand,'home',1,true) then
  280.             local sAxes = string.match( sCommand, '_(%a+)$' );
  281.             fShowInfo("home - "..sAxes);
  282.             fRunCommand( tCommands["home"][sAxes] );
  283.         -- machine cmd (eStop,play,pause,stop)
  284.         elseif string.find(sCommand,'machine',1,true) then
  285.             local sCMD = string.match( sCommand, '_(%a+)$' );
  286.             fShowInfo("machine - "..sCMD);
  287.             fRunCommand( tCommands["machine"][sCMD] );
  288.         end
  289.     end
  290.  
  291.     -- find and do MOVE actions
  292.     if string.find(sCommand,'move_',1,true) then
  293.         fFocusMainWindow();
  294.        
  295.         local sAxis = string.sub(sCommand,-1);
  296.         local sDir  = "down";
  297.        
  298.         if string.find(sCommand,'up',1,true) then sDir  = "up"; end
  299.  
  300.         -- step move
  301.         if string.find(sCommand,'step',1,true) then
  302.             keyboard.stroke( unpack( tKeys["move"][sDir]["step"][sAxis] ) );
  303.         -- continious move
  304.         else
  305.             -- move start
  306.             if string.find(sCommand,'start',1,true) then
  307.                 keyboard.down( unpack( tKeys["move"][sDir][sAxis] ) );
  308.             -- move end
  309.             else
  310.                 keyboard.up( unpack( tKeys["move"][sDir][sAxis] ) );
  311.             end
  312.         end
  313.     end
  314. end
  315.  
  316.  
  317.  
  318.  
  319.  
  320. -- INIT --
  321.  
  322. do
  323.     nMainWindowHandler = win.window(sProcessName);
  324.     fFindAllWindowsHandlers();
  325.  
  326.     uMainWindowScanTimerID = tm.interval(
  327.         function()
  328.             local nHandler = win.window(sProcessName);
  329.  
  330.             if nHandler < 1 then
  331.                 fShowInfo("Process "..sProcessName.." isn't running");
  332.                 nMainWindowHandler = nHandler;
  333.             elseif  (nHandler ~= nMainWindowHandler) or
  334.                     (nMainWindowHandler < 1) or
  335.                     (tEditWindowHandler._parent < 1)
  336.             then
  337.                 nMainWindowHandler = nHandler;
  338.                 fFindAllWindowsHandlers();
  339.             end
  340.         end,
  341.        
  342.         nMainWindowScanInterval
  343.     );
  344.  
  345.     uAxisPosChangeTimerID = tm.interval(
  346.         function()
  347.             if nMainWindowHandler < 1 or tEditWindowHandler.X < 1 then return end
  348.  
  349.             layout.xPos.text = fGetWindowText( tEditWindowHandler.X );
  350.             layout.yPos.text = fGetWindowText( tEditWindowHandler.Y );
  351.             layout.zPos.text = fGetWindowText( tEditWindowHandler.Z );
  352.             layout.aPos.text = fGetWindowText( tEditWindowHandler.A );
  353.             layout.bPos.text = fGetWindowText( tEditWindowHandler.B );
  354.             layout.cPos.text = fGetWindowText( tEditWindowHandler.C );
  355.  
  356.             layout.spd.text = fGetWindowText( tEditWindowHandler.SPD );
  357.             layout.jog.text = fGetWindowText( tEditWindowHandler.JOG );
  358.         end,
  359.        
  360.         nAxisPosChangeInterval
  361.     );
  362.    
  363.     fShowInfo("Remote started and works fine");
  364. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement