ssd1306 using I2C on the esp8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
mflmartin
Posts: 43
Joined: Sat Jul 23, 2016 7:30 pm

Re: ssd1306 using I2C on the esp8266

Post by mflmartin » Sun Jul 31, 2016 6:35 pm

deshipu wrote: Yes, you can do it -- if you are using numbers. However, if you want to save space, at some point you will want to save this as raw
strings of bytes.
When you say, "as a raw strings of bytes", you mean like this? 010101010110
Does this save space, as opossed as using numbers? In code, i would occupy more characters, no?
deshipu wrote: At some point I would probably save this as one large string, something like this:
Interesting technique, to wich I am completely new. How do you convert it to hexadecimal, and, more importantly, how do you save it as "compiled bytecode"? I looked into and I found about pyc_compile. So you would use python 3.5, for instance, and compile some parts into bytecode? How do you then implement it into the project, by putting the pyc file somewhere?

When is this technique recommended, in your oppinion?

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

Re: ssd1306 using I2C on the esp8266

Post by pythoncoder » Mon Aug 01, 2016 11:00 am

To convert it to a hex string experiment on these lines. The first step is to turn your DATA (which is a tuple of tuples) into a simple list, then convert that into a string which can be interpreted by Python:

Code: Select all

lst = []
for t in DATA:
    for d in t:
        lst.append(d)

s = "data = '"
for x, d in enumerate(lst):
    if x > 0 and not x % 16:
        s = ''.join((s, "'\\\n'"))
    s = ''.join((s, "\\x{:02x}".format(d)))

s = ''.join((s, "'"))
print(s)
You'd save the string to a file with a .py extension and check that it can be imported into Python. To freeze the code you need to be able to create a firmware build. By far the easiest way is to grab a nightly build of the toolchain from here http://www.kaltpost.de/~wendlers/micropython/, get the latest source from Github and follow the README to compile it. To freeze your code, simply put it in the esp8266/modules directory and repeat the build.
Peter Hinch
Index to my micropython libraries.

fdufnews
Posts: 76
Joined: Mon Jul 25, 2016 11:31 am

Re: ssd1306 using I2C on the esp8266

Post by fdufnews » Mon Aug 01, 2016 12:06 pm

May be you can consider using the export function of Gimp. Using the pbm export filter gives you a binary file with a short header. For example, a 128x64 image generates a 1073 bytes file.
The file has 49 bytes of header and following it the image is encoded eight pixels in a byte.
You can copy the file to the flashdisk and open it when necessary and just copy the data into the buffer no need to put the image in your code.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: ssd1306 using I2C on the esp8266

Post by deshipu » Mon Aug 01, 2016 12:42 pm

fdufnews wrote:May be you can consider using the export function of Gimp. Using the pbm export filter gives you a binary file with a short header. For example, a 128x64 image generates a 1073 bytes file.
The file has 49 bytes of header and following it the image is encoded eight pixels in a byte.
You can copy the file to the flashdisk and open it when necessary and just copy the data into the buffer no need to put the image in your code.
You can also export to .h or .c files, in which case you will get arrays with the data.

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

Re: ssd1306 using I2C on the esp8266

Post by pythoncoder » Tue Aug 02, 2016 5:57 am

Creating a .py file and freezing it as bytecode has the advantage of not using RAM for the buffer.
Peter Hinch
Index to my micropython libraries.

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: ssd1306 using I2C on the esp8266

Post by mcauser » Tue Aug 02, 2016 3:32 pm

This is a WiFi module. One could even http get to save RAM :)

MicroPythoneer
Posts: 2
Joined: Sat Aug 13, 2016 6:18 pm

Re: ssd1306 using I2C on the esp8266

Post by MicroPythoneer » Sat Aug 13, 2016 7:42 pm

[quote="mcauser"]@deshipu I'd like to test your modifications to the ssd1306 driver on my WeMos OLED shield (64x48).
I think I am missing a step.

[code]
>>> import ssd1306
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'ssd1306'
[/code]

Do I need to copy/symlink the driver from /drivers/display/ssd1306.py into /esp8266/scripts and make/flash again to make it work?
Is that the same for all scripts in /drivers?[/quote]


I've got the same issue however it seems you solved it.

How exactly did you get the ssd1306 module onto the ESP8266 so that you can use the import ssd1306 without an error? I just want to drive my OLED using MicroPython and a ESP8266

I'm new to all of this, thanks!

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: ssd1306 using I2C on the esp8266

Post by torwag » Sun Aug 14, 2016 11:04 pm

you "simply" have to copy the file directly onto the esp. E. g. using webrepl. After that you can call it via import. Remember for webrepl you have to establish a network connection first and start webrepl on the esp. Sounds tricky but it is well described in the docs.

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: ssd1306 using I2C on the esp8266

Post by mcauser » Sun Aug 21, 2016 10:34 pm

Yes, either
a) copy /drivers/display/ssd1306.py to /esp8266/scripts/ssd1306.py and make/flash
b) start a webrepl and upload the file
c) enter paste-mode on a REPL and paste the contents of the file

joehunt588
Posts: 26
Joined: Wed Jul 27, 2016 5:06 am

Re: ssd1306 using I2C on the esp8266

Post by joehunt588 » Sun Oct 09, 2016 12:59 am

deshipu wrote:I didn't save the snippets that I normally use, but here's one that I recently used to generate data for a 2-bit font:

Code: Select all


import pygame

colors = {
    (0, 0, 0, 255): 0,
    (102, 102, 102, 255): 1,
    (204, 204, 204, 255): 2,
    (255, 255, 255, 255): 3,
}
image = pygame.image.load("font.png")
images = []

for tile_x in range(0, image.get_size()[0]/4):
    rect = (tile_x * 4, 0, 4, 6)
    images.append(image.subsurface(rect))
    
for image in images:
    print '(%s),' % ', '.join('%d' %
        sum(colors[tuple(image.get_at((x, y)))] << (x * 2)
            for x in range(4))
        for y in range(6))
Hai deshipu,can i have file 'font.png' for the code im still learn about pixel and need example :)

Post Reply