View Source Wafer.GPIO protocol (wafer v1.1.0)

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

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

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.

Types

@type interrupt_option() :: {:suppress_glitches, boolean()} | {:receiver, pid()}
@type interrupt_options() :: [interrupt_option()]
@type pin_condition() :: :none | :rising | :falling | :both
@type pin_direction() :: :in | :out
@type pin_number() :: non_neg_integer()
@type pin_value() :: 0 | 1
@type pull_mode() :: :not_set | :none | :pull_up | :pull_down
@type t() :: term()

All the types that implement this protocol.

Functions

Link to this function

direction(conn, pin_direction)

View Source
@spec 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)

View Source
@spec 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)

View Source
@spec 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

  • 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}.

Implementers 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 module.

Link to this function

pull_mode(conn, pull_mode)

View Source
@spec 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}.

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

Read the current pin value.

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

Set the pin value.