Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: gorf23 on April 11, 2017, 01:00:44 PM

Title: Screen load script question
Post by: gorf23 on April 11, 2017, 01:00:44 PM
If you want to call a global function that has been put into the screen load script, is there a special way to call it from a script

I put a WriteRegister function in the load screen script  but if I try to call it from a button script I always get the error
attempt to call global 'WriteRegister' (a nil value)...

Is there a special way to call functions that are in the screen load script or do I need to add it somewhere else also
it will run fine if I have the function in the button script and run debug...

Thanks
Title: Re: Screen load script question
Post by: Chaoticone on April 11, 2017, 04:16:53 PM
Nothing special to run a function from the screen load script in a button script. Just call the function by the same name used in the screen load script and pass it any parameters if it has any.

MyFunction()
MyFunction(param1, param2, param3)

Another way to do it is to put the function in a module. Then you can run it from a buttons script or Mcode. You just have to load the module in screen load (so buttons can use its functions) and in a module that is in the macros folder (so Mcodes can use its functions) of the profile your using.

There has not been a release since I added the load_modules.mcs file to all of the default profiles macros folders but you can manually add it to any version. I have already posted this somewhere before but here it is again.

Code: [Select]
--This is the mcUserScript.lua module that goes in the modules folder
local mcUserScript = {}

function mcUserScript.UserMessage(Message)
if (Message == nil) then --No message was passed
Message = "No message passed" --If no message is passed this will be the default message
end
wx.wxMessageBox(Message)
end

return mcUserScript -- Module End

--This is what loads the module above.
--Add this to the modules load section of the screen load script so you can call its functions from buttons on the screen.
--Add this to the load_modules.mcs file in the macros folder of the profile that will be using it so you can call its functions from M codes.
package.loaded.mcUserScript = nil
us = require "mcUserScript"

--Put this in a buttons clicked script and when you click it a message box will pop up that says... No message passed
us.UserMessage() --This is not passing a message for the Message parameter so the message will be the default

--m110 goes in the macros folder of the profile that will be using it. It will be named m110.mcs
function m110()

us.UserMessage("This is my message") --When M110 is executed in Gcode or MDI a message box will pop up that says This is my message.

end

if (mc.mcInEditor() == 1) then
 m110()
end
Title: Re: Screen load script question
Post by: gorf23 on April 11, 2017, 08:10:25 PM
Nothing special to run a function from the screen load script in a button script. Just call the function by the same name used in the screen load script and pass it any parameters if it has any.

MyFunction()
MyFunction(param1, param2, param3)

That's what I thought but doesn't seem to work for me

My Function was called  WriteRegister(param1,param2,param3)
I am passing 3 param's to it... from the button script

then I call it from a button script with the 3 param's
and l get the message (a nil value)...

if I put the WriteRegister(param1,param2,param3) function from the screen load script into
the button script  and call that function form the same button script it works

Thanks gary
Title: Re: Screen load script question
Post by: Chaoticone on April 12, 2017, 09:13:08 AM
Paste in all of your script......... the function from the screen load script and the script you put in a button that works.
Title: Re: Screen load script question
Post by: gorf23 on April 12, 2017, 04:23:08 PM
This works in button script

inst=mc.mcGetInstance()
function WriteRegister(whatregister, regname, regvalue)
    local hreg = mc.mcRegGetHandle(inst, string.format(whatregister,regname))
    mc.mcRegSetValueString(hreg, tostring(regvalue))
end


function GetRegister(whatregister,regname)
  local hreg = mc.mcRegGetHandle(inst, string.format(whatregister,regname))
  return mc.mcRegGetValueString(hreg)
end

WhatRegister = "iRegs0/%s"
wx.wxMessageBox("WhatRegister= "..WhatRegister)

RegName = "Probe_How" --Y_Probe
Z_Probeprobe = GetRegister(WhatRegister, RegName)
wx.wxMessageBox("Return reg value = "..Z_Probeprobe)

MyRegValue =  "10"

WriteRegister(WhatRegister, RegName, MyRegValue )
regval = GetRegister(WhatRegister,RegName)
wx.wxMessageBox("regval = "..regval)

I put this in screen load

function WriteRegister(whatregister, regname, regvalue)
    local hreg = mc.mcRegGetHandle(inst, string.format(whatregister,regname))
    mc.mcRegSetValueString(hreg, tostring(regvalue))
end


function GetRegister(whatregister,regname)
  local hreg = mc.mcRegGetHandle(inst, string.format(whatregister,regname))
  return mc.mcRegGetValueString(hreg)
end

I tried calling from a button like this

RegName = "Probe_How"
WhatRegister = "iRegs0/%s"
 retval = GetRegister(whatregister,regname)

MyRegValue = "X"
WriteRegister(WhatRegister, RegName, MyRegValue )

Thanks for your help
I did get the macros and modules to work from a button so over that problem

Gary

Title: Re: Screen load script question
Post by: Chaoticone on April 12, 2017, 10:56:16 PM
Get rid of the formatting tags and see how it does.

WhatRegister = "iRegs0/%s"

Should be

WhatRegister = "iRegs0"

This isn't right either.........

local hreg = mc.mcRegGetHandle(inst, string.format(whatregister,regname))

"core/global/Version"

local hreg = mc.mcRegGetHandle(inst, string.format(your missing part of the srting here .. '/' .. whatregister .. '/' .. regname))

Play with until you can read and write to a single register in debugger then move on to functions then functions with parameters passing.
Title: Re: Screen load script question
Post by: gorf23 on April 13, 2017, 03:04:44 PM
ok I will mess with it some more not sure what part of the string I may be missing

and it does work in the debugger as is, I can write to a single register and read
from a single register in the debugger also works as a mcode I saved it as m760
and ran it with the MDI m760 and I put messagebox's to display the results worked fine..

thanks gary
Title: Re: Screen load script question
Post by: gorf23 on April 13, 2017, 08:11:32 PM
WhatRegister = "iRegs0" I tried it this way and if when I get register
and then messagebox the results its empty no display seems that I still need it
to be formatted like I had it WhatRegister = "iRegs0/%s" this works for me..

gary
Title: Re: Screen load script question
Post by: Chaoticone on April 14, 2017, 01:19:07 AM
Good, no shortage of ways to do it in lua, that's for sure. Just pick the way that works best for you.

Something like this might work too......
local hreg = mc.mcRegGetHandle(inst, string.format(your missing part of the srting here .. '//' .. whatregister .. '//' .. regname))

Or

"iRegs0//"

The best way to find out for sure is to just do it and test and adjust until you get what you want.
Title: Re: Screen load script question
Post by: gorf23 on April 14, 2017, 04:30:42 PM
just an update

Something like this might work too......
local hreg = mc.mcRegGetHandle(inst, string.format(your missing part of the srting here .. '//' .. whatregister .. '//' .. regname))
tried this put in a string get get bad  value for #2

Or

"iRegs0//"
Tried this didn't work for me either

seems the way I have it originally is the only way it will run in the debugger at least for me...
But still doesn't fix the problem of having the functions added to screen load script
and being able to call the function from a button if I add the
register functions to the screen load  do I also have to add them to a module ? or a m function?..
 
Title: Re: Screen load script question
Post by: DazTheGas on April 14, 2017, 05:48:36 PM
So I have just read through this thread and puzzles me why you are trying to send the register name?? this will always stay the same if using the register plugin, you cannot change it.

In my own screen I use

Code: [Select]
function GetRegister(regname)
    local hreg = mc.mcRegGetHandle(inst, string.format("Custom_Gui/%s", regname))
    return mc.mcRegGetValueString(hreg)
end

function WriteRegister(regname, regvalue)
    local hreg = mc.mcRegGetHandle(inst, string.format("Custom_Gui/%s", regname))
    mc.mcRegSetValueString(hreg, tostring(regvalue))
end

so just change string.format("Custom_Gui/%s" to the reg you are using string.format("iRegs0/%s" and all should work fine

DazTheGas
Title: Re: Screen load script question
Post by: gorf23 on April 15, 2017, 02:14:37 PM
mine is close to yours I did get it to work now I put in a module and can call it from a button or m code now
guess that was the part I wasn't getting...

Daz you should do a video on setting up modules that can be called  from buttons or m code  that would save a lot of people like me so
much trouble and maybe a lot less questions..

Thanks for all your help..
Gary