Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Question about the stock: Switch - Button

For general discussion related FlowStone

Question about the stock: Switch - Button

Postby tor » Tue Jan 22, 2013 8:33 pm

The Switch - Button in FS has a bolean input from the preset module the input is called: setState. But I can not find @setState anywhere in the code.

Where in the code does this appear and and how does it work?
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Question about the stock: Switch - Button

Postby tor » Tue Jan 22, 2013 9:56 pm

What I want is the same behaviour as in the stock button. Exept I just need it to toggle the "LED on" layer on/off.
Button.fsm
(143.02 KiB) Downloaded 999 times
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Question about the stock: Switch - Button

Postby trogluddite » Tue Jan 22, 2013 11:15 pm

tor wrote:Where in the code does this appear and and how does it work?

They've sneakily hidden it away inside the 'event' method....
Code: Select all
def event i,v
   if i==2
      if @on != v
         @on = v
         output 0,@on
      end
   end
   redraw 0
end

Still don't see it? - that's because there's more than one way to get the value of an input...

The 'event' method in the code gets triggered every time any input changes. When there's a change, 'i' gets set to the index number of the input that changed, and 'v' gets set to whatever the new value is, then the code runs.
First thing in this bit of code is "if i==2", so it only ever does much when the third input changes (inputs start from zero) - which is the 'set_value' input. So the new set value ends up in 'v' without ever having to call the input by name - the little bit of code inside the 'if' statement does all the work using 'v' instead of '@set_value'., but the end result is exactly the same.

(NB - The 'redraw 0' is not inside the 'if' statement because it already 'end'ed, so redrawing will happen for any input change, not just the 'set_value'.)

If you have an 'event' method anywhere in your code, then the code will always refer to that whenever an input changes to see what it should do - without 'event', the whole code runs every time for every input whether you want it to or not.
For this control, event only needs to react to that particular input as pretty much everything else is done by the GUI and mouse.

The third way of getting an input value is to use the @ins array - a single array that contains all of the current input values, no matter what type they are. To look up a particular input, you look up the array index...
second_input_value = @ins[1]
Looking up array indexes is not as efficient as just calling a regular variable though, so @ins is probably best used only if you need the inputs in array form for some special chunk of code.

(Oh yes, and there's a fourth way! - if you only have one input, you can get the value from plain old '@in')

That's the biggest trouble with Ruby - too much choice! ;)
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Question about the stock: Switch - Button

Postby tor » Tue Jan 22, 2013 11:49 pm

I kind of imagined that 'i' was the index of the incoming event values 'v' in serial order.
1,5,4,8,9,7-->Values in serial
0,1,2,3,4,5-->Indexes

But the 'i' is the index of what input.

Thanks for the clarification. I see the :idea: now.
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Question about the stock: Switch - Button

Postby support » Wed Jan 23, 2013 5:47 pm

Great explanation trogluddite!
User avatar
support
 
Posts: 151
Joined: Fri Sep 07, 2012 2:10 pm

Re: Question about the stock: Switch - Button

Postby trogluddite » Wed Jan 23, 2013 7:26 pm

support wrote:Great explanation trogluddite

Thankyou very much! :D
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Question about the stock: Switch - Button

Postby tor » Wed Jan 23, 2013 7:31 pm

Like!
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm


Return to General

Who is online

Users browsing this forum: No registered users and 56 guests