BBB 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ module BBB
2
+ module Pins
3
+ class I2C
4
+ include Pinnable
5
+
6
+ def read(address, size, *params)
7
+ io.read(address, size, *params)
8
+ end
9
+
10
+ def write(address, *params)
11
+ io.write(address, *params)
12
+ end
13
+
14
+ private
15
+
16
+ def default_io
17
+ IO::I2C.new(position)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -20,7 +20,11 @@ module BBB
20
20
 
21
21
  def set_mode
22
22
  direction_file = gpio_pin_dir + "/direction"
23
- file_class.open(direction_file, "w") {|f| f.write(direction)}
23
+
24
+ d = "in" if direction == :input
25
+ d = "out" if direction == :output
26
+
27
+ file_class.open(direction_file, "w") {|f| f.write(d)}
24
28
  end
25
29
 
26
30
  def io
@@ -38,7 +42,7 @@ module BBB
38
42
 
39
43
  def read
40
44
  io.rewind
41
- value_map[io.read]
45
+ value_map.fetch(io.read.to_i)
42
46
  end
43
47
 
44
48
  def value_map
@@ -18,32 +18,33 @@ module BBB
18
18
  #
19
19
  # @param cape [String] The cape name of the I2C chip.
20
20
  #
21
- def initialize(cape)
22
- @cape = cape
21
+ def initialize(path)
22
+ @path = path
23
23
  self.export
24
24
  end
25
25
 
26
26
  def position
27
- @cape
27
+ @path
28
28
  end
29
29
 
30
30
  def export
31
- tree = pin_map.devicetree
32
- system("echo #{tree} > #{cape_dir}")
31
+ if pin_map.respond_to?(:devicetee)
32
+ system("echo #{pinmap.devicetree} > #{cape_dir}")
33
+ end
33
34
  sleep(0.2) # Give the kernel time to load the cape
34
- @backend = I2C.create(pin_map.path)
35
+ @backend = ::I2C.create(pin_map.path)
35
36
  end
36
37
 
37
38
  def write(address, *params)
38
- @backend.write(address, params)
39
+ @backend.write(address, *params)
39
40
  end
40
41
 
41
42
  def read(address, size, *params)
42
- @backend.read(address, size, params)
43
+ @backend.read(address, size, *params)
43
44
  end
44
45
 
45
46
  def pin_map_key
46
- :devicetree
47
+ :path
47
48
  end
48
49
 
49
50
  end
@@ -58,7 +58,7 @@ module BBB
58
58
  def self.map(pin_symbol)
59
59
  @map ||= convert(PIN_MAP_FILE)
60
60
  begin
61
- sym = pin_symbol.upcase
61
+ sym = pin_symbol #pin_symbol.upcase
62
62
  map = @map.pins.fetch(sym, nil)
63
63
  map = @map.i2c.fetch(sym) if map.nil?
64
64
 
@@ -207,7 +207,7 @@ module BBB
207
207
  end
208
208
  instance[:filesystem] = filesystem
209
209
 
210
- hash[instance.devicetree] = instance
210
+ hash[filesystem] = instance
211
211
  end
212
212
 
213
213
  return hash
@@ -32,7 +32,7 @@ module BBB
32
32
  end
33
33
 
34
34
  ##
35
- # Return wheter or not this is a mock pin
35
+ # Returns if this is a mock pin.
36
36
  #
37
37
  # @return [Boolean]
38
38
  #
@@ -57,12 +57,12 @@ module BBB
57
57
  # point the developer into the right direction.
58
58
  #
59
59
  # The default_io method should return an instantiated object that makes
60
- # the inteface between the filesystem, drivers etc, and the ruby code.
60
+ # the interface between the filesystem, drivers etc, and the ruby code.
61
61
  # Probably the object will behave like an IO object, relying on the linux
62
62
  # kernel and drivers to get things done on the board.
63
63
  #
64
64
  def default_io
65
- raise NotImplementedError
65
+ fail NotImplementedError
66
66
  end
67
67
 
68
68
  ##
data/lib/BBB/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module BBB
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -7,31 +7,21 @@ describe BBB::Application do
7
7
  end
8
8
 
9
9
  class TestLedCircuit < BBB::Circuit
10
- def initialize
11
- attach BBB::Components::Led, pin: :P8_3, as: :led
12
- end
10
+ attach BBB::Components::Led, as: :led
13
11
  end
14
12
 
15
13
  class TestConnectionApp < BBB::Application
16
- circuit TestLedCircuit.new
17
-
18
- def run
19
- "yeah, this one!"
20
- end
14
+ attach TestLedCircuit, as: :circuit
21
15
  end
22
16
 
23
17
  it "attaches virtual pins to board pins" do
24
18
  app = TestConnectionApp.new
25
-
26
19
  app.circuit.respond_to?(:led).should be_true
27
- app.circuit.led.should_receive(:on!)
28
-
29
- app.led.should eql(app.circuit.led)
30
- app.led.on!
20
+ app.circuit.led.respond_to?(:on!).should be_true
31
21
  end
32
22
 
33
23
  class FunctionsInApp < BBB::Application
34
- circuit TestLedCircuit.new
24
+ attach TestLedCircuit.new, as: :circuit
35
25
 
36
26
  def run
37
27
  raise StopIteration
data/spec/circuit_spec.rb CHANGED
@@ -11,31 +11,28 @@ describe BBB::Circuit do
11
11
 
12
12
  context "#attach" do
13
13
  it "initializes object when passed a class" do
14
- c.attach led, as: :led
15
- c.components.values.first.class.should eql(led)
14
+ c.class.attach led, as: :led
15
+ c.led.class.should eql(led)
16
16
  end
17
17
 
18
18
  it "keeps instance when passed an instance" do
19
19
  instance = led.new
20
- c.attach instance, as: :instance
21
- c.components.values.first.should eql(instance)
20
+ c.class.attach instance, as: :instance
21
+ c.instance.should eql(instance)
22
22
  end
23
23
 
24
24
  context "naming" do
25
25
  it "downcases class names" do
26
26
  l = led.new
27
- c.attach l, as: :led
27
+ c.class.attach l, as: :led
28
28
  c.led.should eql(l)
29
- c.components[:led].should eql(l)
30
29
  end
31
30
  end
32
31
 
33
32
  end
34
33
 
35
34
  class TestAttachCircuit < BBB::Circuit
36
- def initialize
37
- attach BBB::Components::Led, pin: :P8_1, as: :led
38
- end
35
+ attach BBB::Components::Led, as: :led
39
36
  end
40
37
 
41
38
  it "knows about the attached led" do
@@ -9,13 +9,13 @@ describe BBB::Components::AnalogComponent do
9
9
  end
10
10
 
11
11
  it "registers AnalogPin" do
12
- component_class.pins.should eql([BBB::Pins::AnalogPin])
12
+ component_class.pin_classes.should eql([BBB::Pins::AnalogPin])
13
13
  end
14
14
 
15
15
  context "initialized" do
16
16
  before :each do
17
17
  @c = component
18
- @c.initialize_pins(:P8_13)
18
+ @c.connect(:P8_13)
19
19
  end
20
20
 
21
21
  it "#pin" do
@@ -31,6 +31,11 @@ describe BBB::Components::AnalogComponent do
31
31
  @c.should_receive(:read)
32
32
  @c.value
33
33
  end
34
+
35
+ it "can intitialize with a pin position" do
36
+ c = component_class.new(:pin=>:P8_13)
37
+ c.positions.should == [:P8_13]
38
+ end
34
39
  end
35
40
 
36
41
  end
@@ -4,7 +4,7 @@ describe BBB::Components::Led do
4
4
  let(:led) {BBB::Components::Led.new}
5
5
 
6
6
  before :each do
7
- led.initialize_pins(:P8_4, mock: true)
7
+ led.connect(:P8_4, mock: true)
8
8
  end
9
9
 
10
10
  it "initializes off" do
@@ -18,11 +18,11 @@ describe BBB::Components::Pinnable do
18
18
  let(:dummy) { DummyComponent }
19
19
  let(:double_dummy) { DoubleDummyComponent }
20
20
 
21
- context "#initialize_pins" do
21
+ context "#connect" do
22
22
  it "with single position" do
23
23
  DummyPin.should_receive(:new).once.and_return(@mock_pin)
24
24
  c = dummy.new
25
- c.initialize_pins(:P8_3)
25
+ c.connect(:P8_3)
26
26
  c.pins[0].should eql(@mock_pin)
27
27
  end
28
28
 
@@ -30,14 +30,14 @@ describe BBB::Components::Pinnable do
30
30
  DummyPin.should_receive(:new).with(:P8_3, {:key=>"value"}).once
31
31
  .and_return(@mock_pin)
32
32
  c = dummy.new
33
- c.initialize_pins(:P8_3, key: "value")
33
+ c.connect(:P8_3, key: "value")
34
34
  c.pins[0].should eql(@mock_pin)
35
35
  end
36
36
 
37
37
  it "with multiple positions" do
38
38
  DummyPin.should_receive(:new).exactly(2).times.and_return(@mock_pin)
39
39
  c = double_dummy.new
40
- c.initialize_pins(:P8_3, :P8_4)
40
+ c.connect(:P8_3, :P8_4)
41
41
  c.pins[0].should eql(@mock_pin)
42
42
  c.pins[1].should eql(@mock_pin)
43
43
  end
@@ -48,7 +48,7 @@ describe BBB::Components::Pinnable do
48
48
  DummyPin.should_receive(:new).with(:P8_4, {:key=>"value"}).once
49
49
  .and_return(@mock_pin)
50
50
  c = double_dummy.new
51
- c.initialize_pins(:P8_3, :P8_4, key: "value")
51
+ c.connect(:P8_3, :P8_4, key: "value")
52
52
  c.pins[0].should eql(@mock_pin)
53
53
  c.pins[1].should eql(@mock_pin)
54
54
  end
@@ -56,7 +56,7 @@ describe BBB::Components::Pinnable do
56
56
  it "verifies the pin count" do
57
57
  c = dummy.new
58
58
  c.should_receive(:verify_pin_position_count).with([:P8_3])
59
- c.initialize_pins(:P8_3)
59
+ c.connect(:P8_3)
60
60
  end
61
61
  end
62
62
 
@@ -81,4 +81,14 @@ describe BBB::Components::Pinnable do
81
81
  c = dummy.new
82
82
  c.pinnable?.should be_true
83
83
  end
84
+
85
+ context "callbacks" do
86
+ context "after_connect" do
87
+ it "should use send on function name" do
88
+ end
89
+
90
+ it "should call if given a proc" do
91
+ end
92
+ end
93
+ end
84
94
  end
@@ -1,6 +1,52 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe BBB::Components::Servo do
4
- let(:servo) { BBB::Components::Servo.new }
4
+ let(:servo) { BBB::Components::Servo }
5
+
6
+ it "initializes with a pin as options" do
7
+ s = servo.new(:pin=>:P8_13)
8
+ s.positions.should eql([:P8_13])
9
+ end
10
+
11
+ %w(period min_duty max_duty max_degrees).each do |method|
12
+ it "#{method} as option" do
13
+ s = servo.new(method.to_sym=>123)
14
+ s.send(method.to_sym).should eql(123)
15
+ end
16
+ end
17
+
18
+ context "after initialization" do
19
+ before :each do
20
+ @servo = servo.new
21
+ @pin = double(BBB::Pins::PWMPin)
22
+ allow(@servo).to receive(:pin).and_return(@pin)
23
+ end
24
+
25
+ it "sets the period, duty and run" do
26
+ %w(period duty run).each do |method|
27
+ @pin.should_receive("#{method}=".to_sym)
28
+ end
29
+ @servo.send(:after_pin_initialization)
30
+ end
31
+ end
32
+
33
+ context "setting angles" do
34
+ before :each do
35
+ @servo = servo.new
36
+ @pin = double("pin")
37
+ allow(@servo).to receive(:pin).and_return(@pin)
38
+ allow(@pin).to receive(:"duty=")
39
+ end
40
+
41
+ it "#angle calls pin.duty" do
42
+ @pin.should_receive(:"duty=")
43
+ @servo.angle(100)
44
+ end
45
+
46
+ it "#angle converts angle to duty" do
47
+ @servo.should_receive(:degrees_to_ns).with(100)
48
+ @servo.angle(100)
49
+ end
50
+ end
5
51
 
6
52
  end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Blinking a Led" do
4
+
5
+ ##
6
+ # Setup the actual Application
7
+ #
8
+ class LedExampleApplication < BBB::Application
9
+ # Connect the led
10
+ attach BBB::Components::Led, as: :led
11
+
12
+ def initialize
13
+ led.connect(:P8_10, mock: true)
14
+ end
15
+
16
+ # This is the basic run loop
17
+ def run
18
+ led.on! # this does what you think it does
19
+ sleep(1) # sleep for 1 second, kind of blunt, since it blocks everthing.
20
+ led.off!
21
+ sleep(1)
22
+ end
23
+ end
24
+
25
+ # Initialize and run the LedExampleApplication
26
+
27
+ it "should initialize" do
28
+ lambda{ LedExampleApplication.new}.should_not raise_error
29
+ end
30
+
31
+ it "should respond to led" do
32
+ app = LedExampleApplication.new
33
+ app.respond_to?(:led).should be_true
34
+ end
35
+
36
+ it "should be able to turn led on" do
37
+ app = LedExampleApplication.new
38
+ lambda {app.led.on!}.should_not raise_error
39
+ end
40
+ end
@@ -63,4 +63,5 @@ describe BBB::Pins::DigitalPin do
63
63
  input.send(:default_io)
64
64
  end
65
65
  end
66
+
66
67
  end
@@ -7,10 +7,6 @@ describe BBB::Pins::IO::PinMapper do
7
7
  mapper.map(:P8_3).gpio.should eql(38)
8
8
  end
9
9
 
10
- it "should upcase" do
11
- mapper.map(:p8_3).gpio.should eql(38)
12
- end
13
-
14
10
  it "should raise an error when requesting an unknown pin" do
15
11
  lambda{ mapper.map(:P10_1) }.should raise_exception(BBB::UnknownPinException)
16
12
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: BBB
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wilco van Duinkerken
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-06 00:00:00.000000000 Z
11
+ date: 2014-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: i2c
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ! '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
@@ -56,28 +56,28 @@ dependencies:
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ! '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ! '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ! '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description: Helper functions to ruby around on the BeagleBone Black
@@ -91,24 +91,33 @@ files:
91
91
  - .travis.yml
92
92
  - BBB.gemspec
93
93
  - Gemfile
94
+ - Guardfile
94
95
  - README.md
95
96
  - Rakefile
96
97
  - examples/analog_pin.rb
98
+ - examples/ldr_light_switch.rb
97
99
  - examples/led.rb
98
100
  - examples/light_switch.rb
101
+ - examples/nunchuck.rb
99
102
  - examples/servo_ldr.rb
100
103
  - examples/sketches.rb
101
104
  - lib/BBB.rb
102
105
  - lib/BBB/application.rb
103
106
  - lib/BBB/circuit.rb
107
+ - lib/BBB/components.rb
104
108
  - lib/BBB/components/analog_component.rb
109
+ - lib/BBB/components/button.rb
105
110
  - lib/BBB/components/esc.rb
106
111
  - lib/BBB/components/led.rb
112
+ - lib/BBB/components/nunchuck.rb
107
113
  - lib/BBB/components/pinnable.rb
108
114
  - lib/BBB/components/servo.rb
115
+ - lib/BBB/components/wii_motion_plus.rb
109
116
  - lib/BBB/exceptions.rb
117
+ - lib/BBB/pins.rb
110
118
  - lib/BBB/pins/analog_pin.rb
111
119
  - lib/BBB/pins/digital_pin.rb
120
+ - lib/BBB/pins/i2c.rb
112
121
  - lib/BBB/pins/io/ain.rb
113
122
  - lib/BBB/pins/io/cape.rb
114
123
  - lib/BBB/pins/io/gpio.rb
@@ -127,6 +136,7 @@ files:
127
136
  - spec/components/led_spec.rb
128
137
  - spec/components/pinnable_spec.rb
129
138
  - spec/components/servo_spec.rb
139
+ - spec/examples/led_spec.rb
130
140
  - spec/pins/analog_pin_spec.rb
131
141
  - spec/pins/digital_input_pin_spec.rb
132
142
  - spec/pins/digital_output_pin_spec.rb
@@ -147,17 +157,17 @@ require_paths:
147
157
  - lib
148
158
  required_ruby_version: !ruby/object:Gem::Requirement
149
159
  requirements:
150
- - - '>='
160
+ - - ! '>='
151
161
  - !ruby/object:Gem::Version
152
162
  version: '0'
153
163
  required_rubygems_version: !ruby/object:Gem::Requirement
154
164
  requirements:
155
- - - '>='
165
+ - - ! '>='
156
166
  - !ruby/object:Gem::Version
157
167
  version: '0'
158
168
  requirements: []
159
169
  rubyforge_project:
160
- rubygems_version: 2.0.3
170
+ rubygems_version: 2.1.11
161
171
  signing_key:
162
172
  specification_version: 4
163
173
  summary: Helper functions to ruby around on the BeagleBone Black
@@ -168,6 +178,7 @@ test_files:
168
178
  - spec/components/led_spec.rb
169
179
  - spec/components/pinnable_spec.rb
170
180
  - spec/components/servo_spec.rb
181
+ - spec/examples/led_spec.rb
171
182
  - spec/pins/analog_pin_spec.rb
172
183
  - spec/pins/digital_input_pin_spec.rb
173
184
  - spec/pins/digital_output_pin_spec.rb
@@ -178,4 +189,3 @@ test_files:
178
189
  - spec/pins/pinnable_spec.rb
179
190
  - spec/pins/pwm_pin_spec.rb
180
191
  - spec/spec_helper.rb
181
- has_rdoc: