Grove-OLED-i2cをFramebufferモジュールで機能拡張する
2020/4/24
MCP2221A adafruit_blinka Grove-OLED128x128-i2c Framebuffer
MCP2221A adafruit_blinka Grove-OLED128x128-i2c Framebuffer
概要
MCP2221Aボードで以下のGrove-OLED-i2cをFramebufferモジュールで機能拡張する。本記事は「MCP2221AボードでGrove-OLED-i2cを動かす」の続きになる。
本ボードは、ボードのバージョンによって、解像度が96x96または128x128であり、それぞれでコントローラも異なる。 入手したボードは、v1.1で解像度は128x128でコントローラはSH1107Gであった。(Vdd:3.3V/5V両用対応)
接続
MCP2221Aボードのi2cに本件のOLEDを接続する。(Vdd=3.3V)
モジュールのインストール
cd ~/mcp2221a_ws
# 仮想環境に切り替える
source blinka/bin/activate
# sh1107g_cpy.pyをダウンロードする
wget https://beta-notes.way-nifty.com/blog/files/sh1107g_cpy.py
# adafruit_framebufをインストールする
pip3 install adafruit-circuitpython-framebuf
# framebufで使用するフォントをダウンロードする
wget https://github.com/adafruit/Adafruit_CircuitPython_framebuf/blob/master/examples/font5x8.bin
デモ・スクリプト
test_fb_sh1107g.py
# for CircuitPython
import time
import busio
import board
i2c = busio.I2C(board.SCL, board.SDA)
i2c.try_lock()
from sh1107g_cpy import SH1107G
oled = SH1107G(i2c)
oled.clear()
#-----------------------------
# framebuffer related
import adafruit_framebuf
# pixel size for display
WIDTH = 128
HEIGHT = 128
fbuf = bytearray(round(WIDTH * HEIGHT / 8))
fb = adafruit_framebuf.FrameBuffer(fbuf, WIDTH, HEIGHT,
buf_format=adafruit_framebuf.MVLSB)
#-------------------------------
# (for debug) Ascii printer for very small framebufs!
def print_buffer(the_fb):
print("." * (the_fb.width+2))
for y in range(the_fb.height):
print(".", end='')
for x in range(the_fb.width):
if fb.pixel(x, y):
print("*", end='')
else:
print(" ", end='')
print(".")
print("." * (the_fb.width+2))
# clear framebuffer
def clear_fbuf():
for i, _ in enumerate(fbuf):
fbuf[i] = 0
#----------------------------------
print("Text test: ")
clear_fbuf()
fb.text("hello world! #0", 0, 0, True)
fb.text("Integer:{} #1".format(1234), 0, 8, True)
fb.text("Float:{} #2".format(12.34), 0, 16, True)
fb.text("1234567890-^\\ #3", 0, 24, True)
fb.text("!\"#$%&'()~=~| #4", 0, 32, True)
fb.text("ABCDEFGHIJKLM #5", 0, 40, 1)
fb.text("OPQRSTUVWXYZ #6", 0, 48, 1)
fb.text("abcdefghijklm #7", 0, 56, 1)
fb.text("opqrstuvwxyz #8", 0, 64, 1)
fb.text(",./\;:[] #9", 0, 72, 1)
fb.text("<>?_+*{} #10", 0, 80, 1)
fb.text("Hello World! #11", 0, 88, 1)
fb.text("hello world! #12", 0, 96, 1)
fb.text("Hello World! #13", 0, 104, 1)
fb.text("hello world! #14", 0, 112, 1)
fb.text("Hello World! #15", 0, 120, 1)
# show display
oled.draw(fbuf, len(fbuf))
time.sleep(4)
print("Shapes test: ")
clear_fbuf()
fb.pixel(13, 5, 1)
fb.rect(0, 0, fb.width, fb.height, 1)
fb.line(1, 1, fb.width-2, fb.height-2, 1)
fb.fill_rect(25, 4, 4, 5, 1)
fb.circle(64,64,20,1)
fb.hline(20,30,40,1)
fb.vline(20,30,40,1)
fb.scroll(10,20)
# show display
oled.draw(fbuf, len(fbuf))
time.sleep(4)
print("larger text test: ")
clear_fbuf()
fb.text("Hello", 8, 0, 1, size = 4)
# show display
oled.draw(fbuf, len(fbuf))
実行
PCの場合
# 仮想環境を有効化する
source blinka/bin/activate
# 実行する
export BLINKA_MCP2221=1
python3 test_fb_sh1107g.py
CircuitPythonの場合
REPL:
import test_fb_sh1107g
なお、CircuitPythonで実行する場合、 以下の4つのファイルをCIRCUITPYにコピーする:
(1)sh1107g_cpy.py
(2)blinka/lib/python3.6/site-packages/adafruit_framebuf.py
# 「python3.6」の部分は、ホストPCの状況に合わせる
(3)font5x8.bin
(4)test_fb_sh1107g.py
動作実績
(1)MCP2221Aボード
正常に動作した(Vdd=3.3V)
(2)CircutPython Teensy4.0
正常に動作した(Vdd=3.3V)
(3)FT232Hボード
i2c.scan()でデバイス・アドレスが返ってこない。(動作せず)(Vdd=5V)
デバイスの仕様としては、5V/3.3V両用対応になっているが本ボードでは動作しないようだ。
Framebufferの関数リスト
circle(center_x, center_y, radius, color)
Draw a circle at the given midpoint location, radius and color. The `circle` method draws only a 1 pixel outline.
fill(color)
Fill the entire FrameBuffer with the specified color.
fill_rect(x, y, width, height, color)
Draw a rectangle at the given location, size and color. The fill_rect method draws both the outline and interior.
hline(x, y, width, color)
Draw a horizontal line up to a given length.
image(img)
Set buffer to value of Python Imaging Library image. The image should be in 1 bit mode and a size equal to the display size.
line(x_0, y_0, x_1, y_1, color)
Bresenham’s line algorithm
pixel(x, y, color=None)
If color is not given, get the color value of the specified pixel. If color is given, set the specified pixel to the given color.
rect(x, y, width, height, color, *, fill=False)
Draw a rectangle at the given location, size and color. The `rect` method draws only a 1 pixel outline.
rotation
The rotation setting of the display, can be one of (0, 1, 2, 3)
scroll(delta_x, delta_y)
shifts framebuf in x and y direction
text(string, x, y, color, *, font_name='font5x8.bin', size=1)
Place text on the screen in variables sizes. Breaks on to next line.
Does not break on line going off screen.
vline(x, y, height, color)
Draw a vertical line up to a given length.
追記:
colorは、モノクロの場合、1またはTrueになる。
参照情報
Framebuffer API
Famebuffer docs (MicroPython-based)
Python library for Seeedstudio Grove devices
Arduino Library for this Grove - OLED Display 1.12inch
download datasheet SH1107_v2.1.pdf
USB-I2CブリッジボードV2(GROVE対応ボード)【MR-GROVE-B】
USB-I2CブリッジボードV2(GROVE対応ブレッドボード版)【MR-GROVE-BB】
MCP2221 Blinka Pinout
CircuitPython Libraries on any Computer with MCP2221
CircuitPython on Linux and Raspberry Pi
CircuitPython 5.0.x - I2C
PCのpython3でCircuitPythonのAPIを使用する(FT232H版)
PCのpython3でCircuitPythonのAPIを使用する(MCP2221A版)
以上
| 固定リンク
「linux」カテゴリの記事
- platfomioを使ってnaitive(linux/windows)のプログラムをビルドする方法(2021.03.10)
- micro:bit Yotta開発ツール(linux版)(2021.01.09)
- PlatformIOをRaspberryPi4で動かしてみる(実験)(2020.10.20)
- headless RaspberryPiインストール方法(v2)(2020.10.20)
- wio-terminalのファームウェア・アップデートについて(linux版)(2020.10.15)
「CircuitPython」カテゴリの記事
- MicroPython/CircuitPython Performance Test(2021.02.07)
- PicoボードにMicropython/CircuitPythonをインストールする(2021.02.03)
- XIAOでMPL3115A2(気圧・高度・気温センサ)を使用する(CircuitPython版)(2020.07.02)
- XIAOでMCP9808(温度センサ)を使用する(CircuitPython版)(2020.07.02)
- XIAOにCircuitPythonをインストールする(2020.06.09)
「SH1107G」カテゴリの記事
- XIAO/M5AtomでOLED128x128(I2C)を制御する((XIAO/Arduino版、M5Atom/Arduino版)(2020.10.15)
- Grove-OLED-i2cにFramebufferモジュールを使ってイメージを表示する(2020.04.25)
- Grove-OLED-i2cをFramebufferモジュールで機能拡張する(2020.04.24)
- MCP2221AボードでGrove-OLED-i2cを動かす(2020.04.23)
「Framebuffer」カテゴリの記事
- Grove-OLED-i2cをFramebufferモジュールで機能拡張する(2020.04.24)
コメント