Class: FB::Arduino

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

Defined Under Namespace

Classes: EmergencyStop, 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



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

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.



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

def commands
  @commands
end

#inbound_queueObject

Returns the value of attribute inbound_queue.



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

def inbound_queue
  @inbound_queue
end

#inputsObject

Returns the value of attribute inputs.



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

def inputs
  @inputs
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#outbound_queueObject

Returns the value of attribute outbound_queue.



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

def outbound_queue
  @outbound_queue
end

#serial_portObject

Returns the value of attribute serial_port.



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

def serial_port
  @serial_port
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#current_positionObject



62
63
64
# File 'lib/arduino.rb', line 62

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

#disconnectObject

Handle loss of serial connection



57
58
59
60
# File 'lib/arduino.rb', line 57

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

#log(message) ⇒ Object

Log to screen/file/IO stream



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

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

#onchange(&blk) ⇒ Object



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

def onchange(&blk)
  @onchange = blk
end

#onclose(&blk) ⇒ Object



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

def onclose(&blk)
  @onclose = blk
end

#onmessage(&blk) ⇒ Object

Handle incoming text from arduino into pi



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

def onmessage(&blk)
  @onmessage = blk
end

#write(gcode) ⇒ Object

Send outgoing test to arduino from pi



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

def write(gcode)
  @outbound_queue.unshift gcode
  execute_command_next_tick
end