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
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Ruby UP/Down Arrows
Re: Ruby UP/Down Arrows
I would like to see a method to prevent the counter itself going past set minimum and maximum values. I’m real busy at the moment otherwise I’d try to contribute.
Cheers
Spogg
Re: Ruby UP/Down Arrows
That's called "clamping", which is a very associative term. There are dozen of ways to do it. For example, in Ruby someone on the internet came up with this brilliant solutionSpogg wrote:I would like to see a method to prevent the counter itself going past set minimum and maximum values.
Code: Select all
myvalue = [min_value, myvalue, max_value].sort[1]Re: Ruby UP/Down Arrows
Interesting regarding the 'Clamp' concept.
I've been using something like this example. Restrict to values: 1 to 12
Code: Select all
s=[[12, x].min, 1].maxThe SORT concept is something I need to play with/understand.
First question on it. What does the [1] identify ? [.sort[1]]
Re: Ruby UP/Down Arrows
Yes, using min and max is what I would call the common way. First get the smaller of x and max_value then get the larger of that and min_value. That's also, how I would prefer to do it in green.RJHollins wrote:ah ... nice.
Interesting regarding the 'Clamp' concept.
I've been using something like this example. Restrict to values: 1 to 12where x is Input values.Code: Select all
s=[[12, x].min, 1].max
The SORT concept is something I need to play with/understand.
First question on it. What does the [1] identify ? [.sort[1]]
Another way would be to work with conditionals.
Code: Select all
def clamp(x, min_value, max_value)
if x < min_value
return min_value
elsif x > max_value
return max_value
else
return x
end
result = clamp(3, 5, 20) #returns 5If you use some numbers it becomes apparent:
[1, 5, 12].sort[1] -> after sort it's still [1, 5, 12] -> middle value is returned -> 5
[1, 0, 12].sort[1] -> after sort it's [0, 1, 12] -> middle value is returned -> 1
[1, 13, 12].sort[1] -> after sort it's [1, 12, 13] -> middle value is returned -> 12
Re: Ruby UP/Down Arrows
Ahhhh .... thanks Tulamide.tulamide wrote:Yes, using min and max is what I would call the common way. First get the smaller of x and max_value then get the larger of that and min_value. That's also, how I would prefer to do it in green.RJHollins wrote:ah ... nice.
Interesting regarding the 'Clamp' concept.
I've been using something like this example. Restrict to values: 1 to 12where x is Input values.Code: Select all
s=[[12, x].min, 1].max
The SORT concept is something I need to play with/understand.
First question on it. What does the [1] identify ? [.sort[1]]
Another way would be to work with conditionals.The array sorting method however is so elegant and instinctively understandable. Layout is "left boundary, value, right boundary", which is easy to grasp. What happens is that an array is filled with these three values, then sorted and then the middle value from the array is returned [1] <- index starts at 0, so 1 is the second value.Code: Select all
def clamp(x, min_value, max_value) if x < min_value return min_value elsif x > max_value return max_value else return x end result = clamp(3, 5, 20) #returns 5
If you use some numbers it becomes apparent:
[1, 5, 12].sort[1] -> after sort it's still [1, 5, 12] -> middle value is returned -> 5
[1, 0, 12].sort[1] -> after sort it's [0, 1, 12] -> middle value is returned -> 1
[1, 13, 12].sort[1] -> after sort it's [1, 12, 13] -> middle value is returned -> 12
Not only for the explanation of the 'common' use ... but also the SORT method.
I find I'm looking toward RUBY solutions more and more. I use various Ruby tut sites to help with the concepts.
One of the difficulties is searching for solutions that work with our FS Ruby version.
The next thing I'm facing is the eventual organizing a library of RUBY routines. I've a very scattered approach at present. But the more I add to and use RUBY, the more important to get some kind of order to all this.
Thanks again !
- HughBanton
- Posts: 268
- Joined: Sat Apr 12, 2008 3:10 pm
- Location: Evesham, Worcestershire
- Contact:
Re: Ruby UP/Down Arrows
Now, I've discovered that my previous Up/Down auto-repeat offering is defective .. once keyed, input100 continues to cycle ad infinitum, sending out regular triggers. Not good at all, sorry
So herewith v3, which seems to avoid that. (Also incorporating our latest favourite clamping friend inside the counter)
Does it work OK for everyone else, or if you get what I get, is there an explanation?
Re: Ruby UP/Down Arrows
That's exactly what his problem wasHughBanton wrote:Next .. I wanted to try and revisit DaveyBoy's original purpose for this thread. Going right back to the very start, I can't get his original code to work properly. I get the 'watched' @clock OK - 0,1,0,1 etc, but I can only make isKeyPressed increment @selected if I substitute "shift" or "control" or "alt" keys; nothing at all happens with "up", "down", or for that matter any other key like "q" or "81" etc. (I haven't gone through many more ..)
Does it work OK for everyone else, or if you get what I get, is there an explanation?
Or did I misunderstand something?Only problem so far is that it doesn't respond instantly as nine times out of ten the key will be pressed in between 'ticks'. (I don't want to use a fast clock to keep the CPU load down)
Edit: There are no string shortcuts for the alphabet. So "q" can't work and 81 has to be a number not a string (so, not "81"). Try the numbers (not strings) 38 and 40 for up and down as well.
- HughBanton
- Posts: 268
- Joined: Sat Apr 12, 2008 3:10 pm
- Location: Evesham, Worcestershire
- Contact:
Re: Ruby UP/Down Arrows
Code: Select all
ef init
@clock = 0
@selected = 0
input 100, nil
end
def event i,v,t
case i
when 100
input 100, nil, t + 0.25
@clock += 1
@clock = 0 if @clock == 2
if isKeyPressed "shift"
@selected += 1
elsif isKeyPressed "alt"
@selected -= 1
end
@selected = [0,@selected,10].sort[1]
end
watch "@clock", @clock
watch "@selected", @selected
end"q" & 81 are specifically quoted in the FS User Guide for isKeyPressed (page 202) but as I say I can only get very few of the keys on their list to work at all here, it's strange.
I expect DaveyBoy has long since moved on to something else, six months is a long time in Flowstone
Re: Ruby UP/Down Arrows
Fun fact: I also referred to page 202 and it never, never ocurred to me that you can actually use just a string! I always read it as 'strings only for special keys, codes for everything else'HughBanton wrote:Well, on my PC if I run it like this, substituting "shift" & "alt", it works (sort of) :.. but in its original form, with "up" & "down", I get no response at all. (Nor with their equivalents 38 & 40).Code: Select all
ef init @clock = 0 @selected = 0 input 100, nil end def event i,v,t case i when 100 input 100, nil, t + 0.25 @clock += 1 @clock = 0 if @clock == 2 if isKeyPressed "shift" @selected += 1 elsif isKeyPressed "alt" @selected -= 1 end @selected = [0,@selected,10].sort[1] end watch "@clock", @clock watch "@selected", @selected end
"q" & 81 are specifically quoted in the FS User Guide for isKeyPressed (page 202) but as I say I can only get very few of the keys on their list to work at all here, it's strange.
I expect DaveyBoy has long since moved on to something else, six months is a long time in Flowstone
I copied the above code, pasted it in a RubyEdit, added the "d" from 'def' and changed shift and alt to Up and Down. Worked like a charm. There seems to be something wrong with your setup.
Re: Ruby UP/Down Arrows
Yes and noI expect DaveyBoy has long since moved on to something else
Still working on the same project of which this is a part of, no marks for guessing what it is
I managed to get this working pretty much like it should:
Slight pause before it starts to auto_scroll
Scroll bar auto moves when required
Only works when window has focus
I really wanted to do it Ruby only (no particular reason, just a Ruby challenge for myself) but I couldn't get my head round the Windows Proc that Tulamide suggested.
Feel free to use as you wish,
Suggestions for improvement would be most welcome.
Hugh . . I've no idea why it doesn't work on your machine, it works fine on the 3 machines I've tried it on!
Oh and yes . . . Our clamping friend is in there