pi_piper 2.0.beta.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +10 -0
  5. data/Gemfile +3 -3
  6. data/Gemfile.lock +39 -26
  7. data/LICENCE.md +23 -0
  8. data/README.md +53 -16
  9. data/Rakefile +4 -22
  10. data/examples/2_bit_counter/2_bit_counter.rb +21 -0
  11. data/examples/7-segment/7-segment.rb +37 -0
  12. data/examples/dsl_switch/dsl_switch.rb +15 -0
  13. data/examples/elro/README.md +75 -0
  14. data/examples/elro/docs/elro-dips.jpg +0 -0
  15. data/examples/elro/docs/elro-switch.jpg +0 -0
  16. data/examples/elro/docs/setup.jpg +0 -0
  17. data/examples/elro/docs/wireplan.jpg +0 -0
  18. data/examples/elro/docs/wrl10534.jpg +0 -0
  19. data/examples/elro/elro.rb +15 -0
  20. data/examples/elro/lib/elro_switch.rb +51 -0
  21. data/examples/elro/lib/elro_util.rb +64 -0
  22. data/examples/elro/spec/elro_spec.rb +35 -0
  23. data/examples/elro/spec/elro_util_spec.rb +51 -0
  24. data/examples/elro/spec/spec_helper.rb +6 -0
  25. data/examples/mcp3008/circuit.png +0 -0
  26. data/examples/mcp3008/mcp3008.rb +55 -0
  27. data/examples/mcp3008_spi/mcp3008_spi.rb +24 -0
  28. data/examples/morse_code/circuit.png +0 -0
  29. data/examples/morse_code/morse_code.rb +49 -0
  30. data/examples/simple_switch/circuit.png +0 -0
  31. data/examples/simple_switch/simple_switch.rb +10 -0
  32. data/lib/pi_piper.rb +5 -3
  33. data/lib/pi_piper/bcm2835.rb +84 -14
  34. data/lib/pi_piper/i2c.rb +0 -1
  35. data/lib/pi_piper/libbcm2835.so +0 -0
  36. data/lib/pi_piper/pin.rb +102 -63
  37. data/lib/pi_piper/pin_error.rb +3 -0
  38. data/lib/pi_piper/pin_values.rb +35 -0
  39. data/lib/pi_piper/platform.rb +5 -5
  40. data/lib/pi_piper/pwm.rb +95 -0
  41. data/lib/pi_piper/spi.rb +30 -45
  42. data/lib/pi_piper/stub_driver.rb +124 -0
  43. data/lib/pi_piper/version.rb +3 -0
  44. data/pi_piper.gemspec +24 -29
  45. data/spec/bcm2835_spec.rb +132 -0
  46. data/spec/i2c_spec.rb +62 -0
  47. data/spec/pin_spec.rb +140 -0
  48. data/spec/pwm_spec.rb +83 -0
  49. data/spec/spec_helper.rb +7 -0
  50. data/spec/spi_spec.rb +38 -0
  51. data/spec/stub_driver_spec.rb +140 -0
  52. metadata +100 -26
  53. data/Manifest +0 -14
  54. data/lib/pi_piper/libbcm2835.img +0 -0
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+ include PiPiper
3
+
4
+ describe 'Pwm' do
5
+ before(:example) do |example|
6
+ Platform.driver = StubDriver.new
7
+ end
8
+
9
+ describe '#new' do
10
+ it 'should get the options right' do
11
+ default_pwm = PiPiper::Pwm.new(pin: 18)
12
+ expect(default_pwm.options).to eq({ pin: 18, mode: :balanced, clock: 19200000.0, range: 1024 })
13
+ expect(default_pwm.value).to eq(0)
14
+
15
+ custom_pwm = PiPiper::Pwm.new(pin: 18, mode: :markspace, clock: 1.megahertz, range: 10000, value: 1)
16
+ expect(custom_pwm.options).to eq({ pin: 18, mode: :markspace, clock: 1000000.0, range: 10000 })
17
+ expect(custom_pwm.value).to eq(1)
18
+ end
19
+
20
+ it 'should reject wrong options value' do
21
+ expect { PiPiper::Pwm.new pin: 0 }.to raise_error(ArgumentError, ':pin should be one of [12, 13, 18, 19, 40, 41, 45, 52, 53] not 0')
22
+ expect { PiPiper::Pwm.new pin: 18 }.not_to raise_error
23
+ expect { PiPiper::Pwm.new pin: 18, mode: :balanced }.not_to raise_error
24
+ expect { PiPiper::Pwm.new pin: 18, mode: :markspace}.not_to raise_error
25
+ expect { PiPiper::Pwm.new pin: 18, mode: :another_mode }.to raise_error(ArgumentError, ':mode should be one of [:balanced, :markspace], not another_mode')
26
+ end
27
+
28
+ it 'should set the options' do
29
+ expect(Platform.driver).to receive(:gpio_select_function).with(18, 0b010)
30
+ expect(Platform.driver).to receive(:pwm_clock).with(9)
31
+ expect(Platform.driver).to receive(:pwm_range).with(0, 2)
32
+ expect(Platform.driver).to receive(:pwm_mode).with(0, 1, 1)
33
+
34
+ pwm = PiPiper::Pwm.new pin: 18, range: 2, mode: :markspace, clock: 2.megahertz
35
+ end
36
+
37
+ end
38
+
39
+ let!(:pwm) { PiPiper::Pwm.new pin: 18 }
40
+
41
+ describe 'start/stop & status' do
42
+ it '#on' do
43
+ expect(Platform.driver).to receive(:pwm_mode).twice.with(0, 0, 1)
44
+ pwm.on
45
+ pwm.start
46
+ end
47
+
48
+ it '#off' do
49
+ expect(Platform.driver).to receive(:pwm_mode).twice.with(0, 0, 0)
50
+ pwm.off
51
+ pwm.stop
52
+ end
53
+
54
+ it '#on?' do
55
+ pwm.on
56
+ expect(pwm.on?).to be true
57
+ pwm.off
58
+ expect(pwm.on?).to be false
59
+ end
60
+
61
+ it '#off?' do
62
+ pwm.on
63
+ expect(pwm.off?).to be false
64
+ pwm.off
65
+ expect(pwm.off?).to be true
66
+ end
67
+ end
68
+
69
+ describe '#value=' do
70
+ it 'should set value between 0 and 1' do
71
+ expect(Platform.driver).to receive(:pwm_data).with(0, (0.4*1024).to_i)
72
+ pwm.value = 0.4
73
+ expect(pwm.value).to eq 0.4
74
+ end
75
+
76
+ it 'should set caped value outside 0 and 1' do
77
+ pwm.value = 2
78
+ expect(pwm.value).to eq 1
79
+ pwm.value = -2
80
+ expect(pwm.value).to eq 0
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,7 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+ require 'pi_piper.rb'
4
+ require 'rspec'
5
+ require 'mocha/api'
6
+
7
+ include PiPiper
@@ -0,0 +1,38 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe 'Spi' do
4
+
5
+ before(:example) do |example|
6
+ Platform.driver = StubDriver.new
7
+ end
8
+
9
+ describe 'when in block' do
10
+ it 'should call spi_begin' do
11
+ expect(Platform.driver).to receive(:spi_begin)
12
+ Spi.begin {}
13
+ end
14
+
15
+ it 'should call spi_chip_select to set and unset chip' do
16
+ expect(Platform.driver).to receive(:spi_chip_select).with(Spi::CHIP_SELECT_1)
17
+ expect(Platform.driver).to receive(:spi_chip_select).with(Spi::CHIP_SELECT_NONE)
18
+
19
+ Spi.begin(Spi::CHIP_SELECT_1) do
20
+ read
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'set mode' do
26
+ it 'should call spi_set_data_mode' do
27
+ expect(Platform.driver).to receive(:spi_set_data_mode).with(Spi::SPI_MODE3)
28
+ Spi.set_mode(1, 1)
29
+ end
30
+ end
31
+
32
+ describe '#spidev_out' do
33
+ it 'should attempt to write data to spi' do
34
+ expect(Platform.driver).to receive(:spidev_out).with([0, 1, 2, 3, 4, 5])
35
+ Spi.spidev_out([0, 1, 2, 3, 4, 5])
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+
3
+ describe StubDriver do
4
+ let(:stub_driver) { StubDriver.new }
5
+
6
+ before do
7
+ @logger = double
8
+ @driver = StubDriver.new(logger: @logger)
9
+ end
10
+
11
+ describe '#pin_input' do
12
+ it 'should set pin as input' do
13
+ stub_driver.pin_input(10)
14
+ expect(stub_driver.pin_direction(10)).to eq(:in)
15
+ end
16
+
17
+ it 'should log that pin is set' do
18
+ expect(@logger).to receive(:debug).with('Pin #10 -> Input')
19
+ @driver.pin_input(10)
20
+ end
21
+ end
22
+
23
+ describe '#pin_output' do
24
+ it 'should set pin as output' do
25
+ stub_driver.pin_output(10)
26
+ expect(stub_driver.pin_direction(10)).to eq(:out)
27
+ end
28
+
29
+ it 'should log that pin is set' do
30
+ expect(@logger).to receive(:debug).with('Pin #10 -> Output')
31
+ @driver.pin_output(10)
32
+ end
33
+ end
34
+
35
+ describe '#pin_set' do
36
+ it 'should set pin value' do
37
+ stub_driver.pin_set(22, 42)
38
+ expect(stub_driver.pin_read(22)).to eq(42)
39
+ end
40
+
41
+ it 'should log the new pin value' do
42
+ expect(@logger).to receive(:debug).with('Pin #21 -> 22')
43
+ @driver.pin_set(21, 22)
44
+ end
45
+ end
46
+
47
+ describe '#pin_set_pud' do
48
+ it 'should set pin value' do
49
+ stub_driver.pin_set_pud(12, Pin::GPIO_PUD_UP)
50
+ expect(stub_driver.pin_read(12)).to eq(Pin::GPIO_HIGH)
51
+ end
52
+
53
+ it 'should not overwrite set value' do
54
+ stub_driver.pin_set(12, 0)
55
+ stub_driver.pin_set_pud(12, Pin::GPIO_PUD_DOWN)
56
+ expect(stub_driver.pin_read(12)).to eq(Pin::GPIO_LOW)
57
+ end
58
+
59
+ it 'should log the new pin value' do
60
+ expect(@logger).to receive(:debug).with('PinPUD #21 -> 22')
61
+ @driver.pin_set_pud(21, 22)
62
+ end
63
+ end
64
+
65
+ describe '#spidev_out' do
66
+ it 'should log the array sent to ada_spi_out' do
67
+ expect(@logger).to receive(:debug).with("SPIDEV -> \u0000\u0001\u0002")
68
+ @driver.spidev_out([0x00, 0x01, 0x02])
69
+ end
70
+ end
71
+
72
+ describe '#spi_begin' do
73
+ it 'should should clear spi data' do
74
+ expect(@logger).to receive(:debug)
75
+ @driver.spi_transfer_bytes([0x01, 0x02])
76
+ expect(@logger).to receive(:debug).with('SPI Begin')
77
+ @driver.spi_begin
78
+ expect(@driver.send(:spi_data)).to eq([])
79
+ end
80
+ end
81
+
82
+ describe '#spi_transfer_bytes' do
83
+ it 'should log and store sent data' do
84
+ expect(@logger).to receive(:debug).with('SPI CS0 <- [1, 2, 3]')
85
+ @driver.spi_transfer_bytes([0x01, 0x02, 0x03])
86
+ expect(@driver.send(:spi_data)).to eq([0x01, 0x02, 0x03])
87
+ end
88
+ end
89
+
90
+ describe '#spi_chip_select' do
91
+ it 'should return default 0 if nothing provided' do
92
+ expect(@logger).to receive(:debug).with('SPI Chip Select = 0')
93
+ expect(@driver.spi_chip_select).to eq(0)
94
+ end
95
+
96
+ it 'should set chip select value if passed in' do
97
+ expect(@logger).to receive(:debug).with('SPI Chip Select = 3').twice
98
+ @driver.spi_chip_select(3)
99
+ expect(@driver.spi_chip_select).to eq(3)
100
+ end
101
+ end
102
+
103
+ describe '#reset' do
104
+ it 'should not reset unless asked' do
105
+ StubDriver.new()
106
+ StubDriver.pin_set(1,3)
107
+ expect(StubDriver.pin_read(1)).to eq(3)
108
+ StubDriver.reset
109
+ expect(StubDriver.pin_read(1)).to be_nil
110
+ end
111
+ end
112
+
113
+ describe '#release_pins' do
114
+ before(:each) do
115
+ StubDriver.new
116
+ StubDriver.pin_input(4)
117
+ StubDriver.pin_output(6)
118
+ end
119
+
120
+ it 'should keep track of open pins and release them' do
121
+ expect(@driver).to receive(:release_pin).with(4)
122
+ expect(@driver).to receive(:release_pin).with(6)
123
+
124
+ StubDriver.release_pins
125
+ end
126
+
127
+ it 'should remove released pins' do
128
+ StubDriver.release_pins
129
+
130
+ expect(StubDriver.pin_direction(4)).to be_nil
131
+ expect(StubDriver.pin_direction(6)).to be_nil
132
+ end
133
+
134
+ it 'should keep track of released pins' do
135
+ StubDriver.release_pins
136
+ expect(@driver).not_to receive(:release_pin)
137
+ StubDriver.release_pins
138
+ end
139
+ end
140
+ end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pi_piper
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.beta.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Whitehorn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-14 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
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
@@ -30,70 +30,144 @@ dependencies:
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 1.0.3
33
+ version: 1.0.9
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
- version: 1.0.3
40
+ version: 1.0.9
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  description: Event driven Raspberry Pi GPIO library
42
84
  email: jason.whitehorn@gmail.com
43
85
  executables: []
44
86
  extensions: []
45
87
  extra_rdoc_files:
46
88
  - README.md
47
- - lib/pi_piper.rb
48
- - lib/pi_piper/bcm2835.rb
49
- - lib/pi_piper/frequency.rb
50
- - lib/pi_piper/i2c.rb
51
- - lib/pi_piper/libbcm2835.img
52
- - lib/pi_piper/pin.rb
53
- - lib/pi_piper/platform.rb
54
- - lib/pi_piper/spi.rb
89
+ - lib/pi_piper/libbcm2835.so
55
90
  files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
56
94
  - Gemfile
57
95
  - Gemfile.lock
58
- - Manifest
96
+ - LICENCE.md
59
97
  - README.md
60
98
  - Rakefile
99
+ - examples/2_bit_counter/2_bit_counter.rb
100
+ - examples/7-segment/7-segment.rb
101
+ - examples/dsl_switch/dsl_switch.rb
102
+ - examples/elro/README.md
103
+ - examples/elro/docs/elro-dips.jpg
104
+ - examples/elro/docs/elro-switch.jpg
105
+ - examples/elro/docs/setup.jpg
106
+ - examples/elro/docs/wireplan.jpg
107
+ - examples/elro/docs/wrl10534.jpg
108
+ - examples/elro/elro.rb
109
+ - examples/elro/lib/elro_switch.rb
110
+ - examples/elro/lib/elro_util.rb
111
+ - examples/elro/spec/elro_spec.rb
112
+ - examples/elro/spec/elro_util_spec.rb
113
+ - examples/elro/spec/spec_helper.rb
114
+ - examples/mcp3008/circuit.png
115
+ - examples/mcp3008/mcp3008.rb
116
+ - examples/mcp3008_spi/mcp3008_spi.rb
117
+ - examples/morse_code/circuit.png
118
+ - examples/morse_code/morse_code.rb
119
+ - examples/simple_switch/circuit.png
120
+ - examples/simple_switch/simple_switch.rb
61
121
  - lib/pi_piper.rb
62
122
  - lib/pi_piper/bcm2835.rb
63
123
  - lib/pi_piper/frequency.rb
64
124
  - lib/pi_piper/i2c.rb
65
- - lib/pi_piper/libbcm2835.img
125
+ - lib/pi_piper/libbcm2835.so
66
126
  - lib/pi_piper/pin.rb
127
+ - lib/pi_piper/pin_error.rb
128
+ - lib/pi_piper/pin_values.rb
67
129
  - lib/pi_piper/platform.rb
130
+ - lib/pi_piper/pwm.rb
68
131
  - lib/pi_piper/spi.rb
132
+ - lib/pi_piper/stub_driver.rb
133
+ - lib/pi_piper/version.rb
69
134
  - pi_piper.gemspec
135
+ - spec/bcm2835_spec.rb
136
+ - spec/i2c_spec.rb
137
+ - spec/pin_spec.rb
138
+ - spec/pwm_spec.rb
139
+ - spec/spec_helper.rb
140
+ - spec/spi_spec.rb
141
+ - spec/stub_driver_spec.rb
70
142
  homepage: http://github.com/jwhitehorn/pi_piper
71
- licenses: []
143
+ licenses:
144
+ - BSD
72
145
  metadata: {}
73
146
  post_install_message:
74
147
  rdoc_options:
75
- - --line-numbers
76
- - --inline-source
77
- - --title
148
+ - "--line-numbers"
149
+ - "--inline-source"
150
+ - "--title"
78
151
  - Pi_piper
79
- - --main
152
+ - "--main"
80
153
  - README.md
81
154
  require_paths:
82
155
  - lib
83
156
  required_ruby_version: !ruby/object:Gem::Requirement
84
157
  requirements:
85
- - - '>='
158
+ - - ">="
86
159
  - !ruby/object:Gem::Version
87
160
  version: '0'
88
161
  required_rubygems_version: !ruby/object:Gem::Requirement
89
162
  requirements:
90
- - - '>='
163
+ - - ">="
91
164
  - !ruby/object:Gem::Version
92
- version: '1.2'
165
+ version: 2.0.0
93
166
  requirements: []
94
- rubyforge_project: pi_piper
95
- rubygems_version: 2.0.0
167
+ rubyforge_project:
168
+ rubygems_version: 2.4.5
96
169
  signing_key:
97
170
  specification_version: 4
98
171
  summary: Event driven Raspberry Pi GPIO library
99
172
  test_files: []
173
+ has_rdoc: