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
- 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
-
#enable_stop_on_disconnect ⇒ 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.
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/sphero.rb', line 40
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.
14
15
16
|
# File 'lib/sphero.rb', line 14
def connection_types
@connection_types
end
|
#messages ⇒ Object
Returns the value of attribute messages.
14
15
16
|
# File 'lib/sphero.rb', line 14
def messages
@messages
end
|
#packets ⇒ Object
Returns the value of attribute packets.
14
15
16
|
# File 'lib/sphero.rb', line 14
def packets
@packets
end
|
#response_queue ⇒ Object
Returns the value of attribute response_queue.
14
15
16
|
# File 'lib/sphero.rb', line 14
def response_queue
@response_queue
end
|
#responses ⇒ Object
Returns the value of attribute responses.
14
15
16
|
# File 'lib/sphero.rb', line 14
def responses
@responses
end
|
Class Method Details
.start(dev, &block) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/sphero.rb', line 17
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 RubySerial::Exception => e
if e.message == 'EBUSY'
puts retries_left
retries_left = retries_left - 1
retry unless retries_left < 0
end
end
end
|
Instance Method Details
#auto_reconnect=(time_s) ⇒ Object
104
105
106
|
# File 'lib/sphero.rb', line 104
def auto_reconnect= time_s
queue_packet Request::SetAutoReconnect.new(@seq, limit1(time_s) )
end
|
#back_led_output=(h) ⇒ Object
155
156
157
|
# File 'lib/sphero.rb', line 155
def back_led_output= h
queue_packet Request::SetBackLEDOutput.new(@seq, limit1(h) )
end
|
#bluetooth_info ⇒ Object
90
91
92
93
94
|
# File 'lib/sphero.rb', line 90
def bluetooth_info
packet = Request::GetBluetoothInfo.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|
#close ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/sphero.rb', line 66
def close
return if @sp.nil? || @sp.closed?
begin
stop
sleep 2
rescue Exception => e
puts e.message
ensure
@sp.close
end
end
|
#color(colorname, persistant = false) ⇒ Object
144
145
146
147
|
# File 'lib/sphero.rb', line 144
def color colorname, persistant = false
color = COLORS[colorname]
rgb color[:r], color[:g], color[:b], persistant
end
|
configure collision detection messages
183
184
185
186
187
188
|
# File 'lib/sphero.rb', line 183
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
112
113
114
|
# File 'lib/sphero.rb', line 112
def disable_auto_reconnect
queue_packet Request::SetAutoReconnect.new(@seq, 0, flag(false) )
end
|
#enable_stop_on_disconnect ⇒ Object
116
117
118
|
# File 'lib/sphero.rb', line 116
def enable_stop_on_disconnect
queue_packet Request::SetTempOptionFlags.new(@seq, flag(true))
end
|
#heading=(h) ⇒ Object
136
137
138
|
# File 'lib/sphero.rb', line 136
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
165
166
167
|
# File 'lib/sphero.rb', line 165
def keep_going(duration)
Kernel::sleep duration
end
|
#ping ⇒ Object
78
79
80
81
82
|
# File 'lib/sphero.rb', line 78
def ping
packet = Request::Ping.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|
#rgb(r, g, b, persistant = false) ⇒ Object
149
150
151
|
# File 'lib/sphero.rb', line 149
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
128
129
130
|
# File 'lib/sphero.rb', line 128
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
160
161
162
|
# File 'lib/sphero.rb', line 160
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
177
178
179
180
|
# File 'lib/sphero.rb', line 177
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
172
173
174
|
# File 'lib/sphero.rb', line 172
def set_power_notification enable=true
queue_packet Request::SetPowerNotification.new(@seq, flag(enable) )
end
|
#sphero_sleep(wakeup = 0, macro = 0) ⇒ Object
124
125
126
|
# File 'lib/sphero.rb', line 124
def sphero_sleep wakeup = 0, macro = 0
queue_packet Request::Sleep.new(@seq, limit2(wakeup), limit1(macro) )
end
|
#stabilization=(on) ⇒ Object
140
141
142
|
# File 'lib/sphero.rb', line 140
def stabilization= on
queue_packet Request::Stabilization.new(@seq, on)
end
|
#stop ⇒ Object
132
133
134
|
# File 'lib/sphero.rb', line 132
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).
98
99
100
101
102
|
# File 'lib/sphero.rb', line 98
def user_led
packet = Request::GetRGB.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|
#version ⇒ Object
84
85
86
87
88
|
# File 'lib/sphero.rb', line 84
def version
packet = Request::GetVersioning.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|