Class: FB::Arduino
- Inherits:
-
Object
- Object
- FB::Arduino
- Defined in:
- lib/arduino.rb
Defined Under Namespace
Classes: EmergencyStop
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#serial_port ⇒ Object
readonly
Returns the value of attribute serial_port.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#disconnect ⇒ Object
Handle loss of serial connection.
-
#initialize(serial_port = DefaultSerialPort.new, logger = STDOUT) ⇒ Arduino
constructor
Initial 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.
- #onclose(&blk) ⇒ Object
-
#onmessage(&blk) ⇒ Object
Handle incoming text from arduino into pi.
-
#parse_incoming(gcode) ⇒ Object
Highest priority message when processing incoming Gcode.
-
#write(string) ⇒ Object
Send outgoing test to arduino from pi.
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
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
11 12 13 |
# File 'lib/arduino.rb', line 11 def commands @commands end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/arduino.rb', line 11 def logger @logger end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
11 12 13 |
# File 'lib/arduino.rb', line 11 def queue @queue end |
#serial_port ⇒ Object (readonly)
Returns the value of attribute serial_port.
11 12 13 |
# File 'lib/arduino.rb', line 11 def serial_port @serial_port end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
11 12 13 |
# File 'lib/arduino.rb', line 11 def status @status end |
Instance Method Details
#disconnect ⇒ Object
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() logger.puts() 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 (&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 |