/*
Digital Ports as Objects
2023-05-19
*/
class Output {
protected:
const uint8_t pin; // the GPIO used for output
const int8_t active; // HIGH active (default), or LOW active
public:
Output (uint8_t pin, int8_t active = HIGH) : pin(pin), active(active) {}
void begin() { // initialize pin in setup()
pinMode(pin, OUTPUT);
this->off(); // optional
}
void on() { // switch pin on
digitalWrite(pin, active);
}
void off() { // switch pin off
digitalWrite(pin, active == HIGH ? LOW : HIGH);
}
};
Output led(13); // active HIGH (default)
Output relay(7, LOW); // active LOW (inverse)
void setup() {
led.begin();
relay.begin();
}
void loop() {
led.on();
relay.on();
delay(500);
led.off();
relay.off();
delay(500);
}