Class: FB::Gcode

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

Defined Under Namespace

Classes: GcodeToken

Constant Summary collapse

GCODE_DICTIONARY =
YAML.load_file(File.join(File.dirname(__FILE__), 'gcode.yml'))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Gcode

Returns a new instance of Gcode.



8
9
10
# File 'lib/gcode.rb', line 8

def initialize(&block)
  @block  = block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



6
7
8
# File 'lib/gcode.rb', line 6

def block
  @block
end

#cmdObject

Returns the value of attribute cmd.



6
7
8
# File 'lib/gcode.rb', line 6

def cmd
  @cmd
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/gcode.rb', line 6

def params
  @params
end

Class Method Details

.parse_lines(string) ⇒ Object

Turns a string of many gcodes into an array of many gcodes. Used to parse incoming serial.



14
15
16
# File 'lib/gcode.rb', line 14

def self.parse_lines(string)
  string.gsub("\r", '').split("\n").map { |s| self.new { s } }
end

Instance Method Details

#nameObject

Returns a symbolized english version of the gcode’s name.



19
20
21
# File 'lib/gcode.rb', line 19

def name
  GCODE_DICTIONARY[cmd.to_sym] || :unknown
end

#to_sObject



23
24
25
26
# File 'lib/gcode.rb', line 23

def to_s
  # self.to_s # => "A12 B23 C45"
  [cmd, *params].map(&:to_s).join(" ")
end

#value_of(param) ⇒ Object



41
42
43
# File 'lib/gcode.rb', line 41

def value_of(param)
  params.find{ |p| p.head == param.to_sym.upcase }.tail
end