-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_bot.rb
42 lines (34 loc) · 870 Bytes
/
c_bot.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
require 'rubyserial'
class Bot
attr_reader :read_worker, :pos, :distance, :serialport, :stopped
def initialize
@distance = 0
@pos = 0
@stopped = false
@serialport = Serial.new '/dev/tty.usbmodem141101'
Thread.new { read_worker }
end
def stop
@stopped = true
end
def move(pos)
@pos = pos
@serialport.write("s#{pos}")
p "moving to #{pos}"
end
def read_worker
buffer = ''
while !@stopped
# TODO read the serial port
next_char = @serialport.getbyte&.chr
buffer += next_char if next_char
match = buffer.match(/(\d+)\r\n/)
if match
@distance = match[1]
buffer = ''
end
end
puts 'Exiting read thread!'
end
end
# bot = Bot.new