Thanks for the suggestions. Below is the code that I went with. Nice and simple. I think CRC check would be overkill since it is rare I will be setting the time and date.
I still need to add more error checking to make sure the strings are assembled correctly. Right now I am forced to set the time as HH:MM:SS and the date as MM/DD/YY.
I still need to add more error checking to make sure the strings are assembled correctly. Right now I am forced to set the time as HH:MM:SS and the date as MM/DD/YY.
Code:
if (PIR1bits.RC1IF)
{
unsigned c = uart_getChar();
if (c == '\r' || c == ';')
{
processCmd(cmdBuffer);
memset(cmdBuffer,0,strlen(cmdBuffer));
cmdBufferNdx = 0;
}
else
{
cmdBuffer[cmdBufferNdx] = c;
cmdBufferNdx++;
}
}
Code:
#include <string.h>
#include <stdio.h>
#include "processCmd.h"
#include "uart.h"
#include "picrtcc.h"
unsigned processCmdGetTime()
{
char buffer[25];
struct PICRTCC_date picRTCCDate;
struct PICRTCC_time picRTCCTime;
PICRTCC_readDateTime(&picRTCCDate,&picRTCCTime);
sprintf(buffer,"%c%c:%c%c:%c%c",picRTCCTime.hour.BCDparts.HRTEN+48,picRTCCTime.hour.BCDparts.HRONE+48,picRTCCTime.minute.BCDparts.MINTEN+48,picRTCCTime.minute.BCDparts.MINONE+48,picRTCCTime.second.BCDparts.SECTEN+48,picRTCCTime.second.BCDparts.SECONE+48);
return PROCESS_CMD_SUCCESS;
}
unsigned processCmdGetDate()
{
char buffer[25];
struct PICRTCC_date picRTCCDate;
struct PICRTCC_time picRTCCTime;
PICRTCC_readDateTime(&picRTCCDate,&picRTCCTime);
sprintf(buffer,"%c%c/%c%c/%c%c",picRTCCDate.day.BCDparts.DAYTEN+48,picRTCCDate.day.BCDparts.DAYONE,picRTCCDate.month.BCDparts.MTHTEN+48,picRTCCDate.month.BCDparts.MTHONE+48,picRTCCDate.year.BCDparts.YRTEN+48,picRTCCDate.year.BCDparts.YRTEN+48);
return PROCESS_CMD_SUCCESS;
}
unsigned char processCmdGet(char * buffer)
{
switch(buffer[0])
{
case 'T':
case 't':
return processCmdGetTime();
case 'D':
case 'd':
return processCmdGetDate();
}
return PROCESS_CMD_INVALID_GET;
}
unsigned char processCmdSetDate(char *buffer)
{
char *token;
char params[4][10];
char delimter[2] = "/";
unsigned char i=0;
token = strtok(buffer,delimter);
while (token != NULL)
{
strcpy(params[i],token);
i++;
token = strtok(NULL, delimter);
}
PICRTCC_setMonth(params[0][0], params[0][1]);
PICRTCC_setDay(params[1][0], params[1][1]);
PICRTCC_setYear(params[2][0], params[2][1]);
return PROCESS_CMD_SUCCESS;
}
unsigned char processCmdSetTime(char *buffer)
{
char *token;
char params[4][10];
char delimter[2] = ":";
unsigned char i=0;
token = strtok(buffer,delimter);
while (token != NULL)
{
strcpy(params[i],token);
i++;
token = strtok(NULL, delimter);
}
PICRTCC_setHour(params[0][0]-48, params[0][1]-48);
PICRTCC_setMinutes(params[1][0]-48, params[1][1]-48);
PICRTCC_setSeconds(params[2][0]-48, params[2][1]-48);
return PROCESS_CMD_SUCCESS;
}
unsigned char processCmdSet(char *buffer)
{
switch(buffer[0])
{
case 'T':
case 't':
return processCmdSetTime(&buffer[1]);
case 'D':
case 'd':
processCmdSetDate(&buffer[1]);
}
return PROCESS_CMD_INVALID_SET;
}
unsigned char processCmd(char *buffer)
{
switch(buffer[0])
{
case 'G':
case 'g':
return processCmdGet(&buffer[1]);
case 'S':
case 's':
return processCmdSet(&buffer[1]);
}
return PROCESS_CMD_INVALID_CMD;
}