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

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(self)

  start_event_listeners
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



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

def commands
  @commands
end

#inbound_queueObject (readonly)

Returns the value of attribute inbound_queue.



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

def inbound_queue
  @inbound_queue
end

#inputsObject (readonly)

Returns the value of attribute inputs.



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

def inputs
  @inputs
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#outbound_queueObject (readonly)

Returns the value of attribute outbound_queue.



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

def outbound_queue
  @outbound_queue
end

#serial_portObject (readonly)

Returns the value of attribute serial_port.



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

def serial_port
  @serial_port
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#disconnectObject

Handle loss of serial connection



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

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

#onchange(&blk) ⇒ Object



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

def onchange(&blk)
  @onchange = blk
end

#onclose(&blk) ⇒ Object



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

def onclose(&blk)
  @onclose = blk
end

#onmessage(&blk) ⇒ Object

Handle incoming text from arduino into pi



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

def onmessage(&blk)
  @onmessage = blk
end

#write(string) ⇒ Object

Send outgoing test to arduino from pi



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

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