The MGC3130 is the world’s first electrical-field (E-field) based three-dimensional (3D) tracking and gesture controller

Dependencies:   BufferedArray

Dependents:   NucleoMGC3130 i2c_master

GestILibrarMessage/BufferedArray.cpp

Committer:
yangcq88517
Date:
2015-10-13
Revision:
6:b511421e7dc8
Child:
7:eacd776fce29

File content as of revision 6:b511421e7dc8:

#include "BufferedArray.h"

BufferedArray::BufferedArray()
{
    max = EXPANDSIZE;
    data = new char[EXPANDSIZE];
    index = 0;
}

BufferedArray::BufferedArray(BufferedArray * bufferedArray)
{
    this->data = bufferedArray->data;
    this->index = bufferedArray->index;
}

BufferedArray::~BufferedArray()
{
    if (data == NULL)
        return;

    delete[] data;
}

char * BufferedArray::gets()
{
    return data;
}

char * BufferedArray::gets(int position)
{
    return data + position;
}

char BufferedArray::get(int position)
{
    return *(data + position);
}

int BufferedArray::getPosition()
{
    return index;
}

void BufferedArray::setPosition(int position)
{
    if (this->index > max)
        this->index = max;
    else this->index = position;
}

void BufferedArray::allocate(int length)
{
    if (length <= 0)
        return;

    if (length > max) {
        delete[] data;
        data = new char[length];
    }

    rewind();
}

void BufferedArray::rewind()
{
    index = 0;
}

void BufferedArray::expandSpace(int length)
{
    max += EXPANDSIZE * (1 + length / EXPANDSIZE);
    char * temp = new char[max];
    memcpy(temp, data, index);
    delete[] data;
    data = temp;
}

void BufferedArray::set(int position, char value)
{
    if (position < 0)
        return;

    if (position >= max)
        expandSpace(1);

    data[position] = value;
}

void BufferedArray::set(char value)
{
    set(index, value);
    index++;
}

void BufferedArray::sets(char * value, int offset, int length)
{
    if (length <= 0)
        return;
        
    if (offset < 0)
        return;

    sets(index, value, offset, length);
    index += length;
}

void BufferedArray::sets(int position, char * value, int offset, int length)
{
    if (position < 0)
        return;

    if (length <= 0)
        return;

    if (offset < 0)
        return;

    if (position + length - offset > max)
        expandSpace(position + length - offset - max);

    memcpy(data + position, value + offset, length);
}