Class: Bitlash

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

Overview

sp.close

Constant Summary collapse

PROMPT =
">\s"
COMMANDS =
%w[boot help if ls peep print ps rm run stop while]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port_addr, baud = 57600, verbose = false, name = "Bitflash##{(rand*1000).to_i}") ⇒ Bitlash

Returns a new instance of Bitlash.



29
30
31
32
33
# File 'lib/rubitlash.rb', line 29

def initialize(port_addr, baud=57600, verbose=false, name="Bitflash##{(rand*1000).to_i}")
  @port_addr, @baud, @name = port_addr, baud, name
  self.verbose = verbose
  self.open
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

def run(macro); self.talk macro.to_s; end def background(macro); self.talk “run ##macro”; end



67
68
69
70
71
72
73
74
# File 'lib/rubitlash.rb', line 67

def method_missing(m, *args)
  if COMMANDS.include? m.to_s
    cmd = args.size > 0 ? "#{m.to_s} #{args * ','}" : "#{m.to_s}"
  else
    cmd = args.size > 0 ? "print #{m.to_s}(#{args * ','})" : "print #{m.to_s}"
  end
  self.talk(cmd)
end

Instance Attribute Details

#baudObject (readonly)

Returns the value of attribute baud.



27
28
29
# File 'lib/rubitlash.rb', line 27

def baud
  @baud
end

#portObject (readonly)

Returns the value of attribute port.



27
28
29
# File 'lib/rubitlash.rb', line 27

def port
  @port
end

#port_addrObject

Returns the value of attribute port_addr.



26
27
28
# File 'lib/rubitlash.rb', line 26

def port_addr
  @port_addr
end

Instance Method Details

#[](var) ⇒ Object



52
53
54
# File 'lib/rubitlash.rb', line 52

def [](var)
  self.talk "print #{var.to_s}"
end

#[]=(var, value) ⇒ Object



56
57
58
# File 'lib/rubitlash.rb', line 56

def []=(var,value)
  self.talk "#{var.to_s}=#{value.to_i}"
end

#closeObject



40
# File 'lib/rubitlash.rb', line 40

def close; @port.close; end

#macro(name, cmd) ⇒ Object



60
61
62
# File 'lib/rubitlash.rb', line 60

def macro(name,cmd)
  self.talk "#{name}:=\"#{cmd}\""
end

#openObject



35
36
37
38
# File 'lib/rubitlash.rb', line 35

def open    
  @port = SerialPort.new(@port_addr, 57600, 8, 1, SerialPort::NONE)
  @port.expect(/>\s/)
end

#talk(string) ⇒ Object



46
47
48
49
50
# File 'lib/rubitlash.rb', line 46

def talk(string)
  @port.puts string
  reply = @port.expect(/#{PROMPT}/)[0].split("\n\r")[1...-1]
  return reply.size == 1 ? reply[0] : reply
end

#verboseObject



42
# File 'lib/rubitlash.rb', line 42

def verbose; $expect_verbose; end

#verbose=(bool) ⇒ Object



44
# File 'lib/rubitlash.rb', line 44

def verbose=(bool); $expect_verbose=bool; end