module documentation (ssd1306)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Tillmario
Posts: 18
Joined: Mon Nov 19, 2018 2:06 pm

module documentation (ssd1306)

Post by Tillmario » Wed Nov 28, 2018 11:36 am

Hey,
on my ESP2866 running MicroPython I connected an OLED Display (128x64) using this tutorial.

I send the ssd1306.py and the ssd1306.mpy to my board.

after initializing

Code: Select all

import machine
import ssd1306
i2c = machine.I2C(-1, machine.Pin(5), machine.Pin(4))
oled = ssd1306.SSD1306_I2C(128, 32, i2c)
I can type "oled." and then TAB and a list with all the commands is shown.

My first question:
Where can I see a documentation what this different commands mean? (and what variables are supposed to be entered) I figured out oled.text() because you need two points and a color for the line but the rest... I just don't know and google was no help. There needs to be some explaination.

My second question:
Are there different versions of the ssd1306.py and which should I choose?
I'm struggling with the size of the font. I would like to fill the whole display with a number but sadly there is no "oled.FontSize" e.g.

I am aware of some methods to load various fonts to the board. I just want the same font, but bigger. Is there an easy way to archieve that?

Maybe another version of ssd1306.py that has a font size module (?) included?

Greetings, Tom

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: module documentation (ssd1306)

Post by HermannSW » Wed Nov 28, 2018 4:47 pm

I don't know if there exists documentation, I was able to find what I needed using Google search.
I switched to using the official ssd1306 driver, just do "import ssd1306" and its there on ESP32 MicroPython.
Here is source:
https://github.com/micropython/micropyt ... ssd1306.py

With that module and a modified FBConsole from boochow you can even redirect REPL to display:
https://github.com/Hermann-SW/micropyth ... ypi#readme

Code: Select all

import machine, ssd1306
i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

from FBConsole import FBConsole
console = FBConsole(oled)
os.dupterm(console)
Image
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

rpr
Posts: 99
Joined: Sat Oct 27, 2018 5:17 pm

Re: module documentation (ssd1306)

Post by rpr » Wed Nov 28, 2018 8:03 pm


Tillmario
Posts: 18
Joined: Mon Nov 19, 2018 2:06 pm

Re: module documentation (ssd1306)

Post by Tillmario » Thu Nov 29, 2018 1:05 am

thanks, I read it and did it but have another question:
on my raspberry I ran:

Code: Select all

pi@Raspberry:~/Desktop/font_test $ python3 font_to_py.py arial.ttf 60 arial_clock.py -c 1234567890:
Writing Python font file.
Height set in 3 passes. Actual height 60 pixels.
Max character width 45 pixels.
arial_clock.py written successfully.
am I correct that I have to transfer the following files to my ESP2866?:
ssd1306_setup.py
writer.py
arial_clock.py
...anything else?

and then I run this code?:

Code: Select all

import machine
from ssd1306_setup import WIDTH, HEIGHT, setup
from writer import Writer

# Font
import arial_clock

def test(use_spi=False):
    ssd = setup(use_spi)  # Create a display instance
    rhs = WIDTH -1
    ssd.line(rhs - 20, 0, rhs, 20, 1)
    square_side = 10
    ssd.fill_rect(rhs - square_side, 0, square_side, square_side, 1)

    wri = Writer(ssd, arial_clock)
    Writer.set_textpos(ssd, 0, 0)  # verbose = False to suppress console output
    wri.printstring('Sunday\n')
    wri.printstring('12 Aug 2018\n')
    wri.printstring('10.30am')
    ssd.show()

print('Test assumes a 128*64 (w*h) display. Edit WIDTH and HEIGHT in ssd1306_setup.py for others.')
print('Device pinouts are comments in ssd1306_setup.py.')
print('Issue:')
print('writer_demo.test() for an I2C connected device.')
print('writer_demo.test(True) for an SPI connected device.')
I have an i2c display, is that a problem? and how would I implement my code?

Code: Select all

from machine import Pin, I2C
import ssd1306
from time import sleep

i2c = I2C(-1, Pin(5), Pin(4))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
led = Pin(15, Pin.OUT)
button = Pin(12, Pin.IN, Pin.PULL_UP)
count = 60

oled.fill(1)
oled.show()

def frame():
    oled.line(1,1,126,1,0)
# ...
    oled.show()
    
def clear():
    for j in range(6,57):
        oled.line(6,j,121,j,1)
    oled.show()

def countdown():    
        for i in range(60,-1,-1):
            oled.text(str(i),55,32,0)
            oled.show()
            sleep(0.9)
            clear()
            oled.show()
        
frame()
while True:
    clear()
    oled.text(str(count),55,32,0)
    oled.show()
    count -= 1
    if count == -1: led.on()
    for i in range(9):    
        if not button.value():
            count = 60
            led.off()
	sleep(0.1)
I would appreciate any help/clues etc.

Tom

rpr
Posts: 99
Joined: Sat Oct 27, 2018 5:17 pm

Re: module documentation (ssd1306)

Post by rpr » Thu Nov 29, 2018 1:36 am

I don't have an ESP8266 but an ESP32. Here is my simple example. I copied over writer.py, arial_clock.py. You would need to modify the Pin numbers.

Code: Select all

from machine import Pin, I2C
from ssd1306 import SSD1306_I2C

WIDTH = const(128)
HEIGHT = const (64)
sda_pin = Pin(26)
scl_pin = Pin(25)

i2c = I2C(scl=scl_pin, sda=sda_pin, speed=400000)

ssd = SSD1306_I2C(WIDTH, HEIGHT, i2c)

import arial_clock

from writer import Writer
wri2 = Writer(ssd, arial_clock, verbose=True)

Writer.set_clip(True, True)
Writer.set_textpos(0, 0)
wri2.printstring('1200')

ssd.show()

Tillmario
Posts: 18
Joined: Mon Nov 19, 2018 2:06 pm

Re: module documentation (ssd1306)

Post by Tillmario » Thu Nov 29, 2018 7:25 pm

Thanks again. Okay, I now get how it's supposed to work.
However I'm sadly encountering problems. I've read a lot, tried a lot, changed a lot. the writer.py seems to be the troublemaker.

For example:

Code: Select all

from writer import Writer
import courier20
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
MemoryError: memory allocation failed, allocating 4033 bytes
because i read it online I did this:

Code: Select all

import gc
gc.mem_free()
15232

import micropython
micropython.mem_info(1)
stack: 2128 out of 8192
GC: total: 35968, used: 21200, free: 14768
No. of 1-blocks: 122, 2-blocks: 43, max blk sz: 264, max free sz: 135
GC memory layout; from 3ffef550:
00000: MDShhSMMDDhThDMB=BBBh===h====hDBhh==h===========================
00400: ================================================================
00800: ================================================================
00c00: ================================================================
01000: ============================================hhBhDhBh=h===BDh==hB
01400: hDhDBMDDBBBBhB=h=h===B=MDhh=======h=============================
01800: ================================================================
01c00: ================================================================
02000: ====================================================BBB=hBMShSh=
02400: h========B=BSh=Dh=hSMDhh=DDhhSh======hhB=BBTh===h==BDhB=B==BhBhB
02800: =B=BBB=BhB=BBh===h=BBBh=======h========h=..h=h=h=M.Dh====h=B=B=B
02c00: h===h==Sh==h========hh.B=h=======hh===B=.B=h==B=h===h=Bh=h...SSh
03000: =h==hh=h====h=====h=.Sh=....Sh=======================h=======h==
03400: ===========h=h======h====...........h=..........................
03800: .....................................................Sh==.......
03c00: ....h===..................................hh.........h=.........
04000: .......h=======.................................................
04400: ................................................................
04800: ................h=======........................................
04c00: ................................................................
05000: ...............................h=======.........................
05400: ...................................h============================
05800: ================================================================
05c00: ================================================================
06000: ================================================================
06400: ================================...............h============....
       (2 lines all free)
07000: .h=...h=........................................................
07400: ...........hh..................h=...hh........hh................
07800: ................................................................
07c00: .h===.....................h==h===========h==================h===
08000: =========h====h=====h==========h==h===hh====================hh==
08400: ========hh==hh=hh====hh====h=h=====hh=====================h.....
08800: ................................................................
08c00: ........
Does this help? Maybe a mistake flashing the firmware. I did it at 00000 I guess.

Another example:

Code: Select all

import courier20
from writer import Writer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError: memory allocation failed, allocating 80 bytes
Loading other fonts give me the same mistake.

Any clues?
Tom

rpr
Posts: 99
Joined: Sat Oct 27, 2018 5:17 pm

Re: module documentation (ssd1306)

Post by rpr » Thu Nov 29, 2018 7:47 pm

I do have an esp8266 somewhere in my box. Will look for it and see what happens.

Also realized that on the esp32 I'm using the Loboris micropython firmware. I will try on the regular micropython.

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

Re: module documentation (ssd1306)

Post by pythoncoder » Fri Nov 30, 2018 6:42 am

The ESP8266 is quite short of RAM. I don't think I tested the Writer class on that platform but it is just MicroPython code so it should work.

I can replicate your problem. The RAM error is occurring at the compilation phase where the Writer code is compiled to bytecode: the compiler is running out of RAM. I tried cross-compiling writer.py. I deleted writer.py on the ESP8266 and copied writer.mpy across. This fixed the error, so this is a quick and easy fix.

I strongly recommend learning to use frozen bytecode. With frozen bytecode you can run surprisingly complex applications on the ESP8266 so the technique is well worth learning. Freezing your fonts and writer would save a lot of RAM.
Peter Hinch
Index to my micropython libraries.

rpr
Posts: 99
Joined: Sat Oct 27, 2018 5:17 pm

Re: module documentation (ssd1306)

Post by rpr » Fri Nov 30, 2018 7:41 am

I too was able to replicate this.

It took a while but I was able to setup the esp sdk toolchain required to build a firmware with the frozen bytecode. Once built everything ran fine with no memory issues.

Post Reply