class FB::Status

A status register that caches the state of the Arduino into a struct. Also broadcasts changes that can be hooked into via the onchange() event. bot.status # => Returns bot X coordinate.

Constants

DEFAULT_INFO

Map of informational status and default values for status within Arduino.

Public Class Methods

new() click to toggle source
# File lib/arduino/status.rb, line 10
def initialize
  @changes = EM::Channel.new
  @info    = OpenStruct.new(DEFAULT_INFO)
end

Public Instance Methods

[](value) click to toggle source
# File lib/arduino/status.rb, line 27
def [](value)
  @info[value.to_s.upcase] || :unknown
end
[]=(register, value) click to toggle source
# File lib/arduino/status.rb, line 23
def []=(register, value)
  transaction { @info[register.to_s.upcase] = value }
end
gcode_update(gcode) click to toggle source
# File lib/arduino/status.rb, line 35
def gcode_update(gcode)
  transaction do
    gcode.params.each { |p| @info[p.head] = p.tail }
  end
end
get_pin(num) click to toggle source
# File lib/arduino/status.rb, line 49
def get_pin(num)
  @info.PINS[num] || :unknown
end
onchange() { |diff| ... } click to toggle source
# File lib/arduino/status.rb, line 41
def onchange
  @changes.subscribe { |diff| yield(diff) }
end
ready?() click to toggle source
# File lib/arduino/status.rb, line 45
def ready?
  self[:BUSY] == 0
end
set_parameter(key, val) click to toggle source
# File lib/arduino/status.rb, line 58
def set_parameter(key, val)
  transaction do |info|
    info[Gcode::PARAMETER_DICTIONARY.fetch(key,
      "UNKNOWN_PARAMETER_#{key}".to_sym)] = val
  end
end
set_pin(num, val) click to toggle source
# File lib/arduino/status.rb, line 53
def set_pin(num, val)
  val = [true, 1, '1'].include?(val) ? :on : :off
  transaction { |info| info.PINS[num] = val }
end
to_h() click to toggle source
# File lib/arduino/status.rb, line 31
def to_h
  @info.to_h
end
transaction() { |info| ... } click to toggle source
# File lib/arduino/status.rb, line 15
def transaction(&blk)
  old = @info.to_h
  yield(@info)
  # Broadcast a diff between the old status and new status
  diff = (@info.to_h.to_a - old.to_a).to_h
  @changes << diff unless diff.empty?
end