I have an LED display module, well supported by a library. There are 8 LEDs on the module that I use as indicators for various parts of my program. However, it has a function setLEDs() which does not allow me to set/reset an LED without affecting the others. So I have written some functions to do that.
However, I'm not satisfied with my solution, but it does work now. I have instantiated a module object, imaginatively called "module". I then wrote three wrapper functions to work the LEDs. I have two main issues. One is practical, the other more concerning lack of elegance, I guess. The practical problem, is that I want to add a second module, and the code I have won't support that (because it has hard coded the module). The library and hardware already supports multiple display devices. I would hate to have three more functions that only differ in which module they call. The other issue is that I would prefer to leave the 20th century, and finally begin to respect OO more.
Here is my existing, working code:
#include <TM1638.h>
TM1638 module(TM1638dataPin, TM1638clockPin, TM1638strobePin, true, TM1638intensity);
//... my code here
const byte utcLED = 0b00000011;
const byte blinkLED = 0b10000000;
LEDon(blinkLED);
LEDtoggle(utcLED);
//... LED wrapper functions
//
byte LEDstatus = 0;
void LEDon( byte data)
{
module.setLEDs(LEDstatus |= data);
}
void LEDoff( byte data)
{
module.setLEDs(LEDstatus &= ~data);
}
void LEDtoggle( byte data)
{
module.setLEDs(LEDstatus ^= data);
}
What I would really like, is to have something like the following, but I'm not sure of the approach to take. I have looked at some C++ tutorials and I did learn some a few years ago, but it's kind of locked up in my mind somewhere.
// now there are two display modules...
#include <TM1638.h>
TM1638 module(TM1638dataPin, TM1638clockPin, TM1638strobePin, true, TM1638intensity);
TM1638 module_B(TM1638dataPin_B, TM1638clockPin_B, TM1638strobePin_B, true, TM1638intensity);
//...in my code:
const byte utcLED = 0b00000011;
const byte blinkLED = 0b10000000;
module.LEDon(blinkLED);
module_B.LEDtoggle(utcLED);
//...
//etc. etc.
// mystery code to implement this goes somewhere
Is it possible?
Also, I don't want to change the library at all.