Class: Crubyflie::CRTPPacket
- Inherits:
-
Object
- Object
- Crubyflie::CRTPPacket
- Defined in:
- lib/crubyflie/driver/crtp_packet.rb
Overview
A data packet. Raw packet data is sent to the USB driver Some related docs: wiki.bitcraze.se/ projects:crazyflie:firmware:comm_protocol#serial_port
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#data ⇒ Object
Returns the value of attribute data.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
-
.unpack(data) ⇒ Object
Creates a packet from a raw data array.
Instance Method Summary collapse
-
#data_repack ⇒ String
Pack the data of the packet into unsigned chars when needed.
-
#initialize(header = 0, payload = []) ⇒ CRTPPacket
constructor
Initialize a package with a header and data.
-
#modify_header(header = nil, port = nil, channel = nil) ⇒ Object
Modify the full header, or the channel or the port.
-
#pack ⇒ Array
Concat the header and the data and return it.
Constructor Details
#initialize(header = 0, payload = []) ⇒ CRTPPacket
Initialize a package with a header and data
92 93 94 95 96 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 92 def initialize(header=0, payload=[]) modify_header(header) @data = payload || [] @size = data.size #+ 1 # header. Bytes end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
88 89 90 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88 def channel @channel end |
#data ⇒ Object
Returns the value of attribute data.
88 89 90 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88 def data @data end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
88 89 90 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88 def header @header end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
88 89 90 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88 def port @port end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
88 89 90 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88 def size @size end |
Class Method Details
.unpack(data) ⇒ Object
Creates a packet from a raw data array
127 128 129 130 131 132 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 127 def self.unpack(data) return CRTPPacket.new() if !data.is_a?(Array) || data.empty?() header = data[0] data = data[1..-1] CRTPPacket.new(header, data) end |
Instance Method Details
#data_repack ⇒ String
Pack the data of the packet into unsigned chars when needed
142 143 144 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 142 def data_repack return @data.pack('C*') end |
#modify_header(header = nil, port = nil, channel = nil) ⇒ Object
Modify the full header, or the channel or the port
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 109 def modify_header(header=nil, port=nil, channel=nil) if header @header = header @channel = header & 0b11 # lowest 2 bits of header @port = (header >> 4) & 0b1111 # bits 4-7 return end if channel @channel = channel & 0b11 # 2 bits @header = (@header & 0b11111100) | @channel end if port @port = (port & 0b1111) # 4 bits @header = (@header & 0b00001111) | @port << 4 end end |
#pack ⇒ Array
Concat the header and the data and return it
136 137 138 |
# File 'lib/crubyflie/driver/crtp_packet.rb', line 136 def pack [@header].concat(@data) end |