Class: HexData
- Inherits:
-
Object
- Object
- HexData
- Defined in:
- lib/hexdata.rb
Overview
The file hexdata.rb is available under the GNU GPL v3.
Instance Attribute Summary collapse
-
#enddef ⇒ String
The “end of file” mark.
-
#push_address ⇒ Integer
The address of the next record in the (Intel) hex file.
Instance Method Summary collapse
-
#ascii(index = nil) ⇒ Object
Reads the result, the ASCII / ANSI text, the evaluation (interpretation).
-
#checksum?(index = 0) ⇒ Integer
Reads the checksum of a record.
-
#clear! ⇒ NilClass
Resets the hex data.
-
#data ⇒ Array
Returns the records.
-
#data_end? ⇒ Integer
Returns the index of the first record that detected an “End of File” markup.
-
#delete_data!(index) ⇒ NilClass
The string to be written.
-
#initialize(sets = []) ⇒ HexData
constructor
Initalies the cash register hexdata.
-
#interpret ⇒ NilClass
Evaluate the (Intel) Hex data (interpretation) and make it available for reading.
-
#max_index? ⇒ Integer
Returns the number of records.
-
#push(str) ⇒ NilClass
Write a user-entered text to the records as (Intel) Hex.
-
#push_end ⇒ NilClass
Writes the “End of file” mark.
-
#push_user(str) ⇒ NilClass
Writes a string passed by the user to the data.
-
#reset_address! ⇒ NilClass
Resets the write address to the default value (16).
Constructor Details
#initialize(sets = []) ⇒ HexData
Initalies the cash register hexdata
57 58 59 60 61 |
# File 'lib/hexdata.rb', line 57 def initialize sets=[] @sets = sets @push_address = 16 @enddef = ":00000001FF" end |
Instance Attribute Details
#enddef ⇒ String
The “end of file” mark
51 52 53 |
# File 'lib/hexdata.rb', line 51 def enddef @enddef end |
#push_address ⇒ Integer
The address of the next record in the (Intel) hex file
51 52 53 |
# File 'lib/hexdata.rb', line 51 def push_address @push_address end |
Instance Method Details
#ascii(index = nil) ⇒ Object
Reads the result, the ASCII / ANSI text, the evaluation (interpretation).
167 168 169 |
# File 'lib/hexdata.rb', line 167 def ascii index=nil return (index ? @ascii[index] : @ascii.join("")) end |
#checksum?(index = 0) ⇒ Integer
Reads the checksum of a record.
160 161 162 |
# File 'lib/hexdata.rb', line 160 def checksum? index=0 return @sums[index] end |
#clear! ⇒ NilClass
Resets the hex data. All saved hex data will be deleted. Then HexData is ready to write.
136 137 138 139 140 141 |
# File 'lib/hexdata.rb', line 136 def clear! @sets = nil.to_a @sums = nil.to_a @push_address = 16 nil end |
#data ⇒ Array
No copy of the array is created. If you edit the arrays, this affects the HexData class. If you want a copy of the array, you can, for example, use the .clone method.
Returns the records
66 67 68 |
# File 'lib/hexdata.rb', line 66 def data @sets end |
#data_end? ⇒ Integer
Returns the index of the first record that detected an “End of File” markup.
151 152 153 154 155 |
# File 'lib/hexdata.rb', line 151 def data_end? for i in 0...@sets.length return i if @sets[i] == @enddef end end |
#delete_data!(index) ⇒ NilClass
The string to be written.
129 130 131 132 |
# File 'lib/hexdata.rb', line 129 def delete_data! index @sets.delete_at index nil end |
#interpret ⇒ NilClass
Evaluate the (Intel) Hex data (interpretation) and make it available for reading.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/hexdata.rb', line 72 def interpret @ascii = nil.to_a @sums = nil.to_a @sets.each { |data| if data[0] != ":" raise "Invalid data! Data: #{data}" end len = data[1 .. 2].to_i 16 address = data[3 .. 6] type = data[7 .. 8] field = data[9 .. len * 2 + 8] sum = data[-2 .. -1].to_i 16 str = "" csum = len + address.to_i(16) + type.to_i(16) #p field for i in (0..field.length-1).step(2) v = field[i .. i + 1].to_i 16 csum += v str += v.chr end csum &= 255 bsum = "" csum.to_s(2).each_char { |c| bsum += (c=="0"?"1":"0") } while bsum.length < 8 bsum = "1" + bsum end if bsum.to_i(2) + 1 != sum raise "Invalid checksum! Data: #{data}" end @ascii << str if type == "00" @sums << sum } nil end |
#max_index? ⇒ Integer
Returns the number of records.
145 146 147 |
# File 'lib/hexdata.rb', line 145 def max_index? @sets.length end |
#push(str) ⇒ NilClass
Write a user-entered text to the records as (Intel) Hex.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/hexdata.rb', line 181 def push str if @push_address > 65535 raise "To many data!" end sum = str.length + @push_address res = "#{str.length.to_s 16}" while res.length < 2 res = "0" + res end res = ":" + res tmp = @push_address.to_s 16 while tmp.length < 4 tmp = "0" + tmp end res += tmp + "00" str.each_char { |c| sum += c.ord tmp = c.ord.to_s 16 while tmp.length < 2 tmp = "0" + tmp end res += tmp } sum &= 255 bsum = "" sum.to_s(2).each_char { |c| bsum += (c=="0"?"1":"0") } while bsum.length < 8 bsum = "1" + bsum end res += bsum.to_i(2).+(1).to_s 16 @sets << res @push_address += 1 end |
#push_end ⇒ NilClass
Writes the “End of file” mark. This is appended to the end of the records.
173 174 175 176 |
# File 'lib/hexdata.rb', line 173 def push_end @sets << @enddef nil end |
#push_user(str) ⇒ NilClass
Writes a string passed by the user to the data.
121 122 123 124 |
# File 'lib/hexdata.rb', line 121 def push_user str @sets << str nil end |
#reset_address! ⇒ NilClass
Resets the write address to the default value (16).
113 114 115 116 |
# File 'lib/hexdata.rb', line 113 def reset_address! @push_address = 16 nil end |