3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
Diff: CircularArray/CircularArray.cpp
- Revision:
- 83:0d3572a8a851
- Parent:
- 82:668b51a39148
- Child:
- 85:422d0a1b95cf
--- a/CircularArray/CircularArray.cpp Thu May 11 15:53:15 2017 +0000 +++ b/CircularArray/CircularArray.cpp Thu May 11 19:23:55 2017 +0000 @@ -1,9 +1,8 @@ #include "CircularArray.h" #include <stdio.h> #include <ctype.h> -/* - Constructor -*/ + + CircularArray::CircularArray(int limit, MessageLogger *newLogger) { maxSize = limit; @@ -12,6 +11,12 @@ currentSize = 0; logger = newLogger; } + +/** + Adds a new Measure object to the next available space in the buffer. + + @param _measure : Measure object to be added to buffer. +*/ void CircularArray::pushValue(Measure _measure) { if(currentSize >= maxSize) @@ -24,33 +29,48 @@ currentSize++; } +/** + Returns the first element in the buffer. + + @return array : First Measure object in the buffer. +*/ Measure CircularArray::pullValue() { return array[firstValue]; } + char temp2[256]; -void CircularArray::readX(int x) + +/** + Prints the newest n measurements from the buffer, using the MessageLogger. + + @param n : The number of measurements to be printed. +*/ +void CircularArray::readN(int n) { - if(x > currentSize) + if(n > currentSize) { - x = currentSize; - snprintf(temp2, 256, "Not enough measurements taken. Printing all %i measurement(s): \r\n", x); + n = currentSize; + snprintf(temp2, 256, "Not enough measurements taken. Printing all %i measurement(s): \r\n", n); } else { - snprintf(temp2, 256, "Printing %i measurement(s): \r\n", x); + snprintf(temp2, 256, "Printing %i measurement(s): \r\n", n); } logger->SendMessage(temp2); - int currentElement = nextSpace() - x; + int currentElement = nextSpace() - n; + if(currentElement < 0) { currentElement += maxSize; } char* ptr; - for(int i = 0; i < x; i++) + + // Iterates through newest n measurements sending the data to be printed via the MessageLogger. + for(int i = 0; i < n; i++) { ptr = array[currentElement].date.ToString(); snprintf(temp2, 256, "%i. %s, T: %f, H: %f, P: %f\n\r",i + 1,ptr , array[currentElement].temperature, array[currentElement].humidity, array[currentElement].pressure); @@ -59,25 +79,39 @@ currentElement = currentElement % maxSize; } } + +/** + Calls readN to print all measurements that are held in the buffer so far. +*/ void CircularArray::readAll() { logger->SendMessage("Printing all measurementss performed so far: \r\n"); - readX(currentSize); + readN(currentSize); } -void CircularArray::deleteX(int x) + +/** + Moves firstValue pointer n elements forward to allow old elements to be overwritten. + + @param n : Number of elements to be removed. +*/ +void CircularArray::deleteN(int n) { - if(x >= maxSize) + if(n >= maxSize) { deleteAll(); } else { - firstValue += x; - currentSize -= x; + firstValue += n; + currentSize -= n; firstValue = firstValue % maxSize; } } + +/** + Resets currently used size of the buffer to 0, allowing for new measurements to overwrite old measurements. +*/ void CircularArray::deleteAll() { snprintf(temp2, 256, "Deleted %i records.", currentSize); @@ -85,10 +119,20 @@ currentSize = 0; } + +/** + @return currentSize: The current number of elements used in the buffer. +*/ int CircularArray::getSize() { return currentSize; } + +/** + Calculates the next available space in the buffer. + + @return nextSpace : The index of the next available space. +*/ int CircularArray::nextSpace() { int nextSpace = firstValue + currentSize;