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
-
#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
103
104
105
|
# File 'lib/sphero.rb', line 103
def auto_reconnect= time_s
queue_packet Request::SetAutoReconnect.new(@seq, limit1(time_s) )
end
|
#back_led_output=(h) ⇒ Object
150
151
152
|
# File 'lib/sphero.rb', line 150
def back_led_output= h
queue_packet Request::SetBackLEDOutput.new(@seq, limit1(h) )
end
|
#bluetooth_info ⇒ Object
89
90
91
92
93
|
# File 'lib/sphero.rb', line 89
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
|
# File 'lib/sphero.rb', line 66
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
139
140
141
142
|
# File 'lib/sphero.rb', line 139
def color colorname, persistant = false
color = COLORS[colorname]
rgb color[:r], color[:g], color[:b], persistant
end
|
configure collision detection messages
178
179
180
181
182
183
|
# File 'lib/sphero.rb', line 178
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
111
112
113
|
# File 'lib/sphero.rb', line 111
def disable_auto_reconnect
queue_packet Request::SetAutoReconnect.new(@seq, 0, flag(false) )
end
|
#heading=(h) ⇒ Object
131
132
133
|
# File 'lib/sphero.rb', line 131
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
160
161
162
|
# File 'lib/sphero.rb', line 160
def keep_going(duration)
Kernel::sleep duration
end
|
#ping ⇒ Object
77
78
79
80
81
|
# File 'lib/sphero.rb', line 77
def ping
packet = Request::Ping.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|
#rgb(r, g, b, persistant = false) ⇒ Object
144
145
146
|
# File 'lib/sphero.rb', line 144
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
123
124
125
|
# File 'lib/sphero.rb', line 123
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
155
156
157
|
# File 'lib/sphero.rb', line 155
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
172
173
174
175
|
# File 'lib/sphero.rb', line 172
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
167
168
169
|
# File 'lib/sphero.rb', line 167
def set_power_notification enable=true
queue_packet Request::SetPowerNotification.new(@seq, flag(enable) )
end
|
#sphero_sleep(wakeup = 0, macro = 0) ⇒ Object
119
120
121
|
# File 'lib/sphero.rb', line 119
def sphero_sleep wakeup = 0, macro = 0
queue_packet Request::Sleep.new(@seq, limit2(wakeup), limit1(macro) )
end
|
#stabilization=(on) ⇒ Object
135
136
137
|
# File 'lib/sphero.rb', line 135
def stabilization= on
queue_packet Request::Stabilization.new(@seq, on)
end
|
#stop ⇒ Object
127
128
129
|
# File 'lib/sphero.rb', line 127
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).
97
98
99
100
101
|
# File 'lib/sphero.rb', line 97
def user_led
packet = Request::GetRGB.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|
#version ⇒ Object
83
84
85
86
87
|
# File 'lib/sphero.rb', line 83
def version
packet = Request::GetVersioning.new(@seq)
queue_packet packet
return sync_response packet.seq
end
|