3rd Repo, trying to figure this out.

Dependencies:   LPS25H hts221

Fork of SOFT253_Template_Weather_OS_54 by Stage-1 Students SoCEM

Revision:
57:dfcdda1e42b6
Parent:
56:4dd780d8fb47
Child:
58:7fc6e3e4d746
--- a/main.cpp	Thu Apr 06 19:51:21 2017 +0000
+++ b/main.cpp	Fri Apr 07 10:45:32 2017 +0000
@@ -12,11 +12,15 @@
 #define SIGNAL_doMeasure 1
 #define SWITCH1_RELEASE 90
  
- 
+//
+//  MBED DECLARATIONS
+//
 DigitalOut myled(LED1);
 DigitalIn  onBoardSwitch(USER_BUTTON);
 I2C i2c2(I2C_SDA, I2C_SCL);
 
+//
+// SENSOR DECLARATIONS
 // MAKE SURE ONE OF THESE IS COMMENTED OUT
 // Real sensor
 LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
@@ -25,58 +29,58 @@
 //FakeBarometer barometer(1029.0, 1031.0);
 //FakeMeasurer measurer(20.0, 25.0, 30.0, 50.0);
 
- 
-//Threads
+//
+// THREADS DECLARATION
+//
 Thread *produceThread;
 Thread *measureThread;
 Thread *consumeThread;
 
-int count= 0;
-
-//Mail queue
+//
+//  GLOBAL VARIABLES
+// 
 Mail<Measure, 16> mail_box;
- 
 LinkedList *listBuffer;
 LocalDate *localDate;
 bool logging = true;
 
+// 
+//  Called by a TICKER
+//  Adds 1 second every second to the clock
 void RealTimeDate()
 {
     localDate->TickSecond();
-   
 }
+//
+//  SIGNALED BY Ticker at a frequency of <T> Hz
+//  Reads values from sensor board, sends over through mail queue
 void MeasureThread() {
 
     while(true)
     {   
+        //Await signal from ticker
         Thread::signal_wait(SIGNAL_doMeasure);
-        //Read sample - make a copy
+        
         float temperature = 0 , humidity = 0,pressure = 0;
-    
-  
-    
-        //Allocate a block from the memory pool
+        
         Measure *measure = mail_box.alloc();
         if (measure == NULL) 
         {
-           //Out of memory
            printf("Out of memory\n\r");
            return;   
         }
     
-        //Fill in the data
+        //Read and fill in data
         measurer.ReadTempHumi(&temperature,&humidity);
         barometer.get();
         pressure = barometer.pressure();
-
         
         measure->temperature = temperature;
         measure->humidity = humidity;
         measure->pressure = pressure;
         measure->date = new LocalDate(localDate);
 
-        //Write to queue
-        osStatus stat = mail_box.put(measure);    //Note we are sending the "pointer"
+        osStatus stat = mail_box.put(measure);
     
         //Check if succesful
         if (stat == osErrorResource) {
@@ -87,32 +91,36 @@
     }
 }
  
-//Normal priority thread (consumer)
+//
+//  Receives data through mail queue, then adds it to the global declared list
+//  A.K.A. Producer Thread
 void ProducerThread() 
 {      
-    while (true) {
+    while (true) 
+    {
         //Block on the queue
         osEvent evt = mail_box.get();
         
         //Check status
         if (evt.status == osEventMail) {
-            Measure *measure = (Measure*)evt.value.p;  //This is the pointer (address)
-            //Make a copy
-         //   printf("Consumer: %fC |  %f% % |  %f \r\n", measure->temperature, measure->humidity,measure->pressure);
             
+            Measure *measure = (Measure*)evt.value.p;          
             Measure msr(measure->date,measure->temperature, measure->humidity,measure->pressure);
             listBuffer->addValueEnd(msr);
-            //We are done with this, so give back the memory to the pool
             mail_box.free(measure);
-            
-            //Echo to the terminal
-            
         } else {
             printf("ERROR: %x\n\r", evt.status);   
         }  
         
-    } //end while
+    }
 }
+//
+//  Compares two char arrays and returns result
+//  Param1: First char Array / pointer
+//  Param2: Second char Array / pointer
+//  Param3. Size of the smallest char arrays (between param1 and param2)
+//  Return: "-1" IF NOT EQUAL
+//          "1 " IF EQUAL
 int CompareCommands(char command[],char targetcommand[], int size)
 {
        int i;
@@ -120,63 +128,76 @@
        {
             if(command[i] != targetcommand[i])
                 return -1;
-                
         }
         return 1;
 }
+//
+//  Reads commands through PUTTY and 'consumes the data' accordingly
+//  A.K.A. Consumer Thread
 void ConsumeThread()
 {
+    //Last character pressed read (last key input)
     char charCmd;
+    //Char array that stores the command after user presses ENTER
     char command[40];
+    //Current Command Size
     int crtChar = 0;
     printf("\r\nAwaiting command:\r\n");   
     while(1)
     {
-        
         charCmd = NULL;
         charCmd = getchar();
         if(charCmd != NULL)
         {
-            if (charCmd == 127 && crtChar > 0 ) // If Backspace is pressed
+            //If BACKSPACE is pressed, Print "DEL" so it deletes last character typed.
+            if (charCmd == 127 && crtChar > 0 )
             {
                 printf("%c",charCmd);
                 command[--crtChar] = '\0';   
             }
-            else if(charCmd != 13 && charCmd != 127) // If NOT enter AND NOT Backspace is pressed
+            //If NOT enter AND NOT Backspace is pressed, SAVE the char
+            else if(charCmd != 13 && charCmd != 127) 
             {
                 command[crtChar++] = charCmd;
                 printf("%c",charCmd);
             }
+            //If ENTER is pressed, PROCESS it
             else if(charCmd == 13) // If Enter is pressed
             {   
-                // this thing that follows splits a string into multiple strings, just like String.Split(char[] delimiters)
-                // here we will check for commands and parameters :)
+                //Get first word of command:
                 char *charPos;
                 charPos = strtok(command," -,");
+                
+                //Check if it's a "LIST" command
                 if(CompareCommands(charPos, "list",4) == 1)
                 {
                     charPos = strtok(NULL," -,");
+                    //Check if it's a "LIST ALL" command
                     if(CompareCommands(charPos, "all",3) == 1)
                     {
                         printf("\r\n Printing all measures performed so far: \r\n");
                         listBuffer->ListAll();   
                         printf("\r\n D O N E ! \r\n");
                     }
+                    //Check if it's a "LIST X" command
                     else if(strtol(charPos,NULL,10) != 0)
                     {
                         listBuffer->ListX(atoi(charPos));   
                         printf("\r\n D O N E ! \r\n");
                     }
                 }
+                //Check if it's a "DELETE" command
                 else if (CompareCommands(charPos,"delete",6) == 1)
                 {
                     charPos = strtok(NULL," -,");
+                    //Check if it's a "DELETE ALL" command
                     if(CompareCommands(charPos,"all",3) == 1)
                     {
                         printf("\r\n Deleting all measures performed so far: \r\n");
                         listBuffer->DeleteAll();
                         printf("\r\n D O N E ! \r\n");
                     }
+                    //Check if it's a "DELETE X" command
                     else if (strtol(charPos,NULL,10) != 0)
                     {
                         listBuffer->DeleteX(atoi(charPos));
@@ -184,7 +205,8 @@
                     }
                        
                 }
-                else if (CompareCommands(charPos,"status",6) == 1)
+                //Check if it's a "STATE/STATUS" command
+                else if (CompareCommands(charPos,"status",6) == 1 || CompareCommands(charPos,"state",5) == 1)
                 {
                     char *ptr = localDate->ToString();
                     if(logging == true)
@@ -192,24 +214,29 @@
                     else
                         printf("\r\n STATUS: \r\n # of measures: %i \r\n SAMPLING: OFF \r\n Current Date: %s \r\n", listBuffer->GetSize(),ptr);   
                 }
+                //Check if it's a "SETTIME" command
                 else if (CompareCommands(charPos,"settime",7) == 1)
                 {
                     int h,m,s;
+                    //Fetch 1st Param
                     charPos = strtok(NULL," ,");
                     if(strtol(charPos,NULL,10) != 0)
                     {
                         h = atoi(charPos);   
                     }
+                    //Fech 2nd Param
                     charPos = strtok(NULL," ,");
                     if(strtol(charPos,NULL,10) != 0)
                     {
                         m = atoi(charPos);   
                     }
+                    //Fetch 3rd Param
                     charPos = strtok(NULL," ,");
                     if(strtol(charPos,NULL,10) != 0)
                     {
                         s = atoi(charPos);   
                     }
+                    //Check if parameters are valid
                     if((h>=0 && h < 24) && (m>=0 && m<60) && (s>=0 && s<60))
                     {
                         localDate->hour = h;
@@ -217,29 +244,35 @@
                         localDate->sec = s;
                         printf("\r\n D O N E ! \r\n");
                     } 
+                    //If not valid, prompt user
                     else
                     {
                         printf("\r\nWrong format! \r\n");   
                     }
                 }
+                //Check if it's a "SETDATE" command
                 else if (CompareCommands(charPos,"setdate",7) == 1)
                 {
                     int d,m,y;
+                    //Fetch 1st Parameter
                     charPos = strtok(NULL," ,");
                     if(strtol(charPos,NULL,10) != 0)
                     {
                         d = atoi(charPos);   
                     }
+                    //Fetch 2nd Parameter
                     charPos = strtok(NULL," ,");
                     if(strtol(charPos,NULL,10) != 0)
                     {
                         m = atoi(charPos);   
                     }
+                    //Fetch 3rd Parameter
                     charPos = strtok(NULL," ,");
                     if(strtol(charPos,NULL,10) != 0)
                     {
                         y = atoi(charPos);   
                     }
+                    //Check if parameters are valid
                     if((d>=0 && d < 31) && (m>=0 && m<13))
                     {
                         localDate->day = d;
@@ -247,14 +280,17 @@
                         localDate->year = y;
                         printf("\r\n D O N E ! \r\n");
                     } 
+                    // Prompt user if they are not.
                     else
                     {
                         printf("\r\nWrong format! \r\n");   
                     }
                 }
+                // Check if it's a "LOGGING" command
                 else if(CompareCommands(charPos,"logging",7) == 1)
                 {
                     charPos = strtok(NULL," ,");
+                    //Check if it should be turned ON / OFF
                     if(CompareCommands(charPos,"on",2) == 1)
                     {
                         logging = true;   
@@ -265,13 +301,10 @@
                         logging = false;   
                         printf("\r\n Logging turned OFF!\r\n");
                     }
-                }
-                
-              //  printf("%s \r\n", charPos);
-              //  charPos = strtok(NULL," -,");
-                
-                
+                }              
                 printf("Awaiting command: \r\n");
+                //Clear command!
+                //* NOTE * Setting first char in array to '\0' WILL NOT RESET IT...for some reason.
                 int i = 0;
                 for(i =0 ; i < crtChar; i++)
                     command[i] = ' ';
@@ -282,7 +315,9 @@
     }
 }
 
-
+//
+//  Ticker that signals the measureThread to do a measure
+//
  void SendSignalDoMeasure()
  {
     if(logging == true)
@@ -292,7 +327,7 @@
 // Main thread
 int main() {
            
-    //Initialize stuff
+    //Initialize all stuff you need here:
     measurer.init();
     measurer.calib();
     
@@ -304,11 +339,11 @@
    
     //Hook up timer interrupt   
     Ticker timer; 
-    timer.attach(&SendSignalDoMeasure, 2.0);
+    timer.attach(&SendSignalDoMeasure, 0.2);
     Ticker realTimeDate;
     realTimeDate.attach(&RealTimeDate,1.0);
                
-    //Threads
+    //Run Threads
     produceThread = new Thread();
     produceThread->start(ProducerThread); 
     measureThread = new Thread();