Hi there,
I am currently working on an Infinity Mirror Clock project.
I want to refactor my code and implement a State Pattern for which I need polymorphism.
This is my interface class for the states:
#ifndef state_h
#define state_h
#include "InternalState.h"
class stateInterface {
public:
virtual void midButtonPress(InternalState& anIS) = 0;
virtual void loopAction(InternalState& anIS) = 0;
};
#endif
---------------------------------------------------------------
This is my first concrete state:
#ifndef displayTime_state_h
#define displayTime_state_h
#include "stateInterface.h"
#include "InternalState.h"
class displayTime_state : public stateInterface {
public:
void midButtonPress(InternalState& anIS) {
.... concrete Implementation ....
}
void loopAction(InternalState& anIS) {
.... concrete Implementation ....
}
};
#endif
---------------------------------------------------------------
And in the running class I have:
[...]
#include "InternalState.h"
#include "stateInterface.h"
#include "displayTime_state.h"
class Clock {
private:
InternalState _iS;
stateInterface *externalState;
public:
[...]
void setup(){
[...]
externalState = new displayTime_state();
}
void ISR_MidButton() {
[...]
externalState->midButtonPress(_iS);
}
I have noticed the following:
After the upload on the Teensy it takes ≈ 30 seconds until my program starts running.
When I enter the ISR_MidButton() method, my clock stops working and freezes. (That means externalState->midButtonPress(_iS); is not getting called properly)
Everything works fine, as soon as I remove the inheritance public stateInterface) and change the type of *externalState to displayTime_state.
Unfortunately I need the inheritance for what I want to implement.
Do you have any ideas what I might be doing wrong?
Greetings, Divi
I am currently working on an Infinity Mirror Clock project.
I want to refactor my code and implement a State Pattern for which I need polymorphism.
This is my interface class for the states:
#ifndef state_h
#define state_h
#include "InternalState.h"
class stateInterface {
public:
virtual void midButtonPress(InternalState& anIS) = 0;
virtual void loopAction(InternalState& anIS) = 0;
};
#endif
---------------------------------------------------------------
This is my first concrete state:
#ifndef displayTime_state_h
#define displayTime_state_h
#include "stateInterface.h"
#include "InternalState.h"
class displayTime_state : public stateInterface {
public:
void midButtonPress(InternalState& anIS) {
.... concrete Implementation ....
}
void loopAction(InternalState& anIS) {
.... concrete Implementation ....
}
};
#endif
---------------------------------------------------------------
And in the running class I have:
[...]
#include "InternalState.h"
#include "stateInterface.h"
#include "displayTime_state.h"
class Clock {
private:
InternalState _iS;
stateInterface *externalState;
public:
[...]
void setup(){
[...]
externalState = new displayTime_state();
}
void ISR_MidButton() {
[...]
externalState->midButtonPress(_iS);
}
I have noticed the following:
After the upload on the Teensy it takes ≈ 30 seconds until my program starts running.
When I enter the ISR_MidButton() method, my clock stops working and freezes. (That means externalState->midButtonPress(_iS); is not getting called properly)
Everything works fine, as soon as I remove the inheritance public stateInterface) and change the type of *externalState to displayTime_state.
Unfortunately I need the inheritance for what I want to implement.
Do you have any ideas what I might be doing wrong?
Greetings, Divi