Larger fonts on SSD1306 OLED displays

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
uxhamby
Posts: 34
Joined: Thu Nov 14, 2019 9:47 pm

Re: Larger fonts on SSD1306 OLED displays

Post by uxhamby » Mon Nov 18, 2019 7:38 pm

I am having an issue that I don't understand. Some sage advice would be welcome:
MicroPython v1.11-8-g48dcbbe60 on 2019-05-29; ESP module with ESP8266
Type "help()" for more information.
>>>
>>> import writer_demo
Test assumes a 128*64 (w*h) display. Edit WIDTH and HEIGHT in ssd1306_setup.py for others.
Device pinouts are comments in ssd1306_setup.py.
Issue:
writer_demo.test() for an I2C connected device.
writer_demo.test(True) for an SPI connected device.
>>> writer_demo.test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "writer_demo.py", line 41, in test
File "ssd1306_setup.py", line 63, in setup
TypeError: can't convert str to int
>>>
Otherwise, the following initialization of my hardware results in working display:
from machine import Pin, I2C
import ssd1306
from time import sleep

# ESP32 Pin assignment
#i2c = I2C(-1, scl=Pin(22), sda=Pin(21))

# ESP8266 Pin assignment
i2c = I2C(-1, scl=Pin(5), sda=Pin(4))

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
Thanks,

Brian H.
Uxbridge Ont.

uxhamby
Posts: 34
Joined: Thu Nov 14, 2019 9:47 pm

Re: Larger fonts on SSD1306 OLED displays

Post by uxhamby » Tue Nov 19, 2019 3:07 am

I have since updated to 'esp8266-20191118-v1.11-580-g973f68780.bin' the latest daily release of MicroPython.

Now the results of attempting to run writer_demo.py are pretty consistently as follows:
MPY: soft reboot
MicroPython v1.11-580-g973f68780 on 2019-11-18; ESP module with ESP8266
Type "help()" for more information.
>>> import writer_demo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "writer_demo.py", line 38, in <module>
MemoryError: memory allocation failed, allocating 3200 bytes
>>> import gc
>>> gc.mem_free()
17280
>>>
Thoughts or advice ?

Cheers,

Brian H.
Uxbridge Ont.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Larger fonts on SSD1306 OLED displays

Post by jimmo » Tue Nov 19, 2019 4:15 am

writer_demo.py uses ssd1306_setup.py which looks like it's written for the Pyboard. You can replace ssd1306_setup.py to use the working code that you already have that creates the i2c and oled instances.

My guess with the out of memory error is that freesans.py is too large for the ESP8266 (it's only got something like 80kiB of RAM).

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

Re: Larger fonts on SSD1306 OLED displays

Post by pythoncoder » Tue Nov 19, 2019 8:03 am

You need to implement fonts as frozen bytecode to conserve RAM. That is the reason for converting them to Python source: see font-to-py repo.
Peter Hinch
Index to my micropython libraries.

uxhamby
Posts: 34
Joined: Thu Nov 14, 2019 9:47 pm

Re: Larger fonts on SSD1306 OLED displays

Post by uxhamby » Tue Nov 19, 2019 6:39 pm

Thanks for the quick responses.
You can replace ssd1306_setup.py to use the working code that you already have that creates the i2c and oled instances.
Yes, I wondered about that. Cheers!
You need to implement fonts as frozen bytecode to conserve RAM. That is the reason for converting them to Python source
Yes, is that not what the 'freesans20.py' file contains? Or am I missing something rather fundamental?
see font-to-py repo
Yes, I have also run 'font_to_py.py' on another ttf font, to create a 'News_font.py' file. It turned out larger than freesans.py's file size though, so I never actually tried using it.


Thanks,

Brian H.

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

Re: Larger fonts on SSD1306 OLED displays

Post by pythoncoder » Wed Nov 20, 2019 9:07 am

The file freesans20.py is Python sourcecode created using the font_to_py utility.

It is up to you to incorporate such files as frozen bytecode in your project. If you just put the file in the filesystem and import it, it will use lots of RAM. As frozen bytecode, when you import it, it will use very little.
Peter Hinch
Index to my micropython libraries.

uxhamby
Posts: 34
Joined: Thu Nov 14, 2019 9:47 pm

Re: Larger fonts on SSD1306 OLED displays

Post by uxhamby » Thu Nov 21, 2019 8:22 pm

Thanks Peter,

Yes, I see the distinction now. Is there a worthy tutorial somewhere which documents the procedure?

Cheers,

Brian H.

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

Re: Larger fonts on SSD1306 OLED displays

Post by pythoncoder » Fri Nov 22, 2019 11:41 am

This is my attempt to explain the procedure for freezing Python files.

This does assume you have the MicroPython source and the necessary toolchain for compiling a build. Build instructions may be found here. First convince yourself you can clone the MicroPython repo, build and install the firmware. Then add your modules to freeze as described in the link above.
Peter Hinch
Index to my micropython libraries.

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Larger fonts on SSD1306 OLED displays

Post by stanely » Fri Jan 17, 2020 5:23 am

This is really good stuff. Thank you for posting, Peter!

IHOXOHI
Posts: 119
Joined: Sat Apr 25, 2020 7:31 am

Re: Larger fonts on SSD1306 OLED displays

Post by IHOXOHI » Sun Aug 30, 2020 2:54 pm

Hi,

Nice! It works fine. Very usefull!

Just, I tried to use font_to_font.py to make my font for have a bigger font than the courrier20 which is in the repo...

The return of the command "./font_to_font.py courrier.ttf 30 my_font.py" looks good. But when I tried to use this my_font.py, I have the return "font must have an horizontally mapped". I tried 5 differents downloaded fonts with all time the same result.

Could I have an advise to find a font file which could be downloaded, and used with the writer?

Otherwise, when I used the spi interface with a "While True" loop, I haven't succed to resolve a blinked screen on my 1.54 oled display at each loop...?

Thanks.

Post Reply