[solved] U8GLIB in external class

Hi guys,

Noob question incoming: I’m trying to use the U8GLIB in an external class but I can’t seem to figure out how to set up the .h and .cpp files.

Main.ino

#include "src/Display.h"

Display display1(53);

void setup() {
 //
}

void loop() {
 display1.update();

}

Display.h

#ifndef DISPLAY_H
#define DISPLAY_H
#include <Arduino.h>
#include "U8glib.h"

class Display {
  
  private:
    byte pin;
    U8GLIB_ST7920_128X64_1X u8g(); 
  public:
    Display(byte pin);
    void update();
};
#endif

Display.cpp

#include "Display.h"
#include "U8glib.h"

Display::Display(byte pin){
 this->pin = pin;
 U8GLIB_ST7920_128X64_1X u8g(pin);
}

void Display::update() {

 u8g.firstPage();  
 do 
 {
   u8g.setFont(u8g_font_courB14);
   u8g.drawStr( 35, 26, "Test");

 }while(u8g.nextPage());
}

It’s telling me “invalid use of member function ‘U8GLIB_ST7920_128X64_1X Display::u8g()’ (did you forget the ‘()’ ?)”

I’m clearly not correctly declaring the library, could someone point me in the right direction?

Thanks a bunch,

G

It looks to me like you don’t want the “U8GLIB_ST7920_128X64_1X” there in your constructor (you’d be making an instance that immediately goes out of scope), but anyway, are you sure it is safe to put that in a constructor?
It would be more normal to have a “begin” method.

Right so how would I approach that? I'm clearly declaring it wrong but I can't seem to figure out how to do this in an external class. Basically what I'm trying to do, is split the following into an external class:

main.ino (working example without using an external class)

#include "U8glib.h"

#define CS_PIN 53

U8GLIB_ST7920_128X64_1X u8g(CS_PIN);

void setup() 
{
}

void loop() 
{
  u8g.firstPage();  
  
  do 
  {
    u8g.setFont(u8g_font_courB14);
    u8g.drawStr( 35, 26, "Tet");
    
  }while(u8g.nextPage());
}
void Display::begin(void){
	 u8g(pin);
}

etc

Something like this?

Display.h

#ifndef DISPLAY_H
#define DISPLAY_H
#include <Arduino.h>
#include "U8glib.h"

class Display : public U8GLIB_ST7920_128X64_1X{
  
  private:
    byte pin;
    U8GLIB_ST7920_128X64_1X u8g; 
  public:
    Display(byte pin);
    void begin();
    void update();
};
#endif

Display.cpp

#include "Display.h"

Display::Display(byte pin){
 this->pin = pin;
}

void Display::begin(void) {
 u8g(pin)
}

void Display::update() {

 u8g.firstPage();  
 do 
 {
   u8g.setFont(u8g_font_courB14);
   u8g.drawStr( 35, 26, "Test");

 }while(u8g.nextPage());
}

This is throwing me

Display.cpp:3:26: error: no matching function for call to 'U8GLIB_ST7920_128X64_1X::U8GLIB_ST7920_128X64_1X()

Thanks for your help!

Whoa! Where did the inheritance suddenly come from?
And why?

Your previous suggestion made me google for the begin method and I found a similar post, which inherited that in the final solution, so I tried that. Either way, it throws the same error (with and without inheritance). Any ideas?

I’ll have to download the library.

Later.

A otherwise useless example of embedding an object in another.

class Embedded {
    byte pin;
  public:
    Embedded(byte iPin) : pin(iPin) {}
    void begin() {
      pinMode(pin, OUTPUT);
    }
};
class Contains {
    byte options;
    Embedded emb;
  public:
    Contains(byte iPin, byte iOptions) : options(iOptions), emb(iPin) {}
    void begin() {
      emb.begin();
    }
} test(LED_BUILTIN, 0x42);

void setup() {
  test.begin();
}
void loop() {}

Note the ':' initialization list.

Thanks for the reply, feels like I'm getting closer but still can't figure out how to translate it. This is the header of the actual U8G class I'm trying to embed:

class U8GLIB_ST7920_128X64_1X : public U8GLIB 
{
  public:
    U8GLIB_ST7920_128X64_1X(uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t reset = U8G_PIN_NONE) 
      : U8GLIB(&u8g_dev_st7920_128x64_sw_spi, sck, mosi, cs, U8G_PIN_NONE, reset)        // a0 = U8G_PIN_NONE
      { }
    U8GLIB_ST7920_128X64_1X(uint8_t cs, uint8_t reset = U8G_PIN_NONE) 
      : U8GLIB(&u8g_dev_st7920_128x64_hw_spi, cs, U8G_PIN_NONE, reset)           // a0 = U8G_PIN_NONE
      { }
    U8GLIB_ST7920_128X64_1X(uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, 
        uint8_t en, uint8_t di, uint8_t rw = U8G_PIN_NONE, uint8_t reset = U8G_PIN_NONE) 
      : U8GLIB(&u8g_dev_st7920_128x64_8bit, d0, d1, d2, d3, d4, d5, d6, d7, en, U8G_PIN_NONE, U8G_PIN_NONE, di, rw, reset)  // cs1 = cs2 = U8G_PIN_NONE
      { }
};

Thanks a lot guys

You first should decide which of the following statements is more accurate:

  • My Display class IS a type of U8glib.

  • My Display class HAS a U8glib.

If the former, use inheritance. If the latter, embed a U8glib object in your class.

Or, alternatively

  • My Display class KNOWS a U8glib

in that case, you could embedd a U8glib reference/pointer.

gfvalvo:
You first should decide which of the following statements is more accurate:

  • My Display class IS a type of U8glib.

  • My Display class HAS a U8glib.

If the former, use inheritance. If the latter, embed a U8glib object in your class.

Right, so my Display class HAS a U8glib. I want to embed the U8glib and do things with it.

Whandall:
A otherwise useless example of embedding an object in another.

class Embedded {

byte pin;
  public:
    Embedded(byte iPin) : pin(iPin) {}
    void begin() {
      pinMode(pin, OUTPUT);
    }
};
class Contains {
    byte options;
    Embedded emb;
  public:
    Contains(byte iPin, byte iOptions) : options(iOptions), emb(iPin) {}
    void begin() {
      emb.begin();
    }
} test(LED_BUILTIN, 0x42);

void setup() {
  test.begin();
}
void loop() {}



Note the ':' initialization list.

So I’ve tried a few ways to translate this into a .cpp and .h file, but I’m pretty clueless at this point. In your example, what is the test function exactly? How is it linked to the Embedded object?

Grieswald: Right, so my Display class HAS a U8glib. I want to embed the U8glib and do things with it.

Then, @Whandall's example is applicable.

In your example, what is the test function exactly? How is it linked to the Embedded object?

It's not a function, it's an object of the 'Contains' class.

Thanks guys, it works!

#ifndef SEQ3_DISPLAY_H
#define SEQ3_DISPLAY_H
#include <Arduino.h>
#include "U8glib.h"

class Display{
  
  private:
    byte pin;
    U8GLIB_ST7920_128X64_1X u8g; 
  public:
    Display(byte SCK, byte MOSI, byte CS) : u8g(SCK, MOSI, CS) {}

    void begin(){
    	u8g.begin();
    } 
    
    void update();
};

#endif