View Source Grizzly.ZWave.Encoding (grizzly v7.0.2)

Utility functions for encoding/decoding common data types.

Summary

Functions

Converts a bit into a boolean.

Converts a boolean into a bit.

Decodes a 128-bit binary into an IPv6 address tuple.

Converts an integer value and non-zero precision into a float by dividing the integer by 10 ^ precision. If the given precision is zero, the integer is returned as-is.

Encodes an IPv6 address tuple into a 128-bit binary.

Converts a float into a tuple containing an integer representation of the float, the factor of 10 by which the integer must be divided to get the original float, and the number of bytes needed to represent the value as a signed integer.

Types

@type bit() :: 0 | 1
Link to this type

bitmask_index_to_value_fun()

View Source
@type bitmask_index_to_value_fun() :: bitmask_index_to_value_fun(term())
Link to this type

bitmask_index_to_value_fun(v)

View Source
@type bitmask_index_to_value_fun(v) :: (index :: non_neg_integer() -> v | nil)
Link to this type

bitmask_value_to_index_fun()

View Source
@type bitmask_value_to_index_fun() :: bitmask_value_to_index_fun(term())
Link to this type

bitmask_value_to_index_fun(v)

View Source
@type bitmask_value_to_index_fun(v) :: (v -> non_neg_integer())

Functions

@spec bit_to_bool(0 | 1) :: boolean()

Converts a bit into a boolean.

Examples

iex> bit_to_bool(1)
true
iex> bit_to_bool(0)
false
@spec bool_to_bit(boolean()) :: 0 | 1

Converts a boolean into a bit.

Examples

iex> bool_to_bit(true)
1
iex> bool_to_bit(false)
0
Link to this function

decode_indexed_bitmask(bitmask, value_fun \\ &Function.identity/1)

View Source
@spec decode_indexed_bitmask(binary(), bitmask_index_to_value_fun(value_type)) :: [
  {value_type, boolean()}
]
when value_type: var

Decodes an indexed bitmask.

Examples

iex> decode_indexed_bitmask(<<>>)
[]

iex> decode_indexed_bitmask(<<0b10110001::8>>)
[{0, true}, {1, false}, {2, false}, {3, false}, {4, true}, {5, true}, {6, false}, {7, true}]
Link to this function

decode_ipv6_address(binary)

View Source
@spec decode_ipv6_address(binary()) :: :inet.ip6_address()

Decodes a 128-bit binary into an IPv6 address tuple.

Examples

iex> decode_ipv6_address(<<0xfd00::16, 0xaaaa::16, 0::16, 0::16, 0::16, 0::16, 0::16, 2::16>>)
{0xfd00, 0xaaaa, 0, 0, 0, 0, 0, 2}
Link to this function

decode_zwave_float(int_value, precision)

View Source
@spec decode_zwave_float(integer(), non_neg_integer()) :: number()

Converts an integer value and non-zero precision into a float by dividing the integer by 10 ^ precision. If the given precision is zero, the integer is returned as-is.

Examples

iex> decode_zwave_float(0, 0)
0
iex> decode_zwave_float(0, 2)
0.0
iex> decode_zwave_float(1234, 2)
12.34
iex> decode_zwave_float(1234, 1)
123.4
iex> decode_zwave_float(1234, 0)
1234
iex> decode_zwave_float(-1234, 2)
-12.34
Link to this function

encode_indexed_bitmask(values, index_fun \\ &Function.identity/1, opts \\ [])

View Source

Encodes an indexed bitmask.

Examples

iex> encode_indexed_bitmask([])
<<>>

iex> encode_indexed_bitmask(
...>   [{0, true}, {4, true}, {5, true}, {6, false}, {7, true}, {8, true}]
...> )
<<0b10110001::8, 0b00000001::8>>

iex> encode_indexed_bitmask(
...>   [{0, true}, {4, true}, {5, true}, {6, false}, {7, true}, {8, true}, {31, true}]
...> )
<<0b10110001, 0b00000001, 0b00000000, 0b10000000>>
Link to this function

encode_ipv6_address(ipv6_address)

View Source
@spec encode_ipv6_address(:inet.ip6_address()) :: binary()

Encodes an IPv6 address tuple into a 128-bit binary.

Examples

iex> encode_ipv6_address({0xfd00, 0xaaaa, 0, 0, 0, 0, 0, 2})
<<0xfd00::16, 0xaaaa::16, 0::16, 0::16, 0::16, 0::16, 0::16, 2::16>>
Link to this function

encode_zwave_float(value)

View Source
@spec encode_zwave_float(value :: number()) ::
  {int_value :: integer(), precision :: non_neg_integer(), size :: integer()}

Converts a float into a tuple containing an integer representation of the float, the factor of 10 by which the integer must be divided to get the original float, and the number of bytes needed to represent the value as a signed integer.

Examples

iex> encode_zwave_float(0)
{0, 0, 1}
iex> encode_zwave_float(-1.5)
{-15, 1, 1}
iex> encode_zwave_float(-1.50)
{-15, 1, 1}
iex> encode_zwave_float(128)
{128, 0, 2}
iex> encode_zwave_float(127.5)
{1275, 1, 2}
iex> encode_zwave_float(-75.25)
{-7525, 2, 2}
iex> encode_zwave_float(-752.55)
{-75255, 2, 4}
iex> encode_zwave_float(-75.255)
{-75255, 3, 4}