gpio 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/changelog +2 -0
- data/lib/gpio/device.rb +20 -0
- data/lib/gpio/pin.rb +29 -0
- data/lib/gpio/raspberry_pi.rb +44 -0
- data/lib/gpio.rb +1 -0
- metadata +53 -0
data/README.md
ADDED
data/changelog
ADDED
data/lib/gpio/device.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module GPIO
|
2
|
+
class Device
|
3
|
+
attr_reader :mapping, :hardware_pins, :software_pins
|
4
|
+
|
5
|
+
def software_pin(pin)
|
6
|
+
raise "That software pin doesn't exist." unless software.include? pin
|
7
|
+
mapping == :software ? pin : software[hardware.index(pin)]
|
8
|
+
end
|
9
|
+
def hardware_pin(pin)
|
10
|
+
raise "That hardware pin doesn't exist." unless hardware.include? pin
|
11
|
+
mapping == :hardware ? pin : hardware[software.index(pin)]
|
12
|
+
end
|
13
|
+
def load_pins
|
14
|
+
get_pins(:hardware).map{|pin| Pin.new(pin,get_direction(pin),self)}
|
15
|
+
end
|
16
|
+
def unexport_all!
|
17
|
+
load_pins.map(&unexport!)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/gpio/pin.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module GPIO
|
2
|
+
class Pin
|
3
|
+
attr_reader :hardware_pin, :software_pin, :pin, :direction, :device
|
4
|
+
def initialize(pin, direction, device)
|
5
|
+
@device = device
|
6
|
+
|
7
|
+
@hardware_pin = device.hardware_pin(pin)
|
8
|
+
@software_pin = device.software_pin(pin)
|
9
|
+
@pin = device.mapping ? software : hardware
|
10
|
+
|
11
|
+
@direction = direction.to_s || get_direction
|
12
|
+
raise "Direction should be :in, :out." unless ['in','out'].include? @direction
|
13
|
+
|
14
|
+
device.initialize_pin(software_pin, @direction)
|
15
|
+
end
|
16
|
+
|
17
|
+
def on
|
18
|
+
device.write software_pin, 1
|
19
|
+
read
|
20
|
+
end
|
21
|
+
def off
|
22
|
+
device.write software_pin, 0
|
23
|
+
read
|
24
|
+
end
|
25
|
+
def read
|
26
|
+
device.read(pin)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module GPIO
|
2
|
+
class RaspberryPi < Device
|
3
|
+
def initialize(mapping=:hardware)
|
4
|
+
@mapping = mapping
|
5
|
+
raise "mapping must either be :hardware or :software" unless [:hardware,:software].include? mapping
|
6
|
+
@hardware_pins = [3,5,7,8,10,11,12,13,15,16,17,19,21,22,23,24,26]
|
7
|
+
@software_pins = [0,1,4,14,15,17,18,21,22,23,24,10,9,25,11,8,7]
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_pins(mapping=@mapping)
|
11
|
+
pins = `sudo ls /sys/class/gpio`.scan(/(?:gpio)(\d+)/).flatten.map!(&:to_i)
|
12
|
+
pins.map!{|pin| Pin.new(pin,nil,self).pin}
|
13
|
+
end
|
14
|
+
def get_direction(software_pin)
|
15
|
+
`sudo cat /sys/class/gpio/gpio#{software_pin}/direction`.chomp
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize_pin(software_pin, direction)
|
19
|
+
unexport!(software_pin) if exported?(software_pin) && direction != get_direction(software_pin)
|
20
|
+
export!(software_pin) unless exported?(software_pin)
|
21
|
+
end
|
22
|
+
|
23
|
+
def exported?(software_pin)
|
24
|
+
`sudo [ -d /sys/class/gpio/gpio#{software_pin} ] && echo true || false`.chomp == 'true'
|
25
|
+
end
|
26
|
+
def export!(software_pin,direction)
|
27
|
+
`sudo bash -c "echo #{software_pin} > /sys/class/gpio/export"`
|
28
|
+
`sudo bash -c "echo #{direction} > /sys/class/gpio/gpio#{software_pin}/direction"`
|
29
|
+
exported?(software_pin)
|
30
|
+
end
|
31
|
+
def unexport!(software_pin)
|
32
|
+
`sudo bash -c "echo #{software_pin} > /sys/class/gpio/unexport"`
|
33
|
+
!exported?(software_pin)
|
34
|
+
end
|
35
|
+
|
36
|
+
def read(software_pin)
|
37
|
+
`sudo cat /sys/class/gpio/gpio#{software_pin}/value`.chomp == '1'
|
38
|
+
end
|
39
|
+
def write(software_pin,value)
|
40
|
+
raise "This pin is an input." if get_direction(software_pin) == 'in'
|
41
|
+
`sudo bash -c "echo #{value} > /sys/class/gpio/gpio#{software_pin}/value && echo true || false"`.chomp == 'true'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/gpio.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
%w[device raspberry_pi pin].each{|file| require File.dirname(__FILE__)+'/gpio/'+file}
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gpio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christopher Klapp
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-04 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
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. The end goal is for people
|
16
|
+
to contribute code for specific devices, sensors and outputs.
|
17
|
+
email: christopher@klapp.name
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/gpio/device.rb
|
23
|
+
- lib/gpio/pin.rb
|
24
|
+
- lib/gpio/raspberry_pi.rb
|
25
|
+
- lib/gpio.rb
|
26
|
+
- README.md
|
27
|
+
- changelog
|
28
|
+
homepage: http://klappy.github.com/gpio
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.24
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: gpio allows for devices such as raspberry pi or systems with 1wire usb adapters
|
52
|
+
to speak to the system's input/output pins.
|
53
|
+
test_files: []
|