Wafer.GPIO protocol (wafer v1.0.1)

A GPIO is a physical pin which can be read from and written to.

Some hardware supports interrupts, some has internal pull up/down resistors. Wafer supports all of these, however not all drivers do.

deriving

Deriving

If you're implementing your own Conn type that simply delegates to one of the lower level drivers then you can derive this protocol automatically:

defstruct MyPin do
  @derive Wafer.GPIO
  defstruct [:conn]
end

If your type uses a key other than conn for the inner connection you can specify it while deriving:

defstruct MyPin do
  @derive {Wafer.GPIO, key: :gpio_conn}
  defstruct [:gpio_conn]
end

Link to this section Summary

Functions

Set the pin direction.

Disables interrupts for this connection and pin_condition.

Enable an interrupt for this connection and pin_condition.

Set the pull mode for this pin.

Read the current pin value.

Set the pin value.

Link to this section Types

Link to this type

interrupt_option()

Specs

interrupt_option() :: {:suppress_glitches, boolean()} | {:receiver, pid()}
Link to this type

interrupt_options()

Specs

interrupt_options() :: [interrupt_option()]
Link to this type

pin_condition()

Specs

pin_condition() :: :none | :rising | :falling | :both
Link to this type

pin_direction()

Specs

pin_direction() :: :in | :out
Link to this type

pin_number()

Specs

pin_number() :: non_neg_integer()

Specs

pin_value() :: 0 | 1

Specs

pull_mode() :: :not_set | :none | :pull_up | :pull_down

Specs

t() :: term()

All the types that implement this protocol.

Link to this section Functions

Link to this function

direction(conn, pin_direction)

Specs

direction(Wafer.Conn.t(), pin_direction()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Set the pin direction.

Link to this function

disable_interrupt(conn, pin_condition)

Specs

disable_interrupt(Wafer.Conn.t(), pin_condition()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Disables interrupts for this connection and pin_condition.

Link to this function

enable_interrupt(conn, pin_condition, metadata \\ nil)

Specs

enable_interrupt(Wafer.Conn.t(), pin_condition(), any()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Enable an interrupt for this connection and pin_condition.

arguments

Arguments

  • pin_condition - Either :rising or :falling.
  • metadata - any optional data you wish to receive along with your interrupts. Defaults to nil.

Interrupts will be sent to the calling process as messages in the form of {:interrupt, Conn.t(), pin_condition, metadata | nil}.

implementors-note

Implementors note

Wafer starts it's own Registry named Wafer.InterruptRegistry which you can use to publish your interrupts to using the above format. The registry key is set as follows: {PublishingModule, pin, pin_condition}. You can see examples in the Circuits.GPIO.Dispatcher and ElixirALE.GPIO.Dispatcher modules.

Link to this function

pull_mode(conn, pull_mode)

Specs

pull_mode(Wafer.Conn.t(), pull_mode()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Set the pull mode for this pin.

If the hardware contains software-switchable pull-up and/or pull-down resistors you can configure them this way. If they are not supported then this function will return {:error, :not_supported}.

Specs

read(Wafer.Conn.t()) ::
  {:ok, pin_value(), Wafer.Conn.t()} | {:error, reason :: any()}

Read the current pin value.

Link to this function

write(conn, pin_value)

Specs

write(Wafer.Conn.t(), pin_value()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Set the pin value.