Drawing to u8g2 from my own UI class

mrfry

Member
Hi everyone and thank you for taking the time to read this.

I am working on a diy music machine which has an oled display that I am driving using the u8g2 library.

I have started writing a class for the UI and I am trying to use u8g2 methods straight from it to write to the display but I am getting an error when compiling:
Code:
/home/mrfry/Arduino/libraries/ui/UI.cpp:6:55: error: no matching function for call to 'U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI::U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI()'

I am going insane because I got it working yesterday, I'm pretty sure I remember using a "u8g2_t" type somewhere instead of "U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI" but today I did too many undos then erased a line and now my redo options are gone and I can't figure out for the life of me what trickery I'd used.

Here is what my basic knowledge of cpp made me write:

Code:
// groovebox_ui.ino

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <UI.h>

U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI u8g2(U8G2_R0, 10, 9, 8); // Instantiate u8g2
UI UI(u8g2); // Instantiate my lib passing the u8g2 object as an argument

// [a mess of other stuff waiting to be "librarised" as well]

void setup(void) {
}

void loop(void) {
    UI.drawScreen();

    // [redacted stuff]
}

Code:
//ui.h

#ifndef LIBRARIES_UI_UI_H_
#define LIBRARIES_UI_UI_H_
#include "Arduino.h"
#include <U8g2lib.h>

class UI {
    public:
        U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI _display; // Create a variable of the same type as my display to hold my display
        // [some more member variables and methods]
        UI(U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI display); // Declare my constructor

};

#endif /* LIBRARIES_UI_UI_H_ */

Code:
#include <UI.cpp>

UI::UI(U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI display) { // Define my constructor
    _display = display;
    _display.begin();
   // [other constructor stuff]
}

void UI::drawScreen() {
    _display.clearBuffer();
    // [drawing methods]
    _display.sendBuffer();
}

I have been ripping my hair out for hours. I found this post on the devil's website but it didn't work for me (I did adapt it to my case, like changing u8x8 to u8g2 and changing the class name and the constructor to match my display).

Please help me!
 
Last edited:
Surely you want to change the constructor of UI() to pass a reference to U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI instead of passing the actual instance which would cause the compiler to (try to) instantiate a new copy.
 
Surely you want to change the constructor of UI() to pass a reference to U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI instead of passing the actual instance which would cause the compiler to (try to) instantiate a new copy.
OK so I tried that, and got shouted at again by the compiler. The passing by reference thing is definitely the answer but instead of U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI I needed to use U8G2. I think I was getting confused between the class name and the constructor used to instantiate my particular u8g2 setup but it's still a little vague in my mind so if someone can be bothered to explain a bit further I'd be very grateful.

(On top of that I am using Eclipse with the Sloeber plugin which compiles successfully but the Teensy just reboots to a dark screen. Back to using my beloved Kate text editor and a build.sh calling arduino-cli.)

In summary:

Code:
// ino file:
// still calling u8g2 the same way:
U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI u8g2(U8G2_R0, 10, 9, 8);
UI UI(u8g2);
Code:
// UI.h
class UI {
    public:
        U8G2 _display;
        UI(U8G2& display);
Code:
// UI.cpp
UI::UI(U8G2& display) {
    // blah blah blah
}
 
Back
Top