wait function

wait(name, timeout) Command Documentation

The wait(name, timeout) command is designed to pause program execution, waiting for a specific notification to be received. It listens for a notification identified by a given name, with the ability to specify a maximum wait time in milliseconds. If no notification is received within the specified timeout, the command returns NIL.

Structure of the wait Command

The typical usage of the wait command is as follows:

Components:

  1. name (String Literal): Specifies the name of the notification to wait for. It must be a string literal that uniquely identifies the notification within the system.
  2. timeout (Milliseconds): An integer value representing the maximum time (in milliseconds) to wait for the notification. After this duration, if no notification is received, the function returns NIL.

Simple Example

notification=NIL; // declare variable outside of loop

dowhile(notification == NIL)

{

drv.digitalIO.toggle(); // toggle a LED for example

    // Wait for notification named “blink” for up to 1000 milliseconds

  notification = wait(“blink”, 1000);

};

lib.log(“Received stop notification: “+notification);

Explanation:

  • Waiting for Notifications: The command waits for up to 1000 milliseconds for a notification named “blink”.
  • Handling NIL: If no notification is received within the timeout, NIL is returned, indicating a timeout.

Practical Use

  • Asynchronous Event Handling: Use wait to synchronize with events or signals in a non-blocking manner, allowing programs to handle time-based or event-driven logic flexibly.
  • Timeout Mechanism: Incorporates a timeout to prevent indefinite waiting, ensuring that the program can continue execution even if an expected event does not occur.

Considerations

  • String Literal Requirement: The name parameter must be a string literal, ensuring consistent and unambiguous identification of notifications.
  • Timeout Specified in Milliseconds: Be sure to specify an appropriate timeout value depending on the responsiveness and expected behavior of the system or application.

The wait(name, timeout) command provides a structured approach to coordinating program actions with external events, using notifications to enable asynchronous processing and improve program responsiveness.