update code

Dependencies:   X_NUCLEO_IKS01A1 mbed-rtos mbed

Revision:
7:cb29c20009e8
Parent:
3:f61ae69ed662
Child:
8:aec689bd13c0
--- a/main.cpp	Tue May 03 12:27:07 2016 +0000
+++ b/main.cpp	Fri May 06 11:17:18 2016 +0000
@@ -1,69 +1,68 @@
 #include <stdlib.h>
 //#include <pthread.h>
 //#include <semaphore.h>
-#include "buffer.h"
-
 #include "mbed.h"
-
 //#include "string.h"
 #include <stdio.h>
 #include <ctype.h>
 #include "x_nucleo_iks01a1.h"
 #include "rtos.h"
-
+ 
 #define RAND_DIVISOR 100000000
 #define TRUE 1
-
+ 
+void thread1(void *param);
+ 
 //setting out variables
-
+ 
 /**
 / this is setting up the connection from the board
 **/ 
-
+ 
 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
 //static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
-
+ 
 /*
 / this is will make sure the contant valid of how much data will be sorted or not as this
 /could help when the data range.
 */
-const int amountOfSample = 150;
-
-
+const int AMOUNT_OF_SAMPLE = 150;
+ 
 const int BUFFER_SIZE = 75;
 
-// mutex lock
-Mutex *bufferLock;
-
-
-
+int secondsOfsample = 0;
+ 
+// Mutex lock
+Mutex *bufferLock = new Mutex;
+ 
 //buffer
 char buffer_item[BUFFER_SIZE];
-
-
-Thread  tid; //thread id
-pthread_attr_t attr; //set of thread attributes
-
+ 
+ 
+ 
+Thread *tid; //thread id
+//pthread_attr_t attr; //set of thread attributes
+ 
 void *producer(void *param); //producer thread
 void *consumer(void *param); //consumer thread
-
-
-
-volatile char memorySample[amountOfSample];
-
+ 
+ 
+ 
+volatile char memorySample[AMOUNT_OF_SAMPLE];
+ 
 char values[20][20];
-
-
-
-
+ 
+ 
+ 
+ 
 uint8_t id;
-
-
+ 
+ 
 //class
-
+ 
 class KeyLocks {
     
     public:
@@ -102,7 +101,7 @@
         } 
             
 };
-
+ 
 class SampleRate : public KeyLocks 
 {
     public:
@@ -131,8 +130,8 @@
         
         
 };
-
-
+ 
+ 
 class TakeSample : public KeyLocks
 {
     public:
@@ -156,9 +155,9 @@
             
         }       
 };
-
-
-
+ 
+ 
+ 
 class SampleTemp: public KeyLocks
 {
     public: 
@@ -174,7 +173,7 @@
             return currentTemp;  
         }              
 };
-
+ 
 class SampleHumitaty: public KeyLocks
 {
     public: 
@@ -192,10 +191,10 @@
             
         }              
 };
-
-
+ 
+ 
 class SamplePressure: public KeyLocks 
-
+ 
 {
     public:
     
@@ -211,7 +210,7 @@
             return currentPressure;
         }             
 };
-
+ 
 class SampleDisplay: public KeyLocks
 {
     public:
@@ -219,7 +218,7 @@
         /this is the variable to see if the data will be display or not after the data being sorted
         /this can change by the user input
         */
-
+ 
         bool display;
         
         SampleDisplay(){
@@ -236,7 +235,7 @@
             
         }       
 };
-
+ 
  
  
  
@@ -247,9 +246,11 @@
         /this is the pointer to start of the buffer as this will make the buffer be cycler
         */
         int bufferStartPointer;
+        int bufferSize;
         
-        BufferStartPointer(){
+        BufferStartPointer(int newBufferSize){
             bufferStartPointer = 0;
+            bufferSize = newBufferSize;
         }
         
         int getBufferStartPointer (void)
@@ -264,15 +265,15 @@
             
         } 
         void plusOne(void){ 
-            if(bufferStartPointer + 1 == BUFFER_SIZE){
+            if(bufferStartPointer + 1 == bufferSize){
                 bufferStartPointer = 0;
             }else{
                 bufferStartPointer = bufferStartPointer + 1;
                 }
         }    
 };
-
-
+ 
+ 
 class BufferEndPointer: public KeyLocks
 {
     public:
@@ -281,6 +282,13 @@
         */
         int bufferEndPointer;
         
+        int bufferSize;
+        
+        BufferEndPointer(int newBufferSize){
+            bufferEndPointer = 0;
+            bufferSize = newBufferSize;
+        }
+        
         
         BufferEndPointer(){
             bufferEndPointer = 0;
@@ -297,14 +305,14 @@
         } 
         
           void plusOne(void){ 
-            if(bufferEndPointer + 1 == BUFFER_SIZE){
+            if(bufferEndPointer + 1 == bufferSize){
                 bufferEndPointer = 0;
             }else{
                 bufferEndPointer = bufferEndPointer + 1;
                 }
         }        
 };
-
+ 
 class BufferSize: public KeyLocks
 {
     
@@ -351,6 +359,43 @@
     
 };
 
+
+class SampleString: public KeyLocks
+{
+    public:
+            char myString[100];
+            
+            SampleString(){}
+            
+            char*  getWord(){
+                return myString;
+            }
+            
+            void setWord(const char* newWord){
+                
+                strcpy(myString, newWord);
+                
+                //myString = *newWord;
+                
+            }
+    
+};
+
+
+SampleTemp sampleTemp = SampleTemp();
+SampleHumitaty sampleHumitaty = SampleHumitaty();
+SamplePressure samplePressure =  SamplePressure();
+SampleString samplString = SampleString();
+
+SampleRate sampleRate =  SampleRate();
+TakeSample takeSample = TakeSample();
+SampleDisplay sampleDisplay = SampleDisplay();
+
+BufferStartPointer bufferStartPointer = BufferStartPointer(BUFFER_SIZE);
+BufferEndPointer ramEndPointer = BufferEndPointer(AMOUNT_OF_SAMPLE);
+BufferEndPointer bufferEndPointer = BufferEndPointer(BUFFER_SIZE);
+BufferSize bufferSize = BufferSize();
+ 
 /**void initialiseBufferData(){
     //create mutex lock
     pthread_mutex_init(&mutex, NULL);
@@ -367,42 +412,67 @@
     //init buffer
     counter = 0;
 }**/
+ 
+ 
+  
+/*
+/read Record is the selected data which be passed in as a a string
+*/
+void printCurrentString(char* displayString, int length) 
+{        
+    printf("%s\n", displayString);
+    
+    
+}
 
+/*
+/read Record is the selected data which be passed in as a a string
+*/
+void printCurrentRecord(char* displayPressure, char* displayTemp, char* displayHum) 
+{    
+    //strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &time);
+    printf("\nPressure : %s, Temp : %s, Hum : %s Time:%d seconds", displayPressure, displayTemp, displayHum, secondsOfsample);
+    
+    
+}
 //producer thread
-void *producer(char newItem, BufferEndPointer *endPointer, BufferSize *bufferSize){
+void producer(){
    
-   while(!bufferSize->lock()){};
+   while(!bufferSize.lock()){};
    
-   int size = bufferSize -> getBufferSize();
+   int size = bufferSize.getBufferSize();
    
-   while(size < BUFFER_SIZE){
+   if(size < BUFFER_SIZE){
        //sleep for random amount of time
        
        //get the buffer
        bufferLock->lock(); 
-
+ 
            
-        while(!endPointer->lock()){};
-       
-        endPointer->plusOne();
-        const int bufferIndex = endPointer->getBufferEndPointer();
-        buffer_item[bufferIndex] = newItem;
-        endPointer->unlock();
+        while(!bufferEndPointer.lock()){};       
+        bufferEndPointer.plusOne();
+        const int bufferIndex = bufferEndPointer.getBufferEndPointer();
+        bufferEndPointer.unlock();
+        
+        while(!samplString.lock()){};   
+        buffer_item[bufferIndex] = *samplString.getWord();   
+        samplString.unlock();    
         bufferLock->unlock();
-        while(!bufferSize->lock()){};
-        bufferSize ->increaseBufferSize();
-        size = bufferSize -> getBufferSize();       
+        
+        while(!bufferSize.lock()){};
+        bufferSize.increaseBufferSize();
+        bufferSize.unlock();     
            
         
     }
 }
-
+ 
 //consumer thread
-void *consumer(BufferStartPointer *startPointer, BufferSize *bufferSize, BufferEndPointer *endPointer) {
+void consumer() {
    
-   while(!bufferSize->lock()){};
+   while(!bufferSize.lock()){};
    
-   int size = bufferSize -> getBufferSize();
+   int size = bufferSize.getBufferSize();
    
    while(size > 0){
        //sleep for random amount of time
@@ -410,28 +480,28 @@
        //get the buffer
        bufferLock->lock(); 
         
-        while(!endPointer->lock()){};
+        while(!ramEndPointer.lock()){};
         
-        int ramIndex = endPointer->getBufferEndPointer();
+        int ramIndex = ramEndPointer.getBufferEndPointer();
         
-        endPointer -> unlock();
+        ramEndPointer.unlock();
            
-        while(!startPointer->lock()){};
+        while(!bufferStartPointer.lock()){};
        
-        startPointer->plusOne();
+        bufferStartPointer.plusOne();
     
             
-        memorySample[ramIndex] = buffer_item[startPointer->getBufferStartPointer()];
+        memorySample[ramIndex] = buffer_item[bufferStartPointer.getBufferStartPointer()];
             
         
         
-        startPointer->unlock();
+        bufferStartPointer.unlock();
         bufferLock->unlock();
         
         
-        while(!bufferSize->lock()){};
-        bufferSize ->deincreaseBufferSize();
-        size = bufferSize -> getBufferSize();       
+        while(!bufferSize.lock()){};
+        bufferSize.deincreaseBufferSize();
+        size = bufferSize.getBufferSize();       
            
         
     }
@@ -439,33 +509,146 @@
   
 }
 
-//this is setting all the function for the treads in the program
 
+static char *printDouble(char* str, double v, int decimalDigits=2)
+{
+    int i = 1;
+    int intPart, fractPart;
+    int len;
+    char *ptr;
+ 
+    /* prepare decimal digits multiplicator */
+    for (;decimalDigits!=0; i*=10, decimalDigits--);
+ 
+    /* calculate integer & fractinal parts */
+    intPart = (int)v;
+    fractPart = (int)((v-(double)(int)v)*i);
+ 
+    /* fill in integer part */
+    sprintf(str, "%i.", intPart);
+ 
+    /* prepare fill in of fractional part */
+    len = strlen(str);
+    ptr = &str[len];
+ 
+    /* fill in leading fractional zeros */
+    for (i/=10;i>1; i/=10, ptr++) {
+        if(fractPart >= i) break;
+        *ptr = '0';
+    }
+ 
+    /* fill in (rest of) fractional part */
+    sprintf(ptr, "%i", fractPart);
+ 
+    return str;
+}
+ 
+/*
+/this is going to sort the string to see the opertion which will picked the opertion which 
+/need the be carried out and the put the data in to the tread to then which the tread will
+/do it operation
+*/
+void sortString(char*  inputString) 
+{
+     
+     //if("DELETE" == values[0]){         
+         
+         //printCurrentRecord(values[]);
+         
+     //}
+     
+    //https://developer.mbed.org/teams/Students-Plymouth-University-UK-SoCEM/
+    
+}
+ 
+ 
+ 
+ 
+/*
+/this is function that will written to the memory of the from the buffer so that will be fast
+/ and did not lose anydata
+*/
+void writtenToMemory(const void* arg ) 
+{
+    
+}
+ 
+ 
+//this is setting all the function for the treads in the program
+ 
+ 
+ 
 
+ 
 /*
 /this is fucntion which the important the updateSample rate 
 */
-void updateSample(SamplePressure* pressure, SampleHumitaty* humitaty, SampleTemp* tempeture) 
+void updateSample(void const *arg) 
 {
-    while(!pressure->lock()){};
+     bool continueSampling;
+
+    do{
+        
+        char outputSample[200]; 
+        while(samplePressure.lock()){};
+        float currentPressure = samplePressure.getPressure();
+        samplePressure.unlock();
+        
+        char samplePress[10];
+        printDouble(samplePress, currentPressure, 2);
+        
+        
+        
+        
+    
+        while(sampleHumitaty.lock()){};
+        
+        float current= sampleHumitaty.getHumidity();
+        sampleHumitaty.unlock();    
+      
+        char sampleHum[10];
+        printDouble(sampleHum, current, 2);
+    
+        while(sampleTemp.lock()){};
         
-    float currentPressure = pressure->getPressure();
-    pressure->unlock();
+        float currentTemp= sampleTemp.getTemp();
+        sampleTemp.unlock();
+        
+        char sampleTemp[10];
+        printDouble(sampleTemp, currentTemp, 2);
+        
+        //time_t  currentTime;
+        //time(&currentTime);
+        //struct tm tstruct = *localtime(&currentTime);
+        
+        printCurrentRecord(samplePress, sampleTemp, sampleHum);
+        
+        while(takeSample.lock()){};
+        
+        continueSampling = takeSample.getTakeSample();
+        takeSample.unlock();
+        
+        int amountOfsec = sampleRate.getSampleRate();
+        
+        sprintf(outputSample, "Pressure : %s, Temp : %s, Hum : %s Time: seconds", samplePress, sampleTemp, sampleHum);
+        
+        secondsOfsample = secondsOfsample + amountOfsec;
+        
+        
+        wait(amountOfsec);
+        
+        
+        
+    }while (continueSampling);
     
-    while(!humitaty->lock()){};
         
-    float current= humitaty->getHumidity();
-    humitaty->unlock();
     
     
-    while(!tempeture->lock()){};
-        
-    float currentTemp= tempeture->getTemp();
-    tempeture->unlock();
+   
     
 }
-
-
+ 
+ 
 void splitString(char inputString[]){
        int sizeOfArray = 1000;
        char opertaion[sizeOfArray];
@@ -511,71 +694,9 @@
             } 
         }
 }
-
-static char *printDouble(char* str, double v, int decimalDigits=2)
-{
-    int i = 1;
-    int intPart, fractPart;
-    int len;
-    char *ptr;
-
-    /* prepare decimal digits multiplicator */
-    for (;decimalDigits!=0; i*=10, decimalDigits--);
-
-    /* calculate integer & fractinal parts */
-    intPart = (int)v;
-    fractPart = (int)((v-(double)(int)v)*i);
-
-    /* fill in integer part */
-    sprintf(str, "%i.", intPart);
-
-    /* prepare fill in of fractional part */
-    len = strlen(str);
-    ptr = &str[len];
-
-    /* fill in leading fractional zeros */
-    for (i/=10;i>1; i/=10, ptr++) {
-        if(fractPart >= i) break;
-        *ptr = '0';
-    }
-
-    /* fill in (rest of) fractional part */
-    sprintf(ptr, "%i", fractPart);
+ 
 
-    return str;
-}
-
-/*
-/this is going to sort the string to see the opertion which will picked the opertion which 
-/need the be carried out and the put the data in to the tread to then which the tread will
-/do it operation
-*/
-void sortString(char inputString[]) 
-{
-     
-     //if("DELETE" == values[0]){         
-         
-         //printCurrentRecord(values[]);
-         
-     //}
-     
-    //https://developer.mbed.org/teams/Students-Plymouth-University-UK-SoCEM/
-    
-}
-
-
-
-
-/*
-/this is function that will written to the memory of the from the buffer so that will be fast
-/ and did not lose anydata
-*/
-void writtenToMemory(const void* arg ) 
-{
-    
-}
-
-
+ 
 /*
 /this is he deleteRecords which will move the buffer regard so that the bufferent will over 
 /write to the  old data in the fifo buffer
@@ -584,8 +705,8 @@
 {
     
 }
-
-
+ 
+ 
 /*
 /read Record is the most reason data which which is store in the data 
 */
@@ -593,18 +714,9 @@
 {
     
 }
-
+ 
 
-/*
-/read Record is the selected data which be passed in as a a string
-*/
-void printCurrentRecord(char displayString, int length) 
-{        
-    printf("%c\n", displayString);
-    
-    
-}
-
+ 
 void deleteAll(BufferStartPointer* bufferStart, BufferEndPointer* bufferEnd){
  
     while(bufferStart -> lock()){}
@@ -623,7 +735,7 @@
  
     
 }
-
+ 
 /*
 /this is the fuction which update the the sample rate so the int to
 /update  the value whoch might hange
@@ -633,17 +745,17 @@
     while(!sampleRate -> lock()){};
     char outputString = 'OUT OF RANGE';
     
-    if(newSampleRate =>0.1 && newSampleRate =<60.0){
+    if(newSampleRate >= 0.1 && newSampleRate <= 60.0){
          sampleRate -> setSampleRate(newSampleRate); 
          char decimel;
          printDouble(&decimel, newSampleRate, 2);
           
-         outputString = 'UPDATE<' + decimel+ '>'; 
+         outputString = 'UPDATE<' + decimel + '>'; 
     }   
     sampleRate -> unlock();
     
 }
-
+ 
 /*/
 /this is update the the log so see if the log if the the sample rate 
 ozr not
@@ -673,7 +785,7 @@
     //need output tread
     
 }
-
+ 
 /*
 /tthis is update th date which the system will go from and with change
 /  the date so that the records are set with the date.
@@ -682,7 +794,7 @@
 {
     
 }
-
+ 
 /*
 /tthis is update th time which the system will go from and with change
 /  the time so that the records are set with the date.
@@ -691,7 +803,7 @@
 {
     
 }
-
+ 
 void updateDisplayState(SampleDisplay* sampleDisplay, char displayString){
     
     char output = ' ';
@@ -712,33 +824,25 @@
     sampleDisplay -> unlock();
     
 }
-
-void readInput(){
+ 
+void readInput() {
     
-    char ch;
-    char input[100]; 
-    int place = 0;
-    do
-    {
-        ch = getchar(); 
-        if(ch != EOF || ch != '\n'|| ch != ' '){
-            input[place] = ch;
-            place++;
-        }
+   char input[100];
+   int place = 0;
+   char c;
+    while ( (c = getchar()) != 0xD ) {
+       input[place] = c;
+       place++;
+    }
+    input[place]=0; //End of string
+       
+
         
-    }while( ch != '\n' || ch != EOF);
-    
-    if(place> 1){
-        
-        //Thread stringMutipluation;
+    printCurrentString(input, 8);
         
-        //stringMutipluation =7 new Thread(sortString);
-        
-        
-    }
 }
-
-
+ 
+ 
 // setting up gobal treads to be uses in the program
 //Thread producter();
 //Thread customer;
@@ -746,8 +850,6 @@
 //Thread humTread;
 //Thread pressTread(;
 //Thread displayTread;
-//Thread sampleTread;
-Thread inputTread;
 Thread deleteRecordTread(deleteRecord);
 //Thread setDateTread;
 //Thread setTimeTread;
@@ -755,28 +857,27 @@
 //Thread updateTakeDisplay;
 //Thread updateTreadTiming;
 //Thread stringMutipluation; 
-
-
+ 
+ 
 PwmOut mypwm(PWM_OUT);
+ 
+DigitalOut myled(LED1);
 
-DigitalOut myled(LED1);
 
 int main() {
-   
     
-    SampleRate sampleRate =  SampleRate();
-    TakeSample takeSample = TakeSample();
-    SampleTemp sampleTemp = SampleTemp();
-    SampleHumitaty sampleHumitaty = SampleHumitaty();
-    SamplePressure samplePressure =  SamplePressure();
-    SampleDisplay sampleDisplay = SampleDisplay();
-    BufferStartPointer bufferStartPointer = BufferStartPointer();
-    BufferEndPointer bufferEndPointer = BufferEndPointer();
-    bufferLock = new Mutex();
     
     
+
+    //Thread* sampleThread = new Thread(doSomething);
+    Thread sampleTread(updateSample);
+    
+     
+    
     while(1) {
         
+    
+        
         readInput();
         
         
@@ -784,4 +885,4 @@
         wait(1);
     }
 }
-
+