Wafer.GPIO protocol (wafer v0.1.3)
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
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
interrupt_option()
Specs
interrupt_options()
Specs
interrupt_options() :: [interrupt_option()]
pin_condition()
Specs
pin_condition() :: :none | :rising | :falling | :both
pin_direction()
Specs
pin_direction() :: :in | :out
pin_number()
Specs
pin_number() :: non_neg_integer()
pin_value()
Specs
pin_value() :: 0 | 1
pull_mode()
Specs
pull_mode() :: :not_set | :none | :pull_up | :pull_down
Specs
t() :: term()
Link to this section Functions
direction(conn, pin_direction)
Specs
direction(Wafer.Conn.t(), pin_direction()) :: {:ok, Wafer.Conn.t()} | {:error, reason :: any()}
Set the pin direction.
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.
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
pin_condition
- Either:rising
or:falling
.metadata
- any optional data you wish to receive along with your interrupts. Defaults tonil
.
Interrupts will be sent to the calling process as messages in the form of
{:interrupt, Conn.t(), pin_condition, metadata | nil}
.
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.
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}
.
read(conn)
Specs
read(Wafer.Conn.t()) :: {:ok, pin_value(), Wafer.Conn.t()} | {:error, reason :: any()}
Read the current pin value.
write(conn, pin_value)
Specs
write(Wafer.Conn.t(), pin_value()) :: {:ok, Wafer.Conn.t()} | {:error, reason :: any()}
Set the pin value.