Wafer.InterruptRegistry (wafer v1.1.4)

Copy Markdown View Source

This module provides Wafer's interrupt registry. This allows multiple subscribers to be subscribed to interrupts from many different pins.

It is used by Driver.Circuits.GPIO.Dispatcher and you should probably use it if you're writing your own driver which supports sending interrupts to subscribers.

Internals

The underlying Registry uses {key, :rising} and {key, :falling} as its keys. Subscribing with :both simply registers the caller under both keys, so publishing an edge is a plain registry dispatch — no filtering or match specs required.

Summary

Functions

Count the number of active subscriptions for key.

Count the number of active subscriptions for key and pin_condition.

Publish an interrupt to subscribers that are interested in key and pin_condition.

Subscribe the calling process to interrupts for the specified pin_condition using the provided key.

Subscribe the calling process to interrupts for the specified pin_condition using the provided key.

Are there any subscribers to key?

Are there any subscribers to key and pin_condition?

Returns a list of all subscriptions to key.

Returns a list of all subscriptions for key and pin_condition.

Remove all subscriptions for key, pin_condition and conn.

Types

key()

@type key() :: any()

Functions

count_subscriptions(key)

@spec count_subscriptions(key()) :: non_neg_integer()

Count the number of active subscriptions for key.

A subscriber who subscribed with :both is counted twice.

count_subscriptions(key, pin_condition)

@spec count_subscriptions(key(), Wafer.GPIO.pin_condition()) :: non_neg_integer()

Count the number of active subscriptions for key and pin_condition.

Subscribers who subscribed with :both contribute to both counts.

publish(key, pin_condition)

@spec publish(key(), :rising | :falling) :: {:ok, non_neg_integer()}

Publish an interrupt to subscribers that are interested in key and pin_condition.

The interrupt message takes the form {:interrupt, Conn.t(), :rising | :falling, metadata :: any}.

subscribe(key, pin_condition, conn)

@spec subscribe(key(), Wafer.GPIO.pin_condition(), Wafer.Conn.t()) :: :ok

Subscribe the calling process to interrupts for the specified pin_condition using the provided key.

See subscribe/4 for more information.

Example

iex> subscribe({MCP230017.Pin, 13}, :rising, conn)
:ok

subscribe(key, pin_condition, conn, metadata)

@spec subscribe(key(), Wafer.GPIO.pin_condition(), Wafer.Conn.t(), any()) :: :ok

Subscribe the calling process to interrupts for the specified pin_condition using the provided key.

Arguments

  • key a key which uniquely describes the pin. Probably a combination of device name and pin.
  • pin_condition either :rising, :falling or :both. Subscribing with :both is equivalent to subscribing with :rising and :falling separately.
  • conn the receiver's connection to the pin, sent back to them in the interrupt message.
  • metadata arbitrary data which will be sent to the receiver process in the interrupt message. Defaults to nil.

subscribers?(key)

@spec subscribers?(key()) :: boolean()

Are there any subscribers to key?

subscribers?(key, pin_condition)

@spec subscribers?(key(), Wafer.GPIO.pin_condition()) :: boolean()

Are there any subscribers to key and pin_condition?

subscriptions(key)

@spec subscriptions(key()) :: [
  {Wafer.GPIO.pin_condition(), Wafer.Conn.t(), metadata :: any(),
   receiver :: pid()}
]

Returns a list of all subscriptions to key.

A subscriber who subscribed with :both is returned as two entries — one per edge.

subscriptions(key, pin_condition)

@spec subscriptions(key(), Wafer.GPIO.pin_condition()) :: [
  {Wafer.GPIO.pin_condition(), Wafer.Conn.t(), metadata :: any(),
   receiver :: pid()}
]

Returns a list of all subscriptions for key and pin_condition.

Subscribers who subscribed with :both appear in the lists for both :rising and :falling. Calling with :both returns the same as subscriptions/1.

unsubscribe(key, pin_condition, conn)

@spec unsubscribe(key(), Wafer.GPIO.pin_condition(), Wafer.Conn.t()) :: :ok

Remove all subscriptions for key, pin_condition and conn.

Unsubscribing with :both removes the caller's subscription from both the :rising and :falling buckets.