Tickers in Scilab

Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals. Here’s an example of a ticker that ticks periodically until we stop it.

function tickers()
    // Tickers use a similar mechanism to timers: a
    // channel that is sent values. Here we'll use a
    // while loop to simulate the ticker behavior.
    ticker_interval = 0.5; // 500 milliseconds
    start_time = now();
    done = %f;

    function tick()
        disp("Tick at " + string(datestr(now())));
    end

    function stop_ticker()
        done = %t;
        disp("Ticker stopped");
    end

    // Start the ticker in a separate thread
    ticker_thread = createThread("ticker_thread", tick);

    while ~done
        sleep(ticker_interval * 1000);
        if ~done then
            executeThread(ticker_thread);
        end
        if (now() - start_time) > 1.6 then // 1600 milliseconds
            stop_ticker();
        end
    end
endfunction

tickers();

When we run this program the ticker should tick 3 times before we stop it.

--> tickers()
Tick at 23-Sep-2023 11:29:56
Tick at 23-Sep-2023 11:29:57
Tick at 23-Sep-2023 11:29:57
Ticker stopped

In this Scilab implementation:

  1. We define a tickers function that simulates the behavior of a ticker.
  2. Instead of channels, we use a while loop and a separate thread to simulate the ticker behavior.
  3. The tick function is executed every 500 milliseconds (0.5 seconds) using the sleep function.
  4. We use a boolean variable done to control when to stop the ticker.
  5. The ticker runs for approximately 1600 milliseconds (1.6 seconds) before stopping.
  6. We use Scilab’s createThread and executeThread functions to run the ticker in a separate thread.

Note that Scilab doesn’t have built-in tickers or channels like some other languages, so we’ve simulated the behavior using threads and loops.