Class: FB::Arduino
- Inherits:
-
Object
- Object
- FB::Arduino
- Defined in:
- lib/arduino.rb
Defined Under Namespace
Classes: Position
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#inbound_queue ⇒ Object
Returns the value of attribute inbound_queue.
-
#inputs ⇒ Object
Returns the value of attribute inputs.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#outbound_queue ⇒ Object
Returns the value of attribute outbound_queue.
-
#serial_port ⇒ Object
Returns the value of attribute serial_port.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #current_position ⇒ Object
-
#disconnect ⇒ Object
Handle loss of serial connection.
-
#initialize(serial_port: DefaultSerialPort.new, logger: STDOUT) ⇒ Arduino
constructor
Initialize and provide a serial object, as well as an IO object to send log messages to.
-
#log(message) ⇒ Object
Log to screen/file/IO stream.
- #maybe_execute_command ⇒ Object
- #next_cmd ⇒ Object
- #onchange(&blk) ⇒ Object
- #onclose(&blk) ⇒ Object
-
#onmessage(&blk) ⇒ Object
Handle incoming text from arduino into pi.
- #start_event_listeners ⇒ Object
-
#write(gcode) ⇒ Object
Send outgoing test to arduino from pi.
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
#commands ⇒ Object
Returns the value of attribute commands.
12 13 14 |
# File 'lib/arduino.rb', line 12 def commands @commands end |
#inbound_queue ⇒ Object
Returns the value of attribute inbound_queue.
12 13 14 |
# File 'lib/arduino.rb', line 12 def inbound_queue @inbound_queue end |
#inputs ⇒ Object
Returns the value of attribute inputs.
12 13 14 |
# File 'lib/arduino.rb', line 12 def inputs @inputs end |
#logger ⇒ Object
Returns the value of attribute logger.
12 13 14 |
# File 'lib/arduino.rb', line 12 def logger @logger end |
#outbound_queue ⇒ Object
Returns the value of attribute outbound_queue.
12 13 14 |
# File 'lib/arduino.rb', line 12 def outbound_queue @outbound_queue end |
#serial_port ⇒ Object
Returns the value of attribute serial_port.
12 13 14 |
# File 'lib/arduino.rb', line 12 def serial_port @serial_port end |
#status ⇒ Object
Returns the value of attribute status.
12 13 14 |
# File 'lib/arduino.rb', line 12 def status @status end |
Instance Method Details
#current_position ⇒ Object
60 61 62 |
# File 'lib/arduino.rb', line 60 def current_position Position.new(status[:X], status[:Y], status[:Z]) end |
#disconnect ⇒ Object
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() logger.puts() end |
#maybe_execute_command ⇒ Object
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_cmd ⇒ Object
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 (&blk) = blk end |
#start_event_listeners ⇒ Object
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) .call(gcode) if 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 |