6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/rubyfit/writer.rb', line 6
def write(stream, opts = {})
raise "Can't start write mode from #{@state}" if @state
@state = :write
@local_nums = {}
@last_local_num = -1
@stream = stream
%i(start_time duration course_point_count track_point_count name
total_distance time_created start_x start_y end_x end_y).each do |key|
raise ArgumentError.new("Missing required option #{key}") unless opts[key]
end
start_time = opts[:start_time].to_i
duration = opts[:duration].to_i
@data_crc = 0
data_size = calculate_data_size(opts[:course_point_count], opts[:track_point_count])
write_data(RubyFit::MessageWriter.(data_size))
write_message(:file_id, {
time_created: opts[:time_created],
type: 6, manufacturer: 1, product: PRODUCT_ID,
serial_number: 0,
})
write_message(:course, { name: opts[:name] })
write_message(:lap, {
start_time: start_time,
timestamp: start_time,
total_elapsed_time: duration,
total_timer_time: duration,
start_x: opts[:start_x],
start_y: opts[:start_y],
end_x: opts[:end_x],
end_y: opts[:end_y],
total_distance: opts[:total_distance]
})
write_message(:event, {
timestamp: start_time,
event: :timer,
event_type: :start,
event_group: 0
})
yield
write_message(:event, {
timestamp: start_time + duration,
event: :timer,
event_type: :stop_disable_all,
event_group: 0
})
write_data(RubyFit::MessageWriter.crc(@data_crc))
@state = nil
end
|