Class: FB::Status

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

Defined Under Namespace

Classes: Info

Constant Summary collapse

DEFAULT_INFO =

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

{X: 0, Y: 0, Z: 0, S: 10, BUSY: 1, LAST: 'none', PINS: {}}

Instance Method Summary collapse

Constructor Details

#initializeStatus

Returns a new instance of Status.



7
8
9
10
# File 'lib/arduino/status.rb', line 7

def initialize
  @changes = EM::Channel.new
  @info    = Info.new(*DEFAULT_INFO.values)
end

Instance Method Details

#[](value) ⇒ Object



27
28
29
# File 'lib/arduino/status.rb', line 27

def [](value)
  @info[value.upcase.to_sym]
end

#[]=(register, value) ⇒ Object



20
21
22
23
24
25
# File 'lib/arduino/status.rb', line 20

def []=(register, value)
  transaction do
    register = register.upcase.to_sym
    @info[register] = value if @info.members.include?(register)
  end
end

#gcode_update(gcode) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/arduino/status.rb', line 31

def gcode_update(gcode)
  transaction do
    gcode.params.each do |p|
      setter = "#{p.head}="
      @info.send(setter, p.tail) if @info.respond_to?(setter)
    end
  end
end

#onchangeObject



40
41
42
# File 'lib/arduino/status.rb', line 40

def onchange
  @changes.subscribe { |diff| yield(diff) }
end

#pin(num) ⇒ Object



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

def pin(num)
  @info[:PINS][num] || :unknown
end

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  self[:BUSY] == 0
end

#set_pin(num, val) ⇒ Object



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

def set_pin(num, val)
  val = [true, 1, '1'].include?(val) ? :on : :off
  transaction { |info| info.PINS[num] = val }
end

#transaction {|@info| ... } ⇒ Object

Yields:

  • (@info)


12
13
14
15
16
17
18
# File 'lib/arduino/status.rb', line 12

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