Class: Sftui::Frame

Inherits:
Object
  • Object
show all
Defined in:
lib/sftui/frame.rb

Overview

Len Address (4 bytes) Data ----—-----—-----————————----—-+ | | | | | | |0xff|0xff| ----—-----—-----————————----—-+

Constant Summary collapse

LENGTH_LENGTH =
1
ADDRESS_LENGTH =
4
TERMINATE_LENGTH =
2
TERMINATE_CHAR =
0xff
BITWISE_MASK =
0xff
TRANSACTION_ENDING_CHAR =
0x0d

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Frame

Returns a new instance of Frame.



21
22
23
# File 'lib/sftui/frame.rb', line 21

def initialize(bytes)
  @bytes = bytes
end

Instance Attribute Details

#bytesObject

Returns the value of attribute bytes.



12
13
14
# File 'lib/sftui/frame.rb', line 12

def bytes
  @bytes
end

Instance Method Details

#+(other) ⇒ Object

rubocop:disable Metrics/AbcSize



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sftui/frame.rb', line 35

def +(other) # rubocop:disable Metrics/AbcSize
  raise unless other.completed? && other.machine_id == machine_id

  combined_data_bytes = data_bytes + other.data_bytes
  combined_length = LENGTH_LENGTH + ADDRESS_LENGTH + TERMINATE_LENGTH + combined_data_bytes.length

  Frame.new(
    to_bytes(combined_length, LENGTH_LENGTH) +
    addr_bytes +
    combined_data_bytes +
    terminate_bytes
  )
end

#addr_bytesObject



67
68
69
# File 'lib/sftui/frame.rb', line 67

def addr_bytes
  bytes[addr_offset..addr_offset + ADDRESS_LENGTH - 1]
end

#completed?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/sftui/frame.rb', line 25

def completed?
  bytes.length == length && bytes[(-1 - TERMINATE_LENGTH + 1)..-1] == terminate_bytes
end

#data_bytesObject



53
54
55
56
57
# File 'lib/sftui/frame.rb', line 53

def data_bytes
  return [] unless completed?

  bytes[data_offset..(-1 - TERMINATE_LENGTH)]
end

#displayObject

rubocop:disable Metrics/AbcSize



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sftui/frame.rb', line 75

def display # rubocop:disable Metrics/AbcSize
  table = TTY::Table.new(header: %w[Length Address Data Terminate])
  table <<
    [
      length_bytes.map { |byte| to_hex(byte) }.join(' '),
      addr_bytes.map { |byte| to_hex(byte) }.join(' '),
      data_bytes.map { |byte| to_hex(byte) }.join(' '),
      terminate_bytes.map { |byte| to_hex(byte) }.join(' ')
    ]
  table.render(:unicode, alignments: %i[center center])
end

#last_frame_in_transaction?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/sftui/frame.rb', line 59

def last_frame_in_transaction?
  data_bytes[-1] == TRANSACTION_ENDING_CHAR
end

#length_bytesObject



63
64
65
# File 'lib/sftui/frame.rb', line 63

def length_bytes
  bytes[0..LENGTH_LENGTH - 1]
end

#machine_idObject



29
30
31
32
33
# File 'lib/sftui/frame.rb', line 29

def machine_id
  return unless completed?

  to_integer(addr_bytes)
end

#sanitized_dataObject



49
50
51
# File 'lib/sftui/frame.rb', line 49

def sanitized_data
  last_frame_in_transaction? ? data_bytes[0..-2].pack('c*') : data_bytes.pack('c*')
end

#terminate_bytesObject



71
72
73
# File 'lib/sftui/frame.rb', line 71

def terminate_bytes
  Array.new(TERMINATE_LENGTH, TERMINATE_CHAR)
end