Sha256: b2852324fec566ec99bd7630b1ed1c80380feb239ba550ed20cbb9373fe1f3f7
Contents?: true
Size: 1.25 KB
Versions: 2
Compression:
Stored size: 1.25 KB
Contents
require 'serialport' require 'observer' module Dino class TxRx include Observable BAUD = 115200 def initialize @first_write = true end def io raise BoardNotFound unless @io ||= find_arduino @io end def io=(device) @io = SerialPort.new(device, BAUD) end def read @thread ||= Thread.new do loop do if IO.select([io], nil, nil, 0.005) pin, message = *io.gets.chop.split(/::/) pin && message && changed && notify_observers(pin, message) end sleep 0.004 end end end def close_read return nil if @thread.nil? Thread.kill(@thread) @thread = nil end def write(message) IO.select(nil, [io], nil) io.puts(message) end private def tty_devices if RUBY_PLATFORM.include?("mswin") || RUBY_PLATFORM.include?("mingw") ["COM1", "COM2", "COM3", "COM4"] else `ls /dev`.split("\n").grep(/usb|ACM/).map{|d| "/dev/#{d}"} end end def find_arduino tty_devices.map do |device| next if device.match /^cu/ begin SerialPort.new(device, BAUD) rescue nil end end.compact.first end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dino-0.9.1 | lib/dino/tx_rx.rb |
dino-0.9 | lib/dino/tx_rx.rb |