farmbot-serial 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8656fb126d608b73102d0a77ac29d38e5fa60647
4
- data.tar.gz: 67f35f21edc7b07e20894ef6e0f8768dd5d41751
3
+ metadata.gz: 5b9026a21d4da007da2bdc85509d68297763fce5
4
+ data.tar.gz: 78705810cdea9c1b98e6f217859b2be0a08e6090
5
5
  SHA512:
6
- metadata.gz: 99d37958fcc632fea80f2393becb10da870d94289045a6b22c75e2f1a23c820f07c227f8ac92086598e4f55172fa81d88380dbcb591725edb873f0bba81f2eb5
7
- data.tar.gz: c30665c95c08c2bd69be3ef65adf51ea7869ff41b2d94a712c00fdc8f92e16970e951c67de05f0cb434fe7ca6cb3cc9eb7df91dcd521fa421c029bcb313941d6
6
+ metadata.gz: d02a8e074e6964ed3340b0450528b3caac103bc54734266cbd24db2d1db25c1516ed48fbd704cd17416f4a6c6f9134da4cbc1e8c18d0a467e560c13d27614aa6
7
+ data.tar.gz: 1aa4ede5691439074cd5de2e269b52e18a405d65a2faac85aeac8d305b8643e50570de696fa58a72ee4a54468e0f42ad63dc4d688a8f5ce7d19d613710ef5f7f
data/lib/arduino.rb CHANGED
@@ -35,8 +35,8 @@ module FB
35
35
  end
36
36
 
37
37
  # Send outgoing test to arduino from pi
38
- def write(string)
39
- @outbound_queue.unshift string
38
+ def write(gcode)
39
+ @outbound_queue.unshift gcode
40
40
  execute_command_next_tick
41
41
  end
42
42
 
@@ -20,7 +20,7 @@ module FB
20
20
  y += (bot.current_position.y || 0)
21
21
  z += (bot.current_position.z || 0)
22
22
 
23
- write "G00 X#{x} Y#{y} Z#{z}"
23
+ write { FB::Gcode.new { "G00 X#{x} Y#{y} Z#{z}" } }
24
24
  end
25
25
 
26
26
  def move_absolute(x: 0, y: 0, z: 0, s: 100)
@@ -61,8 +61,8 @@ module FB
61
61
 
62
62
  private
63
63
 
64
- def write(str)
65
- bot.write(FB::Gcode.new(str))
64
+ def write(str = "\n")
65
+ bot.write( block_given? ? yield : FB::Gcode.new{ str } )
66
66
  end
67
67
  end
68
68
  end
data/lib/gcode.rb CHANGED
@@ -3,18 +3,16 @@ module FB
3
3
  class Gcode
4
4
  GCODE_DICTIONARY = YAML.load_file(File.join(File.dirname(__FILE__), 'gcode.yml'))
5
5
 
6
- attr_accessor :cmd, :params, :str
6
+ attr_accessor :cmd, :params, :block
7
7
 
8
- def initialize(str)
9
- @str = str
10
- @params = str.split(' ').map { |line| GcodeToken.new(line) }
11
- @cmd = @params.shift || 'NULL'
8
+ def initialize(&block)
9
+ @block = block
12
10
  end
13
11
 
14
12
  # Turns a string of many gcodes into an array of many gcodes. Used to parse
15
13
  # incoming serial.
16
14
  def self.parse_lines(string)
17
- string.gsub("\r", '').split("\n").map { |s| self.new(s) }
15
+ string.gsub("\r", '').split("\n").map { |s| self.new { s } }
18
16
  end
19
17
 
20
18
  # Returns a symbolized english version of the gcode's name.
@@ -24,12 +22,25 @@ module FB
24
22
 
25
23
  def to_s
26
24
  # self.to_s # => "A12 B23 C45"
27
- [@cmd, *@params].map(&:to_s).join(" ")
25
+ [cmd, *params].map(&:to_s).join(" ")
26
+ end
27
+
28
+ def params
29
+ @block
30
+ .call
31
+ .split(' ')
32
+ .map { |line| GcodeToken.new(line) }
33
+ .tap { |p| p.shift }
34
+ end
35
+
36
+ def cmd
37
+ cmd = @block.call.split(' ')
38
+ GcodeToken.new(cmd.any? ? cmd.first : "NULL0")
28
39
  end
29
40
 
30
41
  # A head/tail pair of a single node of GCode. Ex: R01 = [:R, '01']
31
42
  class GcodeToken
32
- attr_reader :head, :tail, :name
43
+ attr_reader :head, :tail
33
44
 
34
45
  def initialize(str)
35
46
  nodes = str.scan(/\d+|\D+/) # ["R", "01"]
@@ -1,17 +1,18 @@
1
1
  require 'spec_helper'
2
2
  describe FB::Gcode do
3
- let(:gcode) { FB::Gcode.new("F31 P8 ")}
3
+ let(:gcode) { FB::Gcode.new{ "F31 P8 " } }
4
+ let(:null_token) { FB::Gcode::GcodeToken.new("NULL0") }
4
5
 
5
6
  it("initializes from string") { expect(gcode).to be_kind_of(FB::Gcode) }
6
7
 
7
8
  it("infers Gcode name") { expect(gcode.name).to eq(:read_status) }
8
9
 
9
10
  it "returns :unknown for bad Gcode tokens" do
10
- unknown = FB::Gcode.new("QQQ31 F32 ").name
11
+ unknown = FB::Gcode.new{ "QQQ31 F32 " }.name
11
12
  expect(unknown).to eq(:unknown)
12
13
  end
13
14
 
14
- it("sets the original input string") { expect(gcode.str).to eq("F31 P8 ") }
15
+ it("sets the original input string") { expect(gcode.block[]).to eq("F31 P8 ") }
15
16
 
16
17
  it("sets @cmd using the first Gcode node") do
17
18
  expect(gcode.cmd).to be_kind_of(FB::Gcode::GcodeToken)
@@ -39,8 +40,14 @@ describe FB::Gcode do
39
40
  end
40
41
 
41
42
  it 'handles parameterless Gcode' do
42
- expect(FB::Gcode.new(" ").name).to be(:unknown)
43
- expect(FB::Gcode.new(" ").cmd).to eq("NULL")
43
+ expect(FB::Gcode.new{ " " }.name).to be(:unknown)
44
+ expect(FB::Gcode.new{ " " }.cmd.head).to eq(null_token.head)
45
+ expect(FB::Gcode.new{ " " }.cmd.tail).to eq(null_token.tail)
46
+ end
47
+
48
+ it 'sets dyanmic parameters' do
49
+ random_gcode = FB::Gcode.new{ "Q#{Time.now.to_f.to_s[-2, 2]}" }
50
+ expect(random_gcode.cmd.tail).to_not eq(random_gcode.cmd.tail)
44
51
  end
45
52
  end
46
53
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: farmbot-serial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Evers
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-10 00:00:00.000000000 Z
12
+ date: 2015-04-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler