class SevenIndicator
{
public:
SevenIndicator(int initValue, int *pins)
{
_curValue = initValue;
for (int i = 0; i < _kSegmentsCount; ++i)
{
pinMode(pins[i], OUTPUT);
digitalWrite(pins[i], HIGH);
}
_pins = pins;
showValue();
}
~SevenIndicator() = default;
void stepUp()
{
++_curValue;
if (_curValue >= _kMaxValue)
{
_curValue = 0;
}
showValue();
}
private:
int _curValue;
int *_pins;
const int _kSegmentsCount = 7;
static const int _kMaxValue = 16;
uint8_t _segmentsCoding[_kMaxValue] =
{
0b11000000, //0
0b11111001, //1
0b10100100, //2
0b10110000, //3
0b10011001, //4
0b10010010, //5
0b10000010, //6
0b11111000, //7
0b10000000, //8
0b10010000, //9
0b10001000, //A
0b10000011, //b
0b11000110, //C
0b10100001, //d
0b10000110, //E
0b10001110, //F
};
showValue()
{
for(int i = 0; i < _kSegmentsCount; ++i)
{
int enable = bitRead(_segmentsCoding[_curValue], i);
digitalWrite(_pins[i], enable);
}
}
};
template<typename T>
class Button
{
public:
Button(T* indicator, void (T::*action)(void), int pinB)
{
_indicator = indicator;
_action =action;
_lastButton = LOW;
_curButton = LOW;
_pinButton = pinB;
pinMode(pinB, INPUT_PULLUP);
}
~Button() = default;
void process()
{
_curButton = debounce();
if (_lastButton == HIGH && _curButton == LOW)
{
(_indicator->*_action)();
}
_lastButton = _curButton;
}
private:
T* _indicator;
void (T::*_action)(void);
int _lastButton;
int _curButton;
int _pinButton;
int debounce ()
{
int current = digitalRead(_pinButton);
if(_lastButton != current)
{
delay(5);
current = digitalRead(_pinButton);
}
return current;
}
};
int pins[7] = {0,1,2,3,4,5,6};
SevenIndicator indicator(0, pins);
Button<SevenIndicator> button(&indicator, &SevenIndicator::stepUp, 7);
void setup()
{
}
void loop()
{
button.process();
}