X-Git-Url: https://git.xandkar.net/?p=erlang-x-plane-data.git;a=blobdiff_plain;f=src%2Fx_plane_data_raw.erl;fp=src%2Fx_plane_data_raw.erl;h=f419137ba6f39e843fce1df261b7792c2e1330ee;hp=0000000000000000000000000000000000000000;hb=07ba60a5e3ff3cf3b8e96ff49bb27ac3c6b46ba6;hpb=c9410825c5416a62558ac6bd108a1dbe6ffbc3c6 diff --git a/src/x_plane_data_raw.erl b/src/x_plane_data_raw.erl new file mode 100644 index 0000000..f419137 --- /dev/null +++ b/src/x_plane_data_raw.erl @@ -0,0 +1,86 @@ +-module(x_plane_data_raw). + +-export_type( + [ t/0 + , index/0 + , group_index/0 + , group_values/0 + , group/0 + , groups/0 + ]). + +-export( + [ of_bin/1 + ]). + +-type parsing_error() :: + packet_bad_header + | packet_bad_length + . + +-type group_index() :: + non_neg_integer(). + +-type group_values() :: + { float() + , float() + , float() + , float() + , float() + , float() + , float() + , float() + }. + +-type group() :: + {group_index(), group_values()}. + +% Packet index byte. Essentially a schema version. +-type index() :: + integer(). + +-type groups() :: + [group()]. + +-type t() :: + {index(), groups()}. + +-define(BYTE_SIZE_OF_EACH_BLOCK, 36). +-define(PACKET_HEADER, "DATA"). + +-spec of_bin(binary()) -> + {ok, t()} + | {error, parsing_error()} + . +of_bin(<>) + when byte_size(ContiguousBlocks) rem ?BYTE_SIZE_OF_EACH_BLOCK =/= 0 -> + {error, packet_bad_length}; +of_bin(<>) -> + Groups = [group_of_bin(B) || B <- blocks_split(ContiguousBlocks)], + {ok, {Index, Groups}}; +of_bin(<<_/binary>>) -> + {error, packet_bad_header}. + +-spec blocks_split(binary()) -> + [binary()]. +blocks_split(<<>>) -> + []; +blocks_split(<>) -> + [Block | blocks_split(Blocks)]. + +-spec group_of_bin(binary()) -> + group(). +group_of_bin( + << Index:32/little-integer + , V1:32/little-float + , V2:32/little-float + , V3:32/little-float + , V4:32/little-float + , V5:32/little-float + , V6:32/little-float + , V7:32/little-float + , V8:32/little-float + >> +) -> + Values = {V1, V2, V3, V4, V5, V6, V7, V8}, + {Index, Values}.