4
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.

NervesJP #10 NervesでGPIO触ってみた

Last updated at Posted at 2021-07-07

参考にした記事

https://qiita.com/kikuyuta/items/5779da9f14f4639ebdbe
https://qiita.com/kikuyuta/items/904acd2ce4117168d22a

やったこと

赤色LED①:赤ボタンが押されている時、消灯
赤色LED②:赤ボタンが押されている時、点灯
緑色LED①:黒ボタンを押す→離すのあと1秒後に消灯
緑色LED②:黒ボタンを押す→離すのあと5秒後に消灯

IMG_0263.jpg

詳細・学んだこと

・NervesでGPIOを使う(Circuits.GPIO ライブラリを使う)
・GPIO制御用モジュール、参考URLそのまま
・workerから呼び出す関数に処理を記述する(今回はButtonLedモジュールの中に記載)
・起動時に実行するためにapplication.exにworkerの記述を追加

※備忘録:export MIX_TARGET=...の実行は忘れないように…。

buttonled.ex
defmodule ButtonLed do
    require Logger
    require GpioInOut

    def button_oneshot(button0_name, button0_no, button1_name, button1_no, led0_name, led0_no, led1_name, led1_no, led2_name, led2_no, led3_name, led3_no, delay2, delay3, interval \\ 50) do
        GpioInOut.start_link(button0_name, button0_no, :input)
        GpioInOut.start_link(button1_name, button1_no, :input)
        GpioInOut.start_link(led0_name, led0_no, :output)
        GpioInOut.start_link(led1_name, led1_no, :output)
        GpioInOut.start_link(led2_name, led2_no, :output)
        GpioInOut.start_link(led3_name, led3_no, :output)
        oneshot_loop(button0_name, button1_name, led0_name, led1_name, led2_name, led3_name, div(delay2, interval), div(delay3, interval), interval)
    end

    defp oneshot_loop(button0_name, button1_name, led0_name, led1_name, led2_name, led3_name, count2, count3, interval, prev \\ 1, timer2 \\ 0, timer3 \\ 0) do
        Logger.debug("#{0..timer2 |> Enum.map(fn _n -> "*" end)}")
        Logger.debug("#{0..timer3 |> Enum.map(fn _n -> "*" end)}")
        
        {:ok, val} = GpioInOut.read(button0_name)
        GpioInOut.write(led0_name, val)
        GpioInOut.write(led1_name, 1-val)

        {:ok, now} = GpioInOut.read(button1_name)
        timer2 = if (now == 0) and (prev == 1), do: count2, else: timer2
        timer2 = if (timer2 > 0), do: timer2 - 1, else: 0
        timer3 = if (now == 0) and (prev == 1), do: count3, else: timer3
        timer3 = if (timer3 > 0), do: timer3 - 1, else: 0
        GpioInOut.write(led2_name, (if (timer2 > 0), do: 1, else: 0))
        GpioInOut.write(led2_name, (if (timer2 > 0), do: 1, else: 0))
        GpioInOut.write(led3_name, (if (timer3 > 0), do: 1, else: 0))
        GpioInOut.write(led3_name, (if (timer3 > 0), do: 1, else: 0))

        Process.sleep(interval)

        oneshot_loop(button0_name, button1_name, led0_name, led1_name, led2_name, led3_name, count2, count3, interval, now, timer2, timer3)
    end
end 

課題・次のステップ

・タイマーの処理のあたりをもう少しきちんと理解したい。
・SPI、イベントドリブン
・Webのインタフェース(Phoenixとか)

4
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
4
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?