bertha 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bertha.rb +26 -0
- data/spec/bertha_spec.rb +84 -0
- metadata +79 -0
data/lib/bertha.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'serialport'
|
2
|
+
|
3
|
+
class Bertha
|
4
|
+
|
5
|
+
def initialize port, speed = 9600
|
6
|
+
@serial = SerialPort.new port, speed, 8, 1
|
7
|
+
@serial.read_timeout = 200
|
8
|
+
@serial.read
|
9
|
+
end
|
10
|
+
|
11
|
+
def close
|
12
|
+
@serial.close
|
13
|
+
end
|
14
|
+
|
15
|
+
def Bertha.open port, speed
|
16
|
+
bertha = Bertha.new port, speed
|
17
|
+
yield bertha
|
18
|
+
bertha.close
|
19
|
+
end
|
20
|
+
|
21
|
+
def version
|
22
|
+
@serial.puts 'version'
|
23
|
+
@serial.gets.chomp
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/spec/bertha_spec.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'bertha'
|
2
|
+
|
3
|
+
describe Bertha do
|
4
|
+
|
5
|
+
let(:port) { '/dev/foo' }
|
6
|
+
let(:speed) { 9600 }
|
7
|
+
let(:serial_port) { mock SerialPort }
|
8
|
+
|
9
|
+
before :each do
|
10
|
+
SerialPort.stub!(:new).and_return(serial_port)
|
11
|
+
serial_port.stub! :read_timeout=
|
12
|
+
serial_port.stub! :read
|
13
|
+
serial_port.stub! :close
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when instantiating' do
|
17
|
+
|
18
|
+
it 'connects to the Arduino' do
|
19
|
+
SerialPort.should_receive :new
|
20
|
+
Bertha.new port, speed
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'connects with the requested port and speed' do
|
24
|
+
SerialPort.should_receive(:new).with(port, speed, 8, 1)
|
25
|
+
Bertha.new port, speed
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'connects with the default speed when not specified' do
|
29
|
+
SerialPort.should_receive(:new).with(port, speed, 8, 1)
|
30
|
+
Bertha.new port
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when opening with a block' do
|
36
|
+
|
37
|
+
it 'opens and closes a connection to the Arduino' do
|
38
|
+
|
39
|
+
SerialPort.should_receive :new
|
40
|
+
serial_port.should_receive :close
|
41
|
+
|
42
|
+
Bertha.open port, speed do end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'calls the block' do
|
47
|
+
block_called = false
|
48
|
+
Bertha.open(port, speed) do
|
49
|
+
block_called = true
|
50
|
+
end
|
51
|
+
block_called.should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'passes an instance of Bertha to the block' do
|
55
|
+
Bertha.open(port, speed) do |bertha|
|
56
|
+
bertha.should_not be_nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'when closing' do
|
63
|
+
|
64
|
+
it 'closes connection' do
|
65
|
+
serial_port.should_receive :close
|
66
|
+
bertha = Bertha.new port, speed
|
67
|
+
bertha.close
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'when connected' do
|
73
|
+
|
74
|
+
subject { Bertha.new port }
|
75
|
+
|
76
|
+
it 'returns the Bertha version' do
|
77
|
+
serial_port.should_receive(:puts).with('version')
|
78
|
+
serial_port.should_receive(:gets).and_return("v1.0.0\r\n")
|
79
|
+
subject.version.should == 'v1.0.0'
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bertha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Guy Royse
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: serialport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.1.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Bertha talks to the Arduino so you don't have to.
|
47
|
+
email: guy@guyroyse.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- lib/bertha.rb
|
53
|
+
- spec/bertha_spec.rb
|
54
|
+
homepage: http://rubygems.org/gems/bertha
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.23
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Bertha Ruby API
|
78
|
+
test_files:
|
79
|
+
- spec/bertha_spec.rb
|