Hello community,
I'm currently messing with some c++ development. My c++ experience equals nearly to zero but I have done a lot of PHP and Java coding before. What I'm trying to acheive is creating a propper object oriented API for a project I'm currently working on. Basically where i got stuck is the implementation of something what could be called a state pattern.
Now the following code shows how I've implemented the inheritance from the base state. It took me quite a while how to do such a thing in c++:
sketch_looper.pde
#include "Looper.h"
Looper *lp;
void setup()
{
[glow]// delay(100);[/glow]
pinMode(2, OUTPUT);
// signal that we are in setup
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
delay(50);
digitalWrite(9, LOW);
delay(50);
digitalWrite(9, HIGH);
delay(50);
digitalWrite(9, LOW);
delay(50);
lp = &Looper();
}
void loop()
{
lp->update();
}
Looper.h
#ifndef Looper_h
#define Looper_h
#include "LooperBaseState.h"
class Looper
{
public:
Looper();
void update();
private:
void setCurrentState(LooperBaseState *s);
LooperBaseState *_currState;
[glow]// int foo;[/glow]
};
#endif
Looper.cpp
#include "Looper.h"
#include "LooperBaseState.h"
#include "StompBoxState.h"
Looper::Looper()
{
setCurrentState(&StompBoxState());
}
void Looper::update()
{
_currState->update();
}
void Looper::setCurrentState(LooperBaseState *s) {
_currState = s;
}
LooperBaseState.h
#ifndef LooperBaseState_h
#define LooperBaseState_h
class LooperBaseState
{
public:
LooperBaseState();
virtual void update() = 0;
};
#endif
LooperBaseState.cpp
#include "LooperBaseState.h"
LooperBaseState::LooperBaseState()
{
}
// this must be implemented or compilation of virtual
// functions(abstract classes) will fail ?!?!?!
extern "C" void __cxa_pure_virtual()
{
while (1);
}
StompBoxState.h
#ifndef StompBoxState_h
#define StompBoxState_h
#include "LooperBaseState.h"
class StompBoxState : public LooperBaseState
{
public:
StompBoxState();
void update();
};
#endif
StompBoxState.cpp
#include "WProgram.h"
#include "StompBoxState.h"
StompBoxState::StompBoxState()
{
}
void StompBoxState::update()
{
//Serial.println("eee");
digitalWrite(2, HIGH);
delay(250);
digitalWrite(2, LOW);
delay(250);
}
The code does the following: it just flashes a LED connected to digtal pin 9 when entering setup. then it just goes in endless loop and flashes a LED connected to digital pin 2 endlessly. This seems to work fine as long as i don't add more lines of code.
Commenting in one of the yellow highlighted lines of code results in re-entering setup (indicated by flashing of LED connected to digital pin 9) and bootstrapping the whole thing again.
No clue whats going on here! I'm guessing it has something to do with my abstract base class / virtual functions / pointers. When i don't inherit from the LooperBaseState, just implementing the state directly, everything seems to work rock solid...
I'm using an Arduino Dicimila
So it would be great if one could give me some help. I've already spent ages on this problem and have no clue what to try next...
Thanks in advance,
Holger