/* myU8G2Display Helper routines for U8g2lib
PM wiegand, July 2024
This file is a demonstration sketch. Copy and paste the functions below (except for setup and loop of course) into your own sketch
Usage:
1. use #define myU8G2Display your_dispay_name to have the compiler replace the default display names with your own
2. use the desired constructor with your_display_name as the object name, and specify buffer size as F, 1 or 2
3. .begin() the display object and
4. .setFont before using the helper functions
5. The functions just write to the buffer. Remember to send the buffer(s) to the display using either .sendBuffer() for 'F' or firstPage()/nextPage() for "1" or "2"
*/
//#define FULL //for demo sketch: uncomment if using F in the constructor (full page mode)
#define PAGED //for demo sketch: uncomment if using 1 or 2 in the constructor (loop and send partial pages each time using smaller buffer)
#include <U8g2lib.h> //the constructor HW_I2C part will tell the u8g2 library to load wire.h so you don't need to explicity include it
#define myU8G2Display oled //put the name of your diplay here (I used 'oled' in the constructor below)
#ifdef FULL
U8G2_SSD1306_128X64_NONAME_F_HW_I2C oled(U8G2_R0, /* reset=*/U8X8_PIN_NONE); //I used 'oled' as the name for my display
#endif
#ifdef PAGED
U8G2_SSD1306_128X64_NONAME_1_HW_I2C oled(U8G2_R0, /* reset=*/U8X8_PIN_NONE); //I used 'oled' as the name for my display
#endif
void setup() {
Serial.begin(9600);
oled.begin();
//myU8G2Display.setFlipMode(1); //uncomment this if your display is mounted upside down
}
void loop() {
int bufferNum = 0;
oled.clearBuffer(); //start with a blank display
oled.setFont(u8g2_font_pressstart2p_8r); // choose a suitable font
oled.setCursor(0, 0); //start printing as upper left corner of display
#ifdef PAGED //using paged mode so we need to tell compiler to compile the do loop
oled.firstPage();
do {
#endif
oled.drawBox(5,5, 120, 60);
oled.drawStr(40, 40, "Hello!");
#ifdef FULL //using the full page mode, don't need the do loop but we do need the send buffer command
oled.sendBuffer(); //this actually causes the display to show the text for a full (nonbuffered) constructor. Use firstpage/nextpage approach if your constructor specifies buffering
Serial.println("Sent 1K buffer to display");
#endif
#ifdef PAGED
bufferNum++;
Serial.println("buffer page " + String(bufferNum) + " sent to display");
} while (oled.nextPage());
#endif
do {
} while (true); //this waits forever. Could also comment this out and the display will be constantly re-drawing the same thing
}
Loading
ssd1306
ssd1306