fuck this

Dependencies:   BMP280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Serial.cpp Source File

Serial.cpp

00001 #include "Serial.h"
00002 #include "mbed.h"
00003 #include "Sampling.h"
00004 #include "TimeInterface.h"
00005 #include "Logging.h"
00006 
00007 #include <string>
00008 
00009 Thread SerialThread;
00010 
00011 //Variables
00012 int DateTimeVar[3];
00013 bool SerialState;
00014 char InputBufferText[128];
00015 unsigned short InputBufferNum;
00016 unsigned short internalIndex;
00017 
00018 //float tempReadingsSerial[BUFFERSIZE] = {};
00019 //float presReadingsSerial[BUFFERSIZE] = {};
00020 //float LDRReadingsSerial[BUFFERSIZE] = {};
00021 //time_t timeReadingsSerial[BUFFERSIZE] = {};
00022 
00023 
00024 void SerialStart(void)
00025 {
00026     SerialThread.start(&SerialCode);   //Start thread running 
00027     Thread SerialThread(osPriorityNormal, 32 * 1024);  //Normal priority with a 32k stack size 
00028     PC.attach(&RXInterruptISR); //Attach interrupt function to hardware interrupt
00029 }
00030 
00031 
00032 void RXInterruptISR(void)
00033 {
00034     SerialThread.signal_set(1); //Set thread signal to start SerialCode
00035     PC.attach(NULL); //Disable interrupt
00036 }
00037 
00038 
00039 void SerialCode(void)
00040 {
00041     while(true) {
00042         Thread::signal_wait(1);          //Wait untill signal set
00043         PC.scanf("%s", InputBufferText); //Scan serial
00044         PC.printf("Command: %s\n\r", InputBufferText); //Print input string. Readback
00045 
00046         string InputBufferString(InputBufferText); //Convert array of char to string
00047 
00048 
00049         if (InputBufferString == "test") {
00050             //Test function
00051             PC.printf("testsuccess\n\r");
00052 
00053 
00054         } else if (InputBufferString == "help") {
00055             //Help function
00056             PC.printf("Here is an usefull list of commands:\n\r");
00057             PC.printf(" readall\n\r deleteall\n\r read <n>\n\r delete <n>\n\r setdate <dd><mm><yyyy>\n\r settime <hh><mm><ss>\n\r sett <T>\n\r state <x>\n\r logging <x>\n\r");
00058 
00059 
00060         } else if (InputBufferString == "readall") {
00061             //Reads all samples in buffers and prints oldest first
00062             Sampling(false); //Stop sampling
00063             internalIndex = currentIndex; //InternalIndex for incrementing out data
00064             PC.printf("Printing all %d reccords\n\r",BUFFERSIZE);
00065             PC.printf("  Date  |  Time  | Temp | Pressure | Light\n\r");
00066             
00067             TakeKeys(true); //Take keys
00068             for (short i = 0; i < BUFFERSIZE; i++) { //For loop of length buffersize
00069                 tm T = ReturnDateTimeStruct(timeReadings[internalIndex]);
00070                 //InternalIndex was set as newest. We will now decrement to display oldest to newest
00071                 PC.printf(" %4d/%2d/%2d    %2d:%2d:%2d    %2.2f     %2.2f     %2.2f\n\r",T.tm_year,T.tm_mon,T.tm_mday,T.tm_hour,T.tm_min,T.tm_sec,tempReadings[internalIndex],presReadings[internalIndex],LDRReadings[internalIndex]);
00072                 //PC.printf("%d\n\r",T.tm_year);
00073                 internalIndex = IndexDecrement(internalIndex); //Decrement internal index
00074             }
00075 
00076             TakeKeys(false); //Return keys
00077             Sampling(true);  //Start sampling
00078 
00079 
00080         } else if (InputBufferString == "deleteall") {
00081             //Deletes all reccords
00082 
00083             Sampling(false);//Stop sampling
00084 
00085             TakeKeys(true); //Take keys
00086 
00087             memset(tempReadings, 0.0, BUFFERSIZE * sizeof(float)); //Fill array with 0s
00088             memset(presReadings, 0.0, BUFFERSIZE * sizeof(float));
00089             memset(LDRReadings, 0.0, BUFFERSIZE * sizeof(float));
00090             memset(timeReadings, 0.0, BUFFERSIZE * sizeof(float));
00091 
00092             nextIndex = 0;   //Reset Index
00093             currentIndex = 0;
00094             oldestIndex = 0;
00095 
00096             firstSample = true;
00097 
00098             TakeKeys(false); //Return keys
00099 
00100             Sampling(true);  //Start sampling
00101 
00102             PC.printf("Deleted %d records\n\r",BUFFERSIZE);
00103 
00104 
00105         } else if (InputBufferString == "read") {
00106             //Reads past <n> records
00107             PC.printf("How many records would you like to view:\n\r");
00108             PC.attach(&RXInterruptISR);  //Enable interrupt
00109             Thread::signal_wait(1);      //Wait untill signal set
00110 
00111             PC.scanf("%d", &InputBufferNum); //Scan serial
00112 
00113             if (InputBufferNum >= BUFFERSIZE) { //If requested number of samples is greater than buffersize
00114                 InputBufferNum = BUFFERSIZE;    //Then set number of samples equal to buffersize
00115             }
00116 
00117             internalIndex = currentIndex; //InternalIndex for incrementing out data
00118             PC.printf("Showing the newest %d records:\n\r", InputBufferNum);
00119             PC.printf("  Date  |  Time  | Temp | Pressure | Light\n\r");
00120             TakeKeys(true); //Take keys
00121 
00122             for (short i = 0; i < InputBufferNum; i++) { //For loop of length InputBufferNum
00123                 tm T = ReturnDateTimeStruct(timeReadings[internalIndex]);
00124                 PC.printf(" %4d/%2d/%2d    %2d:%2d:%2d    %2.2f     %2.2f     %2.2f\n\r",T.tm_year,T.tm_mon,T.tm_mday,T.tm_hour,T.tm_min,T.tm_sec,tempReadings[internalIndex],presReadings[internalIndex],LDRReadings[internalIndex]);
00125                 internalIndex = IndexDecrement(internalIndex); //Decrement internal index
00126             }
00127             TakeKeys(false); //Return keys
00128 
00129 
00130         } else if (InputBufferString == "delete") {
00131             //Deletes oldest <n> records
00132             PC.printf("How many records would you like to delete:\n\r");
00133             PC.attach(&RXInterruptISR); //Enable interrupt
00134             Thread::signal_wait(1);      //Wait untill signal set
00135 
00136             PC.scanf("%d", &InputBufferNum); //Scan serial
00137             Sampling(false);  //Stop sampling
00138             TakeKeys(true); //Take keys
00139 
00140             for (int i = 0; i < InputBufferNum; i++) {
00141                 tempReadings[oldestIndex] = 0; //Fill array with 0s
00142                 presReadings[oldestIndex] = 0;
00143                 LDRReadings[oldestIndex] = 0;
00144                 timeReadings[oldestIndex] = 0;
00145 
00146                 oldestIndex = IndexIncrement(oldestIndex); //Increment index
00147             }
00148 
00149             TakeKeys(false); //Return keys
00150             Sampling(true);  //Start sampling
00151             PC.printf("Deleated %d records", InputBufferNum); //Scan serial
00152 
00153 
00154         } else if (InputBufferString == "setdate") {
00155             //Set date YYYY/MM/DD
00156             Sampling(false);  //Stop sampling
00157             PC.printf("Setting Date\n\r");
00158 
00159             for (int i = 0; i < 3; i++) { //Changes message on each iteration
00160                 if (i==0) {
00161                     PC.printf("Input Year <YYYY>:\n\r");
00162                 } else if(i==1) {
00163                     PC.printf("Input Month <MM>:\n\r");
00164                 } else if (i==2) {
00165                     PC.printf("Input Date <DD>:\n\r");
00166                 }
00167 
00168                 PC.attach(&RXInterruptISR);  //Enable interrupt
00169                 Thread::signal_wait(1);      //Wait untill signal set
00170 
00171                 PC.scanf("%d", &DateTimeVar[i]); //Scan serial
00172             }
00173 
00174             SetDate(DateTimeVar[2], DateTimeVar[1], DateTimeVar[0]); //Set date
00175             PC.printf("Date set to : %d/%d/%d",DateTimeVar[2], DateTimeVar[1], DateTimeVar[0]);
00176             Sampling(true);  //Stop sampling
00177 
00178 
00179         } else if (InputBufferString == "settime") {
00180             //Set time hh/mm/ss
00181             Sampling(false);  //Stop sampling
00182             PC.printf("Setting Time\n\r");
00183 
00184             for (int i = 0; i < 3; i++) { //Changes message on each iteration
00185                 if (i==0) {
00186                     PC.printf("Input Hour <hh>:\n\r");
00187                 } else if(i==1) {
00188                     PC.printf("Input Minute <mm>:\n\r");
00189                 } else if (i==2) {
00190                     PC.printf("Input Seccond <ss>:\n\r");
00191                 }
00192 
00193                 PC.attach(&RXInterruptISR);  //Enable interrupt
00194                 Thread::signal_wait(1);      //Wait untill signal set
00195 
00196                 PC.scanf("%d", &DateTimeVar[i]); //Scan serial
00197             }
00198 
00199             SetTime(DateTimeVar[0], DateTimeVar[1], DateTimeVar[2]); //Set time
00200             PC.printf("Time set to : %d:%d:%d",DateTimeVar[0], DateTimeVar[1], DateTimeVar[2]);
00201             Sampling(true);  //Stop sampling
00202 
00203 
00204         } else if (InputBufferString == "sett") {
00205             //Set sample rate
00206             PC.printf("What would you like to set the sample rate to:\n\r");
00207             PC.attach(&RXInterruptISR);  //Enable interrupt
00208             Thread::signal_wait(1);      //Wait untill signal set
00209 
00210             PC.scanf("%d", &SAMPLERATE); //Scan serial
00211 
00212             ConfigThreadsAndIR(); //Reattach ticker
00213 
00214             PC.printf("Sample rate set to: %d\n\r",SAMPLERATE);
00215 
00216 
00217         } else if (InputBufferString == "state") {
00218             //Toggle sampling
00219             SerialState = !SerialState; //Toggle state
00220             if (SerialState) {
00221                 Sampling(true); //Start sampling
00222                 PC.printf("Sampling started\n\r");
00223             } else {
00224                 Sampling(false);//Stop sampling
00225                 PC.printf("Sampling stopped\n\r");
00226             }
00227 
00228 
00229 
00230         } else if (InputBufferString == "logging") {
00231             //Toggle logging
00232             logging = !logging; //Toggle state
00233             if (logging) {
00234                 PC.printf("Logging started");
00235             } else {
00236                 PC.printf("Logging stopped");
00237             }
00238         }
00239 
00240 
00241         PC.attach(&RXInterruptISR); //Enable interrupt
00242     }
00243 }