gpio 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/changelog +12 -1
- data/lib/gpio/device.rb +43 -3
- data/lib/gpio/raspberry_pi.rb +16 -28
- metadata +29 -45
- data/examples/motion_example.rb +0 -20
data/changelog
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
version 0.0.4
|
2
|
+
- major changes to reading/writing from/to gpio including checking permissions with hands free fallback to older sudo method
|
3
|
+
- path_write method checks permissions and writes using IO.write for performance and falls back to shelling out to sudo echo
|
4
|
+
- using IO.read and Dir.methods where possible for performance gains, added self.pin_prefix
|
5
|
+
- raspberry_pi.rb => device.rb: moved methods that are now generic enough to be in device.
|
6
|
+
- extracted paths to separate methods to allow for generic gpio methods to be moved to device and allow raspberr_pi.rb and other modules that extend device to be configuration.
|
7
|
+
- removed sudo from reads, only needed for writes
|
8
|
+
- print pin in error message raised
|
9
|
+
- benchmarks added to see timings of different methods of reading/writing
|
10
|
+
- found new methods of reading file that may be viable for microsecond intervals
|
11
|
+
|
1
12
|
version 0.0.3
|
2
13
|
- updated readme with example
|
3
14
|
- example usage code added
|
@@ -12,4 +23,4 @@ version 0.0.2
|
|
12
23
|
- lib files now load properly
|
13
24
|
|
14
25
|
version 0.0.1: initial public release
|
15
|
-
- proof of concept / prototype
|
26
|
+
- proof of concept / prototype
|
data/lib/gpio/device.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module GPIO
|
2
2
|
module Device
|
3
3
|
def software_pin(pin)
|
4
|
-
raise "That software pin doesn't exist." unless software_pins.include? pin
|
4
|
+
raise "That software pin #{pin} doesn't exist." unless software_pins.include? pin
|
5
5
|
mapping == :software ? pin : software_pins[hardware_pins.index(pin)]
|
6
6
|
end
|
7
7
|
def hardware_pin(pin)
|
8
|
-
raise "That hardware pin doesn't exist." unless hardware_pins.include? pin
|
8
|
+
raise "That hardware pin #{pin} doesn't exist." unless hardware_pins.include? pin
|
9
9
|
mapping == :hardware ? pin : hardware_pins[software_pins.index(pin)]
|
10
10
|
end
|
11
11
|
def load_pins
|
@@ -14,5 +14,45 @@ module GPIO
|
|
14
14
|
def unexport_all!
|
15
15
|
load_pins.map(&unexport!)
|
16
16
|
end
|
17
|
+
|
18
|
+
def get_pins(mapping)
|
19
|
+
pins = Dir.entries base_path.select!{|pin| pin[/(?:#{pin_prefix})(\d+)/]}.to_i
|
20
|
+
pins.map!{|pin| Pin.new(pin,nil,self).pin}
|
21
|
+
end
|
22
|
+
def get_direction(software_pin)
|
23
|
+
IO.read(direction_path(software_pin)).chomp!
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize_pin(software_pin, direction)
|
27
|
+
unexport!(software_pin) if exported?(software_pin) && direction != get_direction(software_pin)
|
28
|
+
export!(software_pin, direction) unless exported?(software_pin)
|
29
|
+
end
|
30
|
+
|
31
|
+
def exported?(software_pin)
|
32
|
+
Dir.exists? pin_path(software_pin)
|
33
|
+
end
|
34
|
+
def export!(software_pin,direction)
|
35
|
+
path_write export_path, software_pin
|
36
|
+
path_write direction_path(software_pin), direction
|
37
|
+
exported?(software_pin)
|
38
|
+
end
|
39
|
+
def unexport!(software_pin)
|
40
|
+
path_write unexport_path, software_pin
|
41
|
+
!exported? software_pin
|
42
|
+
end
|
43
|
+
|
44
|
+
def read(software_pin)
|
45
|
+
IO.read(value_path(software_pin), 1) == '1'
|
46
|
+
end
|
47
|
+
def write(software_pin,value)
|
48
|
+
raise "This pin is an input." if get_direction(software_pin) == 'in'
|
49
|
+
path_write value_path(software_pin), value
|
50
|
+
end
|
51
|
+
def path_write(path, value)
|
52
|
+
File::Stat.writable?(path) ? IO.write(path, value) : path_write_sudo(path, value)
|
53
|
+
end
|
54
|
+
def path_write_sudo(path, value)
|
55
|
+
`sudo bash -c "echo #{value} > #{path}"`.chomp!
|
56
|
+
end
|
17
57
|
end
|
18
|
-
end
|
58
|
+
end
|
data/lib/gpio/raspberry_pi.rb
CHANGED
@@ -11,39 +11,27 @@ module GPIO
|
|
11
11
|
def self.software_pins
|
12
12
|
[0,1,4,14,15,17,18,21,22,23,24,10,9,25,11,8,7]
|
13
13
|
end
|
14
|
-
|
15
|
-
def self.
|
16
|
-
|
17
|
-
pins.map!{|pin| Pin.new(pin,nil,self).pin}
|
14
|
+
|
15
|
+
def self.base_path
|
16
|
+
'/sys/class/gpio/'
|
18
17
|
end
|
19
|
-
def self.
|
20
|
-
|
18
|
+
def self.pin_path(n)
|
19
|
+
"#{base_path}#{pin_prefix}#{n}/"
|
21
20
|
end
|
22
|
-
|
23
|
-
|
24
|
-
unexport!(software_pin) if exported?(software_pin) && direction != get_direction(software_pin)
|
25
|
-
export!(software_pin, direction) unless exported?(software_pin)
|
21
|
+
def self.direction_path(n)
|
22
|
+
"#{pin_path(n)}direction"
|
26
23
|
end
|
27
|
-
|
28
|
-
|
29
|
-
`sudo [ -d /sys/class/gpio/gpio#{software_pin} ] && echo true || false`.chomp == 'true'
|
24
|
+
def self.value_path(n)
|
25
|
+
"#{pin_path(n)}value"
|
30
26
|
end
|
31
|
-
def self.
|
32
|
-
|
33
|
-
`sudo bash -c "echo #{direction} > /sys/class/gpio/gpio#{software_pin}/direction"`
|
34
|
-
exported?(software_pin)
|
27
|
+
def self.export_path
|
28
|
+
"#{base_path}export"
|
35
29
|
end
|
36
|
-
def self.
|
37
|
-
|
38
|
-
!exported?(software_pin)
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.read(software_pin)
|
42
|
-
`sudo cat /sys/class/gpio/gpio#{software_pin}/value`.chomp == '1'
|
30
|
+
def self.unexport_path
|
31
|
+
"#{base_path}unexport"
|
43
32
|
end
|
44
|
-
def self.
|
45
|
-
|
46
|
-
`sudo bash -c "echo #{value} > /sys/class/gpio/gpio#{software_pin}/value && echo true || false"`.chomp == 'true'
|
33
|
+
def self.pin_prefix
|
34
|
+
"gpio"
|
47
35
|
end
|
48
36
|
end
|
49
|
-
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,70 +1,54 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: gpio
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 3
|
9
|
-
version: 0.0.3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Christopher Klapp
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2012-06-05 00:00:00 -04:00
|
18
|
-
default_executable:
|
12
|
+
date: 2012-06-05 00:00:00.000000000 Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
|
-
|
14
|
+
description: gpio allows for devices such as raspberry pi or systems with 1wire usb
|
15
|
+
adapters to speak to the system's input/output pins.
|
22
16
|
email: christopher@klapp.name
|
23
17
|
executables: []
|
24
|
-
|
25
18
|
extensions: []
|
26
|
-
|
27
19
|
extra_rdoc_files: []
|
28
|
-
|
29
|
-
|
30
|
-
- lib/gpio/device.rb
|
31
|
-
- lib/gpio/motion_detector.rb
|
32
|
-
- lib/gpio/pin.rb
|
20
|
+
files:
|
21
|
+
- lib/gpio.rb
|
33
22
|
- lib/gpio/raspberry_pi.rb
|
34
23
|
- lib/gpio/sensor.rb
|
35
|
-
- lib/gpio.rb
|
36
|
-
-
|
24
|
+
- lib/gpio/pin.rb
|
25
|
+
- lib/gpio/motion_detector.rb
|
26
|
+
- lib/gpio/device.rb
|
37
27
|
- README.md
|
38
28
|
- changelog
|
39
|
-
has_rdoc: true
|
40
29
|
homepage: http://klappy.github.com/gpio
|
41
30
|
licenses: []
|
42
|
-
|
43
31
|
post_install_message:
|
44
32
|
rdoc_options: []
|
45
|
-
|
46
|
-
require_paths:
|
33
|
+
require_paths:
|
47
34
|
- lib
|
48
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
requirements:
|
57
|
-
- -
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
|
60
|
-
- 0
|
61
|
-
version: "0"
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
62
47
|
requirements: []
|
63
|
-
|
64
48
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.
|
49
|
+
rubygems_version: 1.8.24
|
66
50
|
signing_key:
|
67
51
|
specification_version: 3
|
68
|
-
summary: a ruby gem and repository to contribute gpio code for devices such as Raspberry
|
52
|
+
summary: a ruby gem and repository to contribute gpio code for devices such as Raspberry
|
53
|
+
Pi to read sensors and control outputs.
|
69
54
|
test_files: []
|
70
|
-
|
data/examples/motion_example.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# Let's setup a motion sensor on GPIO pin 17 on a RaspberryPi (default).
|
2
|
-
motion = GPIO::MotionDetector.new(pin: 17)
|
3
|
-
|
4
|
-
# Do we detect and motion yet?
|
5
|
-
motion.detect
|
6
|
-
|
7
|
-
# Let's print a message corresponding to the motion.detect?
|
8
|
-
puts motion.detect ? "Motion detected!" : "No motion detected."
|
9
|
-
|
10
|
-
# Was the value any different than the last time it detected?
|
11
|
-
motion.changed?
|
12
|
-
puts "last: #{motion.last_reading}, current: #{motion.reading}"
|
13
|
-
|
14
|
-
# Wait until the value changes before we move on.
|
15
|
-
until motion.changed? do
|
16
|
-
motion.detect
|
17
|
-
sleep 0.5
|
18
|
-
end
|
19
|
-
puts "Something changed!"
|
20
|
-
puts "last: #{motion.last_reading}, current: #{motion.reading}"
|