TheRayTracer
Posts: 51
Joined: Tue Oct 13, 2015 12:23 am
Location: Australia

SSD1306 and SSD1331 Python device modules

Thu Dec 31, 2015 3:14 am

Hi!

I've created a few stand alone Python modules to help drive the SSD1306 and SSD1331 chipsets using the SPI. The primary reason was just to tinker, but I thought others could benefit from these too (I've already had a few requests on this forum and others). I also feel that these single file modules are a little easier, than trying to juggle Adafruit's GFX and OLED driver libraries together.

After wiring up a SSD1306 chipset, all you need to do is create an instance of the SSD1306 class specifying the GPIOs for the CS, D/C, and Reset pins. Then call the required member functions to draw your primitives, and finally call the Flip() member function to send the internal pixel buffer to the display.

For example:

Code: Select all

device = SSD1306(SSD1306_PIN_DC, SSD1306_PIN_RST, SSD1306_PIN_CS)
    try:
        device.EnableDisplay(True)
        device.Clear()
        device.DrawCircle(20, 32, 16)
        device.DrawTriangle(47, 48, 79, 48, 63, 16)
        device.DrawRect(90, 16, 122, 48)
        device.Flip()
        time.sleep(60)
        device.EnableDisplay(False)
    finally:
        device.Remove()
Example output from an ASCII font test:

Image

Please note that the SSD1306 used for testing has two pages (16 pixel rows) of yellow/orange pixels along the bottom (or top) of the display.

The Python source code can be found here: https://gist.github.com/TheRayTracer/33 ... 4beac9cc37

If you are using a SSD1331 chipset, then you are in for more fun. Colours are supported! Again, after wiring up a SSD1331 chipset, all you need to do is create an instance of the SSD1331 class specifying the GPIOs for the CS, D/C, and Reset pins. Same as before, right? Then call the required member functions to draw your primitives (this time many of them are hardware accelerated).

A quick example:

Code: Select all

    device = SSD1331(SSD1331_PIN_DC, SSD1331_PIN_RST, SSD1331_PIN_CS)
    try:
        device.EnableDisplay(True)
        device.Clear()
        time.sleep(0.01) # Allow some time for the screen to wipe clean.
        device.DrawStringBg(18, 4, "Shape Test", COLOR_BLACK, COLOR_WHITE)
        device.DrawCircle(16, 32, 10, COLOR_RED)
        device.DrawTriangle(38, 42, 58, 42, 48, 22, COLOR_GREEN)
        device.DrawRect(70, 22, 90, 42, COLOR_BLUE)
        time.sleep(60)
        device.EnableDisplay(False)
    finally:
        device.Remove()
The Python source code can be found here: https://gist.github.com/TheRayTracer/dd ... ecb9b8b47f

Within the source code I've also included the beginnings of a Space Invaders game to demonstrate usage of the above SSD1331 module. At the moment the game is all AI controlled and is non-interactive.

Image

Image

All of this is a work in progress, and I'll provide updates as they are made. Suggestions, comments, and questions are more than welcome.

tony1812
Posts: 337
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

Re: SSD1306 and SSD1331 Python device modules

Mon Feb 12, 2018 8:23 pm

Hello, I just got a SSD1331 old. I found your post. I downloaded your code, but once I try to run it, I got the error No module named SSD1331. Could you pleas tell me how to install SSD1331? Thanks

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 8 guests