update code
Dependencies: X_NUCLEO_IKS01A1 mbed-rtos mbed
main.cpp
- Committer:
- acarter2
- Date:
- 2016-05-03
- Revision:
- 2:c5fb78148ccc
- Parent:
- 1:4cd93767691a
- Child:
- 3:f61ae69ed662
File content as of revision 2:c5fb78148ccc:
#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; /* / 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 BUFFER_SIZE = 75; // mutex lock Mutex *bufferLock; //buffer char buffer_item[BUFFER_SIZE][200]; 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][200]; char values[20][20]; 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(¤tTemp); 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(¤tHumitaty); 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(¤tPressure); 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; key = 1; } void setBufferStartPointer (int newBufferStartPointer ){ bufferStartPointer = newBufferStartPointer; } void plusOne(void){ if(bufferStartPointer + 1 == BUFFER_SIZE){ bufferStartPointer = 0; }else{ bufferStartPointer = bufferStartPointer + 1; } } }; 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 plusOne(void){ if(bufferEndPointer + 1 == BUFFER_SIZE){ bufferEndPointer = 0; }else{ bufferEndPointer = bufferEndPointer + 1; } } }; class BufferSize: public KeyLocks { public: int bufferSize; Semaphore *EmptyBuffer; Semaphore *fullBuffer; BufferSize(){ EmptyBuffer = new Semaphore(0); sapceAvailable = new Semaphore(BUFFER_SIZE); bufferSize = 0; } void increaseBufferSize(){ bufferSize = bufferSize + 1; } void deincreaseBufferSize(){ bufferSize = bufferSize - 1; } int getBufferSize(){ return bufferSize; } int getEmptyBuffer(){ return EmptyBuffer -> wait(); } int getFullBuffer(){ return fullBuffer -> wait(); } void releaseEmptyBuffer(){ return EmptyBuffer -> release(); } void releaseFullBuffer(){ return fullBuffer -> release(); } }; /**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(char newItem[], BufferEndPointer *endPointer, BufferSize *bufferSize){ while(!bufferSize->lock()){}; int size = bufferSize -> getBufferSize(); while(size < BUFFER_SIZE){ //sleep for random amount of time //get the buffer bufferLock->lock(); while(!endPointer->lock()){}; endPointer->plusOne(); buffer_item[endPointer->getBufferEndPointer()] = newItem[]; endPointer->unlock(); bufferLock->unlock(); while(!bufferSize->lock()){}; bufferSize ->increaseBufferSize(); size = bufferSize -> getBufferSize(); } } //consumer thread void *consumer(BufferStartPointer *startPointer, BufferSize *bufferSize, BufferEndPointer *endPointer) { while(!bufferSize->lock()){}; int size = bufferSize -> getBufferSize(); while(size > 0){ //sleep for random amount of time //get the buffer bufferLock->lock(); while(!endPointer->lock()){}; int index = endPointer->getBufferEndPointer(); endPointer -> unlock; while(!startPointer->lock()){}; startPointer->plusOne(); memorySample[index] = buffer_item[startPointer->getBufferStartPointer()]; startPointer->unlock(); bufferLock->unlock(); while(!bufferSize->lock()){}; bufferSize ->deincreaseBufferSize(); size = bufferSize -> getBufferSize(); } } //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(); bufferLock = new Mutex(); while(1) { readInput(); myled = !myled; wait(1); } }