| | 1 | -module(x_plane_data_raw). |
| | 2 | |
| | 3 | -export_type( |
| | 4 | [ t/0 |
| | 5 | , index/0 |
| | 6 | , group_index/0 |
| | 7 | , group_values/0 |
| | 8 | , group/0 |
| | 9 | , groups/0 |
| | 10 | ]). |
| | 11 | |
| | 12 | -export( |
| | 13 | [ of_bin/1 |
| | 14 | ]). |
| | 15 | |
| | 16 | -type parsing_error() :: |
| | 17 | packet_bad_header |
| | 18 | | packet_bad_length |
| | 19 | . |
| | 20 | |
| | 21 | -type group_index() :: |
| | 22 | non_neg_integer(). |
| | 23 | |
| | 24 | -type group_values() :: |
| | 25 | { float() |
| | 26 | , float() |
| | 27 | , float() |
| | 28 | , float() |
| | 29 | , float() |
| | 30 | , float() |
| | 31 | , float() |
| | 32 | , float() |
| | 33 | }. |
| | 34 | |
| | 35 | -type group() :: |
| | 36 | {group_index(), group_values()}. |
| | 37 | |
| | 38 | % Packet index byte. Essentially a schema version. |
| | 39 | -type index() :: |
| | 40 | integer(). |
| | 41 | |
| | 42 | -type groups() :: |
| | 43 | [group()]. |
| | 44 | |
| | 45 | -type t() :: |
| | 46 | {index(), groups()}. |
| | 47 | |
| | 48 | -define(BYTE_SIZE_OF_EACH_BLOCK, 36). |
| | 49 | -define(PACKET_HEADER, "DATA"). |
| | 50 | |
| | 51 | -spec of_bin(binary()) -> |
| | 52 | {ok, t()} |
| | 53 | | {error, parsing_error()} |
| | 54 | . |
| | 55 | of_bin(<<?PACKET_HEADER, _:8/integer, ContiguousBlocks/binary>>) |
| | 56 | when byte_size(ContiguousBlocks) rem ?BYTE_SIZE_OF_EACH_BLOCK =/= 0 -> |
| | 57 | {error, packet_bad_length}; |
| | 58 | of_bin(<<?PACKET_HEADER, Index:8/integer, ContiguousBlocks/binary>>) -> |
| | 59 | Groups = [group_of_bin(B) || B <- blocks_split(ContiguousBlocks)], |
| | 60 | {ok, {Index, Groups}}; |
| | 61 | of_bin(<<_/binary>>) -> |
| | 62 | {error, packet_bad_header}. |
| | 63 | |
| | 64 | -spec blocks_split(binary()) -> |
| | 65 | [binary()]. |
| | 66 | blocks_split(<<>>) -> |
| | 67 | []; |
| | 68 | blocks_split(<<Block:?BYTE_SIZE_OF_EACH_BLOCK/bytes, Blocks/binary>>) -> |
| | 69 | [Block | blocks_split(Blocks)]. |
| | 70 | |
| | 71 | -spec group_of_bin(binary()) -> |
| | 72 | group(). |
| | 73 | group_of_bin( |
| | 74 | << Index:32/little-integer |
| | 75 | , V1:32/little-float |
| | 76 | , V2:32/little-float |
| | 77 | , V3:32/little-float |
| | 78 | , V4:32/little-float |
| | 79 | , V5:32/little-float |
| | 80 | , V6:32/little-float |
| | 81 | , V7:32/little-float |
| | 82 | , V8:32/little-float |
| | 83 | >> |
| | 84 | ) -> |
| | 85 | Values = {V1, V2, V3, V4, V5, V6, V7, V8}, |
| | 86 | {Index, Values}. |