Please be aware of all shipping deadlines before placing your order - we cannot guarantee orders will arrive before Christmas!

SSD1306 Library (simple code question)
Moderators: adafruit_support_bill, adafruit

SSD1306 Library (simple code question)

by flxad41 on Sun Mar 17, 2013 5:33 pm

Hi, ive been playing around with my new OLED 128x64 display and i've followed the awesome tutorial and everything works great! (thank you guys for awesome instructions).
now im just reading the Adafruit_SSD1306 and Adafruit_GFX libraries to see how they work.

can someone tell me how this line works?
this is from ssd1306_128x64_spi.ino

Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

i can see the Adafruit_SSD1306 constructor in Adafruit_SSD1306.h as

Adafruit_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS);

which matches the call from the arduino sketch, however im confused by the display call as display is just defined as
void Adafruit_SSD1306::display(void)

im just trying to understand the code, i hope my question is not too stupid.

thanks.
flxad41
 
Posts: 5
Joined: Sun Mar 17, 2013 5:24 pm

Re: SSD1306 Library (simple code question)

by adafruit_support_rick on Sun Mar 17, 2013 6:07 pm

flxad41 wrote:Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

That line declares an instance of the Adafruit_SSD1306 class named 'display'.

flxad41 wrote:void Adafruit_SSD1306::display(void)

That line defines a member function named 'display' of the Adafruit_SSD1306 class

In the sketch, you could call display.display(), which would call the 'display' method of the class instance 'display'. It is kind of confusing, isn't it?

adafruit_support_rick
 
Posts: 14541
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: SSD1306 Library (simple code question)

by flxad41 on Sun Mar 17, 2013 6:37 pm

thanks, that makes sense, its not that confusing if you know whats going on :P

i guess i would have done it like this:

Adafruit_SSD1306* display;
display = new Adafruit_SSD1306(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

is that just a matter of style? or are the two ways doing different things in the background?
flxad41
 
Posts: 5
Joined: Sun Mar 17, 2013 5:24 pm

Re: SSD1306 Library (simple code question)

by adafruit_support_rick on Sun Mar 17, 2013 9:07 pm

Well, what you have creates a pointer to a class object. You would have to dereference the pointer every time you access the class. For example,
Code: Select all | TOGGLE FULL SIZE
display->display();


But it doesn't really clear up the confusion. What would make more sense would be to give the class object a different name:
Code: Select all | TOGGLE FULL SIZE
Adafruit_SSD1306* oled;

setup()
{
... blah blah blah ...
  oled.display();
}

adafruit_support_rick
 
Posts: 14541
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY