0

Does anyone know how to wrap and or text using the U8G2 Libary? I know there are other libraries that do this for you, but I'd like to use U8G2.

Thank you!

My code's here for reference. It's not clean, but it works for now.

2
  • You have to do it manually. Scrolling is a simple matter of printing substrings and padding with spaces. Wrapping is a simple matter of printing substrings on different lines.
    – Majenko
    Commented Feb 11, 2017 at 12:17
  • The Arduino Uno has very limited amount of memory to implement scrolling and wrapping of text for an LCD driver in general. The sketch has to do this, e.g. calculate line breaks, sub-section of text to be displayed, update when focus changes (i.e. scroll), save text, etc. Your sketch seems to be only a limited amount of text so you should be able to do this calculations, save of state, text box projection, etc. Commented Feb 11, 2017 at 14:23

1 Answer 1

1

You can do something like this:

    void loop(void) {
        u8g2_uint_t x;
        
        u8g2.firstPage();
        do {
        
            // draw the scrolling text at current offset
            x = offset;
            u8g2.setFont(u8g2_font_inb30_mr);       // set the target font
            do {                                // repeated drawing of the scrolling text...
                u8g2.drawUTF8(x, 30, text);         // draw the scolling text
                x += width;                     // add the pixel width of the scrolling text
            } while( x < u8g2.getDisplayWidth() );      // draw again until the complete display is filled
            
            u8g2.setFont(u8g2_font_inb16_mr);       // draw the current pixel width
            u8g2.setCursor(0, 58);
            u8g2.print(width);                  // this value must be lesser than 128 unless U8G2_16BIT is set
            
        } while ( u8g2.nextPage() );
        
        offset-=1;                          // scroll by one pixel
        if ( (u8g2_uint_t)offset < (u8g2_uint_t)-width )    
            offset = 0;                         // start over again
            
        delay(10);                          // do some small delay
    }

The code is taken from https://github.com.

It is an excellent library, but it is can require a big amount of memory, fit very well on ESP8266 (tried on nodeMCU) / ESP32.

Of course on ESP32 it is faster.

Used for small OLED 0.98" screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.