Prototype of an interface array

Technical questions regarding the XTC tools and programming with XMOS.
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Prototype of an interface array

Post by themech »

Hi,

I like to use an array of interfaces which is defined in one file and will be used in a main file. As far as I know I have to prototype the interface in my .h file for that reason. I want to use n client tasks which are linked to one server task.

The following prototypes worked just fine:

Code: Select all

interface my_interface;
void client_task(client interface my_interface i);
But how do I prototype the server end? This prompts me an error:

Code: Select all

void server_task(server interface my_interface i[n], unsigned n);
Help is greatly appreciated


User avatar
sethu_jangala
XCore Expert
Posts: 589
Joined: Wed Feb 29, 2012 10:03 am

Post by sethu_jangala »

Have a look at the Following Example:

Code: Select all

interface if1 {
  void msg(int x);
};

void task1(client interface if1 c);
void task2(client interface if1 c);
void task3(server interface if1 c[n], unsigned n);

int main() {
  interface if1 c[2];
  par {
    task1(c[0]);
    task2(c[1]);
    task3(c, 2);
  }
  return 0;
}

void task1(client interface if1 c)
{
  c.msg(5);
}


void task2(client interface if1 c)
{
  c.msg(7);
}

void task3(server interface if1 c[n], unsigned n)
{
  // receive two messages
  for (int i = 0;i < 2; i++) {
    select {
    case c[int i].msg(int x):
      printf("Received value %d from connection %d\n", x, i);
      break;
    }
  }
}
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Post by themech »

That solved my problem I was sitting over for hours, thank you. Is this an example you came up with, our can I find some of these simple examples somewhere?
User avatar
sethu_jangala
XCore Expert
Posts: 589
Joined: Wed Feb 29, 2012 10:03 am

Post by sethu_jangala »

themech wrote:That solved my problem I was sitting over for hours, thank you. Is this an example you came up with, our can I find some of these simple examples somewhere?
You can find the example in the xTIMEcomposer --> How To Examples window --> "How to use arrays of Interfaces"
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Post by themech »

Ah okay :) Is it allowed to split the interface definition into different files? For example: task1.xc, task2.xc and interfaces.h.

With task1 and 2 defining the interface functions and the header file to define the interface itsself?
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Post by themech »

Got it, just had an error. Sorry for the noise!