Hello Guest it is April 23, 2024, 08:12:42 PM

Author Topic: Make a button toggle an output  (Read 1067 times)

0 Members and 1 Guest are viewing this topic.

Make a button toggle an output
« on: January 12, 2020, 05:19:19 PM »
How close am I on this attempt to toggle output0 with a button? First time I clicked the button it crashed Mach 4 so I know I messed up something.




Code: [Select]
[mc.OSIG_ENABLE0] = function (state)-- Motor enable scripting -mac 1/2020
    if( state == 1) then
       scr.SetProperty('btnMotorEnable', 'Label', 'Motors Enabled');
       scr.SetProperty('btnMotorEnable', 'Bg Color', '#C0C0C0');--Light Grey
       scr.SetProperty('btnMotorEnable', 'Fg Color', '#808080');--Dark Grey
   else
      scr.SetProperty('btnMotorEnable', 'Label', 'Motors Disabled');
       scr.SetProperty('btnMotorEnable', 'Bg Color', '#C0C0C0');--Light Grey
       scr.SetProperty('btnMotorEnable', 'Fg Color', '#808080');--Dark Grey
    end
end,


function ServoMotorEnable() -- enable servo motors with button -mac 1/2020
local osig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
local stat=mc.mcSignalGetState(inst,mc.OSIG_OUTPUT0)
   if stat==0 then
      mc.mcSignalSetState(osig, 1)
   else
      mc.mcSignalSetState(osig, 0)
   end
end


« Last Edit: January 12, 2020, 05:22:21 PM by OldePhart »
Re: Make a button toggle an output
« Reply #1 on: January 12, 2020, 06:14:04 PM »
well what do you know, there a toggle button object that does all this... Nonetheless, I'd like to know what I have wrong in case I want to script something to turn on an output.
BTW I did change
Code: [Select]
local stat=mc.mcSignalGetState(inst,mc.OSIG_OUTPUT0)to
Code: [Select]
local stat=mc.mcSignalGetState(osig) and that stopped the crash
« Last Edit: January 12, 2020, 06:16:58 PM by OldePhart »
Re: Make a button toggle an output
« Reply #2 on: January 12, 2020, 06:43:04 PM »
Hi,
I take it you used a toggle button provided by the GUI editor?

If you look the the properties of the toggle button you will find it very flexible, it has differing colors depending on its state, differing
blink rates, and can set/unset outputs  all without scr.SetProperty(....) statements.

You should really try to avoid all the scr.SetProperty(....) type statements, they work but are far from the best and even intended use
of the scr{} table.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Make a button toggle an output
« Reply #3 on: January 12, 2020, 07:01:27 PM »
Hi,

by the way:

Quote
local stat=mc.mcSignalGetState(inst,mc.OSIG_OUTPUT0)

is completely illegal in Lua.   mc.OSIG_OUTPUT0 in Lua is an integer number, not an address. What you need is to find the 'handle'
of the signal, and the signal is numbered mc.OSIG_OUTPUT0, like this:

local SigHandle=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT0)
local stat=mc.mcSignalGetState(inst,SigHandle)

The reasoning goes that variables in Lua are dynamically created and when no longer required are garbage collected.
It is entirely possible in fact probable that over the course of a Mach4 session a variable will exist at different memory
addresses (handles) at different times.

The term 'local' means that a variable doesn't even get the dignity of a handle, it is stored in a stack. Its 'address' is like this:
'fourth from the top of the local stack'....a bit later as two variables have been popped off the stack it 'address' will now be
'second from the top of the local stack'. A little later still when that function has returned the stack has 'gone out of scope'
the whole stack is garbage collected.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Make a button toggle an output
« Reply #4 on: January 12, 2020, 10:03:19 PM »
Hi,
I take it you used a toggle button provided by the GUI editor?

You should really try to avoid all the scr.SetProperty(....) type statements, they work but are far from the best and even intended use
of the scr{} table.


Yes, in this case I used the button and it is working doing just what I want.  But what if I want an event to change the text or color on the display, or set an output pin. What is the proper technique? 
I mean I would imagine I could have eventually made this button do what I was trying to do in code. But the button exists, does what I need so no need to re-invent the wheel. But if the methods and properties for the objects are exposed, how can I use them? For an odd example, what if I wanted to change the size or location of a data display depending on the value of it ? How would I approach that?

Re: Make a button toggle an output
« Reply #5 on: January 13, 2020, 12:58:42 AM »
Hi,
have you, in the first instance, explored ALL the toggle button properties?

Refering to the attached you can set the Up text, the Down text, you can attach the button to an input OR an output.
You can set fonts and all manner of things, have you tried them all?

The events tab of the toggle button reveals that you can have an Up Action, a Down Action, an Up script and a Down
script. Of course the Actions are what is built in to Mach4 but is quite an extensive list, as the small part of the drop down
list (pictured) shows.

Even more importantly you can have your own scripts, the Up script and the Down script and scripts are limited by
your imagination only.

May  suggest explore the full range of features built in, it may well transpire that with a few keystrokes you could achieve
all you could desire WITHOUT the need for tricky-dickey code.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Make a button toggle an output
« Reply #6 on: January 13, 2020, 10:27:23 AM »
Hi,
have you, in the first instance, explored ALL the toggle button properties?

May  suggest explore the full range of features built in, it may well transpire that with a few keystrokes you could achieve
all you could desire WITHOUT the need for tricky-dickey code.

Craig


Granted this is a frivolous pursuit and not many would want to do such a thing, but it's how you learn the system and exercise the brain. I am thinking about animation during runtime. position for example is a fixed value in that dialog, but I'll bet the position coordinates are available for manipulation somewhere... so all of them must be.