Class: RoadToRubykaigi::SerialReader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/road_to_rubykaigi/serial_reader.rb

Constant Summary collapse

BAUD =
115200

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



16
17
18
# File 'lib/road_to_rubykaigi/serial_reader.rb', line 16

def queue
  @queue
end

Instance Method Details

#drainObject



18
19
20
21
22
23
24
# File 'lib/road_to_rubykaigi/serial_reader.rb', line 18

def drain
  until @queue.empty?
    yield @queue.pop(true)
  end
rescue ThreadError
  # pop(true) raises if the queue empties mid-drain
end

#startObject



26
27
28
29
30
31
32
33
34
# File 'lib/road_to_rubykaigi/serial_reader.rb', line 26

def start
  @queue.clear
  Config.detect_serial_port!
  @port = Config.serial_port
  return if @thread

  @thread = Thread.new { read_loop }
  at_exit { @thread&.kill }
end