-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
65 lines (50 loc) · 1.14 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
$: << '/working/sphero/lib/'
require 'sphero'
require 'socket'
require 'json'
ip = '127.0.0.1'
port = 13854
socket = TCPSocket.new(ip, port)
dev = "COM5"
s = Sphero.new(dev)
s.ping
def send_request(socket, request)
request = request.to_json + "\r"
socket.print(request)
socket.flush
return get_response(socket)
end
def get_response(socket)
response = socket.gets("\r")
begin
response = JSON.parse(response)
rescue
response = Hash.new
end
return response
end
#request = Hash.new
#request['appName'] = 'SpheroMind'
#request['appKey'] = '8bada2ae333f9dc6ee3693e52f20852f658c204d'
#send_request(socket, request)
request = Hash.new
request['enableRawOutput'] = false
request['format'] = "Json"
send_request(socket, request)
attention = [0, 0, 0]
while response = get_response(socket)
eSense = response['eSense']
if eSense
attention.shift
attention << eSense['attention'].to_f
min = 100.0
attention.each { |a| min=[min, a].min }
red = ((255/100.0)*(min)).to_i
puts "#{red} = #{attention.join(',')}"
begin
s.rgb(red, 0, 0)
s.roll(red, 0, true)
rescue
end
end
end