View unanswered posts | View active topics It is currently Thu Oct 02, 2014 7:54 am






Reply to topic  [ 4 posts ] 
RobotC Language Suggestions 
Author Message
Rookie
User avatar

Joined: Mon Dec 31, 2007 12:51 pm
Posts: 5
Location: United States
Post RobotC Language Suggestions
Cheers to the CMU team for making a great development tool!

I am starting this thread so that others like myself might provide some suggestions to the CMU developers as they improve the RobotC language.

Are there any plans to add the C language union to the RobotC language?

-Thanks
Olorin

PS
For those not familiar with the C programming language:
A union allows the same memory space to be interpreted in different ways. For example: a message consisting of four bytes could be interpreted as an array 4 characters, 2 shorts, or 1 long. What good is this? Suppose you want to read two registers in a sensor that store your sensor data as two contiguous bytes...

// The following code will not compile (yet?) in RobotC
#define NUM_BYTES_STATE_MSG 2
typedef struct
{
short sensorState;
}SensorStateDataType;
typedef struct
{
byte msg[NUM_BYTES_STATE_MSG];
}SensorStateMsgType;
// Both of the above structures are the same size, but interpreted differently
typedef union
{
SensorStateDataType data;
SensorStateMsgType dataMsg;
}SensorStateType;

int main(void)
{
string aMsg = "";
eraseDisplay();

SensorStateType sensorState;

sensorState.data.sensorState = 0;

/***Tell your sensor hardware you want some data here***/

// Get reply from hardware with data in message
readI2CReply(S1, sensorState.dataMsg.msg[0], NUM_BYTES_STATE_MSG);

// Display data
// (The union allows the above msg to be reinterpreted as short data without
// any extra processing)
StringFormat(aMsg, "heading: %d", sensorState.data.sensorState);
nxtDisplayTextLine(1, aMsg);

wait1Msec(10000);

StopAllTasks();
}


Mon Dec 31, 2007 8:10 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
I think you'll find the "union" keyword is now supported (release 1.17+) It can be used within a structure definition but is not presently available at the top level of a structure definition.


Wed Mar 05, 2008 1:40 am
Profile
Rookie
User avatar

Joined: Mon Dec 31, 2007 12:51 pm
Posts: 5
Location: United States
Post 
Mr Swan,
Thank you for responding to this post. I thought a union would be useful since some device registers store data larger than a byte, and conveniently ordered MSB LSB. Would you please explain by way of example what you mean by "used within a structure definition"? Would you mind reinterpreting from the above:
Code:
typedef union
{
SensorStateDataType data;
SensorStateMsgType dataMsg;
}SensorStateType;


Thank you for your continued support,
Olorin


Sun Mar 09, 2008 12:50 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
Olorin wrote:
Mr Swan,
Would you please explain by way of example what you mean by "used within a structure definition"?

In earlier versions of ROBOTC you needed to have "union" nested within a struct as follows
Code:
typedef struct
{
  union
  {
    int nValue2;
    char nChar;
  } X;
} StructDeclare;

The compiler was not accepting "typedef union". As of version 1.17, this has been fixed.
Code:
typedef union
{
  int nValue2;
  char nChar;
} StructDeclare;


Wed Mar 12, 2008 3:46 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.