M
Michele
Slowly embracing C++ and as such I've got C code (see snippet below)
I'd like to essentially port to C++. The idea is to
1. Use a TEMPLATE class to handle the 'double' buffering scheme
outlined in the code. Perhaps an operator= approach would be ideal.
I've looked at benchmarks per these newsgroups on std::copy and
realize it's too slow when compared to memcopy.
2. Inside the template class, create a function that'll return the
'latest' double buffered copy of the the appropriate struct
3. Perhaps create a function to compare the two buffers.
Of course I'll leave the class to implement whatever else I could
dream of. The question. Could I garner assistance on an
implementation approach. Perhapts the LDS_MESSAGE_STRUCT version is a
good start.
Assume the LSD_MESSAGE_STRUCT is
typedef struct _LDS_MESSAGE_STRUCT
{
unsigned int x : 16;
unsinged int y : 16;
} LDS_MESSAGE_STRUCT ;
Just unsure which direction to run.
void Rx_SMML_Msg()
{
char buffer[PAYLOAD];
STATUS status;
int size, i;
LDS_HEADER_RECORD MessageID, Message_type;
// call Rx_Message() to process received messages
Rx_Message( (UCHAR*)buffer, &size, &MsgStatus );
// check status
if( MsgStatus != OK )
{
rx_error_count++; // increment error count
printf( "Rx_Message interrupt process error -- %d\n",
rx_error_count);
return;
}
MessageID = ((LDS_HEADER_RECORD *)buffer)->ID;
printf("\n\Received msg ==> MessageID = %x\n",MessageID );
// parse out message type - check error bit
if ( (MessageID & MSG_ID_ERROR_MASK) == 0 )
{
// Good message. Parse out message type from MessageID
Message_type = (((( MessageID >> 4 ) & MSG_ID_MASK ) <<3 )) | (
MessageID & MSG_ID_MASK );
switch( Message_type )
{
case LDS_Control_Message__Control:
if( lds_system_control_message_ready ) // check if not
emptied
{
lds_system_control_message_in_use++;
printf( "LDS_System_Control error ==> buffer use -- %d\n",
lds_system_control_message_in_use);
}
else
{
printf( "LDS_System_Control Message loaded !!!\n");
// start double buffering scheme.
i = lds_system_control_message_index ^ 1;
memcpy( &lds_system_control_message, buffer,
sizeof(LDS_MESSAGE_STRUCT) );
lds_system_control_message_ready = TRUE;
// toggle index 0/1
lds_system_control_message_index ^= 1;
}
break;
case RDS_Message__System_Control:
if( rds_system_control_message_ready ) // check if not
emptied
{
rds_system_control_message_in_use++;
printf( "RDS_System_Control error ==> buffer use -- %d\n",
rds_system_control_message_in_use);
}
else
{
printf( "RDS_System_Control Message loaded !!!\n");
// start double buffering scheme.
i = rds_system_control_message_index ^ 1;
memcpy( &rds_system_control_message, buffer,
sizeof(RDS_MESSAGE_STRUCT) );
rds_system_control_message_ready = TRUE;
// toggle index 0/1
rds_system_control_message_index ^= 1;
}
break;
// LOTS more like the two case statement above
default:// unknown message
printf("UNKNOWN MESSAGE RCV'D !!!\n");
break; // add an error
}
}
else//message error bit set
{
switch( MessageID )
{
case LDS_System_Control_Err:
lds_system_control_message_error++;
printf( "LDS_System_Control_Msg_Err_Bit -- %d\n",
lds_system_control_message_error);
break;
case RDS_System_Control_Err:
rds_system_control_message_error++;
printf( "RDS_System_Control_Msg_Err_Bit -- %d\n",
rds_system_control_message_error);
break;
default:
printf( "Message_Err of some sort -- %d\n");
break; // add an error
}
}
}
return;
}
I'd like to essentially port to C++. The idea is to
1. Use a TEMPLATE class to handle the 'double' buffering scheme
outlined in the code. Perhaps an operator= approach would be ideal.
I've looked at benchmarks per these newsgroups on std::copy and
realize it's too slow when compared to memcopy.
2. Inside the template class, create a function that'll return the
'latest' double buffered copy of the the appropriate struct
3. Perhaps create a function to compare the two buffers.
Of course I'll leave the class to implement whatever else I could
dream of. The question. Could I garner assistance on an
implementation approach. Perhapts the LDS_MESSAGE_STRUCT version is a
good start.
Assume the LSD_MESSAGE_STRUCT is
typedef struct _LDS_MESSAGE_STRUCT
{
unsigned int x : 16;
unsinged int y : 16;
} LDS_MESSAGE_STRUCT ;
Just unsure which direction to run.
void Rx_SMML_Msg()
{
char buffer[PAYLOAD];
STATUS status;
int size, i;
LDS_HEADER_RECORD MessageID, Message_type;
// call Rx_Message() to process received messages
Rx_Message( (UCHAR*)buffer, &size, &MsgStatus );
// check status
if( MsgStatus != OK )
{
rx_error_count++; // increment error count
printf( "Rx_Message interrupt process error -- %d\n",
rx_error_count);
return;
}
MessageID = ((LDS_HEADER_RECORD *)buffer)->ID;
printf("\n\Received msg ==> MessageID = %x\n",MessageID );
// parse out message type - check error bit
if ( (MessageID & MSG_ID_ERROR_MASK) == 0 )
{
// Good message. Parse out message type from MessageID
Message_type = (((( MessageID >> 4 ) & MSG_ID_MASK ) <<3 )) | (
MessageID & MSG_ID_MASK );
switch( Message_type )
{
case LDS_Control_Message__Control:
if( lds_system_control_message_ready ) // check if not
emptied
{
lds_system_control_message_in_use++;
printf( "LDS_System_Control error ==> buffer use -- %d\n",
lds_system_control_message_in_use);
}
else
{
printf( "LDS_System_Control Message loaded !!!\n");
// start double buffering scheme.
i = lds_system_control_message_index ^ 1;
memcpy( &lds_system_control_message, buffer,
sizeof(LDS_MESSAGE_STRUCT) );
lds_system_control_message_ready = TRUE;
// toggle index 0/1
lds_system_control_message_index ^= 1;
}
break;
case RDS_Message__System_Control:
if( rds_system_control_message_ready ) // check if not
emptied
{
rds_system_control_message_in_use++;
printf( "RDS_System_Control error ==> buffer use -- %d\n",
rds_system_control_message_in_use);
}
else
{
printf( "RDS_System_Control Message loaded !!!\n");
// start double buffering scheme.
i = rds_system_control_message_index ^ 1;
memcpy( &rds_system_control_message, buffer,
sizeof(RDS_MESSAGE_STRUCT) );
rds_system_control_message_ready = TRUE;
// toggle index 0/1
rds_system_control_message_index ^= 1;
}
break;
// LOTS more like the two case statement above
default:// unknown message
printf("UNKNOWN MESSAGE RCV'D !!!\n");
break; // add an error
}
}
else//message error bit set
{
switch( MessageID )
{
case LDS_System_Control_Err:
lds_system_control_message_error++;
printf( "LDS_System_Control_Msg_Err_Bit -- %d\n",
lds_system_control_message_error);
break;
case RDS_System_Control_Err:
rds_system_control_message_error++;
printf( "RDS_System_Control_Msg_Err_Bit -- %d\n",
rds_system_control_message_error);
break;
default:
printf( "Message_Err of some sort -- %d\n");
break; // add an error
}
}
}
return;
}