Any way to trim down the u8g2 library?

Hi, I am using a Nano, a 1.3 OLED and u8g2 Library and running into memory issues. I am hitting the 96% mark but if I use the U8x8 Library it is only 37%. But, I need one line of 4 big numbers ("3000" max) and the u8x8 only has 8x8 pixel characters. I am currently using a Bold 8x8 font but it is hard to read from more than 2-feet away.

Does anyone know of a stripped down version of u8g2 or a different small library that will do big numbers ?

The u8g2 is awesome in what it does, but it does not leave a great deal of room on the Nano. I have to use a Nano for size limitations and compatibility with the rest of our IoT network.

Thanks

Read the Wiki. Olikraus advises using the appropriate Font.

e.g. numbers-only for "BIG DIGITS"
e.g. capitals-only for "HEADINGS"
e.g. regular 95-letter ascii for "Regular Text"

There is a wide choice of styles, heights, character ranges.

You will find that suitable fonts will fit in your Nano memory.

If you have a problem, post your complete code. e.g. paste to message or attach a file.

David.

I'm guessing here but, if you only need text, the Adafruit ascii library may be all you need and I understand it is a lot smaller.It is the only librray I have had any joy with but that is with 0.96 OLED.

U8g2 fonts are incredibly efficient. Your "big digit" font costs you 791 bytes.
A readable text font costs you about 2520 bytes

I suggest that you study the Wiki. Choose suitable fonts.

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

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void setup()
{
    // put your setup code here, to run once:
    u8g2.begin();
    u8g2.setFontPosTop();
}

void loop()
{
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_fub35_tn); // 791 bytes
    u8g2.setCursor( 0, 0);
    u8g2.print("23.45");
    u8g2.setFont(u8g2_font_fub20_tr); // 2520 bytes
    u8g2.setCursor( 0, 40);
    u8g2.print("fub35_tn");
    u8g2.sendBuffer();
    delay(5000);
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_fub30_tn); // 669 bytes
    u8g2.setCursor( 0, 0);
    u8g2.print("-34.56");
    u8g2.setFont(u8g2_font_fub20_tr); // 2520 bytes
    u8g2.setCursor( 0, 40);
    u8g2.print("fub30_tn");
    u8g2.sendBuffer();
    delay(5000);
}

David.

Nick_Pyner:
the Adafruit ascii library may be all you need and I understand it is a lot smaller.It is the only librray I have had any joy with but that is with 0.96 OLED.

Thanks but it is pretty unwieldy to use compared to the u8x8, plus this is a SSD1106 so the Adafruit will not work as it only covers the SSD1306 that they sell.

Rubbish. Adafruit_SSD1306 is easy to use. I agree that the default 5x7 font looks "blocky" if you just use setTextSize(5)

A 95-character Free Font is wasteful.
You can use a cut-down digits-only Free Font.

David.

david_prentice:
U8g2 fonts are incredibly efficient. Your "big digit" font costs you 791 bytes.

Thanks, I had studied the wiki before asking. That trimmed down code you kindly included (thanks for that) produces this message, so very little room for my working part of the Sketch.

Sketch uses 8378 bytes (25%) of program storage space. Maximum is 32256 bytes.
Global variables use 1537 bytes (75%) of dynamic memory, leaving 511 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.

And this is the code I used and as you can see the u8g2 library has a lot of overhead. It did produce a nice looking Font and number though. :slight_smile:

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

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void setup()
{
    // put your setup code here, to run once:
    u8g2.begin();
    u8g2.setFontPosTop();
}

void loop()
{
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_fub35_tn); // 791 bytes
    u8g2.setCursor( 0, 0);
    u8g2.print("23.45");
//    u8g2.setFont(u8g2_font_fub20_tr); // 2520 bytes
//    u8g2.setCursor( 0, 40);
//    u8g2.print("fub35_tn");
//    u8g2.sendBuffer();
//    delay(5000);
//    u8g2.clearBuffer();
//    u8g2.setFont(u8g2_font_fub30_tn); // 669 bytes
//    u8g2.setCursor( 0, 0);
//    u8g2.print("-34.56");
//    u8g2.setFont(u8g2_font_fub20_tr); // 2520 bytes
//    u8g2.setCursor( 0, 40);
//    u8g2.print("fub30_tn");
    u8g2.sendBuffer();
    delay(5000);
}

david_prentice:
Rubbish. Adafruit_SSD1306 is easy to use.

Did you miss the SSD1106 part of my reply? :slight_smile:

No, Adafruit_SSD1306 does not support the SH1106. But it is simple to adapt for SH1106.

I built your sketch for an old-bootloader Nano

Using library U8g2 at version 2.24.3 in folder: C:\Users\David\Documents\Arduino\libraries\U8g2 
Using library SPI at version 1.0 in folder: C:\Program Files\Arduino-1.8.9\hardware\arduino\avr\libraries\SPI 
Using library Wire at version 1.0 in folder: C:\Program Files\Arduino-1.8.9\hardware\arduino\avr\libraries\Wire 
"C:\\Program Files\\Arduino-1.8.9\\hardware\\tools\\avr/bin/avr-size" -A "C:\\Users\\David\\AppData\\Local\\Temp\\arduino_build_464796/OLED_digits.ino.elf"
Sketch uses 8344 bytes (27%) of program storage space. Maximum is 30720 bytes.
Global variables use 1537 bytes (75%) of dynamic memory, leaving 511 bytes for local variables. Maximum is 2048 bytes.

If you use a "small-buffer" constructor it will reduce the SRAM usage. But the code is more complex.

I would be quite happy with an 8kB sketch. You still have 22kB for your custom application code.
The fonts are both attractive and readable.

Graphics programs tend to use up Flash with fonts, images, data tables.
You generally have enough space for drawing shapes, floating point maths, ...

It is worth putting "messages" in Flash with F()
But otherwise avoid String class.

David.

WaitSome:
Hi, I am using a Nano, a 1.3 OLED and u8g2 Library and running into memory issues. I am hitting the 96% mark but if I use the U8x8 Library it is only 37%. But, I need one line of 4 big numbers ("3000" max) and the u8x8 only has 8x8 pixel characters. I am currently using a Bold 8x8 font but it is hard to read from more than 2-feet away.

In one of the recent releases I have added bigger fonts to u8x8 interface. So the remark on 8x8 is outdated and I should update this once on the wiki.

On the u8x8 font page (fntlist8x8 · olikraus/u8g2 Wiki · GitHub) you can find several bigger fonts. I assume, the font what you need is "u8x8_font_inr46_4x8_n" It is soo huge, that exactly four numbers only will fit on your display.

The 4x8 postfix of the font refers to the extension over the normal 8x8 pixel font, so u8x8_font_inr46_4x8_n glyphs actually have a size of 32x64 pixel.

Oliver

Edit: There is also a bold version of this font: "u8x8_font_inb46_4x8_n"