Larger fonts on SSD1306 OLED displays

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
uxhamby
Posts: 13
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: 13
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: 1192
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: 3935
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

uxhamby
Posts: 13
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: 3935
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

uxhamby
Posts: 13
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: 3935
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

stanely
Posts: 13
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!

Post Reply