0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspbian Stretch Lite (CUI のみの環境で) Docker コンテナにいる Julia の グラフ描画結果を fbi によって描画する

0
Last updated at Posted at 2019-04-06

本日は

先日は Raspbian Stretch with desktop (要するに普通のPCの感覚でGUIが使える環境) を OS とする RaspberryPi3 上で Julia 環境を Docker を用いて構築しました.

もちろんCUIの環境 Raspbian Stretch Lite でも動かすことができます。
今回はCUIの環境で動かしてみましょう。そして画像などを画面上に描画してみましょう。

環境構築(前提)

ラズパイ布教用にセットアップも書いておこうと思います。

sentdexさんのYouTubeのチュートリアル も参考になります。

Raspbian の導入

Raspbian Stretch Lite のイメージをダウンロードします。なんか3つあるんだけれど(´・ω・`)と戸惑いますが左下の Lite のやつを選択します。Download ZIPを押します。

image.png

これを解凍したイメージをマイクロSDカードに焼きます。個人的には Etcherが便利だと感じます。 Windows, Mac, Ubuntuどれでも使えてます。

ラズパイにSDカードを指して起動すると見慣れた黒いターミナルが開くと思います。これでベース環境が整いました。

Docker の導入はこちらに書きました。

HDMIでモニターと接続

ラズパイをメイン機として使うなど、キーボード、モニターを直接ラズパイに接続して運用する場合を想定しています。
画像描画はフレームバッファを制御することで実現させる方法がよく知られているようです。

Dockerを使わない場合

$ sudo apt install fbi
# 引数やオプションを確認
$ fbi -help
This program displays images using the Linux framebuffer device.
Supported formats: PhotoCD, jpeg, ppm, gif, tiff, xwd, bmp, png,
webp. It tries to use ImageMagick's convert for unknown file formats.

usage: fbi [ options ] file1 file2 ... fileN

    -h  -help               print this help text 
    -V  -version            print fbi version number 
        -store              write cmd line args to config file 
    -l  -list <arg>         read image filelist from file <arg> 
    -P  -text               switch into text reading mode 
    -a  -autozoom           automagically pick useful zoom factor 
        -(no)autoup           like the above, but upscale only      
        -(no)autodown         like the above, but downscale only    
        -(no)fitwidth         use width only for autoscaling        
    -v  -(no)verbose        show filenames all the time             
    -u  -(no)random         show files in a random order            
    -1  -(no)once           don't loop (for use with -t)            
        -(no)comments       display image comments                  
    -e  -(no)edit           enable editing commands (see man page)  
        -(no)backup           create backup files when editing      
        -(no)preserve         preserve timestamps when editing      
        -(no)readahead      read ahead images into cache            
        -cachemem <arg>     image cache size in megabytes           
        -blend <arg>        image blend time in miliseconds         
    -T  -vt <arg>           start on virtual console <arg>          
    -s  -scroll <arg>       scroll image by <arg> pixels            
    -t  -timeout <arg>      load next image after <arg> sec without user input 
    -r  -resolution <arg>   pick PhotoCD resolution (1..5)          
    -g  -gamma <arg>        set display gamma (doesn't work on all hardware) 
    -f  -font <arg>         use font <arg> (anything fontconfig accepts) 
    -d  -device <arg>       use framebuffer device <arg>            
    -m  -mode <arg>         use video mode <arg> (from /etc/fb.modes) 

Large images can be scrolled using the cursor keys.  Zoom in/out
works with '+' and '-'.  Use ESC or 'q' to quit.  Space and PgDn
show the next, PgUp shows the previous image. Jumping to a image
works with <i>g.  Return acts like Space but additionally prints
prints the filename of the currently displayed image to stdout.
# さぁ画像を描画してみよう
$ fbi -T 1 nanikano_gazou.png

fim というのもあるみたいですね。

どちらでもいいと思います。

Dockerを使う場合

あまり情報がなかったのでいろいろトライアンドエラーをして以下のように落ち着きました。

$ cat run_container.sh
sudo docker run --rm -it --privileged \
-v ${PWD}:/work \
-w /work \
terasakisatoshi/myjulia:linux bash
$ bash run_container.sh
root@xxxxxxx # julia
julia > savefig(plot(sin, -pi, pi), "sin_curve.png")
julia> exit()
root@xxxxxxx # fbi -T 1 -t 1 -1 sin_curve.png

/dev/tty1 へのパーミッション問題で苦しみました.
-d でオプションで通してもいいんですが、fbi 実行ご画面が真っ暗でなんもできない。最終的には
--privileged オプションを有効にすることで落ち着きましたがまだモヤモヤする。

fbi コマンドで -t 1 -1 をする理由はこのオプションをつけておかないと画像を描画した後元のターミナルに戻ってこない場合があるからです。 -1 がミソっぽい。 Docker使わない場合はスムーズに切り替えられるんですけどね・・・。
一方で fim を使おうとすると真っ暗な画面しかでないので今は諦めています。

動かしている様子

image.png

image.png

shell でラップする

こんな感じの run.sh をつくっておいて

# !/bin/bash
sudo docker run --rm -it --privileged \
-v ${PWD}:/work \
-w /work \
terasakisatoshi/myjulia:rpi3 "$@"

$ chmod +x run.sh で実行権限与えておけばコンテナの中でやりたいことを ./run.sh やりたいこと で実現できます。

# ラズパイのターミナルの中で操作

# Julia のコードを動かす
$ ./run.sh julia somefile.jl
# Python のコードを動かす
$ ./run.sh python somefile.py
# 対話コンソールが出る
$ ./run.sh bash
# PythonのREPLがでる
$ ./run.sh python
# Julia のREPLがでる
$ ./run.sh julia
# これでもよい
$ ./run.sh

SSH で操作する

# Mac terminalで操作。 -X オプションを忘れずに!
$ ssh -X pi@raspberrypy.local
# on RPI3 terminal
$ cat run_container.sh
docker run --rm -it \
-e DISPLAY=$DISPLAY \
-v $HOME/.Xauthority:/root/.Xauthority:rw \
--net host \
-v $(pwd):/work \
-w /work \
terasakisatoshi/myjulia:rpi3 bash
$ bash run_container.sh
# imagemagick の機能を使う
root@xxxxxx# display your_image.png

Dockerの引数は次のリンク先を参考にしました。

個人的いろいろチップとデール

Font size を変える

Full HD のモニターだと文字が小さいのでサイズを変更したいお気持ちがあったので

How To Change The Command Line Font Size

/etc/default/console-setup を開いて下記のように設定します。

FONTFACE="Terminus"
FONTSIZE="16x32"

Caps lock キーを Ctrl にする

/etc/default/keyboard を nano などのエディタで XKBOPTIONS を下記のように設定する。

XKBOPTIONS="ctrl:nocaps"

nano のシンタックスハイライト

Julia 用の nanorc を作っている人がいるみたい。うごきました。

私は平和主義なのでエディタは nano でいいんじゃないかな(遠い目)。

Vim, Emacs で環境構築、バリバリ開発できるためのエディタブートキャンプあったら入ってみたいですね。

micro editor というものがあるらしい

最近知りました。

で早速なんとなくこんな 感じのキーバインディング にしてみました。

さいきんいーまっくすもなれてきた

こんなかんじのせってい
でテーマを変えることができるようです。ファイル名は hoge-theme.el という命名で管理しないといけないです。

ラズパイのCUIにおいてEmacsで表示できる色を M-x list-color-display でみると
black, red, green, yellow, blue, magenta, cyan, white となかなか制限のあるテーマの設定をする必要があることがわかりました。(回避策ありましたら喜ぶ人が増えると思います。)

ターミナルウィンドウを調節したい

GNU screen を用いています。 screen vs tmux 的なのもあるみたいですが、正直なところどっちでもいいともいます。

screen はモニターが一枚という物理制約の中で情報量を多く取るのに便利ですね。

~/.screenrc に色々と設定を書き込んでいます 例えばこんな感じ.

vbell off

をしておくと screen 起動中に何かコマンドミスやエラーが起きた時に画面がチカチカする(画面を点滅させて視覚的にベルを鳴らす)のを抑えるのに役立ちます。ベルは結構目を刺激するのでOFFにしておいた方が良いでしょう。(英語で visual bell という概念があるのを知りました)。

SSHするとき IP 忘れた

複数台LANに接続していなければこれで問題ない.

$ ssh pi@raspberrypi.local

複数台管理するときはHostnameを変更することをおすすめします。

$ ssh pi@henkousitahostname.local

的なので行けるはず。

ラズパイ以外のPCからファイルを編集したい

sshfs を知った時は感動しましたね。

# MacやUbuntuのターミナルで操作する
$ mkdir rpiwork
$ sshfs pi@raspberrypi.local:/home/pi/work rpiwork
$ cd rpiwork
$ ls
ラズパイに入っているファイルが列挙される

最近だとVSCodeでいろいろ操作なんてのもあるみたいです。

ソフトウェア学習コスト高くない?

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?