How to use a select function

  • 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 a select function

  • 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.

Select statements can be implemented as functions, allowing their reuse in different contexts.

The declaration

select channel_input(chanend c_a, chanend c_b)

declares channel_input to be a select function that takes two arguments c_a and c_b. The body of the select function adheres to the same rules as the select statement.

{
  case c_a :> int chnl_input_a :
    printstr("Channel Input A Received ");
    printintln(chnl_input_a);
    break;
  case c_b :> int chnl_input_b :
    printstr("Channel Input B Received ");
    printintln(chnl_input_b);
    break;
}

You can call the select function from the relevant location in the program. In this example the select function is nested within a select statement where it is inputting data from either one of the two channel ends c_a, c_b or from the input port port_in

select {
  case channel_input(c_a, c_b);
  case port_in when pinsneq(port_input_data) :> port_input_data :
    do_port_input(port_input_data);
    break;
}