Class: Px4LogReader::LogBuffer
- Inherits:
-
Object
- Object
- Px4LogReader::LogBuffer
- Defined in:
- lib/px4_log_reader/log_buffer.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#read_position ⇒ Object
readonly
Returns the value of attribute read_position.
-
#write_position ⇒ Object
readonly
Returns the value of attribute write_position.
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(size) ⇒ LogBuffer
constructor
A new instance of LogBuffer.
- #read(num_bytes) ⇒ Object
- #reset ⇒ Object
- #write(file) ⇒ Object
Constructor Details
#initialize(size) ⇒ LogBuffer
Returns a new instance of LogBuffer.
41 42 43 44 45 |
# File 'lib/px4_log_reader/log_buffer.rb', line 41 def initialize( size ) @data = Array.new( size, 0x00 ) @read_position = 0 @write_position = 0 end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
37 38 39 |
# File 'lib/px4_log_reader/log_buffer.rb', line 37 def data @data end |
#read_position ⇒ Object (readonly)
Returns the value of attribute read_position.
38 39 40 |
# File 'lib/px4_log_reader/log_buffer.rb', line 38 def read_position @read_position end |
#write_position ⇒ Object (readonly)
Returns the value of attribute write_position.
39 40 41 |
# File 'lib/px4_log_reader/log_buffer.rb', line 39 def write_position @write_position end |
Instance Method Details
#empty? ⇒ Boolean
85 86 87 |
# File 'lib/px4_log_reader/log_buffer.rb', line 85 def empty? return ( @read_position == @write_position ) end |
#read(num_bytes) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/px4_log_reader/log_buffer.rb', line 70 def read( num_bytes ) data = '' if !empty? last_index = @read_position + num_bytes last_index = @data.size if last_index > @data.size read_count = last_index - @read_position data = @data[ @read_position, read_count ].pack('C*') @read_position += read_count end return data end |
#reset ⇒ Object
47 48 49 50 |
# File 'lib/px4_log_reader/log_buffer.rb', line 47 def reset @read_position = 0 @write_position = 0 end |
#write(file) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/px4_log_reader/log_buffer.rb', line 52 def write( file ) while ( @write_position < @data.size ) do begin bytes = file.read( @data.size - @write_position ) if bytes write_bytes( bytes.unpack('C*') ) else break end rescue EOFError => error break end end end |