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.4.1'
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
  @messages = Queue.new
end

Instance Attribute Details

#connection_typesObject

Returns the value of attribute connection_types.



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

def connection_types
  @connection_types
end

#messagesObject

Returns the value of attribute messages.



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

def messages
  @messages
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



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

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

#auto_reconnect=(time_s) ⇒ Object



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

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

#back_led_output=(h) ⇒ Object

Brightness 0x00 - 0xFF



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

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

#bluetooth_infoObject



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

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

#closeObject



52
53
54
55
56
57
58
59
60
# File 'lib/sphero.rb', line 52

def close
  begin
    stop
  rescue Exception => e
    puts e.message
  ensure
    @sp.close
  end
end

#color(colorname, persistant = false) ⇒ Object



106
107
108
109
# File 'lib/sphero.rb', line 106

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



149
150
151
# File 'lib/sphero.rb', line 149

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



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

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

#heading=(h) ⇒ Object



102
103
104
# File 'lib/sphero.rb', line 102

def heading= h
  write Request::Heading.new(@seq, h)
end

#keep_going(duration) ⇒ Object

just a nicer alias for Ruby’s own sleep



132
133
134
# File 'lib/sphero.rb', line 132

def keep_going(duration)
  Kernel::sleep duration
end

#pingObject



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

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

#power_stateObject



86
87
88
# File 'lib/sphero.rb', line 86

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

#rgb(r, g, b, persistant = false) ⇒ Object



111
112
113
# File 'lib/sphero.rb', line 111

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



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

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



127
128
129
# File 'lib/sphero.rb', line 127

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



144
145
146
# File 'lib/sphero.rb', line 144

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



139
140
141
# File 'lib/sphero.rb', line 139

def set_power_notification enable=true
  write Request::SetPowerNotification.new(@seq, enable ? 0x01 : 0x00)
end

#sphero_sleep(wakeup = 0, macro = 0) ⇒ Object



90
91
92
# File 'lib/sphero.rb', line 90

def sphero_sleep wakeup = 0, macro = 0
  write Request::Sleep.new(@seq, wakeup, macro)
end

#stopObject



98
99
100
# File 'lib/sphero.rb', line 98

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).



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

def user_led
  write Request::GetRGB.new(@seq)
end

#versionObject



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

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