Skip to content

Instantly share code, notes, and snippets.

@xandkar
Last active August 29, 2015 14:26
Show Gist options
  • Save xandkar/73f529445458df84ba2e to your computer and use it in GitHub Desktop.
Save xandkar/73f529445458df84ba2e to your computer and use it in GitHub Desktop.
module Datum = struct
type t =
{ index : int
; v1 : float
; v2 : float
; v3 : float
; v4 : float
; v5 : float
; v6 : float
; v7 : float
; v8 : float
}
let of_bitstring bits =
bitmatch bits with
| { index : 32 : littleendian
; v1 : 32 : littleendian
; v2 : 32 : littleendian
; v3 : 32 : littleendian
; v4 : 32 : littleendian
; v5 : 32 : littleendian
; v6 : 32 : littleendian
; v7 : 32 : littleendian
; v8 : 32 : littleendian
} ->
{ index = Int32.to_int index
; v1 = Int32.float_of_bits v1
; v2 = Int32.float_of_bits v2
; v3 = Int32.float_of_bits v3
; v4 = Int32.float_of_bits v4
; v5 = Int32.float_of_bits v5
; v6 = Int32.float_of_bits v6
; v7 = Int32.float_of_bits v7
; v8 = Int32.float_of_bits v8
}
let show {index; v1; v2; v3; v4; v5; v6; v7; v8} =
Printf.sprintf
"| %3d | %11f | %11f | %11f | %11f | %11f | %11f | %11f | %11f |"
index v1 v2 v3 v4 v5 v6 v7 v8
end
let sample_packet_base64 =
"REFUQUADAAAAbcpGQLt81EBfZNlATnUoNwDAecSow2RAnCv6QLrbQTcRAAAA3i8VQFL3ZT6dPfFCx4IFQwDAecQAwHnEAMB5xADAecQUAAAA1ZciQg6ik8JGBv9AdDxoPgAAgD9G/o3CAAAgQgAAlsI="
let rec split blocks =
bitmatch blocks with
| { block : 36 * 8 : bitstring
; blocks : -1 : bitstring
} ->
block :: (split blocks)
| {_ : 0 : bitstring} ->
[]
let main () =
let packet = B64.decode sample_packet_base64 in
let packet = Bitstring.bitstring_of_string packet in
bitmatch packet with
| { "DATA" : 4 * 8 : string
; "@" : 1 * 8 : string
; blocks : -1 : bitstring
} ->
let blocks = split blocks in
List.iter (fun b -> print_endline (Datum.show (Datum.of_bitstring b))) blocks
let () = main ()
@xandkar
Copy link
Author

xandkar commented Jul 31, 2015

Install deps, build and run:

$ opam install base64 bitstring
$ ocamlbuild -use-ocamlfind -syntax camlp4o -package base64,bitstring.syntax,bitstring x_plane_data.byte
Finished, 3 targets (0 cached) in 00:00:00.

$ ./x_plane_data.byte
|   3 |    3.106105 |    6.640226 |    6.793502 |    0.000010 | -999.000000 |    3.574442 |    7.817823 |    0.000012 |
|  17 |    2.331047 |    0.224576 |  120.620338 |  133.510849 | -999.000000 | -999.000000 | -999.000000 | -999.000000 |
|  20 |   40.648273 |  -73.816513 |    7.969516 |    0.226793 |    1.000000 |  -70.996628 |   40.000000 |  -75.000000 |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment