Unpack example records.
[erlang-x-plane-data.git] / README.md
1 [![Build Status](https://travis-ci.org/ibnfirnas/erlang-x_plane_data.svg?branch=master)](https://travis-ci.org/ibnfirnas/erlang-x_plane_data)
2
3 X-Plane UDP data parser
4 =======================
5
6 Example
7 -------
8
9 ```erlang
10 -include_lib("include/x_plane_data.hrl").
11
12 main(Port) ->
13 {ok, Socket} = gen_udp:open(Port, [binary, {active, false}]),
14 {ok, {_, _, <<XPlaneDataPacket/binary>>}} = gen_udp:recv(Socket, 0),
15 {ok, XPlaneData} = x_plane_data:of_bin(XPlaneDataPacket),
16
17 % Currently there're 133 possible data types sent by X-Plane 10, of which
18 % I've identified and labeled only some of. See x_plane_datum:t() type for
19 % what is currently labeled.
20 % The types I've not yet labeled are in the format specified by
21 % x_plane_datum:anonymous() and can be looked-up by their index number.
22
23 % Find labeled data types
24 {some, #x_plane_datum_speeds
25 { vind_kias = VindKias
26 , vind_keas = VindKeas
27 , vtrue_ktas = VtrueKtas
28 , vtrue_ktgs = VtrueKtgs
29 , vind_mph = VindMph
30 , vtrue_mphas = VtrueMphas
31 , vtrue_mphgs = VtrueMphgs
32 },
33 } = hope_kv_list:get(XPlaneData, speeds),
34
35 {some, #x_plane_datum_pitch_roll_heading
36 { pitch_deg = PitchDeg
37 , roll_deg = RollDeg
38 , hding_true = HdingTrue
39 , hding_mag = HdingMag
40 },
41 } = hope_kv_list:get(XPlaneData, pitch_roll_heading),
42
43 {some, #x_plane_datum_lat_lon_alt
44 { lat_deg = LatDeg
45 , lon_deg = LonDeg
46 , alt_ftmsl = AltFtmsl
47 , alt_ftagl = AltFtagl
48 , on_runwy = OnRunwy
49 , alt_ind = AltInd
50 , lat_south = LatSouth
51 , lat_west = LatWest
52 },
53 } = hope_kv_list:get(XPlaneData, lat_lon_alt),
54
55 % Find an unlabled data type
56 {some, {10, V1, V2, V3, V4, V5, V6, V7, V8}} = hope_kv_list:get(XPlaneData, 10),
57
58 % Attempt to find a data type that was not included in current packet
59 none = hope_kv_list:get(XPlaneData, 130),
60 none = hope_kv_list:get(XPlaneData, 67),
61
62 ...
63 ```
64
65 Note: you can, of course, use any other method to search a `[{K, V}]` list
66 (which is how `x_plane_data:t()` is structured), such as:
67 `proplists:get_value/2`, `lists:keyfind/3`, etc., but I prefer the API of
68 `hope_kv_list`, so I used that.
69
70
71 Data format references
72 ----------------------
73
74 - http://b58.svglobe.com/data.html
75 - http://www.nuclearprojects.com/xplane/xplaneref.html
This page took 0.049995 seconds and 4 git commands to generate.