Class: SpheroClient

Inherits:
Object
  • Object
show all
Includes:
SpheroUtilities
Defined in:
lib/rubysphero.rb

Overview

mod

Constant Summary collapse

COLOURS =
{   :blue  =>  [0x00,0x00,0xFF],
:green  =>  [0x00,0xFF,0x00],
:red  => [0xFF,0x00,0x00],
:orange =>  [0xFF,0x8C,0x00],
:yellow =>  [0xFF,0xFF,0x00],
:white  => [0xFF,0xFF,0xFF],
:black  => [0x00,0x00,0x00],
:pink =>  [0xFF,0x00,0xFF],
:grey =>  [0x80,0x80,0x80]}

Instance Method Summary collapse

Methods included from SpheroUtilities

#add_checksum, #do_checksum, #logd, #print_format_bytes

Constructor Details

#initialize(bluetooth_address) ⇒ SpheroClient

def



212
213
214
# File 'lib/rubysphero.rb', line 212

def initialize(bluetooth_address)
  return open(bluetooth_address)
end

Instance Method Details

#closeObject

def



225
226
227
# File 'lib/rubysphero.rb', line 225

def close
  @connection.close
end

#open(bluetooth_address) ⇒ Object

class



216
217
218
219
220
221
222
223
# File 'lib/rubysphero.rb', line 216

def open(bluetooth_address)
  begin
    @connection   = Serial.new bluetooth_address, 115200 ,8
  rescue RubySerial::Exception
    open bluetooth_address
  end
  return @connection
end

#orientationObject

def



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rubysphero.rb', line 87

def orientation
    
  set_back_led_output(255)
  set_colour(:black)
  1.step(720,5) do | heading |   
    roll(heading , 0)
    
    inputted_text=gets.chomp
    
    if inputted_text =="" then
      # spin

    elsif inputted_text==" "      
      set_back_led_output(0)
      set_heading(heading)
      set_colour(:white)
      break
    else 
      sleep 1  
    end # if 

  end # upto   


end

#pingObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubysphero.rb', line 55

def ping
  logd()
  logd "Building request: ping"
  request=SpheroRequest.new()

  request.sop1=0xFF  
  request.sop2=0xFF

  request.did=0x00 
  request.cid=0x01 # Ping

  request.seq=0x52
  request.dlen=0x01
  send_data(request.build_packet)
  
  read_data
end

#read_dataObject

def



78
79
80
81
82
83
84
85
# File 'lib/rubysphero.rb', line 78

def read_data
  bytes=@connection.read(7).unpack("C*")
  logd("Wire read finished.")
  response = SpheroResponse.new( bytes)

  return response
  
end

#roll(heading_raw = 0, speed = 0xFF) ⇒ Object

def



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rubysphero.rb', line 185

def roll(heading_raw=0, speed=0xFF)
  logd()
  logd( "Building request: roll")

  request=SpheroRequest.new()
  heading = heading_raw%359
  logd( "Heading: #{heading}")
  
  request.sop1=0xFF  
  request.sop2=0xFF

  request.did=0x02 
  request.cid=0x30 # Roll

  request.seq=0x54
  request.dlen=0x05

  state=0x01
  
  request.push_data speed
  request.push_data(heading , :a16bit)
  request.push_data state
  
  send_data(request.build_packet)
  read_data  

end

#send_data(data) ⇒ Object

def



72
73
74
75
76
# File 'lib/rubysphero.rb', line 72

def send_data(data)
  logd("Wire send next.")
  @connection.write data

end

#set_back_led_output(brightness) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rubysphero.rb', line 111

def set_back_led_output(brightness)
  logd()
  logd "Building request: set back led output b"
  
  request=SpheroRequest.new()

  request.sop1=0xFF  
  request.sop2=0xFF

  request.did=0x02 
  request.cid=0x21 
  request.seq=0x53
  request.dlen=0x02
  request.push_data brightness 
  
  send_data(request.build_packet)
  read_data  

end

#set_colour(chosen_colour) ⇒ Object

def



155
156
157
158
159
# File 'lib/rubysphero.rb', line 155

def set_colour(chosen_colour) 
  logd
  logd("Locating RGB values for: #{chosen_colour}")
  set_colour_rgb(COLOURS[chosen_colour][0],COLOURS[chosen_colour][1],COLOURS[chosen_colour][2])
end

#set_colour_rgb(red_value, green_value, blue_value) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rubysphero.rb', line 162

def set_colour_rgb(red_value,green_value,blue_value)
  logd
  logd "Building request: colour"
  
  request=SpheroRequest.new()

  request.sop1=0xFF  
  request.sop2=0xFF

  request.did=0x02 
  request.cid=0x20 # Set RGB Output

  request.seq=0x53
  request.dlen=0x05
  request.push_data red_value 
  request.push_data green_value
  request.push_data blue_value  
  flag=0x01
  request.push_data flag 
  
  send_data(request.build_packet)
  read_data
end

#set_heading(heading_raw) ⇒ Object

def



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rubysphero.rb', line 131

def set_heading(heading_raw) 
  logd
  logd( "Building request: Set Heading")

  request=SpheroRequest.new()
  heading = heading_raw%359
  logd( "Heading: #{heading}")
  
  request.sop1=0xFF  
  request.sop2=0xFF

  request.did=0x02 
  request.cid=0x01
  request.seq=0x55
  request.dlen=0x03
  
  request.push_data(heading , :a16bit)
  
  send_data(request.build_packet)
  read_data  


end