Built-in encoding/decoding functions
The following encoders are provided out of the box to get you started.
- ascii
- boolean
- signed_int
- unsigned_int
To use them, pass them along with this module name to the :as option. For example:
schema do
holding_register 1..5, :model, as: {ModBoss.Encoding, :ascii}
end
Summary
Functions
Decode integer value(s) representing ASCII characters to text
Interpret 1 as true and 0 as false
Decode value to a signed integer.
Decode value to an unsigned integer.
Encode text to integers representing ASCII characters.
Encode true as 1 and false as 0
Encode value as a signed integer.
Encode value as an unsigned integer.
Functions
Decode integer value(s) representing ASCII characters to text
- Up to 2 ASCII characters are stored per register.
0is interpreted as a terminator.
Examples
iex> decode_ascii(18537)
{:ok, "Hi"}
iex> decode_ascii([18537, 8448, 0])
{:ok, "Hi!"}
Interpret 1 as true and 0 as false
Examples
iex> decode_boolean(0)
{:ok, false}
iex> decode_boolean(1)
{:ok, true}
Decode value to a signed integer.
Examples
iex> decode_signed_int(77)
{:ok, 77}
iex> decode_signed_int(65_535)
{:ok, -1}
@spec decode_unsigned_int(non_neg_integer()) :: {:ok, non_neg_integer()}
Decode value to an unsigned integer.
Examples
iex> decode_unsigned_int(65_535)
{:ok, 65_535}
@spec encode_ascii(binary(), ModBoss.Encoding.Metadata.t()) :: {:ok, [integer()]} | {:error, binary()}
Encode text to integers representing ASCII characters.
- Prepares zero or more characters to be stored in contiguous registers.
- Up to 2 ASCII characters can be stored per register.
0is used as a terminator if fewer than the maximum characters are encoded.- The
ModBoss.Mappingis required in order to determine how many characters are supported.
Examples
iex> metadata = %ModBoss.Encoding.Metadata{type: :holding_register, address_count: 3}
iex> encode_ascii("Hi!", metadata)
{:ok, [18537, 8448, 0]}
iex> metadata = %ModBoss.Encoding.Metadata{type: :holding_register, address_count: 1}
iex> {:error, reason} = encode_ascii("Hi!", metadata)
iex> String.match?(reason, ~r/Too many characters/)
true
Encode true as 1 and false as 0
Examples
iex> encode_boolean(true)
{:ok, 1}
iex> encode_boolean(false)
{:ok, 0}
Encode value as a signed integer.
Valid values are -32,768 to 32,767.
This function assumes the expected output is a regular Elixir integer. It simply provides a guard against overly large values, then returns the provided value.
Examples
iex> {:ok, -32_768} = encode_signed_int(-32_768)
iex> {:error, _too_large} = encode_signed_int(32_768)
@spec encode_unsigned_int(non_neg_integer()) :: {:ok, non_neg_integer()} | {:error, binary()}
Encode value as an unsigned integer.
Valid values are 0 to 65,535.
This function assumes the expected output is a regular Elixir integer. It simply provides a guard against overly large values, then returns the provided value.
Examples
iex> {:ok, 65_535} = encode_unsigned_int(65_535)
iex> {:error, _too_large} = encode_unsigned_int(65_536)