Class: FB::Arduino

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

Defined Under Namespace

Classes: Position

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/arduino.rb', line 18

def initialize(serial_port: DefaultSerialPort.new, logger: STDOUT)
  @outbound_queue = [] # Pi -> Arduino Gcode
  @inbound_queue  = EM::Channel.new # Pi <- Arduino

  @serial_port = serial_port
  @logger      = logger
  @commands    = FB::OutgoingHandler.new(self)
  @inputs      = FB::IncomingHandler.new(self)
  @status      = FB::Status.new

  start_event_listeners
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



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

def commands
  @commands
end

#inbound_queueObject

Returns the value of attribute inbound_queue.



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

def inbound_queue
  @inbound_queue
end

#inputsObject

Returns the value of attribute inputs.



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

def inputs
  @inputs
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#outbound_queueObject

Returns the value of attribute outbound_queue.



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

def outbound_queue
  @outbound_queue
end

#serial_portObject

Returns the value of attribute serial_port.



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

def serial_port
  @serial_port
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#current_positionObject



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

def current_position
  Position.new(status[:X], status[:Y], status[:Z])
end

#disconnectObject

Handle loss of serial connection



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

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

#log(message) ⇒ Object

Log to screen/file/IO stream



32
33
34
# File 'lib/arduino.rb', line 32

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

#maybe_execute_commandObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/arduino.rb', line 68

def maybe_execute_command
  sleep 0.05 # Throttle CPU
  gcode = @outbound_queue.pop
  return unless gcode && status.ready?
  if gcode.is_a?(FB::Gcode)
    serial_port.puts gcode
    status[:last] = gcode.name
    status[:BUSY] = 1 # If not, pi will race arduino and "talk too fast"
  else
    return log "Outbound messages must be GCode objects. Use of "\
               "#{gcode.class}:#{gcode.inspect} is not permitted."
  end
end

#next_cmdObject



64
65
66
# File 'lib/arduino.rb', line 64

def next_cmd
  outbound_queue.first
end

#onchange(&blk) ⇒ Object



41
42
43
# File 'lib/arduino.rb', line 41

def onchange(&blk)
  @onchange = blk
end

#onclose(&blk) ⇒ Object



50
51
52
# File 'lib/arduino.rb', line 50

def onclose(&blk)
  @onclose = blk
end

#onmessage(&blk) ⇒ Object

Handle incoming text from arduino into pi



46
47
48
# File 'lib/arduino.rb', line 46

def onmessage(&blk)
  @onmessage = blk
end

#start_event_listenersObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/arduino.rb', line 82

def start_event_listeners
  EM.tick_loop { maybe_execute_command }
  status.onchange { |diff| @onchange.call(diff) if @onchange }
  inbound_queue.subscribe do |gcodes|
    Array(gcodes).each do |gcode|
      parse_incoming(gcode)
      @onmessage.call(gcode) if @onmessage
    end
  end
end

#write(gcode) ⇒ Object

Send outgoing test to arduino from pi



37
38
39
# File 'lib/arduino.rb', line 37

def write(gcode)
  @outbound_queue.unshift gcode
end