How to use Adafruit Library in another class

I want to move some codes to another class and I am using an oled display. So when I move my code into another class, I can build without problem (I think somehow the compiler does not verify code before executing (aot).

so is there any thing that I should do? Because my arduino micro pro has problem with that. The computer does not recognise anymore unless if i do not reset.

I just add some basic codes.

my header file (OledDisplay.h)

#ifndef OledDisplay_h
#define OledDisplay_h

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Arduino.h"

class OledDisplay
{
public:
OledDisplay(uint16_t color,uint8_t size,uint16_t x, uint16_t y);
void ClearDisplay();
void SetTextColor(uint16_t color);
void SetTextSize(uint8_t size);
void SetCursor(uint16_t x, uint16_t y);
void PrintOneLine(String message,bool isDisplay);
void PrintTwoLinesMessage(String message1,int x0,int y0, String message2,int  x1,int y1, bool clearDisplay);
private:
  Adafruit_SSD1306 _display;

};

#endif

my cpp class OledDisplay.cpp

#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#include "OledDisplay.h"

OledDisplay::OledDisplay(uint16_t color,uint8_t size,uint16_t x, uint16_t y)
{
_display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
ClearDisplay();
SetTextColor(color);
SetTextSize(size);
SetCursor(x,y);
_display.display();

}

void OledDisplay::ClearDisplay()
{
  _display.clearDisplay();
 _display.setCursor(0,0);

}
void OledDisplay::SetTextColor(uint16_t color)
{
_display.setTextColor(color);
}

void OledDisplay::SetTextSize(uint8_t size)
{
  _display.setTextSize(size);

}

void OledDisplay::SetCursor(uint16_t x,uint16_t y)
{
_display.setCursor(x,y);
}

void OledDisplay::PrintOneLine(String message, bool isDisplay)
{
  _display.print(message);
  if(isDisplay)
_display.display();
}

void OledDisplay::PrintTwoLinesMessage(String message1,int x0,int y0, String message2,int x1,int y1, bool clearDisplay)
{

 if(clearDisplay)
 ClearDisplay();
SetCursor(x0,y0);
PrintOneLine(message1,false);

SetCursor(x1,y1);
PrintOneLine(message2,true);

}

both files in my library folder (I did not copy to programfiles, create a library flder in my D drive.)

and here how I am using the library

#include <OledDisplay.h>

OledDisplay _oledDisplay(WHITE,1,0,0);

void setup( )
{
_oledDisplay.PrintTwoLinesMessage("Welcome", 0,0,"bla bla bla", 0,20,true);
}

whats wrong with this code?

I think somehow the compiler does not verify code before executing

The compiler runs on the PC. It does not, of course, get involved with running the code on the Arduino.

_display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

Why do you suppose the Adafruit_SSD1306 needs a begin() method? Do you suppose that it is because the hardware isn't ready when the Adafruit_SSD1306 constructor is called? So, why do you think you can call it from your constructor?

Clearly, you can't, so your class needs a begin() method, and the Adafruit_SSD1306::begin() method needs to be called from you begin() method, not from your constructor. And, of course, you need to call your begin() method at the appropriate time.

PaulS:
The compiler runs on the PC. It does not, of course, get involved with running the code on the Arduino.

_display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

Why do you suppose the Adafruit_SSD1306 needs a begin() method? Do you suppose that it is because the hardware isn't ready when the Adafruit_SSD1306 constructor is called? So, why do you think you can call it from your constructor?

Clearly, you can't, so your class needs a begin() method, and the Adafruit_SSD1306::begin() method needs to be called from you begin() method, not from your constructor. And, of course, you need to call your begin() method at the appropriate time.

I see that its not ahead of time because it will need a compiler. I thought about it i my dream :slight_smile: Well the reason why I tought like this is; when I change the method name of adafruit lib, it still can build. For example

instead of _display.setCursor(0,0); , I wrote _display.setCursorrrr(0,0); and still it compiled...

For the constructor thing, I got the idea. I create an Init method and moved my code there and it works :slight_smile: Thank you very much

I create an Init method and moved my code there and it works

The convention is to call the method begin().