Go Down

Topic: This code works but there must be a better/more elegant way (Read 3564 times) previous topic - next topic

lloyddean

#45
Apr 22, 2013, 03:00 am Last Edit: Apr 22, 2013, 03:02 am by lloyddean Reason: 1
Sorry for the lack of verbose description but hopefully this will be somewhat self explanatory.

Roughly your code aped back at you in the form of arrays of structs as I interpreted the last code you posted above.

Code: [Select]

// logically this describes to the compiler what a block of memory we've named
// as 'record_t' will look like.
//
// From it the compiler will then know such a item in memory will have field
// names as given, in the order given and of the sizes as specified by the data
// 'types' specified before the field name.
//
// collectively a symbol when defined of type 'record_t' occupies 6 bytes

struct record_t
{
   byte    Channel_Number;         // this will occupy 1 byte space
   int     Channel_Frequency;      // this will occupy 2 bytes space
   boolean Output_Pin1;            // this will occupy 1 byte space
   boolean Output_Pin2;            // this will occupy 1 byte space
   boolean Output_Pin3;            // this will occupy 1 byte space
};


// here we actually declare an arrays of our user defined type 'record_t' we've
// specified the layout of above
//
// this array of type 'record_t' will reserve space in memory for the number of
// 'record_t' as specified by the provided initializer values
//
// in this case if I've added them correctly we will have reservered space for 8
// sequential 6 byte blocks of memory in the form of an 'record_t' as specified
// above

// the initial array values can be set during declaration only as follows

record_t records[] =
{
     { 1, 5705, 0, 0, 0 }  // 0
   , { 2, 5685, 1, 0, 0 }  // 1
   , { 3, 5665, 0, 1, 0 }  // 2
   , { 4, 5645, 1, 1, 0 }  // 3
   , { 5, 5885, 0, 0, 1 }  // 4
   , { 6, 5905, 1, 0, 1 }  // 5
   , { 7, 5925, 0, 1, 1 }  // 6
   , { 8, 5945, 1, 1, 1 }  // 7
};

void setup()
{
   // copy the array members into the fields of the structure 'record_t' in the
   // order in which they appear listed n the array
   //
   //  'Channel_Number', 'Channel_Frequency', Output_Pin1, Output_Pin2, Output_Pin3
   //
   // the 'magic' that lets the compiler do this is the cast operator
   // '(record_t)' which tell the compiler the data is in the form of a
   // 'record_t'
   record[8] = (record_t){ 0, 1, true, false, false };
}

void loop()
{}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy