intel_hex 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a67ed5207fb30323f40db0ef565965c501d7d85c3d040bbaf80eec3642c076b0
4
- data.tar.gz: 7242a9b940678f30065fbfe71ff5e06c1d8cc953cd63f122eb1b6d9ce0416837
3
+ metadata.gz: 5d15e5760f2d577b90bcb4265ce05878a505aa0ac0cc613d671324867a821ace
4
+ data.tar.gz: cc2dea512304abc088412f5c3e3338fec83aaf3208bec0d4a31bfa51b305864f
5
5
  SHA512:
6
- metadata.gz: e2db72c94bb8b1c062ce981255320f2b7dbb15441acdecc640ea4a4cfa5f411756c0e9ed22a485aef97c773bca82a37f8f0a8c7f4a7d5e2e134c5e5e9b58e3e3
7
- data.tar.gz: e3db780d5faaac61770594eb0fe85055619d0bf76844afea4e591167a6d535636f8a610046126176bebbadfdcd60560625af92c308869b1cd25b3321dbc640c9
6
+ metadata.gz: 2c39cddd4c1166c65d70b29fee0bb65ed0f6cb0a702d176c80bac7f3377925966cfeb73a1766beeb8dd05c4c4e0436e377e02d826031654ad88305108fce67b9
7
+ data.tar.gz: '04966a59f357db62b1197ee82d5c063f18b115c17496fa1bc8b9429055a6d1b17dc8d45791a20f108484b277f237063748cb32db15b80949b922b544b4ffe43a'
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'optparse'
4
5
  require 'intel_hex'
@@ -9,30 +10,27 @@ require 'pp'
9
10
  }
10
11
 
11
12
  OptionParser.new do |opts|
12
- opts.on('--help', 'Print this help.') { puts; puts opts; puts; exit }
13
- opts.on('-f', '--filename=FILE', 'Load the specified file.') { |o| @options[:file] = o }
13
+ opts.on('--help', 'Print this help.') do
14
+ puts
15
+ puts opts
16
+ puts
17
+ exit
18
+ end
19
+ opts.on('-f', '--filename=FILE', 'Load the specified file.') do |o|
20
+ @options[:file] = o
21
+ end
14
22
  end.parse!
15
23
 
16
- raise "No file specified" unless @options[:file]
24
+ raise 'No file specified' unless @options[:file]
17
25
 
18
26
  intel_hex_file = IntelHex::FileReader.new(@options[:file])
19
27
 
20
28
  intel_hex_file.each_record do |record|
21
- case record.type
22
- when :data
23
- record_data = "data=" + record.data.map { |b| "%02x" % b }.join(" ")
24
- when :eof
25
- record_data = ""
26
- else
27
- record_data = "value=" + record.send(data.type)
28
- end
29
-
30
- puts "Record type=%-4s offset=%-4d length=%-4d checksum=%-4d %s" % [
29
+ puts 'Record type=%-4s offset=%-4d length=%-4d checksum=%-4d %s' % [
31
30
  record.type,
32
31
  record.offset,
33
32
  record.length,
34
33
  record.checksum,
35
- record_data,
34
+ record.value_s,
36
35
  ]
37
36
  end
38
-
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IntelHex
2
4
  end
3
5
 
4
6
  require 'intel_hex/version'
5
7
  require 'intel_hex/record'
6
- require 'intel_hex/file_reader'
8
+ require 'intel_hex/file_reader'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IntelHex
2
4
  class FileReader
3
5
  def initialize(filename)
@@ -12,7 +14,7 @@ module IntelHex
12
14
 
13
15
  def each_record
14
16
  return to_enum(:each_record) unless block_given?
15
-
17
+
16
18
  file = File.open(@filename, 'r')
17
19
 
18
20
  begin
@@ -25,10 +27,10 @@ module IntelHex
25
27
 
26
28
  nil
27
29
  end
28
-
30
+
29
31
  def each_byte_with_address
30
32
  return to_enum(:each_byte_with_address) unless block_given?
31
-
33
+
32
34
  each_record do |record|
33
35
  case record.type
34
36
  when :data
@@ -49,4 +51,4 @@ module IntelHex
49
51
  nil
50
52
  end
51
53
  end
52
- end
54
+ end
@@ -1,23 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IntelHex
2
4
  class MisformattedFileError < RuntimeError; end
3
- class InvalidTypeError < RuntimeError; end
4
- class InvalidLengthError < RuntimeError; end
5
- class InvalidOffsetError < RuntimeError; end
6
- class InvalidDataError < RuntimeError; end
7
- class InvalidChecksumError < RuntimeError; end
5
+
6
+ class ValidationError < StandardError; end
7
+ class InvalidTypeError < ValidationError; end
8
+ class InvalidLengthError < ValidationError; end
9
+ class InvalidOffsetError < ValidationError; end
10
+ class InvalidDataError < ValidationError; end
11
+ class InvalidChecksumError < ValidationError; end
8
12
 
9
13
  class Record
10
- TYPES = [
11
- :data,
12
- :eof,
13
- :esa,
14
- :ssa,
15
- :ela,
16
- :sla,
17
- ]
18
-
19
- TYPE_MAP = TYPES.each_with_index.inject({}) { |h, (k, i)| h[k] = i; h }
20
-
14
+ TYPES = %i[
15
+ data
16
+ eof
17
+ esa
18
+ ssa
19
+ ela
20
+ sla
21
+ ].freeze
22
+
23
+ TYPE_MAP = TYPES.each_with_index.each_with_object({}) { |(k, i), h| h[k] = i }
24
+
21
25
  TYPE_DATA_LENGTH = {
22
26
  data: (1..255),
23
27
  eof: 0,
@@ -25,14 +29,14 @@ module IntelHex
25
29
  ssa: 4,
26
30
  ela: 2,
27
31
  sla: 4,
28
- }
32
+ }.freeze
29
33
 
30
34
  def self.parse(line)
31
- raise MisformattedFileError.new("Expected ':' at start of line") unless line[0] == ':'
32
- raise MisformattedFileError.new("Line length incorrect") unless line.size >= (1 + 2 + 4 + 2)
35
+ raise MisformattedFileError, 'Expected \':\' at start of line' unless line[0] == ':'
36
+ raise MisformattedFileError, 'Line length incorrect' unless line.size >= (1 + 2 + 4 + 2)
33
37
 
34
38
  length = line[1..2].to_i(16)
35
- data_end = (9 + length*2)
39
+ data_end = (9 + length * 2)
36
40
 
37
41
  offset = line[3..6].to_i(16)
38
42
  type = TYPES[line[7..8].to_i(16)]
@@ -81,7 +85,8 @@ module IntelHex
81
85
  attr_reader :checksum
82
86
  attr_reader :line
83
87
 
84
- def initialize(type, length = 0, offset = 0, data = [], checksum=nil, line: nil, validate: false)
88
+ # rubocop:disable Metrics/ParameterLists
89
+ def initialize(type, length = 0, offset = 0, data = [], checksum = nil, line: nil, validate: false)
85
90
  @type = type
86
91
  @length = length
87
92
  @offset = offset
@@ -92,17 +97,28 @@ module IntelHex
92
97
 
93
98
  self.validate if validate
94
99
  end
100
+ # rubocop:enable Metrics/ParameterLists
101
+
102
+ def data_s
103
+ '[' + data.map { |b| '%02x' % b }.join(' ') + ']'
104
+ end
105
+
106
+ def value_s
107
+ return '' if type == :eof
108
+
109
+ "#{type}=#{type == :data ? data_s : send(type)}"
110
+ end
95
111
 
96
112
  def to_s
97
- "#<#{self.class.name} type=#{type} offset=#{offset} length=#{length}>"
113
+ "#<#{self.class.name} type=#{type} offset=#{offset} length=#{length} #{value_s}>"
98
114
  end
99
115
 
100
116
  def to_ascii
101
- ":%02X%04X%02X%s%02X" % [
117
+ ':%02X%04X%02X%s%02X' % [
102
118
  length,
103
119
  offset,
104
120
  TYPE_MAP[type],
105
- data.map { |b| "%02X" % b }.join,
121
+ data.map { |b| '%02X' % b }.join,
106
122
  checksum,
107
123
  ]
108
124
  end
@@ -111,7 +127,7 @@ module IntelHex
111
127
  return @calculated_checksum if @calculated_checksum
112
128
 
113
129
  sum = length + ((offset & 0xff00) >> 8) + (offset & 0x00ff) + TYPE_MAP[type]
114
- sum += data.inject(0) { |s, n| s += n; s }
130
+ sum += data.sum
115
131
 
116
132
  @calculated_checksum = (((sum & 0xff) ^ 0xff) + 1) & 0xff
117
133
  end
@@ -122,12 +138,10 @@ module IntelHex
122
138
  end
123
139
 
124
140
  def valid?
125
- begin
126
- validate
127
- return true
128
- rescue
129
- return false
130
- end
141
+ validate
142
+ true
143
+ rescue ValidationError
144
+ false
131
145
  end
132
146
 
133
147
  def validate
@@ -139,12 +153,14 @@ module IntelHex
139
153
  end
140
154
 
141
155
  def validate_type
142
- raise InvalidTypeError.new("Type #{type} is invalid") unless TYPE_MAP.include?(type)
156
+ return if TYPE_MAP.include?(type)
157
+
158
+ raise InvalidTypeError, "Type #{type} is invalid"
143
159
  end
144
160
 
145
161
  def validate_offset
146
- raise InvalidOffsetError.new("Offset #{offset} is negative") unless offset >= 0
147
- raise InvalidOffsetError.new("Offset #{offset} is too large") unless offset < 2**16
162
+ raise InvalidOffsetError, "Offset #{offset} is negative" unless offset >= 0
163
+ raise InvalidOffsetError, "Offset #{offset} is too large" unless offset < 2**16
148
164
  end
149
165
 
150
166
  def validate_length
@@ -157,20 +173,24 @@ module IntelHex
157
173
  return if valid_length.include?(length)
158
174
  end
159
175
 
160
- raise InvalidLengthError.new("Length for type #{type} must be #{valid_length}; #{length} given")
176
+ raise InvalidLengthError, "Length for type #{type} must be #{valid_length}; #{length} given"
161
177
  end
162
178
 
163
179
  def validate_data
164
- raise InvalidDataError.new("Data length #{data.size} does not match length #{length}") unless data.size == length
180
+ return if data.size == length
181
+
182
+ raise InvalidDataError, "Data length #{data.size} does not match length #{length}"
165
183
  end
166
184
 
167
185
  def validate_checksum
168
- raise InvalidChecksumError.new("Checksum value #{checksum} does not match expected checksum #{calculate_checksum}") unless calculate_checksum == checksum
186
+ return if calculate_checksum == checksum
187
+
188
+ raise InvalidChecksumError, "Checksum value #{checksum} does not match expected checksum #{calculate_checksum}"
169
189
  end
170
190
 
171
191
  def each_byte_with_address
172
192
  return Enumerator.new(self, :each_byte_with_address) unless block_given?
173
-
193
+
174
194
  data.each_with_index do |byte, i|
175
195
  yield byte, offset + i
176
196
  end
@@ -179,12 +199,12 @@ module IntelHex
179
199
  end
180
200
 
181
201
  def data=(value)
182
- raise "Incorrect data type" unless value.is_a?(Array)
183
- raise InvalidLengthError.new("Data length #{value.size} too large") unless value.size <= 255
202
+ raise 'Incorrect data type' unless value.is_a?(Array)
203
+ raise InvalidLengthError, "Data length #{value.size} too large" unless value.size <= 255
204
+
184
205
  @data = value
185
206
  @length = data.size
186
207
  @checksum = recalculate_checksum
187
- nil
188
208
  end
189
209
 
190
210
  def data_to_uint16(offset = 0)
@@ -192,9 +212,9 @@ module IntelHex
192
212
  end
193
213
 
194
214
  def uint16_to_data(value, offset = 0)
195
- self.data[(offset...(offset+2))] = [
215
+ data[(offset...(offset + 2))] = [
196
216
  (value & 0xff00) >> 8,
197
- value & 0x00ff
217
+ value & 0x00ff,
198
218
  ]
199
219
  @length = data.size
200
220
  @checksum = recalculate_checksum
@@ -205,24 +225,24 @@ module IntelHex
205
225
  end
206
226
 
207
227
  def uint32_to_data(value, offset = 0)
208
- self.data[(offset...(offset+4))] = [
228
+ data[(offset...(offset + 4))] = [
209
229
  (value & 0xff000000) >> 24,
210
230
  (value & 0x00ff0000) >> 16,
211
231
  (value & 0x0000ff00) >> 8,
212
- value & 0x000000ff
232
+ value & 0x000000ff,
213
233
  ]
214
234
  @length = data.size
215
235
  @checksum = recalculate_checksum
216
236
  end
217
237
 
218
- alias_method :esa, :data_to_uint16
219
- alias_method :ssa, :data_to_uint32
220
- alias_method :ela, :data_to_uint16
221
- alias_method :sla, :data_to_uint32
238
+ alias esa data_to_uint16
239
+ alias ssa data_to_uint32
240
+ alias ela data_to_uint16
241
+ alias sla data_to_uint32
222
242
 
223
- alias_method :esa=, :uint16_to_data
224
- alias_method :ssa=, :uint32_to_data
225
- alias_method :ela=, :uint16_to_data
226
- alias_method :sla=, :uint32_to_data
243
+ alias esa= uint16_to_data
244
+ alias ssa= uint32_to_data
245
+ alias ela= uint16_to_data
246
+ alias sla= uint32_to_data
227
247
  end
228
- end
248
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IntelHex
2
- VERSION = '0.5.2'
4
+ VERSION = '0.5.3'
3
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intel_hex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Cole