Gets current time from the PC over a serial connection
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 #include "./Parser.h" 00004 #include "./terminal.h" 00005 00006 #define DEBUG_CMD_PROCESS 00007 00008 #define LED_ON 1 00009 #define LED_OFF 0 00010 DigitalOut AlarmLED(LED1); 00011 00012 00013 #define RXBUFFERSIZE 128 00014 #define RXBUFFERMASK 0x7F 00015 char receiveBuffer[RXBUFFERSIZE]; 00016 unsigned char insertPointRxBuffer; 00017 00018 Serial pc(USBTX, USBRX); // tx, rx 00019 void characterReceivedFromPC(); 00020 char globalStartofSentence; 00021 bool globalSentenceFromPCReady; 00022 //extern char *commandTokens[20]; 00023 00024 00025 00026 00027 // initializeRingBufferAndTokenArray: ----------------------------------------- 00028 // 00029 // 00030 //----------------------------------------------------------------------------- 00031 void initializeRingBufferAndTokenArray() 00032 { 00033 insertPointRxBuffer = 0; 00034 globalStartofSentence = 0; 00035 00036 globalSentenceFromPCReady = false; 00037 00038 //make sure the array is full of address 0x000's 00039 for (int i = 0; i< 20; i++) { 00040 commandTokens[i] = NULL; 00041 } 00042 }//eo initializeRingBufferAndTokenArray:: ===================================== 00043 00044 00045 00046 00047 // initializeSystem: ---------------------------------------------------------- 00048 // 00049 // 00050 //----------------------------------------------------------------------------- 00051 00052 void initializeSystem(void) 00053 { 00054 // lcd.printf("Hi Brad\n"); 00055 // lcd.locate(0,1); 00056 // lcd.printf("row 2\n"); 00057 00058 initializeRingBufferAndTokenArray(); 00059 initializePCTerminal(&pc); 00060 clearScreen(); 00061 00062 locateCursor(0, 2); 00063 drawBox(30, 10); 00064 pc.attach(characterReceivedFromPC); // set calback function to be called when a character is received 00065 00066 AlarmLED = LED_ON; 00067 wait(0.2); //wait .2 seconds 00068 AlarmLED = LED_OFF; 00069 wait(0.2); //wait .2 seconds 00070 AlarmLED = LED_ON; 00071 wait(0.2); //wait .2 seconds 00072 AlarmLED = LED_OFF; 00073 }//eo initializeSystem:: ====================================================== 00074 00075 00076 00077 00078 // characterReceivedFromPC: --------------------------------------------------- 00079 // 00080 // 00081 //----------------------------------------------------------------------------- 00082 00083 void characterReceivedFromPC() 00084 { 00085 static unsigned char StartofNextSentence = 0; 00086 while(pc.readable()) 00087 { 00088 receiveBuffer[insertPointRxBuffer] = pc.getc(); 00089 if( receiveBuffer[insertPointRxBuffer] == '$' ) 00090 { 00091 StartofNextSentence = insertPointRxBuffer; 00092 } 00093 if( receiveBuffer[insertPointRxBuffer] == '\r' ) 00094 { 00095 receiveBuffer[insertPointRxBuffer] = '\0'; 00096 00097 //makeSentence available to main loop 00098 globalStartofSentence = StartofNextSentence; 00099 globalSentenceFromPCReady = true; 00100 printf("\n\rsentence rec %s\n\r", &receiveBuffer[StartofNextSentence]); 00101 } 00102 insertPointRxBuffer = (insertPointRxBuffer + 1) & RXBUFFERMASK; 00103 } 00104 }//eo characterReceivedFromPC:: =============================================== 00105 00106 00107 00108 // processCommandMessage: ----------------------------------------------------- 00109 // transfer one sentence from incoming receive buffer into a command buffer for parsing 00110 // 00111 // 00112 //----------------------------------------------------------------------------- 00113 00114 void processCommandMessage() 00115 { 00116 unsigned char fp = globalStartofSentence; // initalize fetch point from receive buffer 00117 unsigned char ip = 0; // initilize insert point into command buffer 00118 00119 // copy the sentence (which contains a command) from the ring buffer (named receiveBuffer) 00120 // into a normal linear array named commandBuffer 00121 char commandBuffer[60]; 00122 #ifdef DEBUG_CMD_PROCESS 00123 printf("\n\r^^Start of sentence:%d\n\r", fp); 00124 #endif 00125 while(receiveBuffer[fp] != '\0') 00126 { 00127 #ifdef DEBUG_CMD_PROCESS 00128 if(pc.writeable()) pc.putc(receiveBuffer[fp]); 00129 #endif 00130 commandBuffer[ip] = receiveBuffer[fp]; 00131 ip++; 00132 fp = (fp+1) & RXBUFFERMASK; 00133 } 00134 commandBuffer[ip] = '\0'; 00135 00136 parseCommandSentence(commandBuffer); 00137 00138 #ifdef DEBUG_CMD_PROCESS 00139 // print out the tokens back to the terminal 00140 { 00141 int i = 0; 00142 while (strcmp(commandTokens[i],"END OF TOKENS" )) { 00143 pc.printf("$token %i: %s\n\r",i, commandTokens[i]); 00144 i++; 00145 } 00146 } 00147 #endif 00148 00149 if(strcmp(commandTokens[0],"$ST" )== 0) 00150 { 00151 long newtime = atol(commandTokens[1]); 00152 #ifdef DEBUG_CMD_PROCESS 00153 pc.printf("Setting time to: %l \n\r ", newtime); 00154 #endif 00155 set_time( newtime); 00156 //int limitValue = atoi(commandTokens[3]); 00157 //if( *commandTokens[2] == 'U') setUpperLimitForChannel(channel, limitValue); 00158 //if( *commandTokens[2] == 'L') setLowerLimitForChannel(channel, limitValue); 00159 } 00160 }//eo processCommandMessage:: ================================================= 00161 00162 00163 00164 // main: ---------------------------------------------------------------------- 00165 // 00166 // 00167 //----------------------------------------------------------------------------- 00168 00169 int main() { 00170 set_time(1256729737); 00171 initializeSystem(); 00172 00173 while(1) 00174 {//so infinite loop ................................................... 00175 if( globalSentenceFromPCReady == true ) 00176 { 00177 processCommandMessage(); 00178 globalSentenceFromPCReady = false; 00179 } 00180 00181 time_t seconds = time(NULL); 00182 00183 printf("Time as seconds since January 1, 1970 = %d\n", seconds); 00184 00185 printf("Time as a basic string = %s", ctime(&seconds)); 00186 00187 char buffer[32]; 00188 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); 00189 printf("Time as a custom formatted string = %s", buffer); 00190 00191 00192 00193 AlarmLED = LED_OFF; 00194 wait(10); 00195 AlarmLED = LED_ON; 00196 wait(1); 00197 }//eo infinite loop ::::::::::::::::::::::::::::::::::::::::::::::::::: 00198 00199 }//eo main ==================================================================== 00200 00201
Generated on Wed Jul 13 2022 12:22:03 by
1.7.2