update code
Dependencies: X_NUCLEO_IKS01A1 mbed-rtos mbed
main.cpp
- Committer:
- snmatteo
- Date:
- 2016-05-03
- Revision:
- 6:397db5c5ad11
- Parent:
- 4:2c73682f05b5
File content as of revision 6:397db5c5ad11:
#include <stdlib.h> //#include <pthread.h> //#include <semaphore.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 BUFFER_SIZE = 75; // Mutex lock Mutex *bufferLock; //buffer char buffer_item[BUFFER_SIZE]; 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]; 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) { key = 1; return bufferStartPointer; } 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); fullBuffer = 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(){ EmptyBuffer -> release(); } void releaseFullBuffer(){ 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(); const int bufferIndex = endPointer->getBufferEndPointer(); buffer_item[bufferIndex] = 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 ramIndex = endPointer->getBufferEndPointer(); endPointer -> unlock(); while(!startPointer->lock()){}; startPointer->plusOne(); memorySample[ramIndex] = 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; } } } 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 */ 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) { printf("%c\n", displayString); } 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 = 'OUT OF RANGE'; if(newSampleRate >= 0.1 && newSampleRate <= 60.0){ sampleRate -> setSampleRate(newSampleRate); char decimel; printDouble(&decimel, newSampleRate, 2); outputString = 'UPDATE<' + decimel + '>'; } 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 = ' '; while(takeSample -> lock()){}; if(logType == 'NO'){ takeSample -> setTakeSample(false); output ='Log<ON>'; } if(logType == 'YES' ){ 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 = ' '; while(!sampleDisplay -> lock()){}; if(displayString == 'NO'){ sampleDisplay -> setDisplay(false); output = 'SAMPLING<ON>'; } if(displayString == 'YES'){ sampleDisplay -> setDisplay(true); output = 'SAMPLING<OFF>'; } sampleDisplay -> unlock(); } void readInput(){ char ch; char input[100]; int place = 0; do { ch = getchar(); if(ch != EOF || ch != '\n'|| ch != ' '){ input[place] = ch; place++; } }while( ch != '\n' || ch != EOF); if(place> 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() { //tid = new Thread(thread1); 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); } }