C/C++ Question Understand Wrapper- Adafruit_SSD1306.h

Tomtom2

New Member
Licensed User
Hallo,
I try to understand in wich way the Library for Adafruit_SSD1306.h was create. There are some statemates that I do not understand. I hope some can explain this.

in rAdafruit_SSD1306.h there will be the definitions. Why i need both definitions of "B4RAdafruitGFX"

B4X:
        private:
           ...
            Adafruit_SSD1306* ssd;
            B4RAdafruitGFX gfx;
        public:
            B4RAdafruitGFX* GFX;

in rAdafruit_SSD1306.cpp is the function definition of init(): The "gfx" Object have no gfx method, what the meanning of.
And why I have to assign the ssd Object to gfx. In the Arduino code the constructor of "Adafruit_SSD1306" get a gfx Object

B4X:
    void B4RAdafruitSSD1306::init() {
        gfx.gfx = ssd;
        GFX = &gfx;
        ssd->setTextColor(WHITE);
}
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why i need both definitions of "B4RAdafruitGFX"
A pointer points to an object. The object allocated memory must be kept somewhere or bad things will happen.
In this case the object (B4RAdafruitGFX instance) memory is inside SSD1306 object.
B4R works with pointer to objects so there is another pointer field.

in rAdafruit_SSD1306.cpp is the function definition of init(): The "gfx" Object have no gfx method, what the meanning of.
And why I have to assign the ssd Object to gfx. In the Arduino code the constructor of "Adafruit_SSD1306" get a gfx Object
The answer starts in the wrapped library code:
B4X:
class Adafruit_SSD1306 : public Adafruit_GFX
As you can see SSD is also a GFX object.

rAdafruitGFX does have a gfx field:
B4X:
    class B4RAdafruitGFX {
        public:
            //~hide
            Adafruit_GFX* gfx;
All the drawing methods use this field.
 
Top