PCA9685.Chip (pca9685 v1.1.0)

Wafer connection for PCA9685 16-channel, 12-bit PWM driver.

This module implements the Wafer.Conn behaviour and provides low-level access to the PCA9685 device over I2C.

Summary

Functions

Read all channels' PWM duty cycles.

Read a specific channel's PWM duty cycle.

Initialise the PCA9685 device and set all outputs to off.

Read the contents of the mode1 register.

Read the contents of the mode2 register.

Read the contents of the prescale register.

Set all channels to the same PWM duty cycle.

Set a specific channel's PWM duty cycle.

Swap the contents of the mode1 register.

Swap the contents of the mode2 register.

Swap the contents of the prescale register.

Update the contents of the mode1 register using a transformation function.

Update the contents of the mode2 register using a transformation function.

Update the contents of the prescale register using a transformation function.

Write new contents to the mode1 register.

Write new contents to the mode2 register.

Write new contents to the prescale register.

Types

t()

@type t() :: %PCA9685.Chip{conn: Wafer.Driver.Circuits.I2C.t()}

Functions

get_all_pwm(chip)

@spec get_all_pwm(t()) :: {:ok, [{on :: 0..4095, off :: 0..4095}]} | {:error, term()}

Read all channels' PWM duty cycles.

get_channel_pwm(chip, channel)

@spec get_channel_pwm(t(), channel :: 0..15) ::
  {:ok, {on :: 0..4095, off :: 0..4095}} | {:error, term()}

Read a specific channel's PWM duty cycle.

initialize(chip, freq_hz, oscillator_freq \\ 25_000_000)

@spec initialize(t(), frequency :: pos_integer(), oscillator_freq :: pos_integer()) ::
  {:ok, t()} | {:error, term()}

Initialise the PCA9685 device and set all outputs to off.

read_mode1(conn)

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

Read the contents of the mode1 register.

Example

iex> read_mode1(conn)
{:ok, <<0>>}

read_mode2(conn)

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

Read the contents of the mode2 register.

Example

iex> read_mode2(conn)
{:ok, <<0>>}

read_prescale(conn)

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

Read the contents of the prescale register.

Example

iex> read_prescale(conn)
{:ok, <<0>>}

set_all_pwm(chip, on, off)

@spec set_all_pwm(t(), on :: 0..4095, off :: 0..4095) :: {:ok, t()} | {:error, term()}

Set all channels to the same PWM duty cycle.

set_channel_pwm(chip, channel, on, off)

@spec set_channel_pwm(t(), channel :: 0..15, on :: 0..4095, off :: 0..4095) ::
  {:ok, t()} | {:error, term()}

Set a specific channel's PWM duty cycle.

set_pwm_frequency(chip, freq_hz, oscillator_freq \\ 25_000_000)

@spec set_pwm_frequency(t(), pos_integer(), oscillator_freq :: pos_integer()) ::
  {:ok, t()} | {:error, term()}

Set the PWM frequency in Hz.

swap_mode1(conn, data)

@spec swap_mode1(Wafer.Conn.t(), data :: binary()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Swap the contents of the mode1 register.

Reads the contents of the register, then replaces it, returning the previous contents. Some drivers may implement this atomically.

Example

iex> swap_mode1(conn, <<0>>)
{:ok, <<0>>, _conn}

swap_mode2(conn, data)

@spec swap_mode2(Wafer.Conn.t(), data :: binary()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Swap the contents of the mode2 register.

Reads the contents of the register, then replaces it, returning the previous contents. Some drivers may implement this atomically.

Example

iex> swap_mode2(conn, <<0>>)
{:ok, <<0>>, _conn}

swap_prescale(conn, data)

@spec swap_prescale(Wafer.Conn.t(), data :: binary()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Swap the contents of the prescale register.

Reads the contents of the register, then replaces it, returning the previous contents. Some drivers may implement this atomically.

Example

iex> swap_prescale(conn, <<0>>)
{:ok, <<0>>, _conn}

update_mode1(conn, callback)

@spec update_mode1(Wafer.Conn.t(), (<<_::_*8>> -> <<_::_*8>>)) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Update the contents of the mode1 register using a transformation function.

Example

iex> transform = fn <<data::size(8)>> -> <<(data * 2)::size(8)>> end
...> update_mode1(conn, transform)
{:ok, _conn}

update_mode2(conn, callback)

@spec update_mode2(Wafer.Conn.t(), (<<_::_*8>> -> <<_::_*8>>)) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Update the contents of the mode2 register using a transformation function.

Example

iex> transform = fn <<data::size(8)>> -> <<(data * 2)::size(8)>> end
...> update_mode2(conn, transform)
{:ok, _conn}

update_prescale(conn, callback)

@spec update_prescale(Wafer.Conn.t(), (<<_::_*8>> -> <<_::_*8>>)) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Update the contents of the prescale register using a transformation function.

Example

iex> transform = fn <<data::size(8)>> -> <<(data * 2)::size(8)>> end
...> update_prescale(conn, transform)
{:ok, _conn}

write_mode1(conn, data)

@spec write_mode1(Wafer.Conn.t(), data :: binary()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Write new contents to the mode1 register.

Example

iex> write_mode1(conn, <<0>>)
{:ok, _conn}

write_mode2(conn, data)

@spec write_mode2(Wafer.Conn.t(), data :: binary()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Write new contents to the mode2 register.

Example

iex> write_mode2(conn, <<0>>)
{:ok, _conn}

write_prescale(conn, data)

@spec write_prescale(Wafer.Conn.t(), data :: binary()) ::
  {:ok, Wafer.Conn.t()} | {:error, reason :: any()}

Write new contents to the prescale register.

Example

iex> write_prescale(conn, <<0>>)
{:ok, _conn}