From 37979af8490b180a8300fad58122c299b666e8c6 Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Thu, 6 Apr 2023 22:44:51 -0400 Subject: [PATCH] Move code around, read header into int/string --- src/FIT.jl | 10 ++++++++-- src/bytes.jl | 26 ++++++++++++++++++++++++++ src/header.jl | 49 +++++++++++++++++++------------------------------ 3 files changed, 53 insertions(+), 32 deletions(-) create mode 100644 src/bytes.jl diff --git a/src/FIT.jl b/src/FIT.jl index 9f241ec..198b399 100644 --- a/src/FIT.jl +++ b/src/FIT.jl @@ -1,9 +1,15 @@ module FIT - export greet_package -export read_header +export read_header, FITHeader + include("functions.jl") include("header.jl") +using .header + end +using .FIT +open("test.FIT", "r") do f + h = FIT.read_header(f) +end \ No newline at end of file diff --git a/src/bytes.jl b/src/bytes.jl new file mode 100644 index 0000000..e4d3baa --- /dev/null +++ b/src/bytes.jl @@ -0,0 +1,26 @@ + +""" + read_data_size(size::Vector{UInt8})::Real + +FIT headers have a data size field, with 4 bytes. +""" +function byte_vec_to_int(byte_vec::Vector{UInt8})::Integer + result::Integer = 0 + byte_vec = Int64.(byte_vec) + + for i ∈ 0:length(byte_vec) - 1 + result += byte_vec[1 + i] << (8 * i) + end + return result +end + +""" + byte_vec_to_string(byte_vec::Vector{UInt8})::AbstractString + +Given the byte vec, create a String +""" +function byte_vec_to_string(byte_vec::Vector{UInt8})::AbstractString + String(Char.(byte_vec)) +end + +# byte_vec_to_string(UInt8.(collect("ABC"))) \ No newline at end of file diff --git a/src/header.jl b/src/header.jl index c88bd01..a3bf477 100644 --- a/src/header.jl +++ b/src/header.jl @@ -1,17 +1,25 @@ -struct FITHeader - sz::UInt8 - protocol::UInt8 - version::AbstractVector{<:Real} - size::Real - type::AbstractVector{<:Real} - crc::AbstractVector{<:Real} -end + """ length in bytes """ + +module header + +export FITHeader, read_header +include("bytes.jl") + const DATA_SIZE_LENGTH = 4 +struct FITHeader + sz::UInt8 + protocol::UInt8 + version::Integer + size::Integer + type::String + crc::AbstractVector{<:Real} +end + """ read_header(fit_file::IOStream) @@ -20,31 +28,12 @@ Read the header of the given fit file, and return a FITHeader struct function read_header(fit_file::IOStream)::FITHeader sz = read(fit_file, 1; all=false)[1] protocol = read(fit_file, 1; all=false)[1] - version = read(fit_file, 2; all=false) - size = read_data_size!(read(fit_file, 4; all=false)) - type = read(fit_file, 4; all=false) + version = byte_vec_to_int(read(fit_file, 2; all=false)) + size = byte_vec_to_int(read(fit_file, 4; all=false)) + type = byte_vec_to_string(read(fit_file, 4; all=false)) crc = read(fit_file, 2; all=false) FITHeader(sz, protocol, version, size, type, crc) end -""" - read_data_size(size::Vector{UInt8})::Real - -FIT headers have a data size field, with 4 bytes. -""" -function read_data_size!(size::Vector{UInt8})::Real - data_size::Int64 = 0 - size = Int64.(size) - - for i ∈ 0:length(size) - 1 - data_size += size[1 + i] << (8 * i) - end - data_size -end - -function test_read_header() - open("test.FIT", "r") do f - read_header(f) - end end