| 1 | -module(x_plane_data_named). |
| 2 | |
| 3 | -export_type( |
| 4 | [ t/0 |
| 5 | , version/0 |
| 6 | , group/0 |
| 7 | ]). |
| 8 | |
| 9 | -export( |
| 10 | [ of_raw/1 |
| 11 | ]). |
| 12 | |
| 13 | -type version() :: |
| 14 | x_plane_data_v10. |
| 15 | |
| 16 | -type group() :: |
| 17 | {speeds , x_plane_data_group_speeds:t()} |
| 18 | | {pitch_roll_heading, x_plane_data_group_pitch_roll_heading:t()} |
| 19 | | {lat_lon_alt , x_plane_data_group_lat_lon_alt:t()} |
| 20 | . |
| 21 | |
| 22 | -type t() :: |
| 23 | {version(), [group()]}. |
| 24 | |
| 25 | -define(DATA_INDEX_V10, 64). |
| 26 | |
| 27 | -spec of_raw(x_plane_data_raw:t()) -> |
| 28 | hope_result:t(t(), unknown_x_plane_version). |
| 29 | of_raw({?DATA_INDEX_V10, GroupsRaw}) -> |
| 30 | ConsKnownDropUnknown = |
| 31 | fun (GroupRaw, Groups1) -> |
| 32 | GroupOpt = v10_group_identify(GroupRaw), |
| 33 | Groups2Opt = hope_option:map(GroupOpt, fun (G) -> [G | Groups1] end), |
| 34 | hope_option:get(Groups2Opt, Groups1) |
| 35 | end, |
| 36 | GroupsNamed = lists:foldl(ConsKnownDropUnknown, [], GroupsRaw), |
| 37 | T = {x_plane_data_v10, GroupsNamed}, |
| 38 | {ok, T}; |
| 39 | of_raw({_, _}) -> |
| 40 | {error, unknown_x_plane_version}. |
| 41 | |
| 42 | -spec v10_group_identify(x_plane_data_raw:group()) -> |
| 43 | hope_option:t(group()). |
| 44 | v10_group_identify({Index, Values}) -> |
| 45 | LabAndConsOpt = v10_index_to_label_and_constructor(Index), |
| 46 | hope_option:map(LabAndConsOpt, fun ({L, C}) -> {L, C(Values)} end). |
| 47 | |
| 48 | v10_index_to_label_and_constructor(Index) -> |
| 49 | F = of_raw_values_v10, |
| 50 | case Index |
| 51 | of 3 -> {some, {speeds , fun x_plane_data_group_speeds:F/1}} |
| 52 | ; 17 -> {some, {pitch_roll_heading, fun x_plane_data_group_pitch_roll_heading:F/1}} |
| 53 | ; 20 -> {some, {lat_lon_alt , fun x_plane_data_group_lat_lon_alt:F/1}} |
| 54 | ; _ -> none |
| 55 | end. |