farmbot-serial 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6cb4643e76ded7ebfa9d5a162ac7054aa5b073a
4
- data.tar.gz: 3ec3133a071facfc6b30c294d0028d4c2aac55a8
3
+ metadata.gz: 9404c763a32e2450e343004905f6be373c533d49
4
+ data.tar.gz: 0fc2f566b496fc2ce390e56c900faa46761eb98f
5
5
  SHA512:
6
- metadata.gz: 6f4e9a3c086c1be7805b9f7a8bba936fb167c5dccb92b22ed6f065b9be4dd68e4680139a82f09c3eb8099af2ebb62d6643fc1ef8ad64d83134b562354e26f182
7
- data.tar.gz: 541032dd5e2dd8462da5e1d0eba48e8ec7b555604d90b228fd64ec5a90f59fba7ed356ad04794e51b99ad3e96b27c1e0caf6431d9716a978b286e7dc888a4d04
6
+ metadata.gz: 1c3a981cf97cdfdf5b125e61d3850927f75741718154856ebb2638bb405858a9176356c832ea41c3268f19a97318b10fc8f158e330f91e9aef9a0f01accabf61
7
+ data.tar.gz: d5788707dc01441531d127a987b69417aaaa91f7054814b8ec74e06a16003355c70bce1e41f89c42422869b247fec8ac2f0e55f1d9c454d38fbb7f2e17041013
@@ -7,7 +7,6 @@ require_relative 'arduino/status'
7
7
  # Communicate with the arduino using a serial interface
8
8
  module FB
9
9
  class Arduino
10
- class EmergencyStop < StandardError; end # Not yet used.
11
10
  Position = Struct.new(:x, :y, :z)
12
11
 
13
12
  attr_accessor :serial_port, :logger, :commands, :inbound_queue, :status,
@@ -63,14 +62,6 @@ module FB
63
62
  Position.new(status[:X], status[:Y], status[:Z])
64
63
  end
65
64
 
66
- private
67
-
68
- # Highest priority message when processing incoming Gcode. Use for system
69
- # level status changes.
70
- def parse_incoming(gcode)
71
- inputs.execute(gcode)
72
- end
73
-
74
65
  def execute_command_next_tick
75
66
  EM.next_tick do
76
67
  status.ready? ? pop_gcode_off_queue : execute_command_next_tick
@@ -93,5 +84,13 @@ module FB
93
84
  end
94
85
  end
95
86
  end
87
+
88
+ private
89
+
90
+ # Highest priority method for processing incoming Gcode. Use for system
91
+ # level status changes.
92
+ def parse_incoming(gcode)
93
+ inputs.execute(gcode)
94
+ end
96
95
  end
97
96
  end
@@ -7,6 +7,8 @@ module FB
7
7
  attr_accessor :arduino
8
8
  end
9
9
 
10
+ attr_reader :q, :buffer, :bot
11
+
10
12
  def initialize
11
13
  @bot = self.class.arduino
12
14
  @q, @buffer = @bot.inbound_queue, ''
@@ -3,8 +3,6 @@ module FB
3
3
  class IncomingHandler
4
4
  attr_reader :bot
5
5
 
6
- class UnhandledGcode < StandardError; end
7
-
8
6
  def initialize(bot)
9
7
  @bot = bot
10
8
  end
@@ -4,8 +4,6 @@ module FB
4
4
  class OutgoingHandler
5
5
  attr_reader :bot
6
6
 
7
- class UnhandledGcode < StandardError; end
8
-
9
7
  def initialize(bot)
10
8
  @bot = bot
11
9
  end
@@ -0,0 +1,7 @@
1
+ require_relative "fake_logger"
2
+ require_relative "fake_serial_port"
3
+
4
+ class FakeArduino < FB::Arduino
5
+ def initialize(serial_port: FakeSerialPort.new, logger: FakeLogger.new)
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ class FakeLogger < StringIO
2
+ def initialize(input = "")
3
+ super
4
+ end
5
+
6
+ def message
7
+ rewind
8
+ read.chomp
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ ## SERIAL PORT SIMULATION
2
+ ## **********************
3
+ class FakeSerialPort < StringIO
4
+ def initialize(*)
5
+ super("")
6
+ end
7
+
8
+ def message
9
+ rewind
10
+ read.chomp
11
+ end
12
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe FB::ArduinoEventMachine do
4
+ let(:logger) { FakeLogger.new }
5
+ let(:serial_port) { FakeSerialPort.new }
6
+ let(:bot) { FB::Arduino.new(serial_port: serial_port, logger: logger) }
7
+ let(:em) do
8
+ FB::ArduinoEventMachine.arduino = bot
9
+ FB::ArduinoEventMachine.new(serial_port)
10
+ end
11
+
12
+ it 'initializes' do
13
+ em
14
+ expect(FB::ArduinoEventMachine.arduino).to eq(bot)
15
+ expect(em.bot).to eq(bot)
16
+ expect(em.q).to eq(bot.inbound_queue)
17
+ expect(em.buffer).to eq('')
18
+ end
19
+
20
+ it 'splits incoming data' do
21
+ text = em.split_into_chunks "hello\r\nworld\r\n"
22
+ expect(text).to include("hello\r\n")
23
+ expect(text).to include("world\r\n")
24
+ end
25
+
26
+ it 'fills / clears the buffer' do
27
+ msg = "A1 B2 C3\r\nD4 E5 F6\r\n"
28
+ em.add_to_buffer("A1 B2 C3\r\nD4 E5 F6\r\n")
29
+ expect(em.buffer).to eq(msg)
30
+
31
+ within_event_loop do
32
+ #subscribe to the message queue and grab the last result.
33
+ em.q.subscribe { |x| @results = x }
34
+ em.send_buffer
35
+ end
36
+
37
+ expect(@results.length).to eq(2)
38
+ expect(@results.first.cmd.head).to eq(:A)
39
+ expect(@results.last.cmd.head).to eq(:D)
40
+
41
+ em.clear_buffer
42
+ expect(em.buffer).to eq('')
43
+ end
44
+ end
@@ -1,15 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FB::Arduino do
4
- let(:logger) { StringIO.new("") }
4
+ let(:logger) { FakeLogger.new }
5
+ let(:serial_port) { FakeSerialPort.new }
5
6
  let(:bot) do
6
- FB::Arduino.new(serial_port: StubSerialPort.new(0, 0),
7
- logger: logger)
7
+ FB::Arduino.new(serial_port: serial_port, logger: logger)
8
8
  end
9
9
 
10
+
11
+ it "logs" do
12
+ bot.log 'Hello, world!'
13
+ expect(logger.message).to eq('Hello, world!')
14
+ end
15
+
16
+
10
17
  it "initializes" do
11
18
  expect(bot).to be_kind_of(FB::Arduino)
12
- expect(bot.serial_port).to be_kind_of(StubSerialPort)
19
+ expect(bot.serial_port).to be_kind_of(FakeSerialPort)
13
20
  expect(bot.logger).to be_kind_of(StringIO)
14
21
  expect(bot.commands).to be_kind_of(FB::OutgoingHandler)
15
22
  expect(bot.inbound_queue).to be_kind_of(EM::Channel)
@@ -19,9 +26,51 @@ describe FB::Arduino do
19
26
 
20
27
  it 'prints to the logger object' do
21
28
  bot.log "Hello, World!"
22
- bot.logger.rewind
23
- expect(bot.logger.gets.chomp).to eq("Hello, World!")
29
+ expect(logger.message).to eq("Hello, World!")
24
30
  end
25
31
 
32
+ it 'writes to outbound command queue' do
33
+ bot.write("A1 B2 C3")
34
+ expect(bot.outbound_queue).to include("A1 B2 C3")
35
+ end
36
+
37
+ it "sets change/message/close callbacks" do
38
+ yowza = ->{ bot.log "QQQ" }
39
+ bot.onmessage(&yowza)
40
+ bot.onclose(&yowza)
41
+ bot.onchange(&yowza)
42
+ expect(bot.instance_variable_get(:@onmessage)).to be(yowza)
43
+ expect(bot.instance_variable_get(:@onclose)).to be(yowza)
44
+ expect(bot.instance_variable_get(:@onchange)).to be(yowza)
45
+ end
46
+
47
+ it "calls onclose callback via disconnect()" do
48
+ calls = []
49
+ bot.onclose { calls << "Hey!" }
50
+ bot.disconnect
51
+ expect(logger.message).to eq("Connection to device lost")
52
+ expect(calls.length).to eq(1)
53
+ expect(calls).to include("Hey!")
54
+ end
55
+
56
+ it "reports current_position" do
57
+ bot.status[:x] = 1
58
+ bot.status[:y] = 2
59
+ bot.status[:z] = 3
60
+ expect(bot.current_position.x).to eq(1)
61
+ expect(bot.current_position.y).to eq(2)
62
+ expect(bot.current_position.z).to eq(3)
63
+ end
64
+
65
+ it "pops gcode off queue" do
66
+ command = FB::Gcode.new { "A1 B2 C3" }
67
+ bot.outbound_queue.push(command)
68
+ expect(bot.outbound_queue.length).to eq(1)
69
+ within_event_loop { bot.pop_gcode_off_queue }
70
+ expect(bot.outbound_queue.length).to eq(0)
71
+ expect(serial_port.message).to eq("A1 B2 C3")
72
+ expect(bot.status.ready?).to be_falsey
73
+ expect(bot.status[:last]).to eq(:unknown)
74
+ end
26
75
  end
27
76
 
@@ -5,10 +5,16 @@ SimpleCov.start do
5
5
  end
6
6
  require 'pry'
7
7
  require 'farmbot-serial'
8
- require_relative 'fixtures/stub_serial_port'
8
+
9
+ require_relative 'fakes/fake_serial_port'
10
+ require_relative 'fakes/fake_logger'
11
+ require_relative 'fakes/fake_arduino'
12
+
9
13
  RSpec.configure do |config|
10
14
  end
11
15
 
16
+ # This is used for testing things that require an event loop. Once run, you can
17
+ # observe / make assertions on side effects.
12
18
  def within_event_loop
13
19
  EM.run do
14
20
  yield
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.2.8
4
+ version: 0.2.9
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-21 00:00:00.000000000 Z
12
+ date: 2015-04-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -130,8 +130,10 @@ files:
130
130
  - lib/farmbot-serial.rb
131
131
  - lib/gcode.rb
132
132
  - lib/gcode.yml
133
- - spec/fixtures/stub_logger.rb
134
- - spec/fixtures/stub_serial_port.rb
133
+ - spec/fakes/fake_arduino.rb
134
+ - spec/fakes/fake_logger.rb
135
+ - spec/fakes/fake_serial_port.rb
136
+ - spec/lib/arduino/event_machine_spec.rb
135
137
  - spec/lib/arduino/status_spec.rb
136
138
  - spec/lib/arduino_spec.rb
137
139
  - spec/lib/gcode_spec.rb
@@ -161,8 +163,10 @@ signing_key:
161
163
  specification_version: 4
162
164
  summary: Serial library for Farmbot
163
165
  test_files:
164
- - spec/fixtures/stub_logger.rb
165
- - spec/fixtures/stub_serial_port.rb
166
+ - spec/fakes/fake_arduino.rb
167
+ - spec/fakes/fake_logger.rb
168
+ - spec/fakes/fake_serial_port.rb
169
+ - spec/lib/arduino/event_machine_spec.rb
166
170
  - spec/lib/arduino/status_spec.rb
167
171
  - spec/lib/arduino_spec.rb
168
172
  - spec/lib/gcode_spec.rb
@@ -1,5 +0,0 @@
1
- class StubLogger < StringIO
2
- def initialize("")
3
- super
4
- end
5
- end
@@ -1,6 +0,0 @@
1
- ## SERIAL PORT SIMULATION
2
- ## **********************
3
- class StubSerialPort < StringIO
4
- def initialize(comm_port, parameters)
5
- end
6
- end