Class: Subduino::ArdIO

Inherits:
Object
  • Object
show all
Defined in:
lib/subduino/ard_io.rb

Class Method Summary collapse

Class Method Details

.read(&proc) ⇒ Object

Read I/O

Starts a thread that loops fetching serial bytes. It’ll buffer it up until a n or r char are found. Feed it with a block to read the text.



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
# File 'lib/subduino/ard_io.rb', line 22

def read(&proc)
  Log.info "[USB] Found Device...#{serial.port}"
  Log.info "[USB] Starting Connect..." + serial.sp.get_modem_params.map { |k,v| "#{k}: #{v}" }.join(" ")
  # Log.info "[USB] Read Timeout #{sp.read_timeout}" # {sp.write_timeout}"

  if serial.up?
    @iothread ||= Thread.new do
      # Thread.current.abort_on_exception = false
      icache = []
      loop do
        begin
          while char = @serial.read
            if !char.valid_encoding?
              bytes = char.bytes.to_a
              hexes = bytes.map { |b| b.to_s(16) }
              puts " - Bad char #{char} - (Hex: #{char.unpack('H')} | Byte(s) #{bytes} | Hexe(s) #{hexes}"
            # elsif char !~ /\n|\r/
            #   # print char if Debug
            #   icache << char
            else
              #data = icache.join(""); icache = []
              data = char
              unless data.empty?
                Log.info "[IO  RX] #{data}"
                proc.call(data)
              end
            end
            # sleep 1
          end
        rescue => e
          Log.error "[USB] Error #{e}"
          Log.error e.backtrace.join("\n")
          stop!
          exit 1
        end
      end
    end
  end

end

.serialObject

Serial Port

Direct access to the SerialPort instance.



11
12
13
# File 'lib/subduino/ard_io.rb', line 11

def serial
  @serial ||= Serial.new(Opts[:bauds])
end

.stop!Object

Finish Him!



79
80
81
82
83
# File 'lib/subduino/ard_io.rb', line 79

def stop!
  serial.kill
  Thread.kill @iothread
  Log.info "[IO] K.I.A"
end

.write(msg) ⇒ Object

Write I/O

Prints bytes to the serial port. It’ll use subduino convention of n to end the message. Use #sp to write directly to the port. ‘ArdIO.sp.puts (’hi’)‘



70
71
72
73
# File 'lib/subduino/ard_io.rb', line 70

def write(msg)
  Log.info "[IO  TX] #{msg}"
  serial.write(msg)
end