Created
April 18, 2025 02:54
-
-
Save smd877/e0915fa6199e48219bb8d6e69beb9226 to your computer and use it in GitHub Desktop.
Pimoroniの電子ペーパーに日本語を表示する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Pimoroniの電子ペーパーに日本語を表示する メインプログラム | |
PicoGraphicsの利用方法は以下を参考にした | |
https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/modules/picographics | |
ボタン操作etcについては以下を参考 | |
https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/pico_inky/show_ip_address | |
採用したディスプレイの公式URLは以下 | |
https://shop.pimoroni.com/products/pico-inky-pack | |
""" | |
from pimoroni import Button | |
from picographics import PicoGraphics, DISPLAY_INKY_PACK | |
import jpegdec | |
from inky_pack_mfont import drawTextLine, drawTextArea, FONT_SIZE | |
# ディスプレイの設定関係 | |
display = PicoGraphics(DISPLAY_INKY_PACK) # Pico Inky Pack | |
display.set_update_speed(1) # 表示リフレッシュの速度 0:超遅い - 3:超早い | |
display.set_font("bitmap8") # デフォルトのフォントを設定しておく | |
# ボタン設定 | |
button_a = Button(12) | |
button_b = Button(13) | |
button_c = Button(14) | |
# サンプル用のメッセージ | |
msg_en_1 = "Hello World." | |
msg_en_2 = "Japanese cannot be displayed." | |
msg_jp_1 = "日本語の表示検証:メッセージが長いと途中で見切れる。" | |
msg_jp_2 = """改行テスト。 | |
ここにテキストが表示されます。 | |
ここにもテキストが表示されますが、文章が長いため折り返されます。""" | |
# ディスプレイをクリアする | |
def clear(): | |
display.set_pen(15) # 数値の意味は 0が黒、15が白。 | |
display.clear() | |
while True: | |
if button_a.read(): # Aボタン押下時 : picographics標準メソッドで英文表示 | |
clear() | |
display.set_pen(0) | |
display.text(msg_en_1, 0, 0, scale=2) | |
display.text(msg_en_2, 8, 32, scale=2) | |
display.update() | |
elif button_b.read(): # Bボタン押下時 : mfontを使って日本語表示 | |
clear() | |
display.set_pen(0) | |
drawTextLine(msg_jp_1, 0, FONT_SIZE * 0, display) | |
drawTextArea(msg_jp_2, FONT_SIZE * 1, FONT_SIZE * 2, display) | |
display.update() | |
elif button_c.read(): # Cボタン押下時 : jpg画像を表示 | |
clear() | |
j = jpegdec.JPEG(display) | |
j.open_file("sample.jpg") | |
j.decode(0, 0, jpegdec.JPEG_SCALE_FULL, dither=True) | |
display.update() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Pimoroniの電子ペーパーに日本語を表示する 表示制御関連 | |
フォントで使わせていただいているもの | |
https://github.com/Tamakichi/pico_MicroPython_Multifont | |
サンプルプログラム③ を参考にした | |
PicoGraphicsのpixelメソッドを利用することでドット単位での印字を行う。 | |
https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/modules/picographics | |
""" | |
from picographics import PicoGraphics, DISPLAY_INKY_PACK | |
from mfont import mfont | |
# Multifontのインスタンスを生成しておく | |
FONT_SIZE = 16 | |
mf = mfont(FONT_SIZE) | |
# フォントの表示 | |
def drawFont(font, x, y, w, h, display): | |
bn = (w + 7) >> 3 | |
py = y | |
for i in range(0, len(font), bn): | |
px = x | |
for j in range(bn): | |
for k in range(8 if (j + 1) * 8 <= w else w % 8): | |
if font[i + j] & 0x80 >> k: | |
display.pixel(px + k, py) | |
px += 8 | |
py += 1 | |
# テキストの表示(1行) | |
def drawTextLine(text, x, y, display): | |
mf.begin() | |
for c in text: | |
code = ord(c) | |
font = mf.getFont(code) | |
drawFont(font, x, y, mf.getWidth(), mf.getHeight(), display) | |
x += mf.getWidth() | |
mf.end() | |
# テキストの表示(複数行) | |
def drawTextArea(text, x, y, display): | |
# PicoGraphicsのget_boundsでディスプレイの縦横サイズを取得できる | |
WIDTH, HEIGHT = display.get_bounds() | |
tmp_x = x | |
mf.begin() | |
for c in text: | |
code = ord(c) | |
font = mf.getFont(code) | |
if c == "\n": # 改行コードの処理 | |
tmp_x = x | |
y = y + mf.getHeight() | |
continue | |
if tmp_x + mf.getWidth() > WIDTH: # 文字がディスプレイ幅を超える場合 | |
tmp_x = x | |
y = y + mf.getHeight() | |
drawFont(font, tmp_x, y, mf.getWidth(), mf.getHeight(), display) | |
tmp_x += mf.getWidth() | |
mf.end() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment