Skip to content

Instantly share code, notes, and snippets.

@jeffweiss
Created November 4, 2015 22:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeffweiss/26267c26a5d6a90a0bbf to your computer and use it in GitHub Desktop.
Save jeffweiss/26267c26a5d6a90a0bbf to your computer and use it in GitHub Desktop.
Sample X10 packet (de)composition in Elixir
defmodule X10 do
# all codes referenced from http://www.haibrain.com/informatie-domotica-x10-protocol-c-131_148_141.html?language=en
@house_codes %{
6 => :house_a,
14 => :house_b,
2 => :house_c,
10 => :house_d,
1 => :house_e,
9 => :house_f,
5 => :house_g,
13 => :house_h,
7 => :house_i,
15 => :house_j,
3 => :house_k,
11 => :house_l,
0 => :house_m,
8 => :house_n,
4 => :house_o,
12 => :house_p,
}
@unit_codes %{
12 => :unit_1,
28 => :unit_2,
4 => :unit_3,
20 => :unit_4,
2 => :unit_5,
18 => :unit_6,
10 => :unit_7,
16 => :unit_8,
14 => :unit_9,
30 => :unit_10,
6 => :unit_11,
22 => :unit_12,
0 => :unit_13,
16 => :unit_14,
8 => :unit_15,
24 => :unit_16,
}
@function_codes %{
1 => :all_units_off,
3 => :all_lights_on,
5 => :on,
7 => :off,
9 => :dim,
11 => :bright,
15 => :extended_code,
17 => :hail_request,
19 => :hail_acknowledge,
21 => :preset_dim,
25 => :extended_data,
25 => :extended_data,
27 => :status_on,
29 => :status_off,
31 => :status_request,
}
for {code, letter} <- @house_codes do
def parse_house_code(unquote(code)), do: unquote(letter)
def construct_code(unquote(letter)), do: unquote(code)
end
for {code, command} <- @function_codes do
def parse_function_code(unquote(code)), do: unquote(command)
def construct_code(unquote(command)), do: unquote(code)
end
for {code, unit} <- @unit_codes do
def parse_function_code(unquote(code)), do: unquote(unit)
def construct_code(unquote(unit)), do: unquote(code)
end
def parse_packet(<<14::size(4), house_code::size(4), other::size(5)>>) do
{parse_house_code(house_code), parse_function_code(other)}
end
def create_packet({house, function}) do
house_code = construct_code(house)
function_code = construct_code(function)
<<14::size(4), house_code::size(4), function_code::size(5)>>
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment