ArtNet. Encoder
(ArtNet v0.1.0)
View Source
Low-level field encoders used by packet schemas.
Most callers should use ArtNet.encode/1 or ArtNet.Packet.encode/1.
This module works one field at a time and mirrors the formats accepted by
ArtNet.Packet.Schema.field/3.
The generic encode/3 dispatcher returns {:ok, binary} or :error.
Packet-level encoding wraps those failures in ArtNet.EncodeError with the
field name that failed.
Summary
Functions
Encodes a binary value into a binary.
Encodes a bit field value into a binary.
Encodes one value using a schema field format.
Encodes a list of values into a binary.
Encodes an enum value into a binary.
Encodes an integer value into a binary.
Encodes a little-endian integer value into a binary.
Types
@type list_encode_error() :: :not_list | {:invalid_length, expected :: non_neg_integer(), actual :: non_neg_integer()} | {:invalid_element, value :: any()} | {:invalid_element, value :: any(), reason :: any()}
Functions
@spec binary(binary(), pos_integer() | nil) :: {:ok, binary()} | :error
Encodes a binary value into a binary.
datais the binary to encode.sizeis the size of the binary in bytes. If the binary is smaller than the size, it is padded with zeros. Ifnil, the binary is returned as-is.
The function returns {:ok, binary} if the binary was successfully encoded.
If the binary could not be encoded, the function returns :error.
Examples
iex> ArtNet.Encoder.binary(<<1, 2>>, 2)
{:ok, <<1, 2>>}
iex> ArtNet.Encoder.binary(<<1>>, 4)
{:ok, <<1, 0, 0, 0>>}
iex> ArtNet.Encoder.binary(<<1, 2>>, 1)
:error
Encodes a bit field value into a binary.
valueis the bit field value to encode.
The function returns {:ok, binary} if the bit field value was successfully
encoded, or :error if one of its fields cannot be converted.
Examples
iex> ArtNet.Encoder.bit_field(%ArtNet.Packet.BitField.TalkToMe{reply_on_change: true,diagnostics: true,diag_unicast: false,vlc: false})
{:ok, <<0b00110>>}
@spec encode(any(), ArtNet.Packet.Schema.format(), Keyword.t()) :: {:ok, binary()} | :error
Encodes one value using a schema field format.
The format argument is one of the formats documented in
ArtNet.Packet.Schema. For list formats, opts[:length] may require an
exact number of elements.
@spec encode_list([any()], ArtNet.Packet.Schema.format(), Keyword.t()) :: {:ok, binary()} | {:error, list_encode_error()}
Encodes a list of values into a binary.
valuesis the list of values to encode.formatis the format of the values.optsis a keyword of format options.
The function returns {:ok, binary} if the values were successfully encoded.
If the values could not be encoded, the function returns an error tuple
describing the list failure.
Examples
iex> ArtNet.Encoder.encode_list([1, 2, 3], {:integer, 8}, [])
{:ok, <<1, 2, 3>>}
iex> ArtNet.Encoder.encode_list([1, 2, 0x1111], {:integer, 16}, [])
{:ok, <<0, 1, 0, 2, 0x11, 0x11>>}
iex> ArtNet.Encoder.encode_list([1, 2, 0x1111], {:integer, 8}, [])
{:error, {:invalid_element, 0x1111}}
iex> ArtNet.Encoder.encode_list(1, {:integer, 8}, [])
{:error, :not_list}
iex> ArtNet.Encoder.encode_list([1], {:integer, 8}, length: 2)
{:error, {:invalid_length, 2, 1}}
Encodes an enum value into a binary.
valueis the enum value to encode.moduleis the module that defines the enum.
The function returns {:ok, binary} if the enum value was successfully encoded.
If the enum value could not be encoded, the function returns :error.
Examples
iex> ArtNet.Encoder.enum_table(:dp_all, ArtNet.Packet.EnumTable.Priority)
{:ok, <<0x00>>}
iex> ArtNet.Encoder.enum_table(:dp_high, ArtNet.Packet.EnumTable.Priority)
{:ok, <<0x80>>}
iex> ArtNet.Encoder.enum_table(:none, ArtNet.Packet.EnumTable.Priority)
:error
@spec integer(non_neg_integer(), pos_integer()) :: {:ok, binary()} | :error
Encodes an integer value into a binary.
valueis the integer to encode.sizeis the size of the integer in bits.
The function returns {:ok, binary} if the integer was successfully encoded.
If the integer could not be encoded, the function returns :error.
Examples
iex> ArtNet.Encoder.integer(0x0102, 16)
{:ok, <<1, 2>>}
iex> ArtNet.Encoder.integer(0x01020304, 32)
{:ok, <<1, 2, 3, 4>>}
iex> ArtNet.Encoder.integer(0x0102, 8)
:error
@spec little_integer(non_neg_integer(), pos_integer()) :: {:ok, binary()} | :error
Encodes a little-endian integer value into a binary.
The integer must be non-negative and fit in size bits.
Examples
iex> ArtNet.Encoder.little_integer(0x0102, 16)
{:ok, <<2, 1>>}
iex> ArtNet.Encoder.little_integer(0x01020304, 32)
{:ok, <<4, 3, 2, 1>>}
iex> ArtNet.Encoder.little_integer(0x0102, 8)
:error