bitmapfont + ssd1306?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tedder42
 
Posts: 24
Joined: Wed Apr 20, 2016 12:11 pm

bitmapfont + ssd1306?

Post by tedder42 »

Can bitmapfont be used to put text on the SSD1306?

It seems that CircuitPython and framebuf don't support it, but it should be possible to throw pixels together.

Any examples?

User avatar
jerryn
 
Posts: 1868
Joined: Sat Sep 14, 2013 9:05 am

Re: bitmapfont + ssd1306?

Post by jerryn »

Here is an example that works on an M0 Express with the CircuitPython-0.9.7-17 (has some recent updates)
It walks "tft" across and down the display.
edited - Just realized that I cut/pasted "tft" from another test case - I suppose I should have changed it to "oled" - oh well ;-)

Note - for the M0 Express build - the internal framebuf does support text as well, but this uses bitmapfont as an example.
What board and version of CircuitPython are you using?
Good luck!

Code: Select all

# Bitmapfont example for ssd1306
# Import all board pins.
from board import *
import busio

# Import the SSD1306 module.
import adafruit_ssd1306
import time
import bitmapfont


# Create the I2C interface.
i2c = busio.I2C(SCL, SDA)

# Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height.  Change these
# to the right size for your display!
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)

MAXROW = 32
MAXCOL= 128


display.fill(0)
display.show()

with bitmapfont.BitmapFont(MAXCOL,MAXROW,display.pixel) as bf:
    try:
        column=0
        row=0
        while(True):
            bf.text('tft',column,row,1)
            display.show()
            row=row+8
            if(row>=MAXROW):
              row=0
              display.fill(0)
            column=column+32
            if(column>=MAXCOL):
              column=0
            time.sleep(5)           

    except:
        pass
    finally:
        pass


User avatar
tedder42
 
Posts: 24
Joined: Wed Apr 20, 2016 12:11 pm

Re: bitmapfont + ssd1306?

Post by tedder42 »

Thanks! I needed to do `bf.init()`, otherwise it worked.

For fun, here's my test loop.

Code: Select all

bf = bitmapfont.BitmapFont(MAXCOL, MAXROW, display.pixel)
bf.init()

for i in range(0, MAXCOL*MAXROW):
  x = i % MAXCOL
  y = i // MAXCOL

  display.fill(0)
  bf.text('tft {} {}'.format(x, y), x, y, 1)
  display.show()

User avatar
jerryn
 
Posts: 1868
Joined: Sat Sep 14, 2013 9:05 am

Re: bitmapfont + ssd1306?

Post by jerryn »

Glad it is working for you. Nice compact code in your example!

By the way- just to clarify:
you can either to the explicit init/deinit or use the "with ... as ..." construction.
That is:

Code: Select all

bf = bitmapfont.BitmapFont(MAXCOL, MAXROW, display.pixel)
bf.init()

do stuff here

bf.deinit()
or

Code: Select all

with bitmapfont.BitmapFont(MAXCOL,MAXROW,display.pixel) as bf:
    do stuff here (indented)
This is explained nicely in Tony DiCola's tutorial:
https://learn.adafruit.com/micropython- ... awing-text

The explicit init/deinit wold probably have made a better example, but I tend the use the with/as so I don't forget the deinit!

User avatar
tedder42
 
Posts: 24
Joined: Wed Apr 20, 2016 12:11 pm

Re: bitmapfont + ssd1306?

Post by tedder42 »

jerryn wrote:Glad it is working for you. Nice compact code in your example!

By the way- just to clarify:
you can either to the explicit init/deinit or use the "with ... as ..." construction.
Is this a micro/circuitpython thing? I didn't know Python had implicit init/deinit in this way, I'm guessing it is either micro or circuit that does it. Couldn't find any references to it, unfortunately.

User avatar
jerryn
 
Posts: 1868
Joined: Sat Sep 14, 2013 9:05 am

Re: bitmapfont + ssd1306?

Post by jerryn »

This may help but I am a newcomer to python myself and slowly sorting out the magic ;-)
https://jeffknupp.com/blog/2016/03/07/p ... -managers/

User avatar
tedder42
 
Posts: 24
Joined: Wed Apr 20, 2016 12:11 pm

Re: bitmapfont + ssd1306?

Post by tedder42 »

Yeah, but init/deinit is much more specific. This is the closest I've been able to find:
https://github.com/micropython/micropyt ... ime.c#L131

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: bitmapfont + ssd1306?

Post by tannewt »

Having a consistent deinit() is a CircuitPython thing because its stricter about sharing underlying resources. Unlike MicroPython, we don't have an init() because thats the job of the constructor. :-) For convenience we support Python's context managers which utilizes __enter__ which returns itself and __exit__ which calls deinit.

You don't actually need to call deinit unless you plan on reusing resources later in the code. On reload all the hardware a user can use is reset.

What do you think of that design? Is it a good thing?

User avatar
tedder42
 
Posts: 24
Joined: Wed Apr 20, 2016 12:11 pm

Re: bitmapfont + ssd1306?

Post by tedder42 »

tannewt2 wrote: What do you think of that design? Is it a good thing?
I like it- there's certainly need to have a constructor/destructor, to help with memory if nothing else. OTOH, the difference in behavior between the fluent ('with') and standard code is disorienting- because it isn't apparent that it is (or isn't) happening.

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: bitmapfont + ssd1306?

Post by tannewt »

Yeah, I agree with you that it can be disorienting. Coming from C++ its weird that context managers in Python aren't strictly destructors. However, its the only way I know of in Python to get reliable deinit. Finalizers can run at any point and the deinit() method afterwards wouldn't actually be called when an exception occurs.

Overall, I think its the right approach. Its just takes a little while to get used to it.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”