BBB 0.0.10 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/BBB.rb +13 -17
- data/lib/BBB/application.rb +4 -21
- data/lib/BBB/circuit.rb +18 -33
- data/lib/BBB/components/analog_component.rb +29 -6
- data/lib/BBB/components/led.rb +39 -9
- data/lib/BBB/components/pinnable.rb +90 -4
- data/lib/BBB/components/servo.rb +43 -0
- data/lib/BBB/pins/analog_pin.rb +39 -0
- data/lib/BBB/pins/digital_pin.rb +106 -0
- data/lib/BBB/pins/io/ain.rb +58 -0
- data/lib/BBB/pins/io/gpio.rb +77 -0
- data/lib/BBB/pins/io/mapped.rb +39 -0
- data/lib/BBB/pins/io/pin_mapper.rb +224 -0
- data/lib/BBB/pins/io/pwm.rb +61 -0
- data/lib/BBB/pins/pinnable.rb +78 -0
- data/lib/BBB/pins/pwm_pin.rb +43 -0
- data/lib/BBB/version.rb +1 -1
- data/spec/application_spec.rb +1 -26
- data/spec/circuit_spec.rb +0 -1
- data/spec/components/analog_component_spec.rb +36 -0
- data/spec/components/led_spec.rb +14 -19
- data/spec/components/pinnable_spec.rb +73 -8
- data/spec/components/servo_spec.rb +6 -0
- data/spec/pins/analog_pin_spec.rb +33 -0
- data/spec/pins/digital_input_pin_spec.rb +13 -0
- data/spec/pins/digital_output_pin_spec.rb +14 -0
- data/spec/pins/digital_pin_spec.rb +66 -0
- data/spec/pins/io/mapped_spec.rb +41 -0
- data/spec/{board → pins/io}/pin_mapper_spec.rb +2 -2
- data/spec/pins/pinnable_spec.rb +68 -0
- data/spec/pins/pwm_pin_spec.rb +29 -0
- metadata +34 -37
- data/lib/BBB/adc/analog_pin.rb +0 -42
- data/lib/BBB/adc/setup.rb +0 -20
- data/lib/BBB/board/base.rb +0 -45
- data/lib/BBB/board/json_pin_mapper.rb +0 -203
- data/lib/BBB/board/pin_mapper.rb +0 -20
- data/lib/BBB/board/test_board.rb +0 -12
- data/lib/BBB/gpio/base.rb +0 -56
- data/lib/BBB/gpio/digital_pin.rb +0 -69
- data/lib/BBB/gpio/pin_converter.rb +0 -28
- data/lib/BBB/io/analog_pin.rb +0 -24
- data/lib/BBB/io/digital_pin.rb +0 -67
- data/lib/BBB/io/mock_pin.rb +0 -8
- data/lib/BBB/io/pinnable.rb +0 -12
- data/spec/adc/analog_pin_spec.rb +0 -100
- data/spec/adc/setup_spec.rb +0 -9
- data/spec/board/board_spec.rb +0 -49
- data/spec/gpio/base_spec.rb +0 -48
- data/spec/gpio/digital_pin_spec.rb +0 -100
- data/spec/gpio/pin_converter_spec.rb +0 -19
- data/spec/io/digital_pin_spec.rb +0 -14
- data/spec/io/mock_pin_spec.rb +0 -13
- data/spec/io/pinnable_spec.rb +0 -13
data/lib/BBB/io/analog_pin.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
module BBB
|
2
|
-
module IO
|
3
|
-
class AnalogPin
|
4
|
-
include Pinnable
|
5
|
-
|
6
|
-
attr_reader :pin_io, :position
|
7
|
-
attr_accessor :pin_io
|
8
|
-
|
9
|
-
def initialize(pin_io=nil, position=nil)
|
10
|
-
@pin_io = pin_io || MockPin.new
|
11
|
-
@position = position
|
12
|
-
end
|
13
|
-
|
14
|
-
def read
|
15
|
-
pin_io.read
|
16
|
-
end
|
17
|
-
|
18
|
-
def scale
|
19
|
-
pin_io.scale
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
data/lib/BBB/io/digital_pin.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
module BBB
|
2
|
-
module IO
|
3
|
-
##
|
4
|
-
# Credit goes to the great work done at the Artoo framework
|
5
|
-
# The original idea for this class came from them.
|
6
|
-
# https://github.com/hybridgroup/artoo/blob/master/lib/artoo/adaptors/io/digital_pin.rb
|
7
|
-
#
|
8
|
-
class DigitalPin
|
9
|
-
include Pinnable
|
10
|
-
attr_reader :mode, :status
|
11
|
-
attr_accessor :pin_io
|
12
|
-
|
13
|
-
##
|
14
|
-
# Digital pin which is abstracted away from any kind of IO.
|
15
|
-
# This has the benefit of being able to write a generic application
|
16
|
-
# that you can then easily port to a BBB, Pi or Arduino
|
17
|
-
#
|
18
|
-
# @param pin_num [Symbol]
|
19
|
-
# @param mode [Symbol, nil] the mode of the pin, :input or :output. Defaults
|
20
|
-
# to :input
|
21
|
-
# @pin_io [IO, nil] the IO object
|
22
|
-
#
|
23
|
-
def initialize(mode=:input, pin_io=nil, position=nil)
|
24
|
-
@pin_io = pin_io || MockPin.new
|
25
|
-
@mode = mode
|
26
|
-
@position = position
|
27
|
-
end
|
28
|
-
|
29
|
-
# Writes to the specified pin Digitally
|
30
|
-
# accepts values :high or :low
|
31
|
-
def write(value)
|
32
|
-
raise ArgumentError unless [:high, :low].include?(value)
|
33
|
-
@status = value
|
34
|
-
@pin_io.write(value)
|
35
|
-
end
|
36
|
-
|
37
|
-
# Reads digitally from the specified pin on initialize
|
38
|
-
def read
|
39
|
-
if mode == :input
|
40
|
-
pin_io.read
|
41
|
-
else
|
42
|
-
status
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# Sets digital write for the pin to HIGH
|
47
|
-
def on!
|
48
|
-
write(:high)
|
49
|
-
end
|
50
|
-
#
|
51
|
-
# Sets digital write for the pin to LOW
|
52
|
-
#
|
53
|
-
def off!
|
54
|
-
write(:low)
|
55
|
-
end
|
56
|
-
|
57
|
-
def on?
|
58
|
-
(status == :high) ? true : false
|
59
|
-
end
|
60
|
-
|
61
|
-
def off?
|
62
|
-
!self.on?
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
data/lib/BBB/io/mock_pin.rb
DELETED
data/lib/BBB/io/pinnable.rb
DELETED
data/spec/adc/analog_pin_spec.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe BBB::ADC::AnalogPin do
|
4
|
-
let(:pin) { BBB::ADC::AnalogPin.new(:P9_39, :mock=>true) }
|
5
|
-
let(:pin_class) { BBB::ADC::AnalogPin }
|
6
|
-
|
7
|
-
|
8
|
-
describe "#initialize" do
|
9
|
-
|
10
|
-
describe "mock" do
|
11
|
-
it "initializes mock" do
|
12
|
-
lambda{ pin }.should_not raise_error
|
13
|
-
end
|
14
|
-
|
15
|
-
it "gets a mock handle when initialized with mock" do
|
16
|
-
pin.file_handle.class.should eql(StringIO)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "saves the mock value in instance variable" do
|
20
|
-
pin.mock.should be_true
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
describe "the real deal" do
|
25
|
-
before :each do
|
26
|
-
pin_class.any_instance.should_receive(:ain_path).and_return("some/file")
|
27
|
-
File.should_receive(:open).with("some/file","r").and_return(StringIO.new("foo"))
|
28
|
-
end
|
29
|
-
|
30
|
-
it "gets file handle" do
|
31
|
-
pin_class.new(:P9_39).file_handle.read.should eql("foo")
|
32
|
-
end
|
33
|
-
|
34
|
-
it "saves the mock value in an instance variable" do
|
35
|
-
pin_class.new(:P9_39).mock.should_not be_true
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
it "sets the pin position" do
|
40
|
-
pin_class.any_instance.should_receive(:'position=')
|
41
|
-
pin
|
42
|
-
end
|
43
|
-
|
44
|
-
it "fails when initializing a non AIN pin position" do
|
45
|
-
lambda{pin_class.new(:P8_2, :mock=>true)}.should raise_error(ArgumentError)
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
describe "#get_file_handle" do
|
51
|
-
before :all do
|
52
|
-
@p = BBB::ADC::AnalogPin.new(:P9_39, :mock=>true)
|
53
|
-
end
|
54
|
-
|
55
|
-
it "gets mock handle with mock argument" do
|
56
|
-
handle = @p.get_file_handle(true)
|
57
|
-
handle.class.should eql(StringIO)
|
58
|
-
end
|
59
|
-
|
60
|
-
it "gets a File handle if explicit no mock" do
|
61
|
-
File.should_receive(:open).and_return("file")
|
62
|
-
handle = @p.get_file_handle(false)
|
63
|
-
handle.should eql("file")
|
64
|
-
end
|
65
|
-
|
66
|
-
it "gets a File by default" do
|
67
|
-
File.should_receive(:open).and_return("file")
|
68
|
-
handle = @p.get_file_handle
|
69
|
-
handle.should eql("file")
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
describe "#position=" do
|
74
|
-
it "sets the instance variable position" do
|
75
|
-
pin.position = :P9_40
|
76
|
-
pin.instance_variable_get("@position").should eql(:P9_40)
|
77
|
-
end
|
78
|
-
|
79
|
-
it "sets the pin_map variable" do
|
80
|
-
p = pin
|
81
|
-
p.position = :P9_40
|
82
|
-
p.instance_variable_get("@pin_map").ain.should eql(1)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
it "#read calls the file handle" do
|
87
|
-
p = pin
|
88
|
-
p.file_handle.should_receive(:read).and_return("bazinga")
|
89
|
-
p.read.should eql("bazinga")
|
90
|
-
end
|
91
|
-
|
92
|
-
it "#ain_path calls the map" do
|
93
|
-
path = pin.ain_path
|
94
|
-
path.should eql("/sys/devices/ocp.3/helper.15/AIN0")
|
95
|
-
end
|
96
|
-
|
97
|
-
it "#scale tells the scale" do
|
98
|
-
pin.scale.should eql(4096)
|
99
|
-
end
|
100
|
-
end
|
data/spec/adc/setup_spec.rb
DELETED
data/spec/board/board_spec.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe BBB::Board::Base do
|
4
|
-
let(:bbb) {BBB::Board::TestBoard.new}
|
5
|
-
|
6
|
-
it "initializes with a converter" do
|
7
|
-
BBB::Board::Base.should_not_receive(:pin_converter)
|
8
|
-
converter = "SomeConverter"
|
9
|
-
board = BBB::Board::Base.new(converter)
|
10
|
-
board.pin_converter.should eql(converter)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "initializes with a default configuration" do
|
14
|
-
BBB::Board::Base.should_receive(:pin_converter) { 'Default Config' }
|
15
|
-
board = BBB::Board::Base.new
|
16
|
-
end
|
17
|
-
|
18
|
-
context "setting pins" do
|
19
|
-
Pin = BBB::IO::DigitalPin
|
20
|
-
|
21
|
-
it "defines input function for input pin" do
|
22
|
-
pin = Pin.new(:input, nil, :P8_3)
|
23
|
-
bbb.setup_pin(pin)
|
24
|
-
bbb.respond_to?("p8_3").should be_true
|
25
|
-
end
|
26
|
-
|
27
|
-
it "defines output function for input pin" do
|
28
|
-
pin = Pin.new(:output, nil, :P8_3)
|
29
|
-
bbb.setup_pin(pin)
|
30
|
-
bbb.respond_to?("p8_3=").should be_true
|
31
|
-
end
|
32
|
-
|
33
|
-
it "enables output function for output pin" do
|
34
|
-
pin = Pin.new(:output, nil, :P8_3)
|
35
|
-
bbb.setup_pin(pin)
|
36
|
-
bbb.p8_3=:low
|
37
|
-
pin.status.should eql(:low)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should keep the reference to the pin" do
|
41
|
-
pin = Pin.new(:output, nil, :P8_4)
|
42
|
-
bbb.connect_io_pin(pin)
|
43
|
-
pin.write :high
|
44
|
-
bbb.p8_4.should eql(:high)
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
data/spec/gpio/base_spec.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe BBB::GPIO::Base do
|
4
|
-
let(:gpio_81) { BBB::GPIO::Base.new(:p8_3, mock: true) }
|
5
|
-
let(:gpio_base) { BBB::GPIO::Base.new(:p8_3, mock: true) }
|
6
|
-
|
7
|
-
context "Statics values" do
|
8
|
-
[:gpio_path, :gpio_direction_input, :gpio_direction_output,
|
9
|
-
:export_path, :unexport_path, :gpio_pin_dir].each do |method|
|
10
|
-
|
11
|
-
it "#{method.to_s}" do
|
12
|
-
gpio_base.public_send(method).should_not be_nil
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should be able to mock using StringIO" do
|
19
|
-
BBB::GPIO::Base.new(:p8_3, mock: true).file_class.should eql(StringIO)
|
20
|
-
end
|
21
|
-
|
22
|
-
# Temporarily disabled, because of inability to mock the call to the
|
23
|
-
# gpio files
|
24
|
-
xit "should default to File class" do
|
25
|
-
BBB::GPIO::Base.new(:p8_3).file_class.should eql(File)
|
26
|
-
end
|
27
|
-
|
28
|
-
# Temporarily disabled, because of inability to mock the call to the
|
29
|
-
# gpio files
|
30
|
-
xit "should be able to explicitly set non mock File class" do
|
31
|
-
BBB::GPIO::Base.new(:p8_3, mock: false).file_class.should eql(File)
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should have a file_class" do
|
35
|
-
gpio_base.respond_to?(:file_class).should be_true
|
36
|
-
end
|
37
|
-
|
38
|
-
context "Filesystem functions" do
|
39
|
-
it "should export" do
|
40
|
-
gpio_81.export.should eql(2)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should unexport" do
|
44
|
-
gpio_81.unexport.should eql(2)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
@@ -1,100 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe BBB::GPIO::DigitalPin do
|
4
|
-
let(:digital_pin) { BBB::GPIO::DigitalPin }
|
5
|
-
let(:input_pin) { BBB::GPIO::DigitalPin.new(:p8_9, :input, mock: true) }
|
6
|
-
let(:output_pin) { BBB::GPIO::DigitalPin.new(:p8_3, :output, mock: true) }
|
7
|
-
|
8
|
-
it "should initialize with a pin number and mode" do
|
9
|
-
lambda{ input_pin }.should_not raise_exception
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should set the pin number" do
|
13
|
-
input_pin.position.should eql(:p8_9)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should set the mode" do
|
17
|
-
input_pin.mode.should eql(:input)
|
18
|
-
end
|
19
|
-
|
20
|
-
it "initialize with unknown mode pin raises exception" do
|
21
|
-
lambda{ digital_pin.new(:p8_3, "unknown", mock: true) }.should\
|
22
|
-
raise_exception(BBB::UnknownPinModeException)
|
23
|
-
end
|
24
|
-
|
25
|
-
context "direction" do
|
26
|
-
it "input pin" do
|
27
|
-
pin = input_pin
|
28
|
-
pin.direction.should eql(pin.gpio_direction_input)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "output pin" do
|
32
|
-
pin = output_pin
|
33
|
-
pin.direction.should eql(pin.gpio_direction_output)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context "#set mode" do
|
38
|
-
##
|
39
|
-
# Temporarily disabled because of inability to catch results of
|
40
|
-
# StringIO.open. Possible solution is factoring out the direction_file
|
41
|
-
# argument and then use a real file system call to a different file to test
|
42
|
-
# this.
|
43
|
-
xit "should set input mode" do
|
44
|
-
pin = input_pin
|
45
|
-
pin.set_mode.string.should eql(pin.direction.size)
|
46
|
-
end
|
47
|
-
|
48
|
-
##
|
49
|
-
# Temporarily disabled because of inability to catch results of
|
50
|
-
# StringIO.open. Possible solution is factoring out the direction_file
|
51
|
-
# argument and then use a real file system call to a different file to test
|
52
|
-
# this.
|
53
|
-
xit "should set output mode" do
|
54
|
-
pin = output_pin
|
55
|
-
pin.set_mode.string.should eql(pin.direction.size)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
it "#io input" do
|
60
|
-
pin = input_pin
|
61
|
-
StringIO.should_receive(:open).with(anything, "r")
|
62
|
-
pin.io
|
63
|
-
end
|
64
|
-
|
65
|
-
it "#io output" do
|
66
|
-
pin = output_pin
|
67
|
-
StringIO.should_receive(:open).with(anything, "w+")
|
68
|
-
pin.io
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should not call the io file twice" do
|
72
|
-
pin = output_pin
|
73
|
-
StringIO.should_receive(:open).with(anything, "w+").once.and_return("foo")
|
74
|
-
pin.io; pin.io
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should write :low as 0 to io" do
|
78
|
-
pin = output_pin
|
79
|
-
pin.io.should_receive(:write).with(0)
|
80
|
-
pin.write(:low)
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should write :high as 1 to io" do
|
84
|
-
pin = output_pin
|
85
|
-
pin.io.should_receive(:write).with(1)
|
86
|
-
pin.write(:high)
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should read 1 as :high from io" do
|
90
|
-
pin = output_pin
|
91
|
-
pin.io.should_receive(:read).and_return(1)
|
92
|
-
pin.read.should eql(:high)
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should read 0 as :low from io" do
|
96
|
-
pin = output_pin
|
97
|
-
pin.io.should_receive(:read).and_return(0)
|
98
|
-
pin.read.should eql(:low)
|
99
|
-
end
|
100
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe BBB::GPIO::PinConverter do
|
4
|
-
let(:converter) { BBB::GPIO::PinConverter.new }
|
5
|
-
|
6
|
-
it "converts DigitalPin input to DigitalPin" do
|
7
|
-
pin = BBB::IO::DigitalPin.new(:input, nil, :P8_3)
|
8
|
-
result = converter.convert(pin, mock: true)
|
9
|
-
result.kind_of?(BBB::GPIO::DigitalPin).should be_true
|
10
|
-
result.mode.should eql(:input)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "converts DigitalPin output to DigitalPin" do
|
14
|
-
pin = BBB::IO::DigitalPin.new(:output, nil, :P8_3)
|
15
|
-
result = converter.convert(pin, mock: true)
|
16
|
-
result.kind_of?(BBB::GPIO::DigitalPin).should be_true
|
17
|
-
result.mode.should eql(:output)
|
18
|
-
end
|
19
|
-
end
|