Class: Sphero

Inherits:
Object
  • 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

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_messagesObject

Returns the value of attribute async_messages.



15
16
17
# File 'lib/sphero.rb', line 15

def async_messages
  @async_messages
end

#connection_typesObject

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_reconnectObject



74
75
76
# File 'lib/sphero.rb', line 74

def auto_reconnect
  write(Request::GetAutoReconnect.new(@seq)).time
end

#auto_reconnect=(time_s) ⇒ Object



70
71
72
# File 'lib/sphero.rb', line 70

def auto_reconnect= time_s
  write Request::SetAutoReconnect.new(@seq, time_s)
end

#back_led_output=(h) ⇒ Object

Brightness 0x00 - 0xFF



118
119
120
# File 'lib/sphero.rb', line 118

def back_led_output= h
  write Request::SetBackLEDOutput.new(@seq, h)
end

#bluetooth_infoObject



66
67
68
# File 'lib/sphero.rb', line 66

def bluetooth_info
  write Request::GetBluetoothInfo.new(@seq)
end

#closeObject



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(meth, x_t, y_t, x_spd, y_spd, dead) ⇒ Object

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_reconnectObject



78
79
80
# File 'lib/sphero.rb', line 78

def disable_auto_reconnect
  write Request::SetAutoReconnect.new(@seq, 0, 0x00)
end

#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

#pingObject



58
59
60
# File 'lib/sphero.rb', line 58

def ping
  write Request::Ping.new(@seq)
end

#power_stateObject



82
83
84
# File 'lib/sphero.rb', line 82

def power_state
  write Request::GetPowerState.new(@seq)
end

#read_async_messagesObject

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
  header, body = nil
  new_responses = []

  @lock.synchronize do
    header, body = read_next_response

    while header && Response.async?(header)
      new_responses << Response::AsyncResponse.response(header, body)
      header, 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



123
124
125
# File 'lib/sphero.rb', line 123

def rotation_rate= h
  write Request::SetRotationRate.new(@seq, h)
end

#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

#stopObject



94
95
96
# File 'lib/sphero.rb', line 94

def stop
  roll 0, 0
end

#user_ledObject

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

#versionObject



62
63
64
# File 'lib/sphero.rb', line 62

def version
  write Request::GetVersioning.new(@seq)
end