このドキュメンテーションは、MicroPython の最新開発ブランチのためのものです。 リリースバージョンでは利用できない機能に言及することがあります。

特定のリリースのドキュメントをお探しの場合は、左側のドロップダウンメニューを使って、 望みのバージョンを選択します。

14. SSD1306 OLED ディスプレイの使い方

SSD1306 OLED ディスプレイには SPI インタフェースのものと I2C インタフェースのものがあります。サイズはいろいろで(128x64, 128x32, 72x40, 64x48)、カラーもいろいろです(白、黄色、青、黄色+青)。

ハードウェア SPI インタフェース:

from machine import Pin, SPI
import ssd1306

hspi = SPI(1)  # sck=14 (scl), mosi=13 (sda), miso=12 (unused)

dc = Pin(4)    # data/command
rst = Pin(5)   # reset
cs = Pin(15)   # chip select: このためのピンが無いモジュールもあります

display = ssd1306.SSD1306_SPI(128, 64, hspi, dc, rst, cs)

ソフトウェア SPI インタフェース:

from machine import Pin, SoftSPI
import ssd1306

spi = SoftSPI(baudrate=500000, polarity=1, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))

dc = Pin(4)   # data/command
rst = Pin(5)  # reset
cs = Pin(15)  # chip select: このためのピンが無いモジュールもあります

display = ssd1306.SSD1306_SPI(128, 64, spi, dc, rst, cs)

I2C インタフェース:

from machine import Pin, I2C
import ssd1306

# using default address 0x3C
i2c = I2C(sda=Pin(4), scl=Pin(5))
display = ssd1306.SSD1306_I2C(128, 64, i2c)

先頭行に Hello World を印字:

display.text('Hello, World!', 0, 0, 1)
display.show()

基本関数:

display.poweroff()     # ディスプレイの電源オフ、ピクセルはメモリに残存
display.poweron()      # ディスプレイの電源オン、ピクセルを再描画
display.contrast(0)    # 暗くする
display.contrast(255)  # 明るくする
display.invert(1)      # 反転
display.invert(0)      # 通常表示
display.rotate(True)   # 180度回転
display.rotate(False)  # 0度回転
display.show()         # FrameBuffer の内容をディスプレイメモリに書き出す

中で使われている FrameBuffer は基本的なグラフィック機能をサポートします:

display.fill(0)                         # スクリーン全体を colour=0 で埋める
display.pixel(0, 10)                    # x=0, y=10 のピクセルを取得
display.pixel(0, 10, 1)                 # x=0, y=10 のピクセルに colour=1 を設定
display.hline(0, 8, 4, 1)               # x=0, y=8, width=4, colour=1 で水平線を描画
display.vline(0, 8, 4, 1)               # x=0, y=8, height=4, colour=1 で垂直線を描画
display.line(0, 0, 127, 63, 1)          # 0,0 から 127,63 に線を描画
display.rect(10, 10, 107, 43, 1)        # 10,10 と 117,53 の間、colour=1 で長方形を描画
display.fill_rect(10, 10, 107, 43, 1)   # 10,10 と 117,53 の間、colour=1 で塗り潰した長方形を描画
display.text('Hello World', 0, 0, 1)    # x=0, y=0, colour=1 でテキストを描画
display.scroll(20, 0)                   # 20 ピクセルだけ右にスクロール

# 現在の FrameBuffer の上に、与えられた座標で別の FrameBuffer を描画
import framebuf
fbuf = framebuf.FrameBuffer(bytearray(8 * 8 * 1), 8, 8, framebuf.MONO_VLSB)
fbuf.line(0, 0, 7, 7, 1)
display.blit(fbuf, 10, 10, 0)           # x=10, y=10, key=0 の上に描画
display.show()

MicroPython のロゴを描画し、テキストを印字します:

display.fill(0)
display.fill_rect(0, 0, 32, 32, 1)
display.fill_rect(2, 2, 28, 28, 0)
display.vline(9, 8, 22, 1)
display.vline(16, 2, 22, 1)
display.vline(23, 8, 22, 1)
display.fill_rect(26, 24, 2, 4, 1)
display.text('MicroPython', 40, 0, 1)
display.text('SSD1306', 40, 12, 1)
display.text('OLED 128x64', 40, 24, 1)
display.show()