Page 2 of 3

Re: Larger fonts on SSD1306 OLED displays

Posted: Mon Nov 18, 2019 7:38 pm
by uxhamby
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.

Re: Larger fonts on SSD1306 OLED displays

Posted: Tue Nov 19, 2019 3:07 am
by uxhamby
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.

Re: Larger fonts on SSD1306 OLED displays

Posted: Tue Nov 19, 2019 4:15 am
by jimmo
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).

Re: Larger fonts on SSD1306 OLED displays

Posted: Tue Nov 19, 2019 8:03 am
by pythoncoder
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.

Re: Larger fonts on SSD1306 OLED displays

Posted: Tue Nov 19, 2019 6:39 pm
by uxhamby
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.

Re: Larger fonts on SSD1306 OLED displays

Posted: Wed Nov 20, 2019 9:07 am
by pythoncoder
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.

Re: Larger fonts on SSD1306 OLED displays

Posted: Thu Nov 21, 2019 8:22 pm
by uxhamby
Thanks Peter,

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

Cheers,

Brian H.

Re: Larger fonts on SSD1306 OLED displays

Posted: Fri Nov 22, 2019 11:41 am
by pythoncoder
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.

Re: Larger fonts on SSD1306 OLED displays

Posted: Fri Jan 17, 2020 5:23 am
by stanely
This is really good stuff. Thank you for posting, Peter!

Re: Larger fonts on SSD1306 OLED displays

Posted: Sun Aug 30, 2020 2:54 pm
by IHOXOHI
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.