How to use the select statement with a timeout
version
1.1.1
scope
Example.
This code is provided as example code for a user to base their code on.
description
How to use the select statement with a timeout
boards
Unless otherwise specified, this example runs on the SliceKIT Core Board, but can easily be run on any XMOS device by using a different XN file.
A select statement waits for one of a set of inputs to become ready, performs the selected input and then executes a corresponding body of code. There are however occasions when it is not reasonable to wait on one of the inputs to become ready. In this instance a timer can be used to exit the select statement if no input has become ready within a defined timeout period.
The current time is input from timer t. The value of time is then extended by the TIMEOUT_PERIOD to give a time in the future.
t :> time; time += TIMEOUT_PERIOD;
If data is available from the port before the TIMEOUT_PERIOD expires then the port_input case is executed and the data can be printed out. However, if no data becomes available within the TIMEOUT_PERIOD then the timeout case is executed and the select statement exits with no input received from the port.
select { case port_input when pinsneq(port_input_data) :> port_input_data : printstr("Input received on port_input : "); printintln(port_input_data); break; case t when timerafter(time) :> void : printstrln("Select statement timeout waiting on input."); break; }