class GdbFlasher::IHex::Record

Constants

TYPES

Attributes

address[RW]
data[RW]
length[RW]
type[RW]

Public Class Methods

new() click to toggle source
# File lib/gdbflasher/ihex.rb, line 13
def initialize
  @length = nil
  @address = nil
  @type = nil
  @data = nil
end
parse(line) click to toggle source
# File lib/gdbflasher/ihex.rb, line 24
def self.parse(line)
  if line.match(/^:([0-9a-fA-F]{2})+$/).nil?
    raise "Malformed Intel Hex line: #{line}"
  end

  line.slice! 0

  bytes = Array.new(line.length / 2) { |i| line[i * 2..i * 2 + 1].hex }

  record = Record.new
  record.length = bytes[0]
  record.address = (bytes[1] << 8) | bytes[2]
  record.type = TYPES[bytes[3]]
  record.data = bytes[4..-2].pack("C*")

  checksum = (~bytes.reduce(:+) + 1) & 0xFF

  if !record.valid? || record.type.nil? || checksum != 0
    raise "Malformed Intel Hex line: #{line}"
  end

  record
end

Public Instance Methods

valid?() click to toggle source
# File lib/gdbflasher/ihex.rb, line 20
def valid?
  @length == @data.length
end