update code

Dependencies:   X_NUCLEO_IKS01A1 mbed-rtos mbed

Revision:
0:4a8b751dbe7c
Child:
1:4cd93767691a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon May 02 15:26:28 2016 +0000
@@ -0,0 +1,750 @@
+#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
+
+//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;
+
+typedf int buffer_item;
+#define BUFFER_SIZE 75
+
+// mutex lock
+pthread_mutex_t mutex;
+
+//semaphores
+sem_t full, empty;
+
+//buffer
+buffer_item buffer[BUFFER_SIZE];
+
+//buffer counter
+int counter;
+
+pthread_t 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[150][200];
+
+char values[20][20];
+
+/*
+/ 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.
+*/
+int amountOfSample = 150;
+
+
+uint8_t id;
+
+
+//class
+
+class KeyLocks {
+    
+    public:
+        int key;
+        
+        KeyLocks(){
+            
+            key = 0;
+            
+        }
+        
+        bool unlock(void)
+        {
+             if(key == 0){
+                 
+                 key = 1;
+                 return true;
+                 
+            }else{
+                return false;
+                
+                }
+        } 
+        
+        bool lock(void)
+        {
+             if(key == 1){
+                 
+                 key = 0;
+                 return true;
+                 
+            }else{
+                return false;
+                
+                }
+        } 
+            
+};
+
+class SampleRate : public KeyLocks 
+{
+    public:
+        /*
+        /this is going the amount of time which the data sample will be taking and this will have
+        /a default stating of every 15 second but can be changed
+        */
+        int sampleRate;
+        
+        SampleRate(){
+            
+            sampleRate = 15;
+            
+        }
+        
+        int getSampleRate(void)
+        {
+             return sampleRate;
+        } 
+        void setSampleRate(double newSampleRate){
+            
+            sampleRate = newSampleRate;
+            
+        }
+        
+        
+        
+};
+
+
+class TakeSample : public KeyLocks
+{
+    public:
+        /*
+        /this is going to take the sample or not as this can change if the person thing it could happen 
+        /which will see if the data is stored   
+        */  
+        bool takeSample;
+        
+        TakeSample(){
+            takeSample = true;    
+        }
+        
+        int getTakeSample(void)
+        {
+             return takeSample;
+        } 
+        void setTakeSample(bool newTakeSample){
+            
+            takeSample = newTakeSample;
+            
+        }       
+};
+
+
+
+class SampleTemp: public KeyLocks
+{
+    public: 
+    
+        SampleTemp(){}
+        float currentTemp;       
+        /*
+        /this is the fuction which will get the humidity for the external devices
+        */
+        float getTemp(void) 
+        {
+            temp_sensor1->GetTemperature(&currentTemp); 
+            return currentTemp;  
+        }              
+};
+
+class SampleHumitaty: public KeyLocks
+{
+    public: 
+    
+        SampleHumitaty(){}
+    
+        float currentHumitaty;        
+        /*
+        /this is the fuction which will get the humidity for the external devices
+        */
+        float getHumidity() 
+        {
+            humidity_sensor->GetHumidity(&currentHumitaty);
+            return currentHumitaty;
+            
+        }              
+};
+
+
+class SamplePressure: public KeyLocks 
+
+{
+    public:
+    
+        SamplePressure(){}   
+    
+        float currentPressure;     
+        /*
+        /this is the fuction which will get the pressure for the external devices
+        */
+        float getPressure() 
+        {
+            pressure_sensor->GetPressure(&currentPressure);
+            return currentPressure;
+        }             
+};
+
+class SampleDisplay: public KeyLocks
+{
+    public:
+        /*
+        /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(){
+            display = false;
+        }
+        
+        bool getTakeDisplay(void)
+        {
+             return display;
+        } 
+        void setDisplay(bool newSampleDisplay){
+            
+            display = newSampleDisplay;
+            
+        }       
+};
+
+ 
+ 
+ 
+class BufferStartPointer: public KeyLocks
+{
+    public:
+        /*
+        /this is the pointer to start of the buffer as this will make the buffer be cycler
+        */
+        int bufferStartPointer;
+        
+        BufferStartPointer(){
+            bufferStartPointer = 0;
+        }
+        
+        int getBufferStartPointer (void)
+        {
+             return bufferStartPointer;
+        } 
+        void setBufferStartPointer (int newBufferStartPointer ){
+            
+            bufferStartPointer = newBufferStartPointer;
+            
+        }       
+};
+
+
+class BufferEndPointer: public KeyLocks
+{
+    public:
+        /*
+        /this is the pointer to End of the buffer as this will make the buffer be cycler
+        */
+        int bufferEndPointer;
+        
+        BufferEndPointer(){
+            bufferEndPointer = 0;
+        }
+        
+        int getBufferEndPointer (void)
+        {
+             return bufferEndPointer;
+        } 
+        void setBufferEndPointer (int newBufferEndPointer ){
+            
+            bufferEndPointer = newBufferEndPointer;
+            
+        }       
+};
+
+void initialiseBufferData(){
+    //create mutex lock
+    pthread_mutex_init(&mutex, NULL);
+    
+    //create full semaphore and initialise to 0
+    sem_init(&full, 0, 0);
+    
+    //create empty semaphore and initialise to BUFFER_SIZE
+    sem_init(&empty, 0, BUFFER_SIZE);
+    
+    //get default attributes
+    pthread_attr_init(&attr);
+    
+    //init buffer
+    counter = 0;
+}
+
+//producer thread
+void *producer(void *param){
+   buffer_item item;
+   
+   while(TRUE){
+       //sleep for random amount of time
+       int rNum = rand() / RAND_DIVISOR;
+       sleep(rNum);
+       
+       //generate random number
+       item = rand();
+       
+       //get empty lock
+       sem_wait(&empty);
+       //get mutex lock
+       pthread_mutex_lock(&mutex);
+       
+       if(insert_item(item)){
+           fprint(stderr, "Producer report error condition\n");
+        }
+        else{
+            printf("producer produced %d\n", item);
+        }
+        //release mutex lock
+        pthread_mutex_unlock(&mutex);
+        //signal full
+        sem_post(&full);
+    }
+}
+
+//consumer thread
+void *consumer(void {
+   buffer_item item;
+   
+   while(TRUE){
+       //sleep for random amount of time
+       int rNum = rand() / RAND_DIVISOR;
+       sleep(rNum);
+       
+       //get full lock
+       sem_wait(&full);
+       //get mutex lock
+       pthread_mutex_lock(&mutex);
+       if(remove_item(&item)){
+           fprint(stderr, "Consumer report error condition\n");
+        }
+        else{
+            printf("Consumer consumer %d\n", item);
+        }
+        
+        //release mutex ock
+        pthread_mutex_unlock(&mutex);
+        //signal empty
+        sem_post(&empty);
+    }
+}
+
+//add item to buffer
+int insert_item(buffer_item item) {
+    //when buffer not full add item and increase counter
+    if(counter < BUFFER_SIZE){
+        buffer[counter] = item;
+        counter++
+        return 0;
+    }
+    else{ //buffer full error
+        return -1
+    }
+}
+
+//remove item from buffer
+int remove_item(buffer_item *item){
+    //when buffer not empty remove item and decrease counter
+    if(counter > 0){
+        *item = buffer[(counter-1)];
+        counter--;
+        return 0;
+    }
+    else{ //empty buffer error
+        return -1;
+    }
+}
+
+
+
+   
+    
+
+
+
+//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) 
+{
+    while(!pressure->lock()){};
+        
+    float currentPressure = pressure->getPressure();
+    pressure->unlock();
+    
+    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];
+       char data[sizeOfArray];
+       
+       for (int i = 0; i < 20; i++){
+           for (int j = 0; i < 20; j++){
+               values[i][j] =' ';
+               
+            }
+        }
+        
+       for (int i = 0; i < sizeOfArray; i++){
+         
+            if(inputString[i] == '<'){
+                for(int j=0; j < i; j++){
+                   
+                   opertaion[j] = inputString[j];                  
+                   
+                }
+                
+                int index = 0;
+                
+                for(int k=i+1; k < sizeOfArray - 1; k++){
+                    
+                    data[index] = (inputString[k]);
+                    index++;
+                    
+                } 
+                
+                
+                
+                for(int y = 0; y < 20; y++){
+                  values[0][y] = opertaion[y];
+                }
+                
+                for(int z = 0;z < 20; z++){
+                    values[1][z] = data[z];
+                }
+                         
+                break;
+                   
+            } 
+        }
+}
+
+/*
+/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
+*/
+void deleteRecord(const void* arg ) 
+{
+    
+}
+
+
+/*
+/read Record is the most reason data which which is store in the data 
+*/
+void readRecord(const void* arg ) 
+{
+    
+}
+
+
+/*
+/read Record is the selected data which be passed in as a a string
+*/
+void printCurrentRecord(char displayString[], int length) 
+{
+    
+    for(int i = 0; i < length - 1; i++){
+        
+        const char letter  = displayString[i];
+        printf('%c', letter);
+        
+        
+    }
+    
+    char letter = displayString[length - 1];        
+    //printf('%c\n',letter);
+    
+    
+}
+
+void deleteAll(BufferStartPointer* bufferStart, BufferEndPointer* bufferEnd){
+ 
+    while(bufferStart -> lock()){}
+    bufferStart -> setBufferStartPointer(0);
+    bufferStart -> unlock();
+    
+    while(bufferEnd -> lock()){};
+    bufferEnd -> setBufferEndPointer(0);
+    bufferEnd -> unlock();
+    
+    
+    // need the ram memory
+    
+    
+ 
+ 
+    
+}
+
+/*
+/this is the fuction which update the the sample rate so the int to
+/update  the value whoch might hange
+*/
+void updateSampleRate(double newSampleRate, SampleRate* sampleRate) 
+{
+    while(!sampleRate -> lock()){};
+    char outputString[20];
+    if(newSampleRate =>0.1 && newSampleRate =<60.0){
+         sampleRate -> setSampleRate(newSampleRate);  
+         outputString = 'UPDATE<' + sampleRate+ '>'; 
+    }else{
+        outputString = 'OUT OF RANGE'; 
+     }    
+    sampleRate -> unlock();
+    
+}
+
+/*/
+/this is update the the log so see if the log if the the sample rate 
+ozr not
+*/
+void updateLog(char logType[], TakeSample* takeSample) 
+{
+    char output[20];
+    
+    while(!takeSample -> lock()){}();
+    if(logType[0] == 'N' || logType[0] == 'N'){
+        if(logType[1] == 'O' || logType[0] == 'o'){
+            takeSample -> setTakeSample(false);
+            output ='Log<ON>';
+        }
+            
+    }
+    
+    if(logType[0] == 'Y' || logType[0] == 'y'){
+        if(logType[1] == 'E' || logType[0] == 'e'){
+            if(logType[2] == 'E' || logType[0] == 'e'){
+                takeSample -> setTakeSample(true);
+                output ='Log<OFF>';
+            }            
+        }
+    }
+    takeSample -> unlock();
+    
+    //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.
+*/
+void updateDate(char newDate[]) 
+{
+    
+}
+
+/*
+/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.
+*/
+void updateTime(char newTime[]) 
+{
+    
+}
+
+void updateDisplayState(SampleDisplay* sampleDisplay, char displayString[]){
+    
+    char output[20];
+    
+    while(!sampleDisplay -> lock()){};
+    if(displayString[0] == 'N' || displayString[0] == 'N'){
+        if(displayString[1] == 'O' || displayString[1] == 'o'){
+            sampleDisplay -> setDisplay(false);
+            output ='SAMPLING<ON>';
+        }
+            
+    }
+    
+    if(displayString[0] == 'Y' || displayString[0] == 'y'){
+        if(displayString[1] == 'E' || displayString[0] == 'e'){
+            if(displayString[2] == 'E' || displayString[0] == 'e'){
+                sampleDisplay -> setDisplay(true);
+                output ='SAMPLING<OFF>';
+            }            
+        }
+    }
+    sampleDisplay -> unlock();
+    
+}
+
+void readInput(){
+    
+    char ch;
+    char input[100]; 
+    int index = 0;
+    do
+    {
+        ch = getchar(); 
+        if(ch != EOF || ch != '\n'|| ch != ' '){
+            input[index] = ch;
+            index++;
+        }
+        
+    }while( ch != '\n' || ch != EOF);
+    
+    if(index > 1){
+        
+        //Thread stringMutipluation;
+        
+        //stringMutipluation =7 new Thread(sortString);
+        
+        
+    }
+}
+
+
+// setting up gobal treads to be uses in the program
+//Thread producter();
+//Thread customer;
+//Thread tempTread;
+//Thread humTread;
+//Thread pressTread(;
+//Thread displayTread;
+//Thread sampleTread;
+Thread inputTread;
+Thread deleteRecordTread(deleteRecord);
+//Thread setDateTread;
+//Thread setTimeTread;
+//Thread dsiplaySelectedRecord;
+//Thread updateTakeDisplay;
+//Thread updateTreadTiming;
+//Thread stringMutipluation; 
+
+
+PwmOut mypwm(PWM_OUT);
+
+DigitalOut myled(LED1);
+
+int main() {
+    
+    //loop counter
+    int i;
+    
+    int argc;
+    
+    //verify correct number of arguments passed into buffer
+    if(argc != 4){
+        fprint(stderr, "USAGE:./main.out <INT> <INT> <INT>\n");
+    }
+    
+    int mainSleepTime = atoi(argv[1]);//time in seconds for main to sleep
+    int numProd = atoi(argv[2]); //number of producer threads
+    int numCons = atoi(argv[3]); //number of consumer threads
+    
+    //initialise buffer data
+    initialiseBufferData();
+    
+    //create producer threads
+    for(i = 0; i < numProd; i++){
+        pthread_create(&tid, &attr, producer, NULL);
+    
+    }
+    
+    //create cinsumer threads
+    for(i = 0; i < numProd; i++){
+        pthread_create(&tid, &attr, consumer, NULL);
+    }
+    
+    //sleep for specified amount of time in ms
+    sleep(mainSleepTime);
+    
+    SampleRate sampleRate =  SampleRate();
+    TakeSample takeSample = TakeSample();
+    SampleTemp sampleTemp = SampleTemp();
+    SampleHumitaty sampleHumitaty = SampleHumitaty();
+    SamplePressure samplePressure =  SamplePressure();
+    SampleDisplay sampleDisplay = SampleDisplay();
+    BufferStartPointer bufferStartPointer = BufferStartPointer();
+    BufferEndPointer bufferEndPointer = BufferEndPointer();
+    
+    
+    while(1) {
+        
+        readInput();
+        
+        
+        myled = !myled;
+        wait(1);
+    }
+}
+