Files
FIT.jl/src/body.jl
T

77 lines
1.9 KiB
Julia

module body
# TODO: Why do this?
import Base.show
export DefinitionHeader, DataRecord, read_row
include("bytes.jl"); using.bytes
include("mesg_num.jl"); using .mesg_num
struct DefinitionHeader
endianness::AbstractString
number::Int64
fields::UInt8
end
# TODO: field_number has a string value, type has a Julia type.
struct DefinitionField
field_number::AbstractString
size::UInt8
type::UInt8
end
struct DataRecord
hdr::DefinitionHeader
end
"""
read_row(f::io)
Read a row of the file at a time. Parse the row into a data or def message.
TODO: Endianness matters here. Need to resolve endianness with the architecture bit.
"""
function read_row(f::IO)::DefinitionHeader
header_byte = read(f, 1; all=false)[1]
definition_message = Bool(bit_at(header_byte, 2))
# The message byte is 7
# hdr = RecordHeader(
# Bool(bit_at(header_byte, 2)),
# Bool(bit_at(header_byte, 1)))
# TODO: hdr if compressed timestamp needs to be handled in special way.
if definition_message
fixed_bytes = read(f, 5; all=false)
hdr = DefinitionHeader(
endianness(fixed_bytes[2]), byte_vec_to_int(fixed_bytes[3:4]), fixed_bytes[end]
)
end
hdr_fields = [hdr_field(read(f, 3; all=false)) for i 1:hdr.fields + 1]
println(hdr_fields)
hdr
end
function hdr_field(vec::Vector{UInt8})::DefinitionField
println(vec)
DefinitionField(
get_mesg_num_string(vec[1]), vec[2], vec[3]
)
end
function endianness(b::UInt8)::AbstractString
Bool(b) ? "BigEndian" : "LittleEndian"
end
function show(io::IO, header::DefinitionHeader)
print(io, "DefinitionHeader($(header.endianness), $(header.number), $(header.fields) messages)")
end
function show(io::IO, field::DefinitionField)
print(io, "DefinitionField($(field.field_number), $(field.size), $(field.type))")
end
end