Adsense HTML/JavaScript

Monday, October 11, 2021

CircuitPython 7 exercises on STM32F411, with SSD1306 I2C OLED

This post show how to run CircuitPython code on STM32F411, display on SSD1306 I2C 128x64 OLED using adafruit_ssd1306 library.

It's assumed CircuitPython 7.0.0 is installed on STM32F411 minimum development board, a STM32F411CE Black Pill-like board. To install CircuitPython firmware on STM32F411, refer to the post "Flash CircuitPython to STM32F411, and install Thonny IDE on Ubuntu 20.10".

Connection

SSD1306 I2C	STM32F411
-------------------------
GND		GND
VCC		3V3
SCL		B6
SDA		B7

Library

To run the exercise code,  Adafruit CircuitPython Library Bundle's adafruit_ssd1306 and font5x8.bin is need.

Visit https://circuitpython.org/libraries, download the appropriate bundle (adafruit-circuitpython-bundle-7.x-mpy- here) for your version of CircuitPython.

Unzip the file, copy adafruit_ssd1306.mpy, and adafruit_framebuf.mpy from the "lib" folder to the lib folder on your CIRCUITPY drive. 

And copy font5x8.bin from the unzipped "examples" folder to CIRCUITPY driver.

Exercise code

cpyF411_scanI2C.py, scan the address of connected I2C devices.
import os
import busio
import board

for u in os.uname():
    print(u)    
print()

#Assign I2C:
#https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/index.html#board.I2C
print(dir(board))
print("SCL: ", board.SCL)
print("SDA: ", board.SDA)
i2c = board.I2C()
print(i2c)
print()

#Scan I2C devices
if(i2c.try_lock()):
    print("i2c.scan(): " + str(i2c.scan()))
    i2c.unlock()
print()


cpyF411_SSD1306.py
from os import uname
from board import SCL, SDA
from sys import implementation as sysImplementation
import busio
import time

import adafruit_ssd1306


# Create the I2C interface and display object of SSD1306_I2C.
i2c = busio.I2C(SCL, SDA)
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)

for u in uname():
    print(u)
print()
for s in sysImplementation:
    print(s)
print()

print(display)
print("display.width x height: ", display.width, " x ", display.height)
print(dir(display))
    
display.fill(0)
display.show()
time.sleep(1)
display.fill(1)
display.show()
time.sleep(1)

display.fill(0)


strSys = sysImplementation[0] + ' ' + \
         str(sysImplementation[1][0]) +'.'+ \
         str(sysImplementation[1][1]) +'.'+ \
         str(sysImplementation[1][2])

strLib = adafruit_ssd1306.__name__ + '\n' + adafruit_ssd1306.__version__

def drawInfo():
    display.text(strSys, 0, 0, 1)
    display.text(strLib, 0, 10, 1)
    display.text("STM32F411", 0, 30, 1)
    display.text("SSD1306", 0, 40, 1)
    strResolution = str(display.rotation) + ' : ' + str(display.width) + ' x ' + str(display.height)
    display.text(strResolution, 0, 50, 1)
    
for r in range(0, 4):
    display.fill(0)
    display.rotation = r
    drawInfo()
    display.show()
    time.sleep(3)

display.rotation = 0

#draw rectangle
display.fill(0)
display.rect(0, 0, display.width, display.height, 1)
display.show()
time.sleep(1)
display.fill(0)
display.fill_rect(0, 0, display.width, display.height, 1)
display.show()
time.sleep(1)

#draw circle
display.fill(0)
if display.width > display.height:
    r = (int)(display.height/2)
else:
    r = (int)(display.width/2)
display.circle((int)(display.width/2), (int)(display.height/2), r, 1)
display.show()
time.sleep(1)

display.fill(0)
display.show()

# draw pixels
for y in range(0, display.height, 8):
    for x in range(0, display.width, 8):
        display.pixel(x, y, 1)
        display.show()
time.sleep(1)
display.invert(1)
time.sleep(2)
display.invert(0)
time.sleep(1)

No comments:

Post a Comment