#define visibility

Hello,

I've build a library for 7 segments displays. This library has a table computed at compilation time. If a COMMON_CATHODE is defined, the table's bits are inverted.

With COMMON_CATHODE defined in the header of the library, it works. Is there a way to define this flag in the .ino (so no need to modify the library if I switch to another display type)?

Regards,

Pfeuh

I don't think so. But you could add a flag to the compiler to define it. However, it would just be the same amount of work.

No, unfortunately.

BUT, the compiler (linker actually) is smart! If you just have both tables in your .h and you make don't use one of the two, the linker will just leave it out :slight_smile:

OK, I understand. Thanks a lot.

Pfeuh

You can do it by moving all the code that is affected by COMMON_CATHODE to the library’s .h file and then placing the #define directive for COMMON_CATHODE before the #include directive for the .h file:

#define COMMON_CATHODE
#include <PfeuhSevSeg.h>

pfeuh:
has a table computed at compilation time. If a COMMON_CATHODE is defined, the table's bits are inverted.

It may be more compact and less complex - if you simply invert the segment bits at runtime.

segmentBits = !segmentBits ;

Unless the pins associations are different, but then may simply be a choice of pin arrays with different pin/bit patterns.

But the define-before-include only works for the included .h. Not for any separate compiled .cpp which probably contains the rest of the library.

#include <PfeuhSevSeg.h>

I’ve done that, but I’ve got an error undefined COMMON_CATHODE. Perhaps because the font is defined privately in the .cpp, as there is no need to increase size of .h

But the define-before-include only works for the included .h. Not for any separate compiled .cpp which probably contains the rest of the library.

Yes, it is like that.

It may be more compact and less complex - if you simply invert the segment bits at runtime.
segmentBits = !segmentBits ;

Of corse, you’re right, I will use it. Some time as passed and I’ve changed my mind. I will use a class with these input parameters:

  • segments logic (0-1)
  • digits logics (0-1)
  • number of digits

I would like to separate io and software to make the lib more generic. In my case, for instance, I use 2 shifters (74HC595) to get 4 digits on 3 io pins. I would like to provide a buffer of 1 byte per digit, but at the moment I don’t know how to allocate that at compilation time. Let’s googling something.

Thanks for your time.

Pfeuh

I would say by defining 4 objects. Object per digit. Or have an external buffer in code or something.

I've selectionned the external buffer solution. I do like this actually, in the main (.ino):

#define NB_DIGITS 4
byte displayBuf[NB_DIGITS];

then I call the class constructor:

DISPLAY_7_SEG display = DISPLAY_7_SEG(0, 1, NB_DIGITS, displayBuf);

Here is some lib's code:

DISPLAY_7_SEG::DISPLAY_7_SEG(byte nb_digits, bool segment_logic, bool digit_logic, byte* _digits)
{
   segmentLogic = nb_digits;
   digitLogic = segment_logic;
   nbDigits = digit_logic;
   digits = _digits;
}

class DISPLAY_7_SEG
{
    public:
        DISPLAY_7_SEG(byte nb_digits, bool segment_logic, bool digit_logic, byte* _digits);

    private:
        bool segmentLogic;
        bool digitLogic;
        byte nbDigits;
        byte* digits;
};

It's heavy, it's a recurrent question, but I haven't found any better solution.

Look good to me. Although I would suggest:

const byte NbDigits = 4
byte displayBuf[NbDigits];

To use the C++ alternative of a macro :slight_smile:

I've only experienced C++ with Arduino, so I am an absolute beginner. Concerning:

const byte NbDigits = 4

This declaration consumes one byte of program memory, contrary to #define, doesn't it?

It has to store that define in your program somewhere. It can not leave that number out and make sense of your code. So that also takes a byte of your program memory :wink: So they consume the same amount of memory :slight_smile: