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.3.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
-
#read_async_messages ⇒ Object
read all outstanding async packets and store in async_responses would not do well to receive simple responses this way…
-
#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
-
#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
|
# 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
@async_messages = []
end
|
Instance Attribute Details
#async_messages ⇒ Object
Returns the value of attribute async_messages.
15
16
17
|
# File 'lib/sphero.rb', line 15
def async_messages
@async_messages
end
|
#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
|
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
#back_led_output=(h) ⇒ Object
#close ⇒ Object
52
53
54
55
56
|
# File 'lib/sphero.rb', line 52
def close
@lock.synchronize do
@sp.close
end
end
|
#color(colorname, persistant = false) ⇒ Object
102
103
104
105
|
# File 'lib/sphero.rb', line 102
def color colorname, persistant = false
color = COLORS[colorname]
rgb color[:r], color[:g], color[:b], persistant
end
|
configure collision detection messages
145
146
147
|
# File 'lib/sphero.rb', line 145
def configure_collision_detection meth, x_t, y_t, x_spd, y_spd, dead
write Request::ConfigureCollisionDetection.new(@seq, meth, x_t, y_t, x_spd, y_spd, dead)
end
|
#disable_auto_reconnect ⇒ Object
#heading=(h) ⇒ Object
98
99
100
|
# File 'lib/sphero.rb', line 98
def heading= h
write Request::Heading.new(@seq, h)
end
|
#keep_going(duration) ⇒ Object
just a nicer alias for Ruby’s own sleep
128
129
130
|
# File 'lib/sphero.rb', line 128
def keep_going(duration)
Kernel::sleep duration
end
|
#ping ⇒ Object
58
59
60
|
# File 'lib/sphero.rb', line 58
def ping
write Request::Ping.new(@seq)
end
|
#read_async_messages ⇒ Object
read all outstanding async packets and store in async_responses would not do well to receive simple responses this way…
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/sphero.rb', line 151
def read_async_messages
, body = nil
new_responses = []
@lock.synchronize do
, body = read_next_response
while && Response.async?()
new_responses << Response::AsyncResponse.response(, body)
, body = read_next_response
end
end
async_messages.concat(new_responses) unless new_responses.empty?
return !new_responses.empty?
end
|
#rgb(r, g, b, persistant = false) ⇒ Object
107
108
109
|
# File 'lib/sphero.rb', line 107
def rgb r, g, b, persistant = false
write Request::SetRGB.new(@seq, r, g, b, persistant ? 0x01 : 0x00)
end
|
#roll(speed, heading, state = true) ⇒ Object
90
91
92
|
# File 'lib/sphero.rb', line 90
def roll speed, heading, state = true
write Request::Roll.new(@seq, speed, heading, state ? 0x01 : 0x00)
end
|
#rotation_rate=(h) ⇒ Object
Rotation Rate 0x00 - 0xFF
#set_data_streaming(n, m, mask, pcnt, mask2) ⇒ Object
configure data streaming notification messages
140
141
142
|
# File 'lib/sphero.rb', line 140
def set_data_streaming n, m, mask, pcnt, mask2
write Request::SetDataStreaming.new(@seq, n, m, mask, pcnt, mask2)
end
|
#set_power_notification(enable = true) ⇒ Object
configure power notification messages
135
136
137
|
# File 'lib/sphero.rb', line 135
def set_power_notification enable=true
write Request::SetPowerNotification.new(@seq, enable ? 0x01 : 0x00)
end
|
#sphero_sleep(wakeup = 0, macro = 0) ⇒ Object
86
87
88
|
# File 'lib/sphero.rb', line 86
def sphero_sleep wakeup = 0, macro = 0
write Request::Sleep.new(@seq, wakeup, macro)
end
|
#stop ⇒ Object
94
95
96
|
# File 'lib/sphero.rb', line 94
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).
113
114
115
|
# File 'lib/sphero.rb', line 113
def user_led
write Request::GetRGB.new(@seq)
end
|