update code
Dependencies: X_NUCLEO_IKS01A1 mbed-rtos mbed
Diff: main.cpp
- Revision:
- 3:f61ae69ed662
- Parent:
- 2:c5fb78148ccc
- Child:
- 4:2c73682f05b5
- Child:
- 7:cb29c20009e8
--- a/main.cpp Tue May 03 10:51:55 2016 +0000 +++ b/main.cpp Tue May 03 12:27:07 2016 +0000 @@ -24,7 +24,7 @@ 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; +//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 @@ -41,7 +41,7 @@ //buffer -char buffer_item[BUFFER_SIZE][200]; +char buffer_item[BUFFER_SIZE]; Thread tid; //thread id @@ -52,7 +52,7 @@ -volatile char memorySample[amountOfSample][200]; +volatile char memorySample[amountOfSample]; char values[20][20]; @@ -254,8 +254,9 @@ int getBufferStartPointer (void) { + key = 1; return bufferStartPointer; - key = 1; + } void setBufferStartPointer (int newBufferStartPointer ){ @@ -314,7 +315,7 @@ BufferSize(){ EmptyBuffer = new Semaphore(0); - sapceAvailable = new Semaphore(BUFFER_SIZE); + fullBuffer = new Semaphore(BUFFER_SIZE); bufferSize = 0; } @@ -340,11 +341,11 @@ } void releaseEmptyBuffer(){ - return EmptyBuffer -> release(); + EmptyBuffer -> release(); } void releaseFullBuffer(){ - return fullBuffer -> release(); + fullBuffer -> release(); } @@ -368,7 +369,7 @@ }**/ //producer thread -void *producer(char newItem[], BufferEndPointer *endPointer, BufferSize *bufferSize){ +void *producer(char newItem, BufferEndPointer *endPointer, BufferSize *bufferSize){ while(!bufferSize->lock()){}; @@ -384,7 +385,8 @@ while(!endPointer->lock()){}; endPointer->plusOne(); - buffer_item[endPointer->getBufferEndPointer()] = newItem[]; + const int bufferIndex = endPointer->getBufferEndPointer(); + buffer_item[bufferIndex] = newItem; endPointer->unlock(); bufferLock->unlock(); while(!bufferSize->lock()){}; @@ -410,14 +412,19 @@ while(!endPointer->lock()){}; - int index = endPointer->getBufferEndPointer(); + int ramIndex = endPointer->getBufferEndPointer(); - endPointer -> unlock; + endPointer -> unlock(); while(!startPointer->lock()){}; startPointer->plusOne(); - memorySample[index] = buffer_item[startPointer->getBufferStartPointer()]; + + + memorySample[ramIndex] = buffer_item[startPointer->getBufferStartPointer()]; + + + startPointer->unlock(); bufferLock->unlock(); @@ -505,6 +512,39 @@ } } +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 @@ -558,19 +598,9 @@ /* /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 printCurrentRecord(char displayString, int length) +{ + printf("%c\n", displayString); } @@ -601,13 +631,15 @@ void updateSampleRate(double newSampleRate, SampleRate* sampleRate) { while(!sampleRate -> lock()){}; - char outputString[20]; + char outputString = 'OUT OF RANGE'; + if(newSampleRate =>0.1 && newSampleRate =<60.0){ - sampleRate -> setSampleRate(newSampleRate); - outputString = 'UPDATE<' + sampleRate+ '>'; - }else{ - outputString = 'OUT OF RANGE'; - } + sampleRate -> setSampleRate(newSampleRate); + char decimel; + printDouble(&decimel, newSampleRate, 2); + + outputString = 'UPDATE<' + decimel+ '>'; + } sampleRate -> unlock(); } @@ -616,26 +648,25 @@ /this is update the the log so see if the log if the the sample rate ozr not */ -void updateLog(char logType[], TakeSample* takeSample) +void updateLog(char logType, TakeSample *takeSample) { - char output[20]; + char output = ' '; + + while(takeSample -> lock()){}; - 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 == 'NO'){ + 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>'; - } - } + if(logType == 'YES' ){ + takeSample -> setTakeSample(true); + output ='Log<OFF>'; + + } takeSample -> unlock(); @@ -661,26 +692,22 @@ } -void updateDisplayState(SampleDisplay* sampleDisplay, char displayString[]){ +void updateDisplayState(SampleDisplay* sampleDisplay, char displayString){ - char output[20]; + char output = ' '; 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 == 'NO'){ + 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>'; - } - } + if(displayString == 'YES'){ + sampleDisplay -> setDisplay(true); + output = 'SAMPLING<OFF>'; + + } sampleDisplay -> unlock(); @@ -690,18 +717,18 @@ char ch; char input[100]; - int index = 0; + int place = 0; do { ch = getchar(); if(ch != EOF || ch != '\n'|| ch != ' '){ - input[index] = ch; - index++; + input[place] = ch; + place++; } }while( ch != '\n' || ch != EOF); - if(index > 1){ + if(place> 1){ //Thread stringMutipluation; @@ -735,37 +762,7 @@ 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();