Class: FB::Arduino

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

Defined Under Namespace

Classes: EmergencyStop

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serial_port = DefaultSerialPort.new, logger = STDOUT) ⇒ Arduino

Initial and provide a serial object, as well as an IO object to send log messages to. Default SerialPort is DefaultSerialPort. Default logger is STDOUT



16
17
18
19
# File 'lib/arduino.rb', line 16

def initialize(serial_port = DefaultSerialPort.new, logger = STDOUT)
  @serial_port, @logger, @queue = serial_port, logger, EM::Channel.new
  @commands, @status = FB::ArduinoCommandSet.new(self), FB::Status.new(self)
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



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

def commands
  @commands
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#queueObject (readonly)

Returns the value of attribute queue.



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

def queue
  @queue
end

#serial_portObject (readonly)

Returns the value of attribute serial_port.



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

def serial_port
  @serial_port
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#disconnectObject

Handle loss of serial connection



53
54
55
56
# File 'lib/arduino.rb', line 53

def disconnect
  log "Connection to device lost"
  @onclose.call if @onclose
end

#log(message) ⇒ Object

Log to screen/file/IO stream



22
23
24
# File 'lib/arduino.rb', line 22

def log(message)
  logger.puts(message)
end

#onclose(&blk) ⇒ Object



43
44
45
# File 'lib/arduino.rb', line 43

def onclose(&blk)
  @onclose = blk
end

#onmessage(&blk) ⇒ Object

Handle incoming text from arduino into pi



33
34
35
36
37
38
39
40
41
# File 'lib/arduino.rb', line 33

def onmessage(&blk)
  raise 'read() requires a block' unless block_given?
  @queue.subscribe do |gcodes|
    gcodes.each do |gcode|
      parse_incoming(gcode)
      blk.call(gcode)
    end
  end
end

#parse_incoming(gcode) ⇒ Object

Highest priority message when processing incoming Gcode. Use for system level status changes.



28
29
30
# File 'lib/arduino.rb', line 28

def parse_incoming(gcode)
  commands.execute(gcode)
end

#write(string) ⇒ Object

Send outgoing test to arduino from pi



48
49
50
# File 'lib/arduino.rb', line 48

def write(string)
  serial_port.puts string
end