How to use select with multiple resources

  • 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 select with multiple resources

  • 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. Each input is proceeded by the keyword case and its body must be terminated with a break or return statement. Case statements are not permitted to contain output operations. Select statements can be used to tie together inputs from multiple resources, in this example we look at combining timers, channels and ports into a single select statement.

The select statement implemented below is used to wait for either an input on chnlend_a, an input on chnlend_b, an input from port_a or a timer value. When an input is received from any of the cases an message is printed to display which case has fired. The timer case can be used to wake up and execute at a particular rate, in this example it is every 10000 timer cycles.

select
{
  case chnlend_a :>  chnl_input_a :
    printstr("Channel Input A Received ");
    printintln(chnl_input_a);
    break;
  case chnlend_b :> chnl_input_b :
    printstr("Channel Input B Received ");
    printintln(chnl_input_b);
    break;
  case port_a when pinseq(1) :> port_input_a:
    printstr("Port Input A Recieved ");
    printintln(port_input_a);
    break;
  case tmr_a when timerafter(start_time + 10000) :> start_time:
    printstrln("Timer A Fired ");
    break;
  }
}