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
35 posts
• Page 3 of 4 • 1, 2, 3, 4
Re: Ruby UP/Down Arrows
Interesting stuff you guys.
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
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
-
Spogg - Posts: 3358
- Joined: Thu Nov 20, 2014 4:24 pm
- Location: Birmingham, England
Re: Ruby UP/Down Arrows
I'm sorry for the delay. I got offered a second PC, and am still in the process of setting everything up. Yes, I accidentally deleted clearEvents. It is needed to not have messages on the queue while this method is waiting for the level meter done drawing. Sorry!
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 solution
Can you figure out, why I think it's brilliant?
Spogg wrote:I would like to see a method to prevent the counter itself going past set minimum and maximum values.
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 solution
- Code: Select all
myvalue = [min_value, myvalue, max_value].sort[1]
Can you figure out, why I think it's brilliant?
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby UP/Down Arrows
ah ... nice.
Interesting regarding the 'Clamp' concept.
I've been using something like this example. Restrict to values: 1 to 12
where x is Input values.
The SORT concept is something I need to play with/understand.
First question on it. What does the [1] identify ? [.sort[1]]
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].max
where x is Input values.
The SORT concept is something I need to play with/understand.
First question on it. What does the [1] identify ? [.sort[1]]
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: Ruby UP/Down Arrows
RJHollins wrote:ah ... nice.
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].max
where x is Input values.
The SORT concept is something I need to play with/understand.
First question on it. What does the [1] identify ? [.sort[1]]
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.
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 5
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.
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
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby UP/Down Arrows
tulamide wrote:RJHollins wrote:ah ... nice.
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].max
where x is Input values.
The SORT concept is something I need to play with/understand.
First question on it. What does the [1] identify ? [.sort[1]]
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.
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 5
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.
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
Ahhhh .... thanks Tulamide.
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 !
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: Ruby UP/Down Arrows
Very nice, so many clever tricks in Ruby. (Unfortunately - because I only use Ruby once in a while - I find it hard to remember any of them, doh!!)
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)
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?
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)
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?
-
HughBanton - Posts: 265
- Joined: Sat Apr 12, 2008 3:10 pm
- Location: Evesham, Worcestershire
Re: Ruby UP/Down Arrows
HughBanton 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?
That's exactly what his problem was
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)
Or did I misunderstand something?
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.
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby UP/Down Arrows
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).
"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
- 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
.. but in its original form, with "up" & "down", I get no response at all. (Nor with their equivalents 38 & 40).
"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
-
HughBanton - Posts: 265
- Joined: Sat Apr 12, 2008 3:10 pm
- Location: Evesham, Worcestershire
Re: Ruby UP/Down Arrows
HughBanton wrote:Well, on my PC if I run it like this, substituting "shift" & "alt", it works (sort of) :
- 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
.. but in its original form, with "up" & "down", I get no response at all. (Nor with their equivalents 38 & 40).
"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
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'
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.
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby UP/Down Arrows
I expect DaveyBoy has long since moved on to something else
Yes and no
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
-
DaveyBoy - Posts: 131
- Joined: Wed May 11, 2016 9:18 pm
- Location: Leeds UK
35 posts
• Page 3 of 4 • 1, 2, 3, 4
Who is online
Users browsing this forum: No registered users and 73 guests