Class: RubyFit::Type
- Inherits:
-
Object
show all
- Extended by:
- Helpers
- Defined in:
- lib/rubyfit/type.rb
Constant Summary
Constants included
from Helpers
Helpers::DEGREES_TO_SEMICIRCLES, Helpers::GARMIN_TIME_OFFSET
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helpers
bytes2hex, bytes2num, bytes2str, deg2semicircles, fit2unix_timestamp, make_message_header, num2bytes, semicircles2deg, str2bytes, unix2fit_timestamp
Constructor Details
#initialize(opts = {}) ⇒ Type
Returns a new instance of Type.
6
7
8
9
10
11
12
13
14
|
# File 'lib/rubyfit/type.rb', line 6
def initialize(opts = {})
@val2bytes = opts[:val2bytes]
@bytes2val = opts[:bytes2val]
@rb2fit = opts[:rb2fit]
@fit2rb = opts[:fit2rb]
@default_bytes = opts[:default_bytes]
@byte_count = opts[:byte_count]
@fit_id = opts[:fit_id]
end
|
Class Method Details
.altitude ⇒ Object
148
149
150
151
152
153
|
# File 'lib/rubyfit/type.rb', line 148
def altitude
uint16({
rb2fit: ->(val, type) { ((val + 500) * 5).truncate },
fit2rb: ->(val, type) { val / 5.0 - 500 }
})
end
|
.byte(byte_count, opts = {}) ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/rubyfit/type.rb', line 68
def byte(byte_count, opts = {})
new({
fit_id: 0x0D,
default_bytes: [0xFF] * length,
val2bytes: ->(val) { val },
bytes2val: ->(bytes) { bytes },
}.merge(opts))
end
|
.centimeters ⇒ Object
141
142
143
144
145
146
|
# File 'lib/rubyfit/type.rb', line 141
def centimeters
uint32({
rb2fit: ->(val, type) { (val * 100).truncate },
fit2rb: ->(val, type) { val / 100.0 }
})
end
|
.duration ⇒ Object
155
156
157
158
159
160
|
# File 'lib/rubyfit/type.rb', line 155
def duration
uint32({
rb2fit: ->(val, type) { (val * 1000).truncate },
fit2rb: ->(val, type) { val / 1000.0 }
})
end
|
.enum(opts = {}) ⇒ Object
54
55
56
|
# File 'lib/rubyfit/type.rb', line 54
def enum(opts = {})
uint8(fit_id: 0x00)
end
|
.integer(opts = {}) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/rubyfit/type.rb', line 33
def integer(opts = {})
unsigned = opts.delete(:unsigned)
default = opts[:default]
unless default
bit_count = opts[:byte_count] * 8
bit_count -= 1 unless unsigned
default = 2**bit_count - 1
end
new({
default_bytes: num2bytes(default, opts[:byte_count]),
val2bytes: ->(val, type) { num2bytes(val, type.byte_count) },
bytes2val: ->(bytes, type) { bytes2num(bytes, type.byte_count, unsigned) },
}.merge(opts))
end
|
.semicircles ⇒ Object
134
135
136
137
138
139
|
# File 'lib/rubyfit/type.rb', line 134
def semicircles
sint32({
rb2fit: ->(val, type) { deg2semicircles(val) },
fit2rb: ->(val, type) { semicircles2deg(val) }
})
end
|
.sint16(opts = {}) ⇒ Object
85
86
87
|
# File 'lib/rubyfit/type.rb', line 85
def sint16(opts = {})
integer({unsigned: false, byte_count: 2, fit_id: 0x83}.merge(opts))
end
|
.sint32(opts = {}) ⇒ Object
93
94
95
|
# File 'lib/rubyfit/type.rb', line 93
def sint32(opts = {})
integer({unsigned: false, byte_count: 4, fit_id: 0x85}.merge(opts))
end
|
.sint64(opts = {}) ⇒ Object
101
102
103
|
# File 'lib/rubyfit/type.rb', line 101
def sint64(opts = {})
integer({unsigned: false, byte_count: 8, fit_id: 0x8E}.merge(opts))
end
|
.sint8(opts = {}) ⇒ Object
77
78
79
|
# File 'lib/rubyfit/type.rb', line 77
def sint8(opts = {})
integer({unsigned: false, byte_count: 1, fit_id: 0x01}.merge(opts))
end
|
.string(byte_count, opts = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'lib/rubyfit/type.rb', line 58
def string(byte_count, opts = {})
new({
fit_id: 0x07,
byte_count: byte_count,
default_bytes: [0x00] * byte_count,
val2bytes: ->(val, type) { str2bytes(val, type.byte_count) },
bytes2val: ->(bytes, type) { bytes2str(bytes) },
}.merge(opts))
end
|
.timestamp ⇒ Object
127
128
129
130
131
132
|
# File 'lib/rubyfit/type.rb', line 127
def timestamp
uint32({
rb2fit: ->(val, type) { unix2fit_timestamp(val) },
fit2rb: ->(val, type) { fit2unix_timestamp(val) }
})
end
|
.uint16(opts = {}) ⇒ Object
89
90
91
|
# File 'lib/rubyfit/type.rb', line 89
def uint16(opts = {})
integer({unsigned: true, byte_count: 2, fit_id: 0x84}.merge(opts))
end
|
.uint16z(opts = {}) ⇒ Object
113
114
115
|
# File 'lib/rubyfit/type.rb', line 113
def uint16z(opts = {})
integer({unsigned: true, default: 0, byte_count: 2, fit_id: 0x8B}.merge(opts))
end
|
.uint32(opts = {}) ⇒ Object
97
98
99
|
# File 'lib/rubyfit/type.rb', line 97
def uint32(opts = {})
integer({unsigned: true, byte_count: 4, fit_id: 0x86}.merge(opts))
end
|
.uint32z(opts = {}) ⇒ Object
117
118
119
|
# File 'lib/rubyfit/type.rb', line 117
def uint32z(opts = {})
integer({unsigned: true, default: 0, byte_count: 4, fit_id: 0x8C}.merge(opts))
end
|
.uint64(opts = {}) ⇒ Object
105
106
107
|
# File 'lib/rubyfit/type.rb', line 105
def uint64(opts = {})
integer({unsigned: true, byte_count: 8, fit_id: 0x8F}.merge(opts))
end
|
.uint64z(opts = {}) ⇒ Object
121
122
123
|
# File 'lib/rubyfit/type.rb', line 121
def uint64z(opts = {})
integer({unsigned: true, default: 0, byte_count: 8, fit_id: 0x90}.merge(opts))
end
|
.uint8(opts = {}) ⇒ Object
81
82
83
|
# File 'lib/rubyfit/type.rb', line 81
def uint8(opts = {})
integer({unsigned: true, byte_count: 1, fit_id: 0x02}.merge(opts))
end
|
.uint8z(opts = {}) ⇒ Object
109
110
111
|
# File 'lib/rubyfit/type.rb', line 109
def uint8z(opts = {})
integer({unsigned: true, default: 0, byte_count: 1, fit_id: 0x0A}.merge(opts))
end
|
Instance Method Details
#bytes2val(bytes) ⇒ Object
23
24
25
26
27
28
|
# File 'lib/rubyfit/type.rb', line 23
def bytes2val(bytes)
result = bytes
result = @bytes2val.call(result, self)
result = @fit2rb.call(result, self) if @fit2rb
result
end
|
#val2bytes(val) ⇒ Object
16
17
18
19
20
21
|
# File 'lib/rubyfit/type.rb', line 16
def val2bytes(val)
result = val
result = @rb2fit.call(result, self) if @rb2fit
result = @val2bytes.call(result, self)
result
end
|