Class: LegoEv3::BaseConnection
- Inherits:
-
Object
- Object
- LegoEv3::BaseConnection
show all
- Defined in:
- lib/connection/base.rb
Instance Method Summary
collapse
Constructor Details
Returns a new instance of BaseConnection.
3
4
5
|
# File 'lib/connection/base.rb', line 3
def initialize
@to_send = []
end
|
Instance Method Details
#flush ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/connection/base.rb', line 11
def flush
@connection ||= create_connection
joined_command = @to_send
.map{ |(c, _)| c }
.join(';')
callbacks = @to_send
.map{ |(_, c)| c }
joined_response, time = LegoEv3::with_timer do
call_connection(joined_command)
end
puts "#{joined_command}. #{time} ms."
responses = joined_response.split("\n")
callbacks.each_with_index.each do |c, i|
c.call(responses[i]) if c
end
@to_send.clear
end
|
#send(command, &callback) ⇒ Object
7
8
9
|
# File 'lib/connection/base.rb', line 7
def send(command, &callback)
@to_send << [command, callback]
end
|