Page 1 of 1

Ruby Ticker

Posted: Fri Jan 03, 2020 1:50 am
by Alexandre
Hi, I have been using the ruby ticker without understanding the code, and now I have to write a description for it (for a report), could anybody give me a hand? I´d highly appreciate.
-Alex

Code: Select all

def init
   # Time step in secs
   @step = 0.1   
   
   # Whether the timer has started ticking
   @ticking = false
end

def event i,v,t
   case i
   when 0
      if !@ticking && @state
          @ticking = true
          input 100,nil,t+@step
      end
   when 100
      if @state
         @step = 0.001 if @step <= 0
         output 0
         input 100,nil,t+@step
      else
         @ticking = false
      end
   end   
end

Re: Ruby Ticker

Posted: Fri Jan 03, 2020 5:26 pm
by trogluddite
Key to understanding it are:

1) The 'event' method is called every time an input changes. It knows which input changed (i), a value (v: not used here), and the time when the event happened (t).

2) The 'input' method creates an input event that looks exactly the same as if a real input had changed. The input in this case is number 100 - this doesn't really exist as a component input, but Ruby doesn't care about that. The final value 't + @step' says when this new event should happen (it's bigger than t, so must be in the future).

So every time a 'tick' happens, the Ruby code sends out an input event to make another 'tick' happen a little bit in the future. The rest is just logic for turning the ticker on and off.

Re: Ruby Ticker

Posted: Thu Jan 09, 2020 2:07 am
by Alexandre
Thanks, I think I got it now.
It also helped me understand some other examples ("Ticker - Custom" included with FS, and also AdamzCounter found elsewhere in this forum).
Funny that the input cannot be 1 on the example above (has to be 100, or 10, or 2, but not 1).

Re: Ruby Ticker

Posted: Thu Jan 09, 2020 10:37 am
by trogluddite
You're welcome!
Alexandre wrote:Funny that the input cannot be 1 on the example above (has to be 100, or 10, or 2, but not 1)

In fact, it could be, so maybe you understand more than you give yourself credit for! It just has to be a number bigger than the last "real" input (100 actually wastes a bit of memory in the RubyEdit, because the array of input data [@ins] takes up space for the missing inputs 1..99).

Re: Ruby Ticker

Posted: Thu Jan 09, 2020 10:56 am
by tulamide
Just for clarity: It can be any input that is not used already. So, if you have 2 inputs in use, index 0 and 1 can't be used, but index 2 would be available. In the DSPR modules, a high index was chosen probably to prevent any multiple use of an input (a user might add inputs to extend functionality, etc)

Re: Ruby Ticker

Posted: Thu Jan 09, 2020 7:10 pm
by Alexandre
Yes, now I got it, 100%
Thanks trog and tulamide!
Simplest possible Ruby ticker below.

capture.png
capture.png (20.42 KiB) Viewed 17416 times

Re: Ruby Ticker

Posted: Thu Jan 09, 2020 11:21 pm
by Alexandre
Consider the first example. If it´s set to a period of 60 seconds and we change it to 1 second then it can take up to 60 seconds to update.
To fix this I will use the attached one for my project.
Thanks,
Alex

Re: Ruby Ticker

Posted: Fri Jan 10, 2020 2:56 am
by Alexandre
Somehow I forgot about the usual "trigger div" component... I could have just used a trigger div instead of what I did above :lol: