Class: Sphero
- Inherits:
-
Object
show all
- Defined in:
- lib/sphero.rb,
lib/sphero/request.rb,
lib/sphero/response.rb
Defined Under Namespace
Classes: Request, Response
Constant Summary
collapse
- VERSION =
'1.5.0'
- FORWARD =
0
- RIGHT =
90
- BACKWARD =
180
- LEFT =
270
- DEFAULT_RETRIES =
3
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#auto_reconnect ⇒ Object
-
#auto_reconnect=(time_s) ⇒ Object
-
#back_led_output=(h) ⇒ Object
-
#bluetooth_info ⇒ Object
-
#close ⇒ Object
-
#color(colorname, persistant = false) ⇒ Object
-
#configure_collision_detection(meth, x_t, y_t, x_spd, y_spd, dead) ⇒ Object
configure collision detection messages.
-
#disable_auto_reconnect ⇒ Object
-
#heading=(h) ⇒ Object
-
#initialize(dev) ⇒ Sphero
constructor
A new instance of Sphero.
-
#keep_going(duration) ⇒ Object
just a nicer alias for Ruby’s own sleep.
-
#ping ⇒ Object
-
#power_state ⇒ Object
-
#rgb(r, g, b, persistant = false) ⇒ Object
-
#roll(speed, heading, state = true) ⇒ Object
-
#rotation_rate=(h) ⇒ Object
Rotation Rate 0x00 - 0xFF.
-
#set_data_streaming(n, m, mask, pcnt, mask2) ⇒ Object
configure data streaming notification messages.
-
#set_power_notification(enable = true) ⇒ Object
configure power notification messages.
-
#sphero_sleep(wakeup = 0, macro = 0) ⇒ Object
-
#stabilization=(on) ⇒ Object
-
#stop ⇒ Object
-
#user_led ⇒ Object
This retrieves the “user LED color” which is stored in the config block (which may or may not be actively driven to the RGB LED).
-
#version ⇒ Object
Constructor Details
#initialize(dev) ⇒ Sphero
Returns a new instance of Sphero.
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/sphero.rb', line 39
def initialize dev
if dev.is_a?(String)
initialize_serialport dev
else
@sp = dev
end
@dev = 0x00
@seq = 0x00
@lock = Mutex.new
@messages = Queue.new
@packets = Queue.new
@response_queue = Queue.new
@responses = []
Thread.new {
loop do
write @packets.pop
end
}
Thread.new {
loop do
@responses << @response_queue.pop
end
}
end
|
Instance Attribute Details
#connection_types ⇒ Object
Returns the value of attribute connection_types.
15
16
17
|
# File 'lib/sphero.rb', line 15
def connection_types
@connection_types
end
|
#messages ⇒ Object
Returns the value of attribute messages.
15
16
17
|
# File 'lib/sphero.rb', line 15
def messages
@messages
end
|
#packets ⇒ Object
Returns the value of attribute packets.
15
16
17
|
# File 'lib/sphero.rb', line 15
def packets
@packets
end
|
#response_queue ⇒ Object
Returns the value of attribute response_queue.
15
16
17
|
# File 'lib/sphero.rb', line 15
def response_queue
@response_queue
end
|
#responses ⇒ Object
Returns the value of attribute responses.
15
16
17
|
# File 'lib/sphero.rb', line 15
def responses
@responses
end
|
Class Method Details
.start(dev, &block) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/sphero.rb', line 18
def start(dev, &block)
retries_left = DEFAULT_RETRIES
begin
sphero = self.new dev
if (block_given?)
begin
sphero.instance_eval(&block)
ensure
sphero.close
end
return nil
end
return sphero
rescue Errno::EBUSY
puts retries_left
retries_left = retries_left - 1
retry unless retries_left < 0
end
end
|
Instance Method Details
#auto_reconnect=(time_s) ⇒ Object
102
103
104
|
# File 'lib/sphero.rb', line 102
def auto_reconnect= time_s
queue_packet Request::SetAutoReconnect.new(@seq, limit1(time_s) )
end
|
#back_led_output=(h) ⇒ Object
149
150
151
|
# File 'lib/sphero.rb', line 149
def back_led_output= h
queue_packet Request::SetBackLEDOutput.new(@seq, limit1(h) )
end
|
#bluetooth_info ⇒ Object
88
89
90
91
92
|
# File 'lib/sphero.rb', line 88
def bluetooth_info
packet = Request::GetBluetoothInfo.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|
#close ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/sphero.rb', line 65
def close
return if @sp.nil? || @sp.closed?
begin
stop
rescue Exception => e
puts e.message
ensure
@sp.close
end
end
|
#color(colorname, persistant = false) ⇒ Object
138
139
140
141
|
# File 'lib/sphero.rb', line 138
def color colorname, persistant = false
color = COLORS[colorname]
rgb color[:r], color[:g], color[:b], persistant
end
|
configure collision detection messages
177
178
179
180
181
182
|
# File 'lib/sphero.rb', line 177
def configure_collision_detection meth, x_t, y_t, x_spd, y_spd, dead
queue_packet Request::ConfigureCollisionDetection.new(@seq, limit1(meth),
limit1(x_t), limit1(y_t),
limit1(x_spd), limit1(y_spd),
limit1(dead) )
end
|
#disable_auto_reconnect ⇒ Object
110
111
112
|
# File 'lib/sphero.rb', line 110
def disable_auto_reconnect
queue_packet Request::SetAutoReconnect.new(@seq, 0, flag(false) )
end
|
#heading=(h) ⇒ Object
130
131
132
|
# File 'lib/sphero.rb', line 130
def heading= h
queue_packet Request::Heading.new(@seq, degrees(h) )
end
|
#keep_going(duration) ⇒ Object
just a nicer alias for Ruby’s own sleep
159
160
161
|
# File 'lib/sphero.rb', line 159
def keep_going(duration)
Kernel::sleep duration
end
|
#ping ⇒ Object
76
77
78
79
80
|
# File 'lib/sphero.rb', line 76
def ping
packet = Request::Ping.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|
#rgb(r, g, b, persistant = false) ⇒ Object
143
144
145
|
# File 'lib/sphero.rb', line 143
def rgb r, g, b, persistant = false
queue_packet Request::SetRGB.new(@seq, limit1(r), limit1(g), limit1(b), flag(persistant) )
end
|
#roll(speed, heading, state = true) ⇒ Object
122
123
124
|
# File 'lib/sphero.rb', line 122
def roll speed, heading, state = true
queue_packet Request::Roll.new(@seq, limit1(speed), degrees(heading), flag(state) )
end
|
#rotation_rate=(h) ⇒ Object
Rotation Rate 0x00 - 0xFF
154
155
156
|
# File 'lib/sphero.rb', line 154
def rotation_rate= h
queue_packet Request::SetRotationRate.new(@seq, limit1(h))
end
|
#set_data_streaming(n, m, mask, pcnt, mask2) ⇒ Object
configure data streaming notification messages
171
172
173
174
|
# File 'lib/sphero.rb', line 171
def set_data_streaming n, m, mask, pcnt, mask2
queue_packet Request::SetDataStreaming.new(@seq, limit2(n), limit2(m),
limit4(mask), limit1(pcnt), limit4(mask2) )
end
|
#set_power_notification(enable = true) ⇒ Object
configure power notification messages
166
167
168
|
# File 'lib/sphero.rb', line 166
def set_power_notification enable=true
queue_packet Request::SetPowerNotification.new(@seq, flag(enable) )
end
|
#sphero_sleep(wakeup = 0, macro = 0) ⇒ Object
118
119
120
|
# File 'lib/sphero.rb', line 118
def sphero_sleep wakeup = 0, macro = 0
queue_packet Request::Sleep.new(@seq, limit2(wakeup), limit1(macro) )
end
|
#stabilization=(on) ⇒ Object
134
135
136
|
# File 'lib/sphero.rb', line 134
def stabilization= on
queue_packet Request::Stabilization.new(@seq, on)
end
|
#stop ⇒ Object
126
127
128
|
# File 'lib/sphero.rb', line 126
def stop
roll 0, 0
end
|
#user_led ⇒ Object
This retrieves the “user LED color” which is stored in the config block (which may or may not be actively driven to the RGB LED).
96
97
98
99
100
|
# File 'lib/sphero.rb', line 96
def user_led
packet = Request::GetRGB.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|
#version ⇒ Object
82
83
84
85
86
|
# File 'lib/sphero.rb', line 82
def version
packet = Request::GetVersioning.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|