Change different screen at runtime using U8glib

Does anybody know if it's possible to change a display at runtime using u8glib? For example, let's say you have one of two different display types you want to use, and you will pick which display to use based off of an EEPROM read. Is this possible?

Here's some pseudo code for what I'm trying to achieve.

#include "U8glib.h"
#include <EEPROM.h>

U8GLIB_NHD27OLED_2X_BW screen1(13, 11, 10, 9);  //screen type 1 constructor
U8GLIB_SSD1306_128X32 screen2(13, 11, 10, 9); //screen type 2 constructor
U8G u8g; //some sort of dummy placeholder that we can assign to the appropriate selected screen?
//all of the display code would use the u8g constructor so that the code can remain universal to
//whichever screen is used

void setup() {
  if (EEPROM.read(0) == 0) {
    u8g = screen1;
  } else {
    u8g = screen2;
  }
}

void loop(void) {
  u8g.firstPage();
  do {
    u8g.setFont(u8g_font_unifont);
    u8g.drawStr( 0, 22, "Hello World!");
  } while ( u8g.nextPage() );
}

Thanks!

This topic was automatically closed after 120 days. New replies are no longer allowed.