LoginSignup
2

More than 1 year has passed since last update.

posted at

updated at

NervesのIExをカスタマイズしたい

NervesのIExをカスタマイズして色んな情報を表示できたらもっと便利になるのではないでしょうか?

customizing-iex-in-nerves 2021-05-08 at 11 06 14 AM

TL;DR

  • Nervesプロジェクト内のrootfs_overlay/etc/iex.exsファイルを探す。
  • rootfs_overlay/etc/iex.exsで自由にElixirプログラミングを楽しむ。

これは一例です。

NervesMOTD.print()

# Add Toolshed helpers to the IEx session
use Toolshed

# I2Cの接続状況を表示
if Code.ensure_loaded?(Circuits.I2C) do
  IO.puts("")
  Circuits.I2C.detect_devices()
end

# お天気情報を表示
try do
  IO.puts("")
  weather()
rescue
  _e -> nil
end

# エイリアスを定義しておくと便利かも
alias HelloNerves.Worker
  • rootfs_overlay/etc/.iex.exsで好きなようにElixirプログラミングするだけです。
  • Toolshedの関数に便利なものがいくつかあります。(例、weather/0uname/0
  • Circuits.I2C.detect_devices/0を呼んで、接続されているI2C周辺機器を表示すると便利な場合があるかもしれません。
  • IExでよく使うモジュールを予めaliasしておけば、後々楽になるかもしれません。
  • weather/0を使用する上での注意点としては、インターネット接続が必要なことと、:inetsmix.exsファイルのextra_applicationに含まれている必要があることです。
  • 万一.iex.exs実行時にでエラーが発生した場合、エラー以降の処理が実行されないので注意が必要です。
lib/hello_nerves/application.ex
def application do
  [
    mod: {HelloNerves.Application, []},
    extra_applications: [:logger, :runtime_tools, :inets]
  ]
end

Elixirの.iex.exsファイル

IExが起動するときに読み込まれるファイルです。詳しくはIExドキュメントをご覧ください。

Nervesの.iex.exsファイル

.iex.exsファイルのテンプレートは、nerves_bootstrapリポジトリーにあります。

More info

おーはらさんのスライドに日本語で包括的に説明されています。Awesome!
https://twitter.com/ohrdev/status/1474968395087237121

:tada::tada::tada:

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
What you can do with signing up
2