3rd Repo, trying to figure this out.

Dependencies:   LPS25H hts221

Fork of SOFT253_Template_Weather_OS_54 by Stage-1 Students SoCEM

CircularArray/CircularArray.cpp

Committer:
aburch1
Date:
2017-04-24
Revision:
74:749727490f44
Parent:
72:ef4a4e3089c1
Child:
79:4e6b53eb678b

File content as of revision 74:749727490f44:

#include "CircularArray.h"
#include <stdio.h>
#include <ctype.h>
/*
    Constructor
*/
CircularArray::CircularArray(int limit)
{
    maxSize = limit;
    array = new Measure[maxSize];
    firstValue = 0;
    currentSize = 0;
}
void CircularArray::pushValue(Measure _measure)
{
    if(currentSize >= maxSize)
    {
        firstValue++;
        currentSize--;
    }
    int next = nextSpace();
    array[next] = _measure;
    currentSize++;
}
void CircularArray::readX(int x)
{
    if(x > currentSize)
    {
        x = currentSize;
        printf("Changed print amount to currentSize");
    }
    int currentElement = nextSpace() - x;
    if(currentElement < 0)
    {
        currentElement += maxSize;
    }
    for(int i = 0; i < x; i++)
    {
        char *ptr = array[currentElement].date.ToString();
        printf("\n\r%i. %s T: %f | H: %f | P: %f |",i + 1,ptr , array[currentElement].temperature, array[currentElement].humidity, array[currentElement].pressure);
        currentElement++;
        currentElement = currentElement % maxSize; 
    }
}
void CircularArray::readAll()
{
    readX(currentSize);
}
void CircularArray::deleteX(int x)
{
    if(x >= maxSize)
    {
        deleteAll();
    }
    else
    {
        firstValue += x;
        currentSize -= x;
        firstValue = firstValue % maxSize;
    }
}
void CircularArray::deleteAll()
{
    currentSize = 0;
}
int CircularArray::getSize()
{
    return currentSize;
}
int CircularArray::nextSpace()
{
    int nextSpace = firstValue + currentSize;
    nextSpace = nextSpace % maxSize; 
    return nextSpace;   
}