Custom Smaller Fonts for SSD1306

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
JoeCharlieH
Posts: 6
Joined: Thu Dec 27, 2018 10:59 pm

Custom Smaller Fonts for SSD1306

Post by JoeCharlieH » Fri May 10, 2019 8:06 pm

Hi all:

I'm currently working in an UI for a glucose sensor, so I need to display the collected data in a small graph (e.g bars, points in time, etc) with an 128x64 display. I have no issues using the frambuf and ssd1306 modules to create simple lines and shapes in the display, but using smaller fonts it's what its driving me crazy.

I've allready used @pythoncoder approach for using custom fonts with writer_minimal.py and it's amazing, but the Limitations section suggest to use hand-made binary fonts with fonts smaller than the default 8x8 from the SSD1306.

The font I came up with is a 4x5 or 3x5 font with the 0-9 chars.
e.g. (BBCode is OFF)
1 1 1 0
1 0 1 0
1 0 1 0
1 0 1 0
1 1 1 0

- How could I create the binary file for those numbers?
- Or Should I use framebuf functions to create every line and/or pixel for each number?

User avatar
pythoncoder
Posts: 3607
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Custom Smaller Fonts for SSD1306

Post by pythoncoder » Mon May 13, 2019 1:16 pm

Very small fonts don't present the problems of data storage and performance associated with more normal sizes. This is specially true if you are only supporting the digits. I would probably use the framebuf pixel drawing method directly and store the glyphs as programmatic constants - perhaps as a list of ten bytearrays. There's no real reason to put such a small amount of data in a file.

The reason for my recommendation to hand-design very small fonts is that this is the generally accepted approach: the exact position of each pixel is critical and a human designer can do a better job than a machine.
Peter Hinch

WRR
Posts: 7
Joined: Wed Jan 09, 2019 8:33 pm

Re: Custom Smaller Fonts for SSD1306

Post by WRR » Mon May 13, 2019 3:29 pm

It probably depends on what you need for your application, but you can see how the default font is defined and structured in its header file:

https://github.com/micropython/micropyt ... e128_8x8.h

If you look for, "font_petme128" in the framebuffer module file, you'll see that it is hard-coded into the "framebuf_text" method, which writes each character as eight 8-pixel tall columns:

https://github.com/micropython/micropyt ... framebuf.c

So you probably could create a new header file with a smaller font, but you wouldn't be able to have each 8-bit byte represent a single column, and you would need to modify the "framebuf_text" method to take that into account. For example, you could define a 3x5 font with each letter represented by 2 bytes (16 bits), but its bits might be structured like [ 0bAAAAABBB, 0bBBCCCCC0 ] to represent this:

A B C
A B C
A B C
A B C
A B C
Remember, the "S" in "IoT" stands for "Security".

JoeCharlieH
Posts: 6
Joined: Thu Dec 27, 2018 10:59 pm

Re: Custom Smaller Fonts for SSD1306

Post by JoeCharlieH » Mon May 13, 2019 4:52 pm

Thanks for both of your suggestions :D
I'll try both of them and choose the one that is more interesting and "fun" to work. :lol: :lol: :lol:

Here is an example of what I'm trying to do:
Image

(The low resolution is just for this example. In my project I'm using a .pbm file to make UI design a little bit faster)

Post Reply