How to use the select statement with a default case
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 default case
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 default case statement can be used to exit the select statement immediately if no inputs are ready.
In this example the select statement is used to wait for either an input on chnlend_a or an input on chnlend_b. If an input is available from either channel the value of the input is printed. If neither channel has an input ready then the default case is executed.
select { case chnlend_a :> int chnl_input_a : printstr("Channel Input A Received "); printintln(chnl_input_a); break; case chnlend_b :> int chnl_input_b : printstr("Channel Input B Received "); printintln(chnl_input_b); break; default : printstrln("Default Case executed"); break; }
Care should be taken when using a default case statement as it can have the same effect as the processor polling for input. This will ultimately have a drain on the power and waste valuable clock cycles.