Skip to content

Instantly share code, notes, and snippets.

@Rolias
Last active November 18, 2016 13:12
Show Gist options
  • Save Rolias/9240803 to your computer and use it in GitHub Desktop.
Save Rolias/9240803 to your computer and use it in GitHub Desktop.
An example of one way to safely share a buffer between two tasks using the NetBurner version of uCOS.
//In the TCP Task
void TcpServer::ProcessMessage(int num_bytes_read)
{
//There will always be room for the terminator because we only read a max of 1 less than full size
_rxBuffer[num_bytes_read++] = '\0';
if (_debuggingTcp) cout << "tcp:Read bytes:" << num_bytes_read << " string read = " << _rxBuffer << endl;
_messageWaiting = true; //Set this first and process pending on mbox MUST call SetMessageProcessed();
BYTE err_code = OSMboxPost(&_mailbox, static_cast<void*> (_rxBuffer)); //Put a message in the Mailbox.
if ((err_code != OS_NO_ERR) && _debuggingTcp) cout << "OSMboxPost error in tcpServer " << endl;
//Wait for response from task that processes the message.
err_code = OSSemPend(&_msgProcessed,MAX_WAIT_MSG_HANDLED_TICKS);
if (err_code != OS_NO_ERR) cout << "OSSemPend Error in TcpServer " <<endl;
}
//In the main event task which is using data passed in from the TCP Task
void Startup::MainEventLoop()
{
const bool FOREVER = true;
const int MAILBOX_WAIT_TICKS = 20;
OSTimeDly(MAILBOX_WAIT_TICKS);
OS_MBOX& tcp_mailbox = TcpServer::GetMailbox();
while (FOREVER)
{
BYTE err;
void * pdata_from_tcp = OSMboxPend( &tcp_mailbox, MAILBOX_WAIT_TICKS, &err);
if (err != OS_TIMEOUT)
{
Debug::Write("!",dl_TACITURN);
string puser_command = (char*) pdata_from_tcp;
ParseIncomingMsg(puser_command);
}
else
{
Debug::Write(".",dl_VERBOSE);
}
}
}
void Startup::ParseIncomingMsg(string userMsg)
{
cout << userMsg;
OS_SEM& msg_processed_sem = TcpServer::GetSemaphore();
OSSemPost(&msg_processed_sem);
}
@leiserhartbeck
Copy link

Hello Sir,
For a long time I was looking for something to refer to for cpp programming on net burner. You examples come as a big relief. It will be great if you could provide one complete example on how this works. A few questions though:

  1. How can we substitute the global variables with a class.
  2. How do we share data across classes without fear of corruption.
    Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment