X-Git-Url: https://git.xandkar.net/?p=erlang-x-plane-data.git;a=blobdiff_plain;f=src%2Fx_plane_data_named.erl;fp=src%2Fx_plane_data_named.erl;h=250bf80a68103719c02ccbdd021caea70731f833;hp=0000000000000000000000000000000000000000;hb=84fa8ccb7143428a0aa49ceeb632ff45b374d8b8;hpb=10d6d4c7bd29e99c67024e8a08a2b35f8da1c4de diff --git a/src/x_plane_data_named.erl b/src/x_plane_data_named.erl new file mode 100644 index 0000000..250bf80 --- /dev/null +++ b/src/x_plane_data_named.erl @@ -0,0 +1,55 @@ +-module(x_plane_data_named). + +-export_type( + [ t/0 + , version/0 + , group/0 + ]). + +-export( + [ of_raw/1 + ]). + +-type version() :: + x_plane_data_v10. + +-type group() :: + {speeds , x_plane_data_group_speeds:t()} + | {pitch_roll_heading, x_plane_data_group_pitch_roll_heading:t()} + | {lat_lon_alt , x_plane_data_group_lat_lon_alt:t()} + . + +-type t() :: + {version(), [group()]}. + +-define(DATA_INDEX_V10, 64). + +-spec of_raw(x_plane_data_raw:t()) -> + hope_result:t(t(), unknown_x_plane_version). +of_raw({?DATA_INDEX_V10, GroupsRaw}) -> + ConsKnownDropUnknown = + fun (GroupRaw, Groups1) -> + GroupOpt = v10_group_identify(GroupRaw), + Groups2Opt = hope_option:map(GroupOpt, fun (G) -> [G | Groups1] end), + hope_option:get(Groups2Opt, Groups1) + end, + GroupsNamed = lists:foldl(ConsKnownDropUnknown, [], GroupsRaw), + T = {x_plane_data_v10, GroupsNamed}, + {ok, T}; +of_raw({_, _}) -> + {error, unknown_x_plane_version}. + +-spec v10_group_identify(x_plane_data_raw:group()) -> + hope_option:t(group()). +v10_group_identify({Index, Values}) -> + LabAndConsOpt = v10_index_to_label_and_constructor(Index), + hope_option:map(LabAndConsOpt, fun ({L, C}) -> {L, C(Values)} end). + +v10_index_to_label_and_constructor(Index) -> + F = of_raw_values_v10, + case Index + of 3 -> {some, {speeds , fun x_plane_data_group_speeds:F/1}} + ; 17 -> {some, {pitch_roll_heading, fun x_plane_data_group_pitch_roll_heading:F/1}} + ; 20 -> {some, {lat_lon_alt , fun x_plane_data_group_lat_lon_alt:F/1}} + ; _ -> none + end.