LoginSignup
2
1

More than 1 year has passed since last update.

micro:bit v2 の MicroPython で OLED を使う(framebuf追加編)

Last updated at Posted at 2021-12-09

micro:bit Advent Calendar 2021の7、8日目が空いてしまっていたので、小ネタとして micro:bit V2 で OLED (SSD1306)を使えるようにしてみました。

世の中には既に microbit_ssd1306というライブラリがあるのですが、framebuf を使ったものにしたかったので MicroPython の拡張に挑戦してみました。

改造とビルド

MicroPython ビルド方法はここを参照してください。

micro:bit V2 用の MicroPython 実装は、本家 MicroPython のソースをサブモジュールとして使っていますし、framebuf は所詮メモリ上でコニョゴニョするだけなのでデバイス依存性は少ないだろうと調べたのですが、なんと改造箇所は src/codal_port/mpconfigport.hに1行追加するだけでした。

--- mpconfigport.h.org  2021-12-09 21:57:53.000000000 +0900
+++ mpconfigport.h  2021-12-09 22:08:48.000000000 +0900
@@ -86,6 +86,7 @@
 #define MICROPY_PY_URANDOM_EXTRA_FUNCS          (1)
 #define MICROPY_PY_MACHINE                      (1)
 #define MICROPY_PY_MACHINE_PULSE                (1)
+#define MICROPY_PY_FRAMEBUF                     (1)

 #define MICROPY_HW_ENABLE_RNG                   (1)
 #define MICROPY_MBFS                            (1)

この1行を追加して、手順どおりにビルドするだけです。できあがったファームウェア MICROBIT.hex を MICROBIT フォルダにコピーすればインストール完了です。

動かしてみる

REPL の利用には mpremoteを使ってみました。

$ mpremote 
Connected to MicroPython at /dev/cu.usbmodem14202
Use Ctrl-] to exit this shell

>>> 
MPY: soft reboot
MicroPython v1.15-64-g1e2f0d280 on 2021-12-09; micro:bit v2.0.0 with nRF52833
Type "help()" for more information.
>>> help('modules')
__main__          love              os                urandom
antigravity       machine           radio             ustruct
audio             math              speech            usys
builtins          microbit          this              utime
framebuf          micropython       uarray
gc                music             ucollections
log               neopixel          uerrno
Plus any modules on the filesystem
>>>

ちゃんと framebuf モジュールがあります。

>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import framebuf
=== 
=== fbuf = framebuf.FrameBuffer(bytearray(64 // 8 * 128), 128, 64, framebuf.MONO_VLSB)
=== 
=== fbuf.fill(0)
=== fbuf.text('MicroPython!', 0, 0, 0xffff)
=== fbuf.hline(0, 9, 96, 0xffff)
>>> 

使えているようです。

では、この framebuf モジュールを使った ssd1306モジュールを使ってみますが、8日目のネタとします。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1