I'm working on an Arduino project that makes use of a simple 128x32 OLED I2C display. After creating a new instance of this display and some custom functions in my sketch that draw to the display, I'd like to move this code out of my sketch and into their own oled.h & oled.cpp files for no other reason other then to clean up my sketch file and compartmentalize my code\logic.
I'm having some problems making this happen, which I feel should be possible. But I'm not familiar enough with C/C++ programming so I'm probably just making a dumb mistake here.
Here is what works:
NOTE: This example is a condensed version of my project so far. I'm posting the simplest version I can just for support proposes. But this still demonstrates my issue pretty well.
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
Adafruit_SSD1306 display(128, 32, &Wire, 4);
void setup() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Draw the UI
drawName("Hello", false, 10, 16, 2);
display.display(); // Update screen
}
void loop() {
}
void drawName(const char *name, bool clearDisp, int x, int y, int textSize) {
if (clearDisp) {
display.clearDisplay();
}
display.setTextSize(textSize);
display.setTextColor(WHITE);
display.setCursor(x, y);
display.println(F(name));
}
Now I want to move all display related code into its own library and this is what I tried:
my sketch file:
#include "Oled.h"
void setup() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Draw the UI
drawName("Hello", false, 10, 16, 2);
display.display(); // Update screen
}
void loop() {
}
Oled.h:
#ifndef Oled_h
#define Oled_h
#include "Arduino.h"
#include <Wire.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 32, &Wire, 4);
void drawName(const char *name, bool clearDisp, int x, int y, int textSize);
#endif
Oled.cpp:
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include "Oled.h"
void drawName(const char *name, bool clearDisp, int x, int y, int textSize) {
if (clearDisp) {
display.clearDisplay();
}
display.setTextSize(textSize);
display.setTextColor(WHITE);
display.setCursor(x, y);
display.println(F(name));
//display.display();
}
And the errors:
sketch\oled_test.ino.cpp.o:(.bss.display+0x0): multiple definition of `display'
sketch\Oled.cpp.o:(.bss.display+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board ESP32 Dev Module.
I've also tried declaring the display instance in the .h and then creating it in the cpp only to get other errors.
Oled.h: Adafruit_SSD1306 display;
Oled.cpp: Adafruit_SSD1306 display(128, 32, &Wire, 4);
Again, I'm assuming that I'm making some rookie mistake here and just doing this wrong. Can anybody point me into the right direction here?
Thanks,
Jason