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

サイコロをつくる(Elixir, Nerves)

Last updated at Posted at 2020-01-27

はじめに

VID_20200127_210610.gif

  • ボタンを押している間はガチャガチャ表示して、離したら適当な数字を1つ表示します

IMG_20200127_210641_copy.jpg

  • 配線が、ガチャガチャしています:man_tone3:

前提記事

ソースコード

ソースコード抜粋

lib/hello_nerves/led/seven_seg.ex
defmodule HelloNerves.Led.SevenSeg do
  @a_led_pin Application.get_env(:hello_nerves, :a_led_pin, 26)
  @b_led_pin Application.get_env(:hello_nerves, :b_led_pin, 6)
  @c_led_pin Application.get_env(:hello_nerves, :c_led_pin, 5)
  @d_led_pin Application.get_env(:hello_nerves, :d_led_pin, 16)
  @e_led_pin Application.get_env(:hello_nerves, :e_led_pin, 23)
  @f_led_pin Application.get_env(:hello_nerves, :f_led_pin, 25)
  @g_led_pin Application.get_env(:hello_nerves, :g_led_pin, 22)

  alias Circuits.GPIO

  require Logger

  def clear do
    [@a_led_pin, @b_led_pin, @c_led_pin, @d_led_pin, @e_led_pin, @f_led_pin, @g_led_pin]
    |> Enum.map(&gpio(&1))
    |> Enum.map(&turn_off(&1))
  end

  def show do
    fun = random_fun()

    1..3
    |> Enum.each(fn _ ->
      fun.()
      Process.sleep(50)
      clear()
      Process.sleep(25)
    end)

    fun.()
  end

  def random do
    fun = random_fun()
    fun.()
  end

  def random_forever do
    random()

    50..75
    |> Enum.random()
    |> Process.sleep()

    random_forever()
  end

  def one do
    clear()
    [@b_led_pin, @c_led_pin] |> flush()
  end

  def two do
    clear()
    [@a_led_pin, @b_led_pin, @g_led_pin, @e_led_pin, @d_led_pin] |> flush()
  end

  def three do
    clear()
    [@a_led_pin, @b_led_pin, @g_led_pin, @c_led_pin, @d_led_pin] |> flush()
  end

  def four do
    clear()
    [@f_led_pin, @g_led_pin, @b_led_pin, @c_led_pin] |> flush()
  end

  def five do
    clear()
    [@f_led_pin, @g_led_pin, @c_led_pin, @d_led_pin, @a_led_pin] |> flush()
  end

  def six do
    clear()
    [@f_led_pin, @e_led_pin, @d_led_pin, @c_led_pin, @g_led_pin] |> flush()
  end

  defp flush(pins) do
    pins
    |> Enum.map(&gpio(&1))
    |> Enum.map(&turn_on(&1))
  end

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

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

  defp gpio(pin) do
    {:ok, output_gpio} = GPIO.open(pin, :output)
    output_gpio
  end

  defp random_fun do
    [&one/0, &two/0, &three/0, &four/0, &five/0, &six/0]
    |> Enum.random()
  end
end
  • 一画ずつGPIOがひもづいていて、それらをONやOFFすることで数字を表しています
  • 特にrandom_fun/0がうまくかけたんじゃないかとおもっています :rocket:
  defp random_fun do
    [&one/0, &two/0, &three/0, &four/0, &five/0, &six/0]
    |> Enum.random()
  end
7
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
7
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?