Hello Guest it is March 29, 2024, 10:59:21 AM

Author Topic: Mach4 Lua programatically clicking button  (Read 873 times)

0 Members and 1 Guest are viewing this topic.

Offline Rimmel

*
  •  207 207
    • View Profile
Mach4 Lua programatically clicking button
« on: July 29, 2022, 11:52:59 AM »
Mach4 Lua programatically clicking button ... or changing the state of a button programatically, can it be done?? e.g. calling the click or state change from code?

thanks

Offline Rimmel

*
  •  207 207
    • View Profile
Re: Mach4 Lua programatically clicking button
« Reply #1 on: July 29, 2022, 01:06:28 PM »
I can see how to set properties of screen items, but not fire their events.

Basically i have a Togglebutton that I can manually turn on and off the Oiler. However I also automatically turn the oiler on and off when the spindle starts/stops. When I automitically start and stop the oiler the manual button does not reflect the Oiler state (On or Off).

I use the ToggleButton state as a visual cue to see if the oiler is one/off... so it's not accidently left on for long periods.

So I want to start spindle - start oiler, change state of the Oiler Toggle Button to Down (or up)...

or

Start spindle, fire the OilerToggle button Click event so that it starts the Oiler Itself... same result different method of achieving it.

thanks
Re: Mach4 Lua programatically clicking button
« Reply #2 on: July 29, 2022, 03:47:59 PM »
If you have assigned the output for the oiler to the toggle button in the Output Signal property, the button state (up / down) will automatically change if this output is activated from else where in Mach.

I would add code to turn on the oiler output in the signal library for the Spindle On output so that every time the spindle output turns on, the oiler will also turn on. If that is what you want of course..
Re: Mach4 Lua programatically clicking button
« Reply #3 on: July 29, 2022, 06:44:26 PM »
Hi,
in Screen Edit mode place a toggle button. On the Properties Tab of the button you can associate an Input and/or an Output.
On the Events Tab of the button you can call from a range of Mach4 internally coded Actions, an Up Action and /or a Down Action.
You can also write Lua scripts, an Up Script and /or a Down Script.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Mach4 Lua programatically clicking button
« Reply #4 on: August 02, 2022, 04:55:47 AM »
you can easily do it with registers and monitoring the state in the PLC

it may or may not be "fast"...depending on your idea of "that"
« Last Edit: August 02, 2022, 05:06:35 AM by compewter_numerical »

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Mach4 Lua programatically clicking button
« Reply #5 on: September 14, 2022, 01:20:31 PM »
Typically it is a BAD idea to try and "click" buttons programmatically.  The correct way to do this in Mach 4 is to use the API to do the same function the button does.  Now, if the button does some custom LUA code, put the code in a function in the screen load script and then call that function from the button AND your other code.  If you want to call this function from a M code script, you will have to put the function in a module that both the screen and the m code scripts load. 

I typically like to put all of my custom code in a LUA module.  That way I can call the functions from the screen buttons, screen PLC script, and M code macro scripts.  But be aware that there no global variables shared between the screen and macro LUA environments.  Use Mach registers to share data. 

And I really really hate to mention this because it is the WRONG way to do it and I regret putting this function in the GUI.  But inside the screen LUA environment, you can use scr.ButtonClick('button-name').  If you want to use this function in a macro script, you will have to load the screen api.  See load_modules.mcs in the lua examples.

scr=require('screenipc') -- require the screen api.
scr.scIpcInit('localhost:48500') -- init the screen api.

Steve
« Last Edit: September 14, 2022, 01:38:38 PM by smurph »