Here is a version where LEDOutputPeripheral is a derived class of OutputPeripherals. That way the OutputPeripherals class doesn't need a list of LED pins and different LEDOutputPeripheral can have different lists of pins.
class OutputPeripherals
{
public:
virtual void init() {}; // !!! správně by tady mělo být vkládání pole(_leds[8] ale pro zjednodušení jsem ho vynechal
virtual void number(byte n) = 0;
};
class InputPeripherals
{
public:
InputPeripherals();
void init(int buttonAdress, OutputPeripherals &periph);
void updateClass();
private:
int _pin;
OutputPeripherals *_periph;
};
class LEDOutputPeripheral : public OutputPeripherals
{
public:
LEDOutputPeripheral() : _leds {3, 4, 5, 6, 7, 8, 9, 10} {}
void init(); // !!! správně by tady mělo být vkládání pole(_leds[8] ale pro zjednodušení jsem ho vynechal
void number(byte n);
private:
int _leds[8];
};
InputPeripherals buttonA;
LEDOutputPeripheral leds;
void setup()
{
delay(20);
Serial.begin(115200);
delay(20);
Serial.println("\nTransfer started \n\n");
leds.init();
leds.number(5);
buttonA.init(12, leds);
}
void loop()
{
buttonA.updateClass();
delay(500);
leds.number(8);
delay(500);
Serial.print("end loop\n\n");
delay(10000);
}
InputPeripherals::InputPeripherals() {}
void InputPeripherals::init(int buttonAdress, OutputPeripherals &periph)
{
Serial.println("Output set INPUT_PULLUP");
pinMode(buttonAdress, INPUT_PULLUP);
_pin = buttonAdress;
_periph = &periph;
}
void InputPeripherals::updateClass()
{
Serial.print("Button value is: ");
Serial.println(digitalRead(_pin));
if (digitalRead(_pin) == 0)
{
Serial.println("calling 3");
_periph->number(3);
}
else
{
Serial.println("calling 1");
_periph->number(1);
}
}
void LEDOutputPeripheral::init()
{
for (int i = 0; i < 8; i++)
{
pinMode(_leds[i], OUTPUT);
}
}
void LEDOutputPeripheral::number(byte number)
{
Serial.print("Incomming number: ");
Serial.println(number);
for (int i = 0; i < 8; i++)
{
digitalWrite(_leds[i], number & (0x01 << i));
}
}