class Crubyflie::CRTPPacket

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

Attributes

channel[R]
data[R]
header[R]
port[R]
size[R]

Public Class Methods

new(header=0, payload=[]) click to toggle source

Initialize a package with a header and data @param header [Integer] represents an 8 bit header @param payload [Array] @see set_data

# 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
unpack(data) click to toggle source

Creates a packet from a raw data array

# 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

Public Instance Methods

data=(new_data) click to toggle source

Set new data for this packet and update the size @param new_data [Array] the new data

# File lib/crubyflie/driver/crtp_packet.rb, line 100
def data=(new_data)
    @data = new_data
    @size = @data.size
end
data_repack() click to toggle source

Pack the data of the packet into unsigned chars when needed @return [String] binary data

# File lib/crubyflie/driver/crtp_packet.rb, line 145
def data_repack
    return @data.pack('C*')
end
modify_header(header=nil, port=nil, channel=nil) click to toggle source

Modify the full header, or the channel or the port @param header [Integer] a new full header. Prevails over the rest @param port [Integer] a new port (4 bits) @param channel [Integer] a new channel (2 bits)

# 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() click to toggle source

Concat the header and the data and return it @return [Array] header concatenated with data

# File lib/crubyflie/driver/crtp_packet.rb, line 136
def pack
    # According to official client, bytes 3 and 4 need
    # to be set for legacy support of the bootloader
    # (no matter what)
    [@header | 0x3 << 2].concat(@data)
end