View Source Grizzly.ZWave.Encoding (grizzly v8.6.6)

Utility functions for encoding/decoding common data types.

Summary

Functions

Converts a bit into a boolean.

Converts a boolean into a bit.

Decodes an indexed bitmask.

Decodes a duration as encoded by encode_duration/1. Returns :unknown if the value is outside the range 0x00..0xFD.

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 a list of bit indexes into a bitmask.

Encodes a duration in seconds into a duration byte.

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.

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

Types

encode_bitmask_opts()

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

Functions

bit_to_bool(bit)

@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

bool_to_bit(bool)

@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

decode_bitmask(bitmask)

@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]

decode_duration(byte)

@spec decode_duration(byte()) :: :unknown | non_neg_integer()

Decodes a duration as encoded by encode_duration/1. Returns :unknown if the value is outside the range 0x00..0xFD.

decode_ipv6_address(binary)

@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}

decode_zwave_float(int_value, precision)

@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

encode_bitmask(values, opts \\ [])

@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>>

encode_duration(secs)

@spec encode_duration(non_neg_integer() | :unknown) :: byte()

Encodes a duration in seconds into a duration byte.

Durations of 0..127 seconds are encoded with 1-second resolution. Durations of 128..7560 seconds are encoded with 1-minute resolution. Larger durations are not supported and will be encoded as unknown (0xFE).

encode_ipv6_address(ipv6_address)

@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>>

encode_zwave_float(value)

@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}

zwave_float_to_binary(value, scale)

@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)