farmbot-serial 0.2.8 → 0.2.9
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 +4 -4
- data/lib/arduino.rb +8 -9
- data/lib/arduino/event_machine.rb +2 -0
- data/lib/arduino/incoming_handler.rb +0 -2
- data/lib/arduino/outgoing_handler.rb +0 -2
- data/spec/fakes/fake_arduino.rb +7 -0
- data/spec/fakes/fake_logger.rb +10 -0
- data/spec/fakes/fake_serial_port.rb +12 -0
- data/spec/lib/arduino/event_machine_spec.rb +44 -0
- data/spec/lib/arduino_spec.rb +55 -6
- data/spec/spec_helper.rb +7 -1
- metadata +10 -6
- data/spec/fixtures/stub_logger.rb +0 -5
- data/spec/fixtures/stub_serial_port.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9404c763a32e2450e343004905f6be373c533d49
|
4
|
+
data.tar.gz: 0fc2f566b496fc2ce390e56c900faa46761eb98f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c3a981cf97cdfdf5b125e61d3850927f75741718154856ebb2638bb405858a9176356c832ea41c3268f19a97318b10fc8f158e330f91e9aef9a0f01accabf61
|
7
|
+
data.tar.gz: d5788707dc01441531d127a987b69417aaaa91f7054814b8ec74e06a16003355c70bce1e41f89c42422869b247fec8ac2f0e55f1d9c454d38fbb7f2e17041013
|
data/lib/arduino.rb
CHANGED
@@ -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
|
@@ -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
|
data/spec/lib/arduino_spec.rb
CHANGED
@@ -1,15 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FB::Arduino do
|
4
|
-
let(:logger) {
|
4
|
+
let(:logger) { FakeLogger.new }
|
5
|
+
let(:serial_port) { FakeSerialPort.new }
|
5
6
|
let(:bot) do
|
6
|
-
FB::Arduino.new(serial_port:
|
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(
|
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
|
-
|
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
|
|
data/spec/spec_helper.rb
CHANGED
@@ -5,10 +5,16 @@ SimpleCov.start do
|
|
5
5
|
end
|
6
6
|
require 'pry'
|
7
7
|
require 'farmbot-serial'
|
8
|
-
|
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.
|
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-
|
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/
|
134
|
-
- spec/
|
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/
|
165
|
-
- spec/
|
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
|