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();
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
sevseg1:COM.1
sevseg1:COM.2
sevseg1:A
sevseg1:B
sevseg1:C
sevseg1:D
sevseg1:E
sevseg1:F
sevseg1:G
sevseg1:DP
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
r5:1
r5:2
r6:1
r6:2
r7:1
r7:2