I want to print text to an OLED display connected to the OpenMV M7.
I’m using Adafruit’s 128 x 32 SPI SSD1306 OLED: Monochrome 128x32 SPI OLED graphic display : ID 661 : $17.50 : Adafruit Industries, Unique & fun DIY electronics and kits.
There’s an Adafruit MicroPython SSD1306 OLED driver I’m importing into OpenMV IDE. This is the SPI section of the adafruit_ssd1306.py driver file:
class SSD1306_SPI(_SSD1306):
"""
SPI class for SSD1306
:param width: the width of the physical screen in pixels,
:param height: the height of the physical screen in pixels,
:param spi: the SPI peripheral to use,
:param dc: the data/command pin to use (often labeled "D/C"),
:param reset: the reset pin to use,
:param cs: the chip-select pin to use (sometimes labeled "SS").
"""
# pylint: disable=no-member
# Disable should be reconsidered when refactor can be tested.
def __init__(self, width, height, spi, dc, reset, cs, *,
external_vcc=False, baudrate=8000000, polarity=0, phase=0):
self.rate = 10 * 1024 * 1024
dc.switch_to_output(value=0)
self.spi_device = spi_device.SPIDevice(spi, cs, baudrate=baudrate,
polarity=polarity, phase=phase)
self.dc_pin = dc
self.buffer = bytearray((height // 8) * width)
super().__init__(memoryview(self.buffer), width, height,
external_vcc=external_vcc, reset=reset)
def write_cmd(self, cmd):
"""Send a command to the SPI device"""
self.dc_pin.value = 0
with self.spi_device as spi:
spi.write(bytearray([cmd]))
def write_framebuf(self):
"""write to the frame buffer via SPI"""
self.dc_pin.value = 1
with self.spi_device as spi:
spi.write(self.buffer)
This is the section of my main script where I import adafruit_ssd1306:
from pyb import Pin
dc = Pin('P8', Pin.OUT_PP)
reset = Pin('P7', Pin.OUT_PP)
cs = Pin('P6', Pin.OUT_PP)
import adafruit_ssd1306
oled = adafruit_ssd1306.SSD1306_SPI(128, 32, spi, dc, reset, cs)
oled.fill(0)
oled.text("Hello World", 0, 0)
oled.show()
When I run the script, I get the error “AttributeError: ‘Pin’ object has no attribute ‘switch_to_output’”.
In the driver code above, there’s the line " dc.switch_to_output(value=0)". It looks like the “switch_to_output” attribute is in a digitalio module that is a core module of Adafruit CircuitPython and not in OpenMV IDE.
I changed the
dc.switch_to_output(value=0)
line to
dc = Pin('P8', Pin.OUT_PP)
and then got the same “AttributeError: ‘Pin’ object has no attribute ‘switch_to_output’”, but this time it highlighted the line
self.write_cmd(SET_COL_ADDR)
in adafruit_ssd1306.py.
I’m not sure how to get around the digitalio dependencies- is there an easy way around this? I’m new to Python/Micropython.