Ben Parkes / Mbed OS Thread_Communication_V5

Dependencies:   BMP280

Fork of Thread_Communication_V4_fortest by BDG

Files at this revision

API Documentation at this revision

Comitter:
dnonoo
Date:
Thu Jan 04 20:33:17 2018 +0000
Parent:
10:c10d1337d754
Child:
12:a244f6f9d2fe
Commit message:
have a look i cant remember

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Dec 30 18:55:22 2017 +0000
+++ b/main.cpp	Thu Jan 04 20:33:17 2018 +0000
@@ -43,6 +43,7 @@
 volatile float LDR = 0;
 volatile double PRES = 0;
 volatile double TEMP = 0;
+volatile char TIME[20];
 
 volatile int sampleTime = 15;
 // int to hold current switch state
@@ -85,8 +86,8 @@
     
     pc.printf("\n\n\nType HELP for list of available Commands\n\n\n\r");
     _serialCMD.start(serialCMD);
+    _sensorRead.start(sensorRead);
     _PrintLCD.start(PrintLCD);
-    _sensorRead.start(sensorRead);
     _circBuff.start(circBuff);
     _writeRemove_SD.start(writeRemove_SD);
     _Network1.start(Network1);
@@ -113,16 +114,11 @@
             
             //Format samples, send to FIFO buffer head
             memset(data_buffer[sample_h],NULL,64);
-            time( &raw_time );
-            sample_epoch = localtime( &raw_time );
-            char sample_time[20];
-            strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch);
             
             dataLock.lock(); //lock critical section
-            sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, 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
             
-            memset(sample_time,NULL,20);
             
             //Set seperate FIFO head and tail for printing data
             data_h = sample_h;
@@ -164,6 +160,12 @@
         TEMP = Sensor.getTemperature();
         PRES = Sensor.getPressure();
         
+        
+        memset(TIME, NULL,20);
+        time( &raw_time );
+        sample_epoch = localtime( &raw_time );
+        strftime(TIME,20,"%d/%m/%Y, %X",sample_epoch);
+        
         dataLock.unlock(); // Exiting Critical Section
     
         Green_int = !Green_int; // debugging
@@ -175,6 +177,7 @@
         mail->press_Value = PRES;
         mail_box.put(mail);
         
+        
         _circBuff.signal_set(DATA_READY);  // Set signal to buffer to store updated values
         Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt
     }
@@ -264,7 +267,7 @@
 
 //Check what command what recieved and execute
 void serialCMD(){
-    
+    bool xx = 1; // State for CMD STATE
     while(1){
         //Wait for thread signal
         Thread::signal_wait(ENTER_KEY);
@@ -315,6 +318,7 @@
             
         //Unlock data buffer
             DataBuffer.unlock();
+            pc.printf(" All previous samples deleted!\n\r");
         }
 /*----READ----------------------------------*/
         else if(strstr(rx_buffer, "READ")){
@@ -333,17 +337,17 @@
             
         //Read N samples from FIFO buffer
             if(N <= 0){
-                pc.puts("####ERROR####\n\r");
+                pc.puts("ERROR - Buffer Empty!\n\r");
             }
             else{
-                for(int n=data_t; n<=MAX_SAMPLES-1; n++){
+                for(int n=data_h; n>=0; n--){
                     if(S>=N){}
                     else{
                         pc.puts(data_buffer[n]);
                         S++;
                     }
                 }
-                for(int n=0; n<=data_t; n++){
+                for(int n=MAX_SAMPLES-1; n<=data_t; n--){
                     if(S>=N){}
                     else{
                         pc.puts(data_buffer[n]);
@@ -381,7 +385,7 @@
             
         //Set date from updated time structure
             set_time(mktime(s_time));
-            strftime(serial_buffer, 80, "\n\r Set Date: %d/%m/%Y\n\r", s_time);
+            strftime(serial_buffer, 80, "\n\n\r Date set to: %d/%m/%Y\n\r", s_time);
             pc.puts(serial_buffer);
         }
 /*----SETTIME---------------------------------*/
@@ -406,20 +410,52 @@
         
         //Set time from updated time structure
             set_time(mktime(s_time));
-            strftime(serial_buffer, 80, "\n\r Set Time: %X\n\r", s_time);
+            strftime(serial_buffer, 80, "\n\n\r Time Set to: %X\n\r", s_time);
             pc.puts(serial_buffer);
         }
 /*----SETT----------------------------------*/
         else if(strstr(rx_buffer, "SETT")){
             pc.puts(" SETT\n\r");
-           // read.detach();
-           // sampleTime = atoi(rx_buffer);
-           // pc.printf("Sample Time %d\n\r", sampleTime);
-           // read.attach(&readISR, sampleTime);
+            read.detach();
+            int AA = atoi(strncpy(tm_n,&rx_buffer[5],2));
+            
+            if (AA < 1 || AA > 60) {
+                AA = 15;
+                pc.puts("Sample Time out of range.\n\r");
+            }
+            sampleTime = AA;
+            pc.printf("Sample Time set to: %d seconds\n\r", sampleTime);
+            read.attach(readISR, sampleTime);
+            memset(tm_n, NULL, 4);
         }
 /*----STATE----------------------------------*/
         else if(strstr(rx_buffer, "STATE")){
-            pc.puts(" STATE\n\r");
+            
+            strncpy(tm_n,&rx_buffer[6],3);
+            if  (strstr(tm_n, "ON")) {
+                if (xx == 1) {
+                    pc.puts("Already Sampling\n\r");
+                }
+                else {
+                    read.attach(&readISR, sampleTime);
+                    pc.puts("Started Sampling\n\r");
+                    xx = 1;
+                }
+            }
+            else if (strstr(tm_n, "OFF")) {
+                if ( xx == 0 ) {
+                    pc.puts("Already not Sampling\n\r");
+                }
+                else {
+                    read.detach();
+                    pc.puts("Sampling stopped. Not taking any samples\n\r");
+                    xx = 0;
+                }
+            }
+            else {
+                pc.puts("Error - STATE can only be ON or OFF\n\r");
+                }
+            //pc.puts(" STATE\n\r");
         }
 /*----LOGGING----------------------------------*/
         else if(strstr(rx_buffer, "LOGGING")){
@@ -433,10 +469,16 @@
             pc.puts("\tSETTIME hh:mm::ss  - Set time in 24hr format\n\r");
             pc.puts("\tSETDATE dd/mm/yyyy - Set time in specified format\n\r");
             pc.puts("\tDELETE ALL   - Delete all sampled held in internal memory\n\n\n\r");
+            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");
+        }
 /*----ERROR---*/
         else{
-            pc.puts("####ERROR####\n\r");    
+            pc.puts("Error - Command not recognised. Type HELP for a list of available commands\n\r");    
         }
 /*----------------------------------------------*/
     
@@ -478,7 +520,7 @@
                 //OpenFiles to write/append to
                 pc.printf("Writing to SDC\n\r");
 
-                FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "w"); //"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) {
@@ -541,6 +583,11 @@
     EthernetInterface eth;
     eth.set_network(IP, NETMASK, GATEWAY);
     eth.connect();
+    if (eth.get_ip_address() == NULL) {
+        pc.printf("Error - Can't get IP. Network not setup\n\r");
+        pc.printf("Reset Required. Make sure network cables are plugged in\n\r");
+        }
+    else {
     printf("The target IP address is '%s'\n\r", eth.get_ip_address());
     
     //Now setup a web server
@@ -557,9 +604,11 @@
     /* Can handle 5 simultaneous connections */
     srv.listen(5);
     
+    TCPSocket clt_sock;      //Socket for communication
+    pc.printf("Server Ready\n\r");
     while (true) {
         
-        TCPSocket clt_sock;      //Socket for communication
+        
         using namespace std;
         //Block and wait on an incoming connection
         srv.accept(&clt_sock, &clt_addr);
@@ -570,24 +619,28 @@
         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];
         
         //Read the LDR value
-        dataLock.lock();
+        dataLock.lock(); // add watchdog?
         float L = LDR ;
         float T = TEMP;
         float P = PRES;
+        char DT[20] = TIME;
         dataLock.unlock();
         
         //Convert to a C String
-        sprintf(l_str, "%1.3f", L );
+        sprintf(l_str, "%1.3f", L ); // try \n\r??
         sprintf(t_str, "%2.2f", T);
         sprintf(p_str, "%4.2f", P);
         
         
+        
         //Build the C++ string response
         response = HTTP_MESSAGE_BODY1;
        // response += strL;
@@ -596,16 +649,19 @@
         response += t_str;
         response += strP;
         response += p_str;
+        response += strDT;
+        response += DT;
         response += HTTP_MESSAGE_BODY2;
         
         //Send static HTML response (as a C string)
         clt_sock.send(response.c_str(), response.size()+6);  
           
     }
+    }
 }
 /*---------------------------POST--------------------------------------------*/
 void POST () {
-    
+    pc.printf(" Basic POST\n\r"); 
     pc.printf(" ALL Leds should be flashing\n\r");
     
     for(unsigned int n = 0; n<10; n++) {
@@ -645,5 +701,5 @@
     lcd.Write("******TEST******");
     wait(1);
     lcd.Clear();
-    pc.printf("Basic POST Pass\n\r");    
+    pc.printf("Basic POST end\n\r");    
 }
--- a/main.h	Sat Dec 30 18:55:22 2017 +0000
+++ b/main.h	Thu Jan 04 20:33:17 2018 +0000
@@ -7,7 +7,6 @@
 #define MAX_SAMPLES 120
 #define SENSOR_UPDATE 1
 #define DATA_READY 1
-#define SAMPLING_PERIOD 10 
 #define ON 1
 #define OFF 0
 
@@ -82,6 +81,7 @@
 } mail_t;
 
 Mail<mail_t, 16> mail_box;
+//Mail<mail_t, 16> mail_box2;
 
 //data FIFO buffer
 char data_buffer[MAX_SAMPLES][64];