3rd Repo, trying to figure this out.

Dependencies:   LPS25H hts221

Fork of SOFT253_Template_Weather_OS_54 by Stage-1 Students SoCEM

Committer:
niallfrancis
Date:
Sat May 13 17:35:58 2017 +0000
Revision:
85:422d0a1b95cf
Parent:
83:0d3572a8a851
Finished commenting classes;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Netaphous 64:8ada3e0b2048 1 #include "CircularArray.h"
Netaphous 64:8ada3e0b2048 2 #include <stdio.h>
Netaphous 64:8ada3e0b2048 3 #include <ctype.h>
aburch1 83:0d3572a8a851 4
niallfrancis 85:422d0a1b95cf 5 /**
niallfrancis 85:422d0a1b95cf 6 @file : CircularArray.cpp
niallfrancis 85:422d0a1b95cf 7 @authors : Radu Marcu, Jacob Williams, Niall Francis, Arron Burch
niallfrancis 85:422d0a1b95cf 8
niallfrancis 85:422d0a1b95cf 9 @section DESCRIPTION
niallfrancis 85:422d0a1b95cf 10
niallfrancis 85:422d0a1b95cf 11 This is the circular array class responsible for managing data stored in the circular array.
niallfrancis 85:422d0a1b95cf 12 It handles all functions related to the stored measures, including pushing measures to the next available
niallfrancis 85:422d0a1b95cf 13 space, pulling the first measure, as well as reading and deleting measures. As this is a circular buffer,
niallfrancis 85:422d0a1b95cf 14 if there is a new measure added and not enough room for it, the oldest measures will be removed in order
niallfrancis 85:422d0a1b95cf 15 to make space.
niallfrancis 85:422d0a1b95cf 16 This class was written to be fairly robust; if it is asked to delete or read more measures than it
niallfrancis 85:422d0a1b95cf 17 contains, the class can adapt and delete or read the highest possible value.
niallfrancis 85:422d0a1b95cf 18
niallfrancis 85:422d0a1b95cf 19 */
aburch1 83:0d3572a8a851 20
aburch1 81:996c0a3319b4 21 CircularArray::CircularArray(int limit, MessageLogger *newLogger)
Netaphous 64:8ada3e0b2048 22 {
Netaphous 64:8ada3e0b2048 23 maxSize = limit;
Netaphous 64:8ada3e0b2048 24 array = new Measure[maxSize];
Netaphous 64:8ada3e0b2048 25 firstValue = 0;
Netaphous 64:8ada3e0b2048 26 currentSize = 0;
aburch1 81:996c0a3319b4 27 logger = newLogger;
aburch1 81:996c0a3319b4 28 }
aburch1 83:0d3572a8a851 29
aburch1 83:0d3572a8a851 30 /**
aburch1 83:0d3572a8a851 31 Adds a new Measure object to the next available space in the buffer.
aburch1 83:0d3572a8a851 32
aburch1 83:0d3572a8a851 33 @param _measure : Measure object to be added to buffer.
aburch1 83:0d3572a8a851 34 */
Netaphous 64:8ada3e0b2048 35 void CircularArray::pushValue(Measure _measure)
Netaphous 64:8ada3e0b2048 36 {
Netaphous 64:8ada3e0b2048 37 if(currentSize >= maxSize)
Netaphous 64:8ada3e0b2048 38 {
Netaphous 64:8ada3e0b2048 39 firstValue++;
Netaphous 64:8ada3e0b2048 40 currentSize--;
Netaphous 64:8ada3e0b2048 41 }
aburch1 72:ef4a4e3089c1 42 int next = nextSpace();
aburch1 72:ef4a4e3089c1 43 array[next] = _measure;
Netaphous 64:8ada3e0b2048 44 currentSize++;
Netaphous 64:8ada3e0b2048 45 }
aburch1 80:959151952153 46
aburch1 83:0d3572a8a851 47 /**
aburch1 83:0d3572a8a851 48 Returns the first element in the buffer.
aburch1 83:0d3572a8a851 49
aburch1 83:0d3572a8a851 50 @return array : First Measure object in the buffer.
aburch1 83:0d3572a8a851 51 */
aburch1 80:959151952153 52 Measure CircularArray::pullValue()
aburch1 80:959151952153 53 {
aburch1 80:959151952153 54 return array[firstValue];
aburch1 80:959151952153 55 }
aburch1 83:0d3572a8a851 56
aburch1 81:996c0a3319b4 57 char temp2[256];
aburch1 83:0d3572a8a851 58
aburch1 83:0d3572a8a851 59 /**
aburch1 83:0d3572a8a851 60 Prints the newest n measurements from the buffer, using the MessageLogger.
aburch1 83:0d3572a8a851 61
aburch1 83:0d3572a8a851 62 @param n : The number of measurements to be printed.
aburch1 83:0d3572a8a851 63 */
aburch1 83:0d3572a8a851 64 void CircularArray::readN(int n)
Netaphous 64:8ada3e0b2048 65 {
aburch1 83:0d3572a8a851 66 if(n > currentSize)
Netaphous 64:8ada3e0b2048 67 {
aburch1 83:0d3572a8a851 68 n = currentSize;
aburch1 83:0d3572a8a851 69 snprintf(temp2, 256, "Not enough measurements taken. Printing all %i measurement(s): \r\n", n);
Netaphous 64:8ada3e0b2048 70 }
aburch1 82:668b51a39148 71 else
aburch1 82:668b51a39148 72 {
aburch1 83:0d3572a8a851 73 snprintf(temp2, 256, "Printing %i measurement(s): \r\n", n);
aburch1 82:668b51a39148 74 }
aburch1 82:668b51a39148 75
aburch1 82:668b51a39148 76 logger->SendMessage(temp2);
aburch1 82:668b51a39148 77
aburch1 83:0d3572a8a851 78 int currentElement = nextSpace() - n;
aburch1 83:0d3572a8a851 79
Netaphous 64:8ada3e0b2048 80 if(currentElement < 0)
Netaphous 64:8ada3e0b2048 81 {
Netaphous 64:8ada3e0b2048 82 currentElement += maxSize;
Netaphous 64:8ada3e0b2048 83 }
aburch1 79:4e6b53eb678b 84
aburch1 79:4e6b53eb678b 85 char* ptr;
aburch1 83:0d3572a8a851 86
aburch1 83:0d3572a8a851 87 // Iterates through newest n measurements sending the data to be printed via the MessageLogger.
aburch1 83:0d3572a8a851 88 for(int i = 0; i < n; i++)
Netaphous 64:8ada3e0b2048 89 {
aburch1 79:4e6b53eb678b 90 ptr = array[currentElement].date.ToString();
aburch1 82:668b51a39148 91 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);
aburch1 81:996c0a3319b4 92 logger->SendMessage(temp2);
Netaphous 64:8ada3e0b2048 93 currentElement++;
Netaphous 64:8ada3e0b2048 94 currentElement = currentElement % maxSize;
Netaphous 64:8ada3e0b2048 95 }
Netaphous 64:8ada3e0b2048 96 }
aburch1 83:0d3572a8a851 97
aburch1 83:0d3572a8a851 98 /**
aburch1 83:0d3572a8a851 99 Calls readN to print all measurements that are held in the buffer so far.
aburch1 83:0d3572a8a851 100 */
Netaphous 65:3723d2729b68 101 void CircularArray::readAll()
Netaphous 64:8ada3e0b2048 102 {
aburch1 82:668b51a39148 103 logger->SendMessage("Printing all measurementss performed so far: \r\n");
aburch1 82:668b51a39148 104
aburch1 83:0d3572a8a851 105 readN(currentSize);
Netaphous 64:8ada3e0b2048 106 }
aburch1 83:0d3572a8a851 107
aburch1 83:0d3572a8a851 108 /**
aburch1 83:0d3572a8a851 109 Moves firstValue pointer n elements forward to allow old elements to be overwritten.
aburch1 83:0d3572a8a851 110
aburch1 83:0d3572a8a851 111 @param n : Number of elements to be removed.
aburch1 83:0d3572a8a851 112 */
aburch1 83:0d3572a8a851 113 void CircularArray::deleteN(int n)
Netaphous 64:8ada3e0b2048 114 {
aburch1 83:0d3572a8a851 115 if(n >= maxSize)
Netaphous 64:8ada3e0b2048 116 {
Netaphous 64:8ada3e0b2048 117 deleteAll();
Netaphous 64:8ada3e0b2048 118 }
Netaphous 64:8ada3e0b2048 119 else
Netaphous 64:8ada3e0b2048 120 {
aburch1 83:0d3572a8a851 121 firstValue += n;
aburch1 83:0d3572a8a851 122 currentSize -= n;
Netaphous 64:8ada3e0b2048 123 firstValue = firstValue % maxSize;
Netaphous 64:8ada3e0b2048 124 }
Netaphous 64:8ada3e0b2048 125 }
aburch1 83:0d3572a8a851 126
aburch1 83:0d3572a8a851 127 /**
aburch1 83:0d3572a8a851 128 Resets currently used size of the buffer to 0, allowing for new measurements to overwrite old measurements.
aburch1 83:0d3572a8a851 129 */
Netaphous 64:8ada3e0b2048 130 void CircularArray::deleteAll()
Netaphous 64:8ada3e0b2048 131 {
aburch1 82:668b51a39148 132 snprintf(temp2, 256, "Deleted %i records.", currentSize);
aburch1 82:668b51a39148 133 logger->SendMessage(temp2);
Netaphous 64:8ada3e0b2048 134 currentSize = 0;
aburch1 82:668b51a39148 135
Netaphous 64:8ada3e0b2048 136 }
aburch1 83:0d3572a8a851 137
aburch1 83:0d3572a8a851 138 /**
aburch1 83:0d3572a8a851 139 @return currentSize: The current number of elements used in the buffer.
aburch1 83:0d3572a8a851 140 */
Netaphous 64:8ada3e0b2048 141 int CircularArray::getSize()
Netaphous 64:8ada3e0b2048 142 {
Netaphous 64:8ada3e0b2048 143 return currentSize;
Netaphous 64:8ada3e0b2048 144 }
aburch1 83:0d3572a8a851 145
aburch1 83:0d3572a8a851 146 /**
aburch1 83:0d3572a8a851 147 Calculates the next available space in the buffer.
aburch1 83:0d3572a8a851 148
aburch1 83:0d3572a8a851 149 @return nextSpace : The index of the next available space.
aburch1 83:0d3572a8a851 150 */
Netaphous 64:8ada3e0b2048 151 int CircularArray::nextSpace()
Netaphous 64:8ada3e0b2048 152 {
Netaphous 64:8ada3e0b2048 153 int nextSpace = firstValue + currentSize;
Netaphous 64:8ada3e0b2048 154 nextSpace = nextSpace % maxSize;
Netaphous 64:8ada3e0b2048 155 return nextSpace;
Netaphous 64:8ada3e0b2048 156 }