First of all i am sorry for alot of code here but i think it is needed to understand whats happening
I am trying to store some animation objects inside of a other Object called Cube.
Therefore i implemented a double linked list like this:
#pragma once
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
/**
* Take care this list is for a maximum of 255!
* Else it's counter isnt valid!
*/
template <typename T>
class LinkedList
{
protected:
private:
struct Node
{
Node* prev;
Node* next;
T value;
};
Node* last;
Node* first;
byte count;
public:
LinkedList()
{
count = -1; //empty
};
~LinkedList()
{
if (count > -1){
clear();
}
};
/** adds to list*/
inline void add(T t);
/**removes the thing at index*/
inline T remove(int index);
/** Returns NULL(ptr) if index is out of range or item not found somehow*/
inline T get(int index);
inline void clear();
/**Returns the first obj*/
inline T getFirst();
/**Returns the last obj*/
inline T getLast();
/**Returns the current size. If -1 its empty!*/
inline int size(){
return count;
};
T operator[](const int i)
{
return get(i);
};
};
template <typename T>
inline void LinkedList<T>::add(T t){
Node* n = new Node();
n->value = t;
if (count > -1)
{
n->next = first;
n->prev = last;
last->next = n;
last = n;
count++;
}
else if (count == -1)//first element
{
first = n;
first->next = n;
first->prev = n;
last = n;
last->next = n;
last->prev = n;
count++;
}
}
template <typename T>
inline T LinkedList<T>::remove(int index){
if (index <= count)
{
Node* n = last;
for (int i = 0; i <= index; i++)
{
n = n->next;
}
n->prev->next = n->next;
n->next->prev = n->prev;
count--;
return n->value; //return the value of that node
}
}
template <typename T>
inline T LinkedList<T>::get(int index){
if (index <= count && index > -1)
{
Node* n = first;
int i = 0;
while (i < index)
{
n = n->next;
i++;
}
return n->value;
}
return NULL;
}
template <typename T>
inline void LinkedList<T>::clear()
{
Node* n = first;
while (count > 0)
{
Node* toBeDeleted = n;
n = n->next;
delete toBeDeleted;
count--;
}
}
/**Returns the first obj*/
template <typename T>
inline T LinkedList<T>::getFirst()
{
return first->value;
};
/**Returns the last obj*/
template <typename T>
inline T LinkedList<T>::getLast()
{
return last->value;
};
The list works fine to me. Just tested it with some simple c++ desktop applications.
Inside of my cube object i store a list of Animations is a pure virtual / abstract class like this:
class Animation
{
public:
/** update with millis instead of float */
virtual void update(short delta) = 0;
};
So the cube is defined like this:
class CubeLib
{
protected:
LinkedList<Animation*> animations;
byte current_Animation;
public:
//some more methods here to change things of the cube
inline void addAnimation(Animation* a){
this->animations.add(a);
};
inline void update(short delta);//update with millis
};
inline void CubeLib::update(short delta)
{
if (animations.size() != -1){ //if its not empty
animations[current_Animation]->update(delta);
} //else do nothing
}
So inside of the Setup i add a Animation to the Cube
CubeLib cube;
void setup()
{
cube.addAnimation(new Sinus(&cube));
};
The SinusClass:
#include "Animation.h"
#include "CubeLib.h"
class Sinus : public Animation
{
private:
RGB color;
CubeLib* cube;
byte colorcounter;
float time;
public:
Sinus(CubeLib* c) : time(0.0), colorcounter(0), cube(c){
color.r = MAX_COLOR;
color.g = MAX_COLOR;
color.b = MAX_COLOR;
};
~Sinus(){};
void update(short delta); //does some math and set some things of the cube
};
And the Loop does call the update of the cube:
void loop()
{
deltaStart = millis();
cube.update(delta);//update the cube and animation
delta = (millis() - deltaStart);
}
THe arduino ceeps resetting at the point of calling the update of the animation from the list. (Found out by adding serials to it. In front it does afterwards does not.(animations[current_Animation]->update(delta)
What have i done wrong? Cant i use pure virtuals? I tried using a virtual which causes the same issue.