Gets current time from the PC over a serial connection

Dependencies:   mbed

Python script available on the wiki page

Committer:
mbedDevLondon
Date:
Thu Nov 12 19:17:47 2015 +0000
Revision:
0:c8dd8b2c6942
Converted to FRDM board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedDevLondon 0:c8dd8b2c6942 1 #include "mbed.h"
mbedDevLondon 0:c8dd8b2c6942 2
mbedDevLondon 0:c8dd8b2c6942 3 #include "./Parser.h"
mbedDevLondon 0:c8dd8b2c6942 4 #include "./terminal.h"
mbedDevLondon 0:c8dd8b2c6942 5
mbedDevLondon 0:c8dd8b2c6942 6 #define DEBUG_CMD_PROCESS
mbedDevLondon 0:c8dd8b2c6942 7
mbedDevLondon 0:c8dd8b2c6942 8 #define LED_ON 1
mbedDevLondon 0:c8dd8b2c6942 9 #define LED_OFF 0
mbedDevLondon 0:c8dd8b2c6942 10 DigitalOut AlarmLED(LED1);
mbedDevLondon 0:c8dd8b2c6942 11
mbedDevLondon 0:c8dd8b2c6942 12
mbedDevLondon 0:c8dd8b2c6942 13 #define RXBUFFERSIZE 128
mbedDevLondon 0:c8dd8b2c6942 14 #define RXBUFFERMASK 0x7F
mbedDevLondon 0:c8dd8b2c6942 15 char receiveBuffer[RXBUFFERSIZE];
mbedDevLondon 0:c8dd8b2c6942 16 unsigned char insertPointRxBuffer;
mbedDevLondon 0:c8dd8b2c6942 17
mbedDevLondon 0:c8dd8b2c6942 18 Serial pc(USBTX, USBRX); // tx, rx
mbedDevLondon 0:c8dd8b2c6942 19 void characterReceivedFromPC();
mbedDevLondon 0:c8dd8b2c6942 20 char globalStartofSentence;
mbedDevLondon 0:c8dd8b2c6942 21 bool globalSentenceFromPCReady;
mbedDevLondon 0:c8dd8b2c6942 22 //extern char *commandTokens[20];
mbedDevLondon 0:c8dd8b2c6942 23
mbedDevLondon 0:c8dd8b2c6942 24
mbedDevLondon 0:c8dd8b2c6942 25
mbedDevLondon 0:c8dd8b2c6942 26
mbedDevLondon 0:c8dd8b2c6942 27 // initializeRingBufferAndTokenArray: -----------------------------------------
mbedDevLondon 0:c8dd8b2c6942 28 //
mbedDevLondon 0:c8dd8b2c6942 29 //
mbedDevLondon 0:c8dd8b2c6942 30 //-----------------------------------------------------------------------------
mbedDevLondon 0:c8dd8b2c6942 31 void initializeRingBufferAndTokenArray()
mbedDevLondon 0:c8dd8b2c6942 32 {
mbedDevLondon 0:c8dd8b2c6942 33 insertPointRxBuffer = 0;
mbedDevLondon 0:c8dd8b2c6942 34 globalStartofSentence = 0;
mbedDevLondon 0:c8dd8b2c6942 35
mbedDevLondon 0:c8dd8b2c6942 36 globalSentenceFromPCReady = false;
mbedDevLondon 0:c8dd8b2c6942 37
mbedDevLondon 0:c8dd8b2c6942 38 //make sure the array is full of address 0x000's
mbedDevLondon 0:c8dd8b2c6942 39 for (int i = 0; i< 20; i++) {
mbedDevLondon 0:c8dd8b2c6942 40 commandTokens[i] = NULL;
mbedDevLondon 0:c8dd8b2c6942 41 }
mbedDevLondon 0:c8dd8b2c6942 42 }//eo initializeRingBufferAndTokenArray:: =====================================
mbedDevLondon 0:c8dd8b2c6942 43
mbedDevLondon 0:c8dd8b2c6942 44
mbedDevLondon 0:c8dd8b2c6942 45
mbedDevLondon 0:c8dd8b2c6942 46
mbedDevLondon 0:c8dd8b2c6942 47 // initializeSystem: ----------------------------------------------------------
mbedDevLondon 0:c8dd8b2c6942 48 //
mbedDevLondon 0:c8dd8b2c6942 49 //
mbedDevLondon 0:c8dd8b2c6942 50 //-----------------------------------------------------------------------------
mbedDevLondon 0:c8dd8b2c6942 51
mbedDevLondon 0:c8dd8b2c6942 52 void initializeSystem(void)
mbedDevLondon 0:c8dd8b2c6942 53 {
mbedDevLondon 0:c8dd8b2c6942 54 // lcd.printf("Hi Brad\n");
mbedDevLondon 0:c8dd8b2c6942 55 // lcd.locate(0,1);
mbedDevLondon 0:c8dd8b2c6942 56 // lcd.printf("row 2\n");
mbedDevLondon 0:c8dd8b2c6942 57
mbedDevLondon 0:c8dd8b2c6942 58 initializeRingBufferAndTokenArray();
mbedDevLondon 0:c8dd8b2c6942 59 initializePCTerminal(&pc);
mbedDevLondon 0:c8dd8b2c6942 60 clearScreen();
mbedDevLondon 0:c8dd8b2c6942 61
mbedDevLondon 0:c8dd8b2c6942 62 locateCursor(0, 2);
mbedDevLondon 0:c8dd8b2c6942 63 drawBox(30, 10);
mbedDevLondon 0:c8dd8b2c6942 64 pc.attach(characterReceivedFromPC); // set calback function to be called when a character is received
mbedDevLondon 0:c8dd8b2c6942 65
mbedDevLondon 0:c8dd8b2c6942 66 AlarmLED = LED_ON;
mbedDevLondon 0:c8dd8b2c6942 67 wait(0.2); //wait .2 seconds
mbedDevLondon 0:c8dd8b2c6942 68 AlarmLED = LED_OFF;
mbedDevLondon 0:c8dd8b2c6942 69 wait(0.2); //wait .2 seconds
mbedDevLondon 0:c8dd8b2c6942 70 AlarmLED = LED_ON;
mbedDevLondon 0:c8dd8b2c6942 71 wait(0.2); //wait .2 seconds
mbedDevLondon 0:c8dd8b2c6942 72 AlarmLED = LED_OFF;
mbedDevLondon 0:c8dd8b2c6942 73 }//eo initializeSystem:: ======================================================
mbedDevLondon 0:c8dd8b2c6942 74
mbedDevLondon 0:c8dd8b2c6942 75
mbedDevLondon 0:c8dd8b2c6942 76
mbedDevLondon 0:c8dd8b2c6942 77
mbedDevLondon 0:c8dd8b2c6942 78 // characterReceivedFromPC: ---------------------------------------------------
mbedDevLondon 0:c8dd8b2c6942 79 //
mbedDevLondon 0:c8dd8b2c6942 80 //
mbedDevLondon 0:c8dd8b2c6942 81 //-----------------------------------------------------------------------------
mbedDevLondon 0:c8dd8b2c6942 82
mbedDevLondon 0:c8dd8b2c6942 83 void characterReceivedFromPC()
mbedDevLondon 0:c8dd8b2c6942 84 {
mbedDevLondon 0:c8dd8b2c6942 85 static unsigned char StartofNextSentence = 0;
mbedDevLondon 0:c8dd8b2c6942 86 while(pc.readable())
mbedDevLondon 0:c8dd8b2c6942 87 {
mbedDevLondon 0:c8dd8b2c6942 88 receiveBuffer[insertPointRxBuffer] = pc.getc();
mbedDevLondon 0:c8dd8b2c6942 89 if( receiveBuffer[insertPointRxBuffer] == '$' )
mbedDevLondon 0:c8dd8b2c6942 90 {
mbedDevLondon 0:c8dd8b2c6942 91 StartofNextSentence = insertPointRxBuffer;
mbedDevLondon 0:c8dd8b2c6942 92 }
mbedDevLondon 0:c8dd8b2c6942 93 if( receiveBuffer[insertPointRxBuffer] == '\r' )
mbedDevLondon 0:c8dd8b2c6942 94 {
mbedDevLondon 0:c8dd8b2c6942 95 receiveBuffer[insertPointRxBuffer] = '\0';
mbedDevLondon 0:c8dd8b2c6942 96
mbedDevLondon 0:c8dd8b2c6942 97 //makeSentence available to main loop
mbedDevLondon 0:c8dd8b2c6942 98 globalStartofSentence = StartofNextSentence;
mbedDevLondon 0:c8dd8b2c6942 99 globalSentenceFromPCReady = true;
mbedDevLondon 0:c8dd8b2c6942 100 printf("\n\rsentence rec %s\n\r", &receiveBuffer[StartofNextSentence]);
mbedDevLondon 0:c8dd8b2c6942 101 }
mbedDevLondon 0:c8dd8b2c6942 102 insertPointRxBuffer = (insertPointRxBuffer + 1) & RXBUFFERMASK;
mbedDevLondon 0:c8dd8b2c6942 103 }
mbedDevLondon 0:c8dd8b2c6942 104 }//eo characterReceivedFromPC:: ===============================================
mbedDevLondon 0:c8dd8b2c6942 105
mbedDevLondon 0:c8dd8b2c6942 106
mbedDevLondon 0:c8dd8b2c6942 107
mbedDevLondon 0:c8dd8b2c6942 108 // processCommandMessage: -----------------------------------------------------
mbedDevLondon 0:c8dd8b2c6942 109 // transfer one sentence from incoming receive buffer into a command buffer for parsing
mbedDevLondon 0:c8dd8b2c6942 110 //
mbedDevLondon 0:c8dd8b2c6942 111 //
mbedDevLondon 0:c8dd8b2c6942 112 //-----------------------------------------------------------------------------
mbedDevLondon 0:c8dd8b2c6942 113
mbedDevLondon 0:c8dd8b2c6942 114 void processCommandMessage()
mbedDevLondon 0:c8dd8b2c6942 115 {
mbedDevLondon 0:c8dd8b2c6942 116 unsigned char fp = globalStartofSentence; // initalize fetch point from receive buffer
mbedDevLondon 0:c8dd8b2c6942 117 unsigned char ip = 0; // initilize insert point into command buffer
mbedDevLondon 0:c8dd8b2c6942 118
mbedDevLondon 0:c8dd8b2c6942 119 // copy the sentence (which contains a command) from the ring buffer (named receiveBuffer)
mbedDevLondon 0:c8dd8b2c6942 120 // into a normal linear array named commandBuffer
mbedDevLondon 0:c8dd8b2c6942 121 char commandBuffer[60];
mbedDevLondon 0:c8dd8b2c6942 122 #ifdef DEBUG_CMD_PROCESS
mbedDevLondon 0:c8dd8b2c6942 123 printf("\n\r^^Start of sentence:%d\n\r", fp);
mbedDevLondon 0:c8dd8b2c6942 124 #endif
mbedDevLondon 0:c8dd8b2c6942 125 while(receiveBuffer[fp] != '\0')
mbedDevLondon 0:c8dd8b2c6942 126 {
mbedDevLondon 0:c8dd8b2c6942 127 #ifdef DEBUG_CMD_PROCESS
mbedDevLondon 0:c8dd8b2c6942 128 if(pc.writeable()) pc.putc(receiveBuffer[fp]);
mbedDevLondon 0:c8dd8b2c6942 129 #endif
mbedDevLondon 0:c8dd8b2c6942 130 commandBuffer[ip] = receiveBuffer[fp];
mbedDevLondon 0:c8dd8b2c6942 131 ip++;
mbedDevLondon 0:c8dd8b2c6942 132 fp = (fp+1) & RXBUFFERMASK;
mbedDevLondon 0:c8dd8b2c6942 133 }
mbedDevLondon 0:c8dd8b2c6942 134 commandBuffer[ip] = '\0';
mbedDevLondon 0:c8dd8b2c6942 135
mbedDevLondon 0:c8dd8b2c6942 136 parseCommandSentence(commandBuffer);
mbedDevLondon 0:c8dd8b2c6942 137
mbedDevLondon 0:c8dd8b2c6942 138 #ifdef DEBUG_CMD_PROCESS
mbedDevLondon 0:c8dd8b2c6942 139 // print out the tokens back to the terminal
mbedDevLondon 0:c8dd8b2c6942 140 {
mbedDevLondon 0:c8dd8b2c6942 141 int i = 0;
mbedDevLondon 0:c8dd8b2c6942 142 while (strcmp(commandTokens[i],"END OF TOKENS" )) {
mbedDevLondon 0:c8dd8b2c6942 143 pc.printf("$token %i: %s\n\r",i, commandTokens[i]);
mbedDevLondon 0:c8dd8b2c6942 144 i++;
mbedDevLondon 0:c8dd8b2c6942 145 }
mbedDevLondon 0:c8dd8b2c6942 146 }
mbedDevLondon 0:c8dd8b2c6942 147 #endif
mbedDevLondon 0:c8dd8b2c6942 148
mbedDevLondon 0:c8dd8b2c6942 149 if(strcmp(commandTokens[0],"$ST" )== 0)
mbedDevLondon 0:c8dd8b2c6942 150 {
mbedDevLondon 0:c8dd8b2c6942 151 long newtime = atol(commandTokens[1]);
mbedDevLondon 0:c8dd8b2c6942 152 #ifdef DEBUG_CMD_PROCESS
mbedDevLondon 0:c8dd8b2c6942 153 pc.printf("Setting time to: %l \n\r ", newtime);
mbedDevLondon 0:c8dd8b2c6942 154 #endif
mbedDevLondon 0:c8dd8b2c6942 155 set_time( newtime);
mbedDevLondon 0:c8dd8b2c6942 156 //int limitValue = atoi(commandTokens[3]);
mbedDevLondon 0:c8dd8b2c6942 157 //if( *commandTokens[2] == 'U') setUpperLimitForChannel(channel, limitValue);
mbedDevLondon 0:c8dd8b2c6942 158 //if( *commandTokens[2] == 'L') setLowerLimitForChannel(channel, limitValue);
mbedDevLondon 0:c8dd8b2c6942 159 }
mbedDevLondon 0:c8dd8b2c6942 160 }//eo processCommandMessage:: =================================================
mbedDevLondon 0:c8dd8b2c6942 161
mbedDevLondon 0:c8dd8b2c6942 162
mbedDevLondon 0:c8dd8b2c6942 163
mbedDevLondon 0:c8dd8b2c6942 164 // main: ----------------------------------------------------------------------
mbedDevLondon 0:c8dd8b2c6942 165 //
mbedDevLondon 0:c8dd8b2c6942 166 //
mbedDevLondon 0:c8dd8b2c6942 167 //-----------------------------------------------------------------------------
mbedDevLondon 0:c8dd8b2c6942 168
mbedDevLondon 0:c8dd8b2c6942 169 int main() {
mbedDevLondon 0:c8dd8b2c6942 170 set_time(1256729737);
mbedDevLondon 0:c8dd8b2c6942 171 initializeSystem();
mbedDevLondon 0:c8dd8b2c6942 172
mbedDevLondon 0:c8dd8b2c6942 173 while(1)
mbedDevLondon 0:c8dd8b2c6942 174 {//so infinite loop ...................................................
mbedDevLondon 0:c8dd8b2c6942 175 if( globalSentenceFromPCReady == true )
mbedDevLondon 0:c8dd8b2c6942 176 {
mbedDevLondon 0:c8dd8b2c6942 177 processCommandMessage();
mbedDevLondon 0:c8dd8b2c6942 178 globalSentenceFromPCReady = false;
mbedDevLondon 0:c8dd8b2c6942 179 }
mbedDevLondon 0:c8dd8b2c6942 180
mbedDevLondon 0:c8dd8b2c6942 181 time_t seconds = time(NULL);
mbedDevLondon 0:c8dd8b2c6942 182
mbedDevLondon 0:c8dd8b2c6942 183 printf("Time as seconds since January 1, 1970 = %d\n", seconds);
mbedDevLondon 0:c8dd8b2c6942 184
mbedDevLondon 0:c8dd8b2c6942 185 printf("Time as a basic string = %s", ctime(&seconds));
mbedDevLondon 0:c8dd8b2c6942 186
mbedDevLondon 0:c8dd8b2c6942 187 char buffer[32];
mbedDevLondon 0:c8dd8b2c6942 188 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
mbedDevLondon 0:c8dd8b2c6942 189 printf("Time as a custom formatted string = %s", buffer);
mbedDevLondon 0:c8dd8b2c6942 190
mbedDevLondon 0:c8dd8b2c6942 191
mbedDevLondon 0:c8dd8b2c6942 192
mbedDevLondon 0:c8dd8b2c6942 193 AlarmLED = LED_OFF;
mbedDevLondon 0:c8dd8b2c6942 194 wait(10);
mbedDevLondon 0:c8dd8b2c6942 195 AlarmLED = LED_ON;
mbedDevLondon 0:c8dd8b2c6942 196 wait(1);
mbedDevLondon 0:c8dd8b2c6942 197 }//eo infinite loop :::::::::::::::::::::::::::::::::::::::::::::::::::
mbedDevLondon 0:c8dd8b2c6942 198
mbedDevLondon 0:c8dd8b2c6942 199 }//eo main ====================================================================
mbedDevLondon 0:c8dd8b2c6942 200
mbedDevLondon 0:c8dd8b2c6942 201