3rd Repo, trying to figure this out.

Dependencies:   LPS25H hts221

Fork of SOFT253_Template_Weather_OS_54 by Stage-1 Students SoCEM

Committer:
aburch1
Date:
Fri Apr 21 18:32:12 2017 +0000
Revision:
71:a935e4b88ad8
Parent:
65:3723d2729b68
Child:
72:ef4a4e3089c1
Updated Circular Array to use nextSpace() method to avoid redundant code

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>
Netaphous 64:8ada3e0b2048 4 /*
Netaphous 64:8ada3e0b2048 5 Constructor
Netaphous 64:8ada3e0b2048 6 */
Netaphous 64:8ada3e0b2048 7 CircularArray::CircularArray(int limit)
Netaphous 64:8ada3e0b2048 8 {
Netaphous 64:8ada3e0b2048 9 maxSize = limit;
Netaphous 64:8ada3e0b2048 10 array = new Measure[maxSize];
Netaphous 64:8ada3e0b2048 11 firstValue = 0;
Netaphous 64:8ada3e0b2048 12 currentSize = 0;
Netaphous 64:8ada3e0b2048 13 }
Netaphous 64:8ada3e0b2048 14 void CircularArray::pushValue(Measure _measure)
Netaphous 64:8ada3e0b2048 15 {
Netaphous 64:8ada3e0b2048 16 if(currentSize >= maxSize)
Netaphous 64:8ada3e0b2048 17 {
Netaphous 64:8ada3e0b2048 18 firstValue++;
Netaphous 64:8ada3e0b2048 19 currentSize--;
Netaphous 64:8ada3e0b2048 20 }
aburch1 71:a935e4b88ad8 21 int nextSpace = nextSpace();
Netaphous 64:8ada3e0b2048 22 array[nextSpace] = _measure;
Netaphous 64:8ada3e0b2048 23 currentSize++;
Netaphous 64:8ada3e0b2048 24 }
Netaphous 65:3723d2729b68 25 void CircularArray::readX(int x)
Netaphous 64:8ada3e0b2048 26 {
Netaphous 64:8ada3e0b2048 27 if(x > currentSize)
Netaphous 64:8ada3e0b2048 28 {
Netaphous 64:8ada3e0b2048 29 x = currentSize;
Netaphous 64:8ada3e0b2048 30 printf("Changed print amount to currentSize");
Netaphous 64:8ada3e0b2048 31 }
Netaphous 64:8ada3e0b2048 32 int currentElement = nextSpace() - x;
Netaphous 64:8ada3e0b2048 33 if(currentElement < 0)
Netaphous 64:8ada3e0b2048 34 {
Netaphous 64:8ada3e0b2048 35 currentElement += maxSize;
Netaphous 64:8ada3e0b2048 36 }
Netaphous 64:8ada3e0b2048 37 for(int i = 0; i < x; i++)
Netaphous 64:8ada3e0b2048 38 {
Netaphous 64:8ada3e0b2048 39 char *ptr = array[currentElement].date.ToString();
Netaphous 64:8ada3e0b2048 40 printf("\n\r%i. %s T: %f | H: %f | P: %f |",i,ptr , array[currentElement].temperature, array[currentElement].humidity, array[currentElement].pressure);
Netaphous 64:8ada3e0b2048 41 currentElement++;
Netaphous 64:8ada3e0b2048 42 currentElement = currentElement % maxSize;
Netaphous 64:8ada3e0b2048 43 }
Netaphous 64:8ada3e0b2048 44 }
Netaphous 65:3723d2729b68 45 void CircularArray::readAll()
Netaphous 64:8ada3e0b2048 46 {
Netaphous 65:3723d2729b68 47 readX(currentSize);
Netaphous 64:8ada3e0b2048 48 }
Netaphous 64:8ada3e0b2048 49 void CircularArray::deleteX(int x)
Netaphous 64:8ada3e0b2048 50 {
Netaphous 64:8ada3e0b2048 51 if(x >= maxSize)
Netaphous 64:8ada3e0b2048 52 {
Netaphous 64:8ada3e0b2048 53 deleteAll();
Netaphous 64:8ada3e0b2048 54 }
Netaphous 64:8ada3e0b2048 55 else
Netaphous 64:8ada3e0b2048 56 {
Netaphous 64:8ada3e0b2048 57 firstValue += x;
Netaphous 64:8ada3e0b2048 58 currentSize -= x;
Netaphous 64:8ada3e0b2048 59 firstValue = firstValue % maxSize;
Netaphous 64:8ada3e0b2048 60 if(currentSize <= 0)
Netaphous 64:8ada3e0b2048 61 {
Netaphous 64:8ada3e0b2048 62 deleteAll();
Netaphous 64:8ada3e0b2048 63 }
Netaphous 64:8ada3e0b2048 64 }
Netaphous 64:8ada3e0b2048 65 }
Netaphous 64:8ada3e0b2048 66 void CircularArray::deleteAll()
Netaphous 64:8ada3e0b2048 67 {
Netaphous 64:8ada3e0b2048 68 currentSize = 0;
Netaphous 64:8ada3e0b2048 69 }
Netaphous 64:8ada3e0b2048 70 int CircularArray::getSize()
Netaphous 64:8ada3e0b2048 71 {
Netaphous 64:8ada3e0b2048 72 return currentSize;
Netaphous 64:8ada3e0b2048 73 }
Netaphous 64:8ada3e0b2048 74 int CircularArray::nextSpace()
Netaphous 64:8ada3e0b2048 75 {
Netaphous 64:8ada3e0b2048 76 int nextSpace = firstValue + currentSize;
Netaphous 64:8ada3e0b2048 77 nextSpace = nextSpace % maxSize;
Netaphous 64:8ada3e0b2048 78 return nextSpace;
Netaphous 64:8ada3e0b2048 79 }