Search documentation of grizzly

# View Source Grizzly.ZWave.Encoding (grizzly v7.4.2)

Utility functions for encoding/decoding common data types.

# Summary

## Types

encode_bitmask_opts()

## Functions

bit_to_bool(bit)

Converts a bit into a boolean.

bool_to_bit(bool)

Converts a boolean into a bit.

decode_bitmask(bitmask)

Decodes an indexed bitmask.

decode_ipv6_address(binary)

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

decode_zwave_float(int_value, precision)

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.

encode_bitmask(values, opts \\ [])

Encodes a list of bit indexes into a bitmask.

encode_ipv6_address(ipv6_address)

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

encode_zwave_float(value)

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.

zwave_float_to_binary(value, scale)

Converts a float into a binary representation of a Z-Wave float according to the typical format.

# Types

Link to this type

# encode_bitmask_opts()

View Source

    @type encode_bitmask_opts() :: [{:min_bytes, non_neg_integer()}]

# Functions

Link to this function

# bit_to_bool(bit)

View Source

    @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

Link to this function

# bool_to_bit(bool)

View Source

    @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_bitmask(bitmask)

View Source

    @spec decode_bitmask(binary()) :: [non_neg_integer()]

Decodes an indexed bitmask.

### Examples

    iex> decode_bitmask(<<>>)
    []

    iex> decode_bitmask(<<0b10110001, 0, 0>>)
    [0, 4, 5, 7]

    iex> decode_bitmask(<<0b10110001, 0b00000001, 0, 0, 0b00001000>>)
    [0, 4, 5, 7, 8, 35]

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_bitmask(values, opts \\ [])

View Source

    @spec encode_bitmask([non_neg_integer()], encode_bitmask_opts()) :: binary()

Encodes a list of bit indexes into a bitmask.

### Examples

    iex> encode_bitmask([])
    <<>>

    iex> encode_bitmask([0, 4, 5, 7, 8, 35])
    <<0b10110001, 0b00000001, 0, 0, 0b00001000>>

    iex> encode_bitmask([31, 8, 5, 0, 4, 7])
    <<0b10110001, 0b00000001, 0b00000000, 0b10000000>>

    iex> encode_bitmask([0, 4, 5, 7], min_bytes: 3)
    <<0b10110001, 0, 0>>

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}

Link to this function

# zwave_float_to_binary(value, scale)

View Source

    @spec zwave_float_to_binary(number(), byte()) :: binary()

Converts a float into a binary representation of a Z-Wave float according to the typical format.

- precision (3 bits)
- scale (2 bits)
- size (3 bits)
- value (n bytes, where n = size)