![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
V4
Fork of Thread_Communication_V4_fortest by
Diff: main.cpp
- Revision:
- 13:089f3adb3813
- Parent:
- 12:a244f6f9d2fe
- Child:
- 14:7c4d87dfc388
diff -r a244f6f9d2fe -r 089f3adb3813 main.cpp --- a/main.cpp Fri Jan 05 12:01:04 2018 +0000 +++ b/main.cpp Fri Jan 05 19:47:33 2018 +0000 @@ -43,9 +43,9 @@ volatile float LDR = 0; volatile double PRES = 0; volatile double TEMP = 0; -volatile char TIME[20]; +volatile char TIME[21]; -volatile int sampleTime = 15; +volatile double sampleTime = 15.0; // int to hold current switch state int userButtonState = FallingEdge; @@ -80,7 +80,7 @@ /*--------------------------------MAIN--------------------------------*/ int main() { - pc.baud(9600); + pc.baud(57600); pc.attach(&Rx_interrupt, Serial::RxIrq); POST(); @@ -113,17 +113,10 @@ DataBuffer.lock(); //Format samples, send to FIFO buffer head - memset(data_buffer[sample_h],NULL,64); - - char sampleTime[20]; - memset(sampleTime, NULL,20); - time( &raw_time ); - sample_epoch = localtime( &raw_time ); - strftime(sampleTime,20,"%d/%m/%Y, %X",sample_epoch); - + memset(data_buffer[sample_h],NULL,64); dataLock.lock(); //lock critical section - sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sampleTime, TEMP, PRES, LDR); + sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", TIME, TEMP, PRES, LDR); dataLock.unlock(); //unlock critical section @@ -167,6 +160,11 @@ TEMP = Sensor.getTemperature(); PRES = Sensor.getPressure(); + memset((char *)TIME, NULL, 21); + time(&raw_time); + sample_epoch = localtime(&raw_time); + strftime((char *)TIME, 21,"%d/%m/%Y, %X", sample_epoch); + dataLock.unlock(); // Exiting Critical Section Green_int = !Green_int; // debugging @@ -261,7 +259,14 @@ //Increment buffer head else{ - rx_in = (rx_in + 1); + if(rx_in>=32){ + puts("\n\rERROR - Stop typing so much!\n\r"); + rx_in = 0; + memset(rx_buffer, NULL, 32); + } + else{ + rx_in = (rx_in + 1); + } } } } @@ -285,7 +290,7 @@ } /*----READ ALL----------------------------------*/ else if(strstr(rx_buffer, "READ ALL")){ - pc.puts(" READ ALL\n\r"); + pc.puts("\n\r Reading all samples...\n\r"); //Lock data buffer DataBuffer.lock(); @@ -302,10 +307,11 @@ //Lock data buffer DataBuffer.unlock(); + pc.puts(" All Samples read!\n\r"); } /*----DELETE ALL----------------------------------*/ else if(strstr(rx_buffer, "DELETE ALL")){ - pc.puts(" DELETE ALL\n\r"); + pc.puts("\n\r Deleting all samples...\n\r"); //Lock data buffer DataBuffer.lock(); @@ -319,14 +325,13 @@ //Unlock data buffer DataBuffer.unlock(); - pc.printf(" All previous samples deleted!\n\r"); + pc.puts(" All previous samples deleted!\n\r"); } /*----READ----------------------------------*/ else if(strstr(rx_buffer, "READ")){ - pc.puts(" READ \n\r"); + pc.puts("\n\r Reading N samples...\n\r"); int N = atoi(strncpy(tm_n,&rx_buffer[5],4)); int S = 0; - pc.printf("N = %d\n\r",N); //Lock data buffer DataBuffer.lock(); @@ -338,7 +343,8 @@ //Read N samples from FIFO buffer if(N <= 0){ - pc.puts("ERROR - Buffer Empty!\n\r"); + pc.puts("ERROR - N must be greater than 0\n\r"); + N = 0; } else{ for(int n=data_h; n>=0; n--){ @@ -356,13 +362,52 @@ } } } - + pc.printf(" Read %d samples\n\r",N); //Unlock data buffer DataBuffer.unlock(); } /*----DELETE----------------------------------*/ else if(strstr(rx_buffer, "DELETE")){ - pc.puts(" DELETE \n\r"); + pc.puts("\n\r Deleting N samples...\n\r"); + int N = atoi(strncpy(tm_n,&rx_buffer[6],4)); + int S = 0; + + //Lock data buffer + DataBuffer.lock(); + + //Check if N is greater than buffer size + if(N >= MAX_SAMPLES){ + N = MAX_SAMPLES; + } + + //Read N samples from FIFO buffer + if(N <= 0){ + pc.puts("ERROR - N must be greater than 0\n\r"); + N = 0; + } + else{ + for(int n=data_t; n<=MAX_SAMPLES; n++){ + if(S>=N){} + else{ + memset(data_buffer[n], NULL, 64); + S++; + data_t = n; + sample_t = n; + } + } + for(int n=MAX_SAMPLES-1; n<=data_t; n--){ + if(S>=N){} + else{ + memset(data_buffer[n], NULL, 64); + S++; + data_t = n; + sample_t = n; + } + } + } + pc.printf(" Deleted %d samples\n\r",N); + //Unlock data buffer + DataBuffer.unlock(); } /*----SETDATE----------------------------------*/ else if(strstr(rx_buffer, "SETDATE")){ @@ -386,7 +431,7 @@ //Set date from updated time structure set_time(mktime(s_time)); - strftime(serial_buffer, 80, "\n\n\r Date set to: %d/%m/%Y\n\r", s_time); + strftime(serial_buffer, 80, "\n\r Date updated to: %d/%m/%Y\n\r", s_time); pc.puts(serial_buffer); } /*----SETTIME---------------------------------*/ @@ -411,21 +456,20 @@ //Set time from updated time structure set_time(mktime(s_time)); - strftime(serial_buffer, 80, "\n\n\r Time Set to: %X\n\r", s_time); + strftime(serial_buffer, 80, "\n\r Time updated to: %X\n\r", s_time); pc.puts(serial_buffer); } /*----SETT----------------------------------*/ else if(strstr(rx_buffer, "SETT")){ - pc.puts(" SETT\n\r"); read.detach(); - int AA = atoi(strncpy(tm_n,&rx_buffer[5],2)); + double AA = atof(strncpy(tm_n,&rx_buffer[5],4)); - if (AA < 1 || AA > 60) { + if (AA < 0.1 || AA > 60) { AA = 15; - pc.puts("Sample Time out of range.\n\r"); + pc.puts("ERROR - Sample Time out of range (0.1<=T<=60)\n\r"); } sampleTime = AA; - pc.printf("Sample Time set to: %d seconds\n\r", sampleTime); + pc.printf("\n\r Sample Time updated to: %2.1f seconds\n\r", sampleTime); read.attach(readISR, sampleTime); memset(tm_n, NULL, 4); } @@ -435,21 +479,21 @@ strncpy(tm_n,&rx_buffer[6],3); if (strstr(tm_n, "ON")) { if (xx == 1) { - pc.puts("Already Sampling\n\r"); + pc.puts("\n\r Already Sampling\n\r"); } else { read.attach(&readISR, sampleTime); - pc.puts("Started Sampling\n\r"); + pc.puts("\n\r Sampling ON\n\r"); xx = 1; } } else if (strstr(tm_n, "OFF")) { if ( xx == 0 ) { - pc.puts("Already not Sampling\n\r"); + pc.puts("\n\r Already not Sampling\n\r"); } else { read.detach(); - pc.puts("Sampling stopped. Not taking any samples\n\r"); + pc.puts("\n\r Sampling OFF\n\r"); xx = 0; } } @@ -464,7 +508,7 @@ } /*----HELP--------------------------------------*/ else if (strstr(rx_buffer, "HELP")) { - pc.puts("\n\n\nCurrently Available Commands:\n\r"); + pc.puts("\n\n\r Currently Available Commands:\n\r"); pc.puts("\tREAD n - Read n previous samples\n\r"); pc.puts("\tREAD ALL - Read All previous samples held in memory\n\r"); pc.puts("\tSETTIME hh:mm::ss - Set time in 24hr format\n\r"); @@ -473,9 +517,9 @@ pc.puts("\tSETT - Set sample period. must be an integer in range 0 < T < 61\n\r"); } else if (strstr(rx_buffer,"tell me a joke")) { - pc.puts("Why do programmers always get Halloween and Christmas mixed up...?\n\r"); - Thread::wait(7000); - pc.puts("Because Oct 31 == Dec 25! LOL"); + pc.puts("\n\r Why do programmers always get Halloween and Christmas mixed up...?\n\r"); + Thread::wait(5000); + pc.puts(" Because Oct 31 == Dec 25! LOL"); } /*----ERROR---*/ else{ @@ -494,17 +538,19 @@ } /*------------------------------------------------*/ /*---------------SD THread------------------------*/ -void writeRemove_SD() { - +void writeRemove_SD() +{ + while(1) { Thread::signal_wait(USER_BUTTON_PRESSED); //wait for debounce signal int sd_state = sdIn; + switch (sd_state) { case 1: pc.printf("SD Card not inserted!\n\r"); pc.printf("Insert SD Card and press User button again\n\r"); break; - default: + default: pc.printf("This should never happen\n\r"); break; case 0: @@ -512,64 +558,65 @@ //check init if (sd.init() != 0) { - pc.printf(" ERROR - SD card failed to initialise.\n\rRestart board\n\r"); - } + pc.printf(" ERROR - SD card failed to initialise.\n\rRestart board\n\r PROBABLY wires come out\n\r"); + } + else { + // Create Filing system for SD Card + FATFileSystem fs("sd", &sd); - // Create Filing system for SD Card - FATFileSystem fs("sd", &sd); + //OpenFiles to write/append to + pc.printf("Writing to SDC\n\r"); - //OpenFiles to write/append to - pc.printf("Writing to SDC\n\r"); - - FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "a"); //"w" to overwrite file ftb + FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "a"); //"w" to overwrite file ftb - // Check for error in opening file - if (fp == NULL) { - pc.printf("*****ERROR - Could not open file for write*****\n\r"); - } - //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???) - //Lock data buffer - DataBuffer.lock(); - dataLock.lock(); - //Print all samples to SD - for(int n=data_t; n<=MAX_SAMPLES; n++) { - fputs(data_buffer[n], fp); - fprintf(fp, "\n\r"); - } - if(data_t>data_h) { - for(int n=0; n<=(data_t-1); n++) { + // Check for error in opening file + if (fp == NULL) { + pc.printf("*****ERROR - Could not open file for write*****\n\r"); + } + //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???) + //Lock data buffer + DataBuffer.lock(); + dataLock.lock(); + //Print all samples to SD + for(int n=data_t; n<=MAX_SAMPLES; n++) { fputs(data_buffer[n], fp); + fprintf(fp, "\n\r"); + } + if(data_t>data_h) { + for(int n=0; n<=(data_t-1); n++) { + fputs(data_buffer[n], fp); + } } - } - //Lock data buffer - DataBuffer.unlock(); - dataLock.unlock(); - //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r"); + //Lock data buffer + DataBuffer.unlock(); + dataLock.unlock(); + //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r"); - fclose(fp); + fclose(fp); - pc.printf("Write Sucessful!\n\r"); + pc.printf("Write Sucessful!\n\r"); - sd.deinit(); - pc.printf("SD Card Ready to Remove\n\r"); - Green_ext = 1; - Thread::wait(500); - Green_ext = 0; - Thread::wait(500); - Green_ext = 1; - Thread::wait(500); - Green_ext = 0; - Thread::wait(500); - Green_ext = 1; - Thread::wait(500); - Green_ext = 0; - Thread::wait(500); - Green_ext = 1; - Thread::wait(500); - Green_ext = 0; - Thread::wait(500); + sd.deinit(); + pc.printf("SD Card Ready to Remove\n\r"); + Green_ext = 1; + Thread::wait(500); + Green_ext = 0; + Thread::wait(500); + Green_ext = 1; + Thread::wait(500); + Green_ext = 0; + Thread::wait(500); + Green_ext = 1; + Thread::wait(500); + Green_ext = 0; + Thread::wait(500); + Green_ext = 1; + Thread::wait(500); + Green_ext = 0; + Thread::wait(500); + }// end if }//End Switch }// End While }// End Thread @@ -605,8 +652,9 @@ /* Can handle 5 simultaneous connections */ srv.listen(5); - + TCPSocket clt_sock; //Socket for communication + pc.printf("Server Ready\n\r"); while (true) { @@ -621,35 +669,42 @@ string strL = "LDR:"; string strP = ", Pressure(mBar): "; string strT = ", Temp(C): "; + string strDT ="Date/Time: ,"; //This is a C string - char l_str[64]; - char p_str[64]; - char t_str[64]; + + char X_str[128]; - //Read the LDR value + /*//Read the LDR value dataLock.lock(); // add watchdog? float L = LDR ; float T = TEMP; float P = PRES; - + char DT[21]; + strcpy((char*)TIME,DT); dataLock.unlock(); //Convert to a C String - sprintf(l_str, "%1.3f", L ); // try \n\r?? - sprintf(t_str, "%2.2f", T); - sprintf(p_str, "%4.2f", P); - + //sprintf(l_str, "%1.3f", L ); // try \n\r?? + //sprintf(t_str, "%2.2f", T); + //sprintf(p_str, "%4.2f", P);*/ + + dataLock.lock(); + sprintf(X_str,"%s, \n\rTemperature: %2.2f, \n\rPressure: %4.2f, \n\rLight: %.4f", TIME, TEMP, PRES, LDR); + dataLock.unlock(); //Build the C++ string response response = HTTP_MESSAGE_BODY1; - // response += strL; + response += X_str; + /* response += strL; + response += strDT; + response += strL; response += l_str; response += strT; response += t_str; response += strP; - response += p_str; + response += p_str;*/ response += HTTP_MESSAGE_BODY2; //Send static HTML response (as a C string)