ya_gpio 0.1.0

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.
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+
3
+ # test permission?
4
+
5
+
6
+ describe YaGPIO do
7
+ include FakeFS::SpecHelpers
8
+
9
+ let(:pin) { 12 }
10
+
11
+ before do
12
+ FileUtils.makedirs("/sys/class/gpio/gpio#{pin}")
13
+ FileUtils.touch("/sys/class/gpio/gpio#{pin}/value")
14
+ end
15
+
16
+ it 'open a GPIO as output' do
17
+ gpio = YaGPIO.new(pin, YaGPIO::OUTPUT)
18
+
19
+ expect(File.read('/sys/class/gpio/export')).to eq pin.to_s
20
+ expect(File.read("/sys/class/gpio/gpio#{pin}/direction")).to eq 'out'
21
+ end
22
+
23
+ it 'open a GPIO as output with default HIGH value' do
24
+ gpio = YaGPIO.new(pin, YaGPIO::OUTPUT_HIGH)
25
+
26
+ expect(File.read('/sys/class/gpio/export')).to eq pin.to_s
27
+ expect(File.read("/sys/class/gpio/gpio#{pin}/direction")).to eq 'high'
28
+ end
29
+
30
+ it 'open a GPIO as input' do
31
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
32
+
33
+ expect(File.read('/sys/class/gpio/export')).to eq pin.to_s
34
+ expect(File.read("/sys/class/gpio/gpio#{pin}/direction")).to eq 'in'
35
+ end
36
+
37
+ it 'close the GPIO' do
38
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
39
+ gpio.unexport
40
+
41
+ expect(File.read('/sys/class/gpio/unexport')).to eq pin.to_s
42
+ end
43
+
44
+ it 'close the GPIO #2' do
45
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
46
+ gpio.close
47
+
48
+ expect(File.read('/sys/class/gpio/unexport')).to eq pin.to_s
49
+ end
50
+
51
+ it 'close the GPIO when destroyed' do
52
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
53
+ gpio = nil
54
+
55
+ GC.start
56
+ expect(File.read('/sys/class/gpio/unexport')).to eq pin.to_s
57
+ end
58
+
59
+ it 'raise when instantiated with wrong direction' do
60
+ expect{YaGPIO.new(pin, 'wrong-direction')}.to raise_error(RuntimeError)
61
+ end
62
+
63
+ it 'set active_low' do
64
+ gpio = YaGPIO.new(pin, YaGPIO::OUTPUT)
65
+
66
+ gpio.active_low = true
67
+ expect(File.read("/sys/class/gpio/gpio#{pin}/active_low")).to eq '1'
68
+
69
+ gpio.active_low = false
70
+ expect(File.read("/sys/class/gpio/gpio#{pin}/active_low")).to eq '0'
71
+ end
72
+
73
+ it 'get active_low' do
74
+ gpio = YaGPIO.new(pin, YaGPIO::OUTPUT)
75
+
76
+ File.write("/sys/class/gpio/gpio#{pin}/active_low", '1')
77
+ expect(gpio.active_low?).to be true
78
+
79
+ File.write("/sys/class/gpio/gpio#{pin}/active_low", '0')
80
+ expect(gpio.active_low?).to be false
81
+ end
82
+
83
+
84
+ it 'reads high value' do
85
+ File.write("/sys/class/gpio/gpio#{pin}/value", '1')
86
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
87
+
88
+ expect(gpio.high?).to be true
89
+ expect(gpio.low?).to be false
90
+ end
91
+
92
+ it 'reads low value' do
93
+ File.write("/sys/class/gpio/gpio#{pin}/value", '0')
94
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
95
+
96
+ expect(gpio.high?).to be false
97
+ expect(gpio.low?).to be true
98
+ end
99
+
100
+ it 'set high value' do
101
+ gpio = YaGPIO.new(pin, YaGPIO::OUTPUT)
102
+ gpio.high
103
+
104
+ expect(File.read("/sys/class/gpio/gpio#{pin}/value")).to eq '1'
105
+ end
106
+
107
+ it 'set low value' do
108
+ gpio = YaGPIO.new(pin, YaGPIO::OUTPUT)
109
+ gpio.low
110
+
111
+ expect(File.read("/sys/class/gpio/gpio#{pin}/value")).to eq '0'
112
+ end
113
+
114
+ it 'setup interrupt with edge' do
115
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
116
+
117
+ gpio.set_interrupt(YaGPIO::EDGE_RISING)
118
+ expect(File.read("/sys/class/gpio/gpio#{pin}/edge")).to eq 'rising'
119
+
120
+ gpio.set_interrupt(YaGPIO::EDGE_FALLING)
121
+ expect(File.read("/sys/class/gpio/gpio#{pin}/edge")).to eq 'falling'
122
+
123
+ gpio.set_interrupt(YaGPIO::EDGE_BOTH)
124
+ expect(File.read("/sys/class/gpio/gpio#{pin}/edge")).to eq 'both'
125
+ end
126
+
127
+ it 'raise when interrupt set with wrong edge' do
128
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
129
+
130
+ expect{gpio.set_interrupt('wrong-edge')}.to raise_error(RuntimeError)
131
+ end
132
+
133
+ it 'clear interrupt' do
134
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
135
+ gpio.clear_interrupt
136
+
137
+ expect(File.read("/sys/class/gpio/gpio#{pin}/edge")).to eq 'none'
138
+ end
139
+
140
+ it 'wait for an interrupt and run callback once' do
141
+ callback_count = 0
142
+ File.write("/sys/class/gpio/gpio#{pin}/value", '1')
143
+
144
+ gpio = YaGPIO.new(pin, YaGPIO::INPUT)
145
+ gpio.set_interrupt(YaGPIO::EDGE_BOTH) do |active|
146
+ callback_count += 1
147
+ expect(active).to be true
148
+
149
+ YaGPIO::resume # Prevent infinite loop during testing
150
+ end
151
+
152
+ allow(IO).to receive(:select).with(nil, nil, [gpio.file]).and_return([nil, nil, [gpio.file]])
153
+ YaGPIO::wait([gpio])
154
+ expect(callback_count).to eq 1
155
+ end
156
+
157
+ end
158
+
159
+ # vim: ts=4:sw=4:ai
data/ya_gpio.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'ya_gpio/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'ya_gpio'
8
+ s.version = YaGPIO::VERSION
9
+ s.date = Time.now.utc.strftime("%Y-%m-%d")
10
+ s.summary = "GPIO module for Raspberry Pi"
11
+ s.description = "YaGPIO is yet another GPIO ruby gem for Raspberry Pi"
12
+ s.authors = ["Nicolas AGIUS"]
13
+ s.email = 'nicolas.agius@lps-it.fr'
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = s.files.grep(%r{^(test|spec)/})
16
+ s.homepage = 'https://github.com/nagius/ya_gpio'
17
+ s.metadata = { "source_code_uri" => "https://github.com/nagius/ya_gpio" }
18
+ s.license = 'GPL-3.0'
19
+ s.add_development_dependency 'rspec', '~> 3.8'
20
+ s.add_development_dependency 'fakefs', '~> 0.20'
21
+ s.add_development_dependency 'yard', '~> 0.9'
22
+ end
23
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ya_gpio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas AGIUS
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fakefs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.20'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.20'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ description: YaGPIO is yet another GPIO ruby gem for Raspberry Pi
56
+ email: nicolas.agius@lps-it.fr
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - ".rspec"
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - lib/ya_gpio.rb
68
+ - lib/ya_gpio/version.rb
69
+ - spec/spec_helper.rb
70
+ - spec/ya_gpio_spec.rb
71
+ - ya_gpio.gemspec
72
+ homepage: https://github.com/nagius/ya_gpio
73
+ licenses:
74
+ - GPL-3.0
75
+ metadata:
76
+ source_code_uri: https://github.com/nagius/ya_gpio
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.7.6.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: GPIO module for Raspberry Pi
97
+ test_files:
98
+ - spec/spec_helper.rb
99
+ - spec/ya_gpio_spec.rb