Class: FB::Status
- Inherits:
-
Object
- Object
- FB::Status
- 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, Q: 0, T: 0, C: '', P: 0, V: 0, W: 0, L: 0, E: 0, M: 0, XA: 0, XB: 0, YA: 0, YB: 0, ZA: 0, ZB: 0,YR: 0, R: 0, BUSY: 1, LAST: 'none'}
Instance Method Summary collapse
- #[](value) ⇒ Object
- #[]=(register, value) ⇒ Object
- #gcode_update(gcode) ⇒ Object
-
#initialize ⇒ Status
constructor
A new instance of Status.
- #onchange ⇒ Object
- #ready? ⇒ Boolean
- #transaction {|@info| ... } ⇒ Object
Constructor Details
#initialize ⇒ Status
Returns a new instance of Status.
9 10 11 12 |
# File 'lib/arduino/status.rb', line 9 def initialize @changes = EM::Channel.new @info = Info.new(*DEFAULT_INFO.values) end |
Instance Method Details
#[](value) ⇒ Object
33 34 35 |
# File 'lib/arduino/status.rb', line 33 def [](value) @info[value.upcase.to_sym] end |
#[]=(register, value) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/arduino/status.rb', line 22 def []=(register, value) transaction do register = register.upcase.to_sym if @info.members.include?(register) @info[register] = value else bot.log "Ignoring attempt to set unknown status value: #{register}" end end end |
#gcode_update(gcode) ⇒ Object
37 38 39 40 41 |
# File 'lib/arduino/status.rb', line 37 def gcode_update(gcode) transaction do gcode.params.each { |p| @info.send("#{p.head}=", p.tail) } end end |
#onchange ⇒ Object
43 44 45 |
# File 'lib/arduino/status.rb', line 43 def onchange @changes.subscribe { |diff| yield(diff) } end |
#ready? ⇒ Boolean
47 48 49 |
# File 'lib/arduino/status.rb', line 47 def ready? self[:BUSY] == 0 end |
#transaction {|@info| ... } ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/arduino/status.rb', line 14 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 |