/*
Reference in a class to another instance
https://forum.arduino.cc/t/common-namespace-in-multiple-translation-units/1225443/4
2024-02-21 by noiasca
source not in thread
keep as example
*/
#include <LiquidCrystal_I2C.h> // if you don´t have I2C version of the display, use LiquidCrystal.h library instead
LiquidCrystal_I2C mydisplay(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
// another class which takes the reference to an existing object
class Indicator {
private:
uint32_t previousMillis; // last time shown
bool active = false; // indicator is active
bool state = false; // shown/not shown
LiquidCrystal_I2C &lcd; // the other instance
const uint8_t character; // the character to be displayed
const uint8_t col; // on which column
const uint8_t row; // on which row
const uint16_t interval; // how long visible (after that time, the indicator will disapear)
public:
Indicator(LiquidCrystal_I2C &lcd, uint8_t character, uint8_t col, uint8_t row, uint16_t interval = 512) :
lcd(lcd), character(character), col(col), row(row), interval(interval) {}
void on() {
if (active == false) {
lcd.setCursor(col, row);
lcd.write(character);
previousMillis = millis();
}
active = true;
}
void off() {
if (active == true) {
lcd.setCursor(col, row);
lcd.write(' ');
}
active = false;
}
void update() { // to be called in loop()
if (active == true && interval > 0) {
if (millis() - previousMillis > interval) {
previousMillis = millis();
lcd.setCursor(col, row);
if (state) lcd.write(character); else lcd.write(' ');
state = !state;
}
}
}
};
Indicator indicatorActivity(mydisplay, '*', 15, 0);
void setup() {
mydisplay.init(); // initialize the 16x2 lcd module
mydisplay.backlight(); // enable backlight for the LCD module
indicatorActivity.on();
}
void loop() {
indicatorActivity.update();
}