Brad Smith
/
rtcController
Gets current time from the PC over a serial connection
Python script available on the wiki page
Revision 0:c8dd8b2c6942, committed 2015-11-12
- Comitter:
- mbedDevLondon
- Date:
- Thu Nov 12 19:17:47 2015 +0000
- Commit message:
- Converted to FRDM board
Changed in this revision
diff -r 000000000000 -r c8dd8b2c6942 Parser.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Parser.cpp Thu Nov 12 19:17:47 2015 +0000 @@ -0,0 +1,94 @@ +#include "./Parser.h" +#include <string.h> +#include <stdio.h> + + char *commandTokens[20]; + char invalidMessage[] = "INVALID NEMA SENTENCE"; + char endOfTokensMessage[] = "END OF TOKENS"; + +char validateCommandSentence(char *pSentence) +{ + char chkSumToken[3]; + unsigned char calculatedChecksum = 0; + char errorCode = NOERROR; + printf("\n\r\n\r>>>>>>>>>>>> %s <<<<\n\n\n\r", pSentence); + if(*pSentence != '$') + { + printf("error no $\n\r"); + return ERROR; + } + pSentence++; + + while (*pSentence != '\0' && *pSentence != '*') { + calculatedChecksum ^= *pSentence; + pSentence++; + } + + if(*pSentence == '*'){ + sprintf(chkSumToken, "%02x", calculatedChecksum); + printf("Calc chksum %s<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\r", chkSumToken); + pSentence++; + if(strcmp(pSentence, chkSumToken) != 0) + { + printf("checksum error>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); + errorCode = ERROR; + } + } + else { + printf("error:***** %s\n\r",pSentence); + errorCode = ERROR; + } + + printf("<<<<< errorcode %d\n\r", errorCode); + return errorCode; +} + +// parseCommandSentence() requires that the trailing <cr><lf> characters +// be removed prior to calling this function. Failing to do so will cause +// validateNemaSentence() to report an ERROR + +void parseCommandSentence( char *pBuffer) +{ + if (validateCommandSentence(pBuffer) == ERROR) { + commandTokens[0]= invalidMessage; + commandTokens[1]= endOfTokensMessage; + } + else{ + //so valid sentence ................................... + int tokenCounter = 0; + + commandTokens[tokenCounter]=pBuffer; + pBuffer++; + tokenCounter++; + + while( *pBuffer != '\0'){ + //until the end of sentence is found, + //search for comma deliminators + if(*pBuffer == ','){ + //found end of token + *pBuffer = '\0'; //replace deliminator with a null character + + pBuffer++; // save address of next token + commandTokens[tokenCounter]=pBuffer; + tokenCounter++; + } + else pBuffer++; + }//eo while not end of string + commandTokens[tokenCounter] = endOfTokensMessage; + + }//eo Valid sentence::::::::::::::::::::::::::::::::::::: + +}//eo parseNemaSentence( char *pBuffer) ======================================== + + +//calculate a XOR checksum for a string +char calcChecksum(char *pSentence) +{ + unsigned char calculatedChecksum = 0; + + while (*pSentence != '\0' ) { + calculatedChecksum ^= *pSentence; + pSentence++; + } + return calculatedChecksum; +}
diff -r 000000000000 -r c8dd8b2c6942 Parser.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Parser.h Thu Nov 12 19:17:47 2015 +0000 @@ -0,0 +1,15 @@ +#ifndef PARSER_H +#define PARSER_H + +#define ERROR 1 +#define NOERROR 0 + +extern char *commandTokens[20]; + +char validateCommandSentence(char *pSentence); +void parseCommandSentence( char *pBuffer); +char calcChecksum(char *pSentence); + + + +#endif \ No newline at end of file
diff -r 000000000000 -r c8dd8b2c6942 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Nov 12 19:17:47 2015 +0000 @@ -0,0 +1,201 @@ +#include "mbed.h" + +#include "./Parser.h" +#include "./terminal.h" + +#define DEBUG_CMD_PROCESS + +#define LED_ON 1 +#define LED_OFF 0 +DigitalOut AlarmLED(LED1); + + +#define RXBUFFERSIZE 128 +#define RXBUFFERMASK 0x7F +char receiveBuffer[RXBUFFERSIZE]; +unsigned char insertPointRxBuffer; + +Serial pc(USBTX, USBRX); // tx, rx +void characterReceivedFromPC(); +char globalStartofSentence; +bool globalSentenceFromPCReady; +//extern char *commandTokens[20]; + + + + +// initializeRingBufferAndTokenArray: ----------------------------------------- +// +// +//----------------------------------------------------------------------------- +void initializeRingBufferAndTokenArray() +{ + insertPointRxBuffer = 0; + globalStartofSentence = 0; + + globalSentenceFromPCReady = false; + + //make sure the array is full of address 0x000's + for (int i = 0; i< 20; i++) { + commandTokens[i] = NULL; + } +}//eo initializeRingBufferAndTokenArray:: ===================================== + + + + +// initializeSystem: ---------------------------------------------------------- +// +// +//----------------------------------------------------------------------------- + +void initializeSystem(void) +{ +// lcd.printf("Hi Brad\n"); +// lcd.locate(0,1); +// lcd.printf("row 2\n"); + + initializeRingBufferAndTokenArray(); + initializePCTerminal(&pc); + clearScreen(); + + locateCursor(0, 2); + drawBox(30, 10); + pc.attach(characterReceivedFromPC); // set calback function to be called when a character is received + + AlarmLED = LED_ON; + wait(0.2); //wait .2 seconds + AlarmLED = LED_OFF; + wait(0.2); //wait .2 seconds + AlarmLED = LED_ON; + wait(0.2); //wait .2 seconds + AlarmLED = LED_OFF; +}//eo initializeSystem:: ====================================================== + + + + +// characterReceivedFromPC: --------------------------------------------------- +// +// +//----------------------------------------------------------------------------- + +void characterReceivedFromPC() +{ + static unsigned char StartofNextSentence = 0; + while(pc.readable()) + { + receiveBuffer[insertPointRxBuffer] = pc.getc(); + if( receiveBuffer[insertPointRxBuffer] == '$' ) + { + StartofNextSentence = insertPointRxBuffer; + } + if( receiveBuffer[insertPointRxBuffer] == '\r' ) + { + receiveBuffer[insertPointRxBuffer] = '\0'; + + //makeSentence available to main loop + globalStartofSentence = StartofNextSentence; + globalSentenceFromPCReady = true; + printf("\n\rsentence rec %s\n\r", &receiveBuffer[StartofNextSentence]); + } + insertPointRxBuffer = (insertPointRxBuffer + 1) & RXBUFFERMASK; + } +}//eo characterReceivedFromPC:: =============================================== + + + +// processCommandMessage: ----------------------------------------------------- +// transfer one sentence from incoming receive buffer into a command buffer for parsing +// +// +//----------------------------------------------------------------------------- + +void processCommandMessage() +{ + unsigned char fp = globalStartofSentence; // initalize fetch point from receive buffer + unsigned char ip = 0; // initilize insert point into command buffer + + // copy the sentence (which contains a command) from the ring buffer (named receiveBuffer) + // into a normal linear array named commandBuffer + char commandBuffer[60]; + #ifdef DEBUG_CMD_PROCESS + printf("\n\r^^Start of sentence:%d\n\r", fp); + #endif + while(receiveBuffer[fp] != '\0') + { + #ifdef DEBUG_CMD_PROCESS + if(pc.writeable()) pc.putc(receiveBuffer[fp]); + #endif + commandBuffer[ip] = receiveBuffer[fp]; + ip++; + fp = (fp+1) & RXBUFFERMASK; + } + commandBuffer[ip] = '\0'; + + parseCommandSentence(commandBuffer); + + #ifdef DEBUG_CMD_PROCESS + // print out the tokens back to the terminal + { + int i = 0; + while (strcmp(commandTokens[i],"END OF TOKENS" )) { + pc.printf("$token %i: %s\n\r",i, commandTokens[i]); + i++; + } + } + #endif + + if(strcmp(commandTokens[0],"$ST" )== 0) + { + long newtime = atol(commandTokens[1]); + #ifdef DEBUG_CMD_PROCESS + pc.printf("Setting time to: %l \n\r ", newtime); + #endif + set_time( newtime); + //int limitValue = atoi(commandTokens[3]); + //if( *commandTokens[2] == 'U') setUpperLimitForChannel(channel, limitValue); + //if( *commandTokens[2] == 'L') setLowerLimitForChannel(channel, limitValue); + } +}//eo processCommandMessage:: ================================================= + + + +// main: ---------------------------------------------------------------------- +// +// +//----------------------------------------------------------------------------- + +int main() { + set_time(1256729737); + initializeSystem(); + + while(1) + {//so infinite loop ................................................... + if( globalSentenceFromPCReady == true ) + { + processCommandMessage(); + globalSentenceFromPCReady = false; + } + + time_t seconds = time(NULL); + + printf("Time as seconds since January 1, 1970 = %d\n", seconds); + + printf("Time as a basic string = %s", ctime(&seconds)); + + char buffer[32]; + strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); + printf("Time as a custom formatted string = %s", buffer); + + + + AlarmLED = LED_OFF; + wait(10); + AlarmLED = LED_ON; + wait(1); + }//eo infinite loop ::::::::::::::::::::::::::::::::::::::::::::::::::: + +}//eo main ==================================================================== + + \ No newline at end of file
diff -r 000000000000 -r c8dd8b2c6942 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Nov 12 19:17:47 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac \ No newline at end of file
diff -r 000000000000 -r c8dd8b2c6942 terminal.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/terminal.cpp Thu Nov 12 19:17:47 2015 +0000 @@ -0,0 +1,64 @@ +#include "mbed.h" +#include "terminal.h" + +Serial *pPCTerminal; +void initializePCTerminal(Serial *p) +{ + pPCTerminal = p; +} + + +void clearScreen() +{ + pPCTerminal->printf("\033[2J"); +} + +void locateCursor(int column, int row) +{ + // Cursor Home <ESC>[{ROW};{COLUMN}H + pPCTerminal->printf("\033[%d;%dH", row + 1, column + 1); +} + +int rgb888tobgr111(int colour) +{ + int r = (colour >> 23) & 1; + int g = (colour >> 15) & 1; + int b = (colour >> 7) & 1; + return (b << 2) | (g << 1) | (r << 0); +} + +void setForeground(int colour) +{ + // Set Attribute Mode <ESC>[{n}m + // Foreground Colours : 30 + bgr + int c = 30 + rgb888tobgr111(colour); + pPCTerminal->printf("\033[%dm", c); +} + +void setbBackground(int colour) +{ + // Set Attribute Mode <ESC>[{n}m + // Background Colours : 40 + bgr + int c = 40 + rgb888tobgr111(colour); + pPCTerminal->printf("\033[%dm", c); +} + +void drawBox(int width, int height) +{ + pPCTerminal->printf("\0154"); + for(int i = 1; i < (width - 2); i++)pPCTerminal->printf("%c",157); + pPCTerminal->printf("\0153 \n\r"); + + for(int row = 1; row < (height - 2); row++) + { + pPCTerminal->printf("\0170"); + for(int i = 1; i < (width - 2); i++)pPCTerminal->printf(" "); + pPCTerminal->printf("\0170\n\r"); + } + + + pPCTerminal->printf("\0155"); + for(int i = 1; i < (width - 2); i++)pPCTerminal->printf("\0163"); + pPCTerminal->printf("\052"); + +} \ No newline at end of file
diff -r 000000000000 -r c8dd8b2c6942 terminal.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/terminal.h Thu Nov 12 19:17:47 2015 +0000 @@ -0,0 +1,12 @@ +#ifndef TERMINAL_H +#define TERMINAL + +void initializePCTerminal(Serial *p); +void clearScreen(); +void locateCursor(int column, int row); +int rgb888tobgr111(int colour); +void setForeground(int colour); +void setbBackground(int colour) ; + +void drawBox(int width, int height); +#endif \ No newline at end of file