Class: FB::Status
- Inherits:
-
Object
- Object
- FB::Status
- Defined in:
- lib/arduino/status.rb
Overview
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.
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
- #[](value) ⇒ Object
- #[]=(register, value) ⇒ Object
- #gcode_update(gcode) ⇒ Object
- #get_pin(num) ⇒ Object
-
#initialize ⇒ Status
constructor
A new instance of Status.
- #onchange ⇒ Object
- #ready? ⇒ Boolean
- #set_parameter(key, val) ⇒ Object
- #set_pin(num, val) ⇒ Object
- #to_h ⇒ Object
- #transaction {|@info| ... } ⇒ Object
Constructor Details
#initialize ⇒ Status
Returns a new instance of Status.
10 11 12 13 |
# File 'lib/arduino/status.rb', line 10 def initialize @changes = EM::Channel.new @info = OpenStruct.new(DEFAULT_INFO) end |
Instance Method Details
#[](value) ⇒ Object
27 28 29 |
# File 'lib/arduino/status.rb', line 27 def [](value) @info[value.to_s.upcase] || :unknown end |
#[]=(register, value) ⇒ Object
23 24 25 |
# File 'lib/arduino/status.rb', line 23 def []=(register, value) transaction { @info[register.to_s.upcase] = value } end |
#gcode_update(gcode) ⇒ Object
35 36 37 38 39 |
# 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) ⇒ Object
49 50 51 |
# File 'lib/arduino/status.rb', line 49 def get_pin(num) @info.PINS[num] || :unknown end |
#onchange ⇒ Object
41 42 43 |
# File 'lib/arduino/status.rb', line 41 def onchange @changes.subscribe { |diff| yield(diff) } end |
#ready? ⇒ Boolean
45 46 47 |
# File 'lib/arduino/status.rb', line 45 def ready? self[:BUSY] == 0 end |
#set_parameter(key, val) ⇒ Object
58 59 60 61 62 63 |
# 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) ⇒ Object
53 54 55 56 |
# 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 ⇒ Object
31 32 33 |
# File 'lib/arduino/status.rb', line 31 def to_h @info.to_h end |
#transaction {|@info| ... } ⇒ Object
15 16 17 18 19 20 21 |
# 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 |