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-05-11
Revision:
81:996c0a3319b4
Parent:
80:959151952153
Child:
82:668b51a39148

File content as of revision 81:996c0a3319b4:

#include "CircularArray.h"
#include <stdio.h>
#include <ctype.h>
/*
    Constructor
*/
CircularArray::CircularArray(int limit, MessageLogger *newLogger)
{
    maxSize = limit;
    array = new Measure[maxSize];
    firstValue = 0;
    currentSize = 0;
    logger = newLogger;
}        
void CircularArray::pushValue(Measure _measure)
{
    if(currentSize >= maxSize)
    {
        firstValue++;
        currentSize--;
    }
    int next = nextSpace();
    array[next] = _measure;
    currentSize++;
}

Measure CircularArray::pullValue()
{
    return array[firstValue];
}
char temp2[256];
void CircularArray::readX(int x)
{
    if(x > currentSize)
    {
        x = currentSize;
        logger->SendMessage("Changed print amount to currentSize\n\r");
    }
    int currentElement = nextSpace() - x;
    if(currentElement < 0)
    {
        currentElement += maxSize;
    }
    
    char* ptr;
    for(int i = 0; i < x; 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);
        logger->SendMessage(temp2);
        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;   
}