Move code around, read header into int/string

This commit is contained in:
2023-04-06 22:44:51 -04:00
parent b6b724f949
commit 37979af849
3 changed files with 53 additions and 32 deletions
+8 -2
View File
@@ -1,9 +1,15 @@
module FIT module FIT
export greet_package export greet_package
export read_header export read_header, FITHeader
include("functions.jl") include("functions.jl")
include("header.jl") include("header.jl")
using .header
end end
using .FIT
open("test.FIT", "r") do f
h = FIT.read_header(f)
end
+26
View File
@@ -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")))
+19 -30
View File
@@ -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 length in bytes
""" """
module header
export FITHeader, read_header
include("bytes.jl")
const DATA_SIZE_LENGTH = 4 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) 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 function read_header(fit_file::IOStream)::FITHeader
sz = read(fit_file, 1; all=false)[1] sz = read(fit_file, 1; all=false)[1]
protocol = read(fit_file, 1; all=false)[1] protocol = read(fit_file, 1; all=false)[1]
version = read(fit_file, 2; all=false) version = byte_vec_to_int(read(fit_file, 2; all=false))
size = read_data_size!(read(fit_file, 4; all=false)) size = byte_vec_to_int(read(fit_file, 4; all=false))
type = read(fit_file, 4; all=false) type = byte_vec_to_string(read(fit_file, 4; all=false))
crc = read(fit_file, 2; all=false) crc = read(fit_file, 2; all=false)
FITHeader(sz, protocol, version, size, type, crc) FITHeader(sz, protocol, version, size, type, crc)
end 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 end