class Strategy
{
public:
virtual void execute();
void save(){
EEPROM.put(source.getEepromPosition(), this);
}
Strategy get(bool pos){
return EEPROM.get(pos ? 0 : 512, Strategy);
}
protected:
ControlButton source;
};
#include <Arduino.h>
class DuplicateStrategy : public Strategy
{
public:
DuplicateStrategy(Button target, ControlButton controlButton){
this->target = target;
this->source = controlButton;
this->save();
}
void execute(){
this->source.hasCommand() ? this->target.push() : this->target.release();
}
private:
Button target;
};
#include <Arduino.h>
class MacroStrategy : public Strategy
{
public:
MacroStrategy(ControlButton source, MacroPosition positions[]){
this->source = source;
this->positions = positions;
this.save();
}
void execute(){
if(source.hasCommand()){
for(int i = 0; i < sizeof(this->positions); i++){
positions[i].execute();
}
}
}
private:
MacroPosition positions[];
}
#include <Arduino.h>
class MultiClickStrategy : public Strategy
{
public:
MultiClickStrategy(Button target, ControlButton source, unsigned int delay){
this->target = target;
this->source = source;
this->delay_ms = delay;
this.save();
}
void execute(){
if(source.hasCommand() && this.timeSpent()){
this->timer = millis();
target.isPushed() ? target.release() : target.push();
}
}
private:
unsigned long timer;
unsigned int delay_ms;
Button target;
bool timeSpent(){
return millis() > this->timer + this->delay_ms;
}
}
std::unique_ptr<Strategy> strategies[N]
. Если нет — приходится как-то извращаться, и об этом не будем.unsigned char code() const;
std::unique_ptr<Strategy>& thatObject = strategies[i];
switch (getSomeByte) {
case CODE_DUPLICATE:
thatObject = new DuplicateStrategy();
break;
case CODE_MACRO:
thatObject = new MacroStrategy();
break;
default:
// можно ничего не делать. Можно сообщить об ошибке.
}
thatObject->read();