12
2

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 3 years have passed since last update.

#NervesJPAdvent Calendar 2019

Day 10

Nervesを使ってRaspberry Pi 2でLEDをチカチカさせる 〜クリスマスの飾り付けをしよう〜

Last updated at Posted at 2019-12-09

(この記事は、「#NervesJP Advent Calendar 2019」の10日目です)
昨日はnishiuchikazumaさんの「ElixirでラズパイのLEDをチカ〜RaspbianOSインストールから〜」です!
こちらもぜひぜひ!
私もチカチカさせてみます!

はじめに :santa_tone1:

作品

EKQP567UYAAomEh.jpeg

変更ポイント

diff --git a/mix.exs b/mix.exs
index 7eb5f71..1e02c45 100644
--- a/mix.exs
+++ b/mix.exs
@@ -61,7 +61,8 @@ defmodule HelloNerves.MixProject do
       {:poison, "~> 3.1"},
       {:oauther, "~> 1.1"},
       {:extwitter, "~> 0.8"},
-      {:cronex, github: "jbernardo95/cronex", ref: "345b57e14667a08280d790afdfbb359f467649df"}
+      {:cronex, github: "jbernardo95/cronex", ref: "345b57e14667a08280d790afdfbb359f467649df"},
+      {:circuits_gpio, "~> 0.4"}
     ]
   end
lib/hello_nerves/led.ex
defmodule HelloNerves.Led do
  def blink do
    turn_on()
    Process.sleep(500)
    turn_off()
    Process.sleep(500)

    spawn(HelloNerves.Led, :blink, [])
  end

  defp turn_on do
    Circuits.GPIO.write(gpio(), 1)
  end

  defp turn_off do
    Circuits.GPIO.write(gpio(), 0)
  end

  defp gpio do
    {:ok, gpio} = Circuits.GPIO.open(18, :output)
    gpio
  end
end
$ export MIX_TARGET=rpi2
$ mix deps.get
$ mix firmware
$ mix firmware.burn

して、Nerves on Raspberry Pi 2にsshで接続して

iex(1)> HelloNerves.Led.blink

なんてやると、LEDがチカチカずっとします。

閑話休題

lib/hello_nerves/led.ex
defmodule HelloNerves.Led do
  def start do
    spawn(Led, :blink, [])
  end

  def blink do
    {:ok, gpio} = Circuits.GPIO.open(18, :output)

    Stream.iterate(0, &(&1 + 1))
    |> Enum.each(fn _ ->
      Circuits.GPIO.write(gpio, 1)
      Process.sleep(500)
      Circuits.GPIO.write(gpio, 0)
      Process.sleep(500)
    end)
  end
end
  • ずっと無限にチカチカするものを作りたかっただけなのですが、最初に書いたのはこんな感じの(たぶん)無限ループでした
  • なんだか美しくないようにみえたのでのように書き直しました
  • どっちがいいのか、はたまたもっと良い書き方があるのかはわかっていません :sweat_smile:
  • ご指導ご鞭撻のほどよろしくお願いします

A very merry Christmas (war is over if you want it)

IMG_20191209_083545.jpg

IMG_20191209_083549.jpg

VID_20191209_200553.gif

次回

12
2
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
12
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?