Class: Crubyflie::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/crubyflie/crazyflie/console.rb

Overview

The Console facility is used to read characters that have been printer using printf in the crazyflie firmware

Instance Method Summary collapse

Constructor Details

#initialize(crazyflie) ⇒ Console

Initialize the console

Parameters:



27
28
29
30
31
# File 'lib/crubyflie/crazyflie/console.rb', line 27

def initialize(crazyflie)
    @crazyflie = crazyflie
    @in_queue = crazyflie.crtp_queues[:console]
    @read_thread = nil
end

Instance Method Details

#read(&block) ⇒ Object

Reads all the characters from the Crazyflie that are queued, until the queue is empty, and then return only after executing the given block for each packet that was in the queue.

Parameters:

  • block (Proc)

    a block to call with the read information



37
38
39
40
41
42
# File 'lib/crubyflie/crazyflie/console.rb', line 37

def read(&block)
    while @in_queue.size > 0 do
        packet = @in_queue.pop() # block
        yield(packet.data_repack) if block_given?
    end
end

#start_reading(&block) ⇒ Object

Reads all the characters from the Crazyflie constantly and yields on the the given block is called with the payload. This call will return immediately, but the block will be called asynchronously until #stop_reading() is called. Use stop_read() to stop.

Parameters:

  • block (Proc)

    a block to call with the read information



51
52
53
54
55
56
57
58
59
# File 'lib/crubyflie/crazyflie/console.rb', line 51

def start_reading(&block)
    stop_reading()
    @read_thread = Thread.new do
        loop do
            read(&block)
            sleep 0.3 # no hurries?
        end
    end
end

#stop_readingObject

Stops reading characters from the Crazyflie



62
63
64
65
# File 'lib/crubyflie/crazyflie/console.rb', line 62

def stop_reading
    @read_thread.kill() if @read_thread
    @read_thread = nil
end