Can you return multiple values from an interface function ?

Technical questions regarding the XTC tools and programming with XMOS.
SpacedCowboy
Experienced Member
Posts: 67
Joined: Fri Aug 24, 2012 9:37 pm

Can you return multiple values from an interface function ?

Post by SpacedCowboy »

... and if so, what is the syntax ?

For example I'd like to get the value back from an ambient light sensor, and it's encoded as 16-bit R,G,B values. The call I'd like to make would look something like:

Code: Select all

#include <stdint.h>

select {
       ...
       case sensor[int i].ambientLight(uint8_t device) -> (uint16_t R, uint16_t G, uint16_t B):
               {
               ... 
               break;
               } 
        }
... but I'm getting a syntax error on the 'case' line. I can do it alternatively by ...

Code: Select all

typedef union
	{
	uint64_t rgba;
	struct
		{
		uint16_t r;
		uint16_t g;
		uint16_t b;
		uint16_t a;
		} val;
	} ALStype;

...
case sensor[int i].ambientLight(uint8_t device) -> ALStype RGB:
...
.. but if the multiple return is possible, I'd prefer to do it that way and not package up the bits into a 64-bit structure.

Cheers
Simon


User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Yes you can return multiple values:

Code: Select all

interface {
 ...
 {uint16_t R, uint16_t G, uint16_t B} ambientLight(uint8_t device);
 ...
} Sensor_i;

...
...

select {
  ...
  case sensor[int i].ambientLight(uint8_t device) -> {uint16_t R, uint16_t G, uint16_t B}:
               {
               // set R,G,B values somewhere in here
               R = 0xEFEF;
               ...
               break;
               } 
        }
Use the curly braces rather than brackets.

regards
Al
SpacedCowboy
Experienced Member
Posts: 67
Joined: Fri Aug 24, 2012 9:37 pm

Post by SpacedCowboy »

Ah! Thankyou :)
SpacedCowboy
Experienced Member
Posts: 67
Joined: Fri Aug 24, 2012 9:37 pm

Post by SpacedCowboy »

Actually, for others' future reference, just a small correction, the interface definition shouldn't have any variable names in it, so it ought to look like:

Code: Select all

interface {
 ...
 {uint16_t, uint16_t, uint16_t} ambientLight(uint8_t device);
 ...
} Sensor_i;
Cheers
Simon
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Oops my bad, it was a lazy copy and paste...