From ce7126b60e869a27b780f6bad1156aa61e999743 Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Thu, 6 Apr 2023 15:30:48 -0400 Subject: [PATCH] Added algorithm to read header --- Project.toml | 3 +++ src/header.jl | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index a288b4c..ceeb780 100644 --- a/Project.toml +++ b/Project.toml @@ -3,6 +3,9 @@ uuid = "5099a4e5-7ef4-45c0-baf4-116637be075d" authors = ["Jake Windle"] version = "1.0.0-DEV" +[deps] +BitConverter = "3a3ce9e8-98e7-11e9-0fa0-055639f146d3" + [compat] julia = "1" diff --git a/src/header.jl b/src/header.jl index 349345d..1763033 100644 --- a/src/header.jl +++ b/src/header.jl @@ -1,13 +1,51 @@ struct FITHeader sz::UInt8 +using Base: sizeof_ios_t + protocol::UInt8 + version::AbstractVector{<:Real} + size::Real + type::AbstractVector{<:Real} + crc::AbstractVector{<:Real} end +""" + length in bytes +""" +const DATA_SIZE_LENGTH = 4 + """ read_header(fit_file::IOStream) 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) - FITHeader(sz[1]) + 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) + 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