gpio 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  gpio
2
2
  ====
3
3
 
4
- gpio allows for devices such as raspberry pi or systems with 1wire usb adapters to speak to the system's input/output pins. The end goal is for people to contribute code for specific devices, sensors and outputs.
4
+ gpio ruby gem allows for ruby code running on devices such as raspberry pi or systems with 1wire usb adapters to speak to the system's input/output pins.
5
+
6
+ the end goal is for people to contribute code for specific devices, sensors and outputs.
data/changelog CHANGED
@@ -1,2 +1,8 @@
1
+ version 0.0.2
2
+ - device/rasberrypi now modules instead of class
3
+ - pin.new(params) now pass hash of params to pin.new {pin: 1, mode: :in, device: :RaspberryPi}
4
+ - pin.new now has default if no device is passed in
5
+ - lib files now load properly
6
+
1
7
  version 0.0.1: initial public release
2
8
  - proof of concept / prototype
@@ -1,19 +1,17 @@
1
1
  module GPIO
2
- class Device
3
- attr_reader :mapping, :hardware_pins, :software_pins
4
-
5
- def software_pin(pin)
2
+ module Device
3
+ def self.software_pin(pin)
6
4
  raise "That software pin doesn't exist." unless software.include? pin
7
5
  mapping == :software ? pin : software[hardware.index(pin)]
8
6
  end
9
- def hardware_pin(pin)
7
+ def self.hardware_pin(pin)
10
8
  raise "That hardware pin doesn't exist." unless hardware.include? pin
11
9
  mapping == :hardware ? pin : hardware[software.index(pin)]
12
10
  end
13
- def load_pins
11
+ def self.load_pins
14
12
  get_pins(:hardware).map{|pin| Pin.new(pin,get_direction(pin),self)}
15
13
  end
16
- def unexport_all!
14
+ def self.unexport_all!
17
15
  load_pins.map(&unexport!)
18
16
  end
19
17
  end
@@ -1,17 +1,17 @@
1
1
  module GPIO
2
2
  class Pin
3
- attr_reader :hardware_pin, :software_pin, :pin, :direction, :device
4
- def initialize(pin, direction, device)
5
- @device = device
3
+ attr_reader :pin, :mode, :device, :hardware_pin, :software_pin
4
+ def initialize(params) #(pin, mode, device=:RaspberryPi)
5
+ @device = GPIO.const_get(params[:device]||:RaspberryPi)
6
6
 
7
+ @pin = params[:pin].to_int
7
8
  @hardware_pin = device.hardware_pin(pin)
8
9
  @software_pin = device.software_pin(pin)
9
- @pin = device.mapping ? software : hardware
10
10
 
11
- @direction = direction.to_s || get_direction
12
- raise "Direction should be :in, :out." unless ['in','out'].include? @direction
11
+ @mode = params[:mode].to_s || device.get_direction
12
+ raise "Mode should be :in, :out, :bi, :pwm." unless ['in','out'].include? @mode
13
13
 
14
- device.initialize_pin(software_pin, @direction)
14
+ device.initialize_pin(software_pin, @mode)
15
15
  end
16
16
 
17
17
  def on
@@ -23,7 +23,7 @@ module GPIO
23
23
  read
24
24
  end
25
25
  def read
26
- device.read(pin)
26
+ device.read(software_pin)
27
27
  end
28
28
  end
29
29
  end
@@ -1,44 +1,62 @@
1
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]
2
+ module RaspberryPi
3
+ def self.mapping
4
+ :hardware
5
+ end
6
+ def self.hardware_pins
7
+ [3,5,7,8,10,11,12,13,15,16,17,19,21,22,23,24,26]
8
+ end
9
+ def self.software_pins
10
+ [0,1,4,14,15,17,18,21,22,23,24,10,9,25,11,8,7]
8
11
  end
9
12
 
10
- def get_pins(mapping=@mapping)
13
+ def self.get_pins(mapping=@mapping)
11
14
  pins = `sudo ls /sys/class/gpio`.scan(/(?:gpio)(\d+)/).flatten.map!(&:to_i)
12
15
  pins.map!{|pin| Pin.new(pin,nil,self).pin}
13
16
  end
14
- def get_direction(software_pin)
17
+ def self.get_direction(software_pin)
15
18
  `sudo cat /sys/class/gpio/gpio#{software_pin}/direction`.chomp
16
19
  end
17
20
 
18
- def initialize_pin(software_pin, direction)
21
+ def self.initialize_pin(software_pin, direction)
19
22
  unexport!(software_pin) if exported?(software_pin) && direction != get_direction(software_pin)
20
- export!(software_pin) unless exported?(software_pin)
23
+ export!(software_pin, direction) unless exported?(software_pin)
21
24
  end
22
25
 
23
- def exported?(software_pin)
26
+ def self.exported?(software_pin)
24
27
  `sudo [ -d /sys/class/gpio/gpio#{software_pin} ] && echo true || false`.chomp == 'true'
25
28
  end
26
- def export!(software_pin,direction)
29
+ def self.export!(software_pin,direction)
27
30
  `sudo bash -c "echo #{software_pin} > /sys/class/gpio/export"`
28
31
  `sudo bash -c "echo #{direction} > /sys/class/gpio/gpio#{software_pin}/direction"`
29
32
  exported?(software_pin)
30
33
  end
31
- def unexport!(software_pin)
34
+ def self.unexport!(software_pin)
32
35
  `sudo bash -c "echo #{software_pin} > /sys/class/gpio/unexport"`
33
36
  !exported?(software_pin)
34
37
  end
35
38
 
36
- def read(software_pin)
39
+ def self.read(software_pin)
37
40
  `sudo cat /sys/class/gpio/gpio#{software_pin}/value`.chomp == '1'
38
41
  end
39
- def write(software_pin,value)
42
+ def self.write(software_pin,value)
40
43
  raise "This pin is an input." if get_direction(software_pin) == 'in'
41
44
  `sudo bash -c "echo #{value} > /sys/class/gpio/gpio#{software_pin}/value && echo true || false"`.chomp == 'true'
42
45
  end
46
+
47
+ def self.software_pin(pin)
48
+ raise "That software pin doesn't exist." unless software_pins.include? pin
49
+ mapping == :software ? pin : software_pins[hardware_pins.index(pin)]
50
+ end
51
+ def self.hardware_pin(pin)
52
+ raise "That hardware pin doesn't exist." unless hardware_pins.include? pin
53
+ mapping == :hardware ? pin : hardware_pins[software_pins.index(pin)]
54
+ end
55
+ def self.load_pins
56
+ get_pins(:hardware).map{|pin| Pin.new(pin,get_direction(pin),self)}
57
+ end
58
+ def self.unexport_all!
59
+ load_pins.map(&unexport!)
60
+ end
43
61
  end
44
62
  end
metadata CHANGED
@@ -1,53 +1,67 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: gpio
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
6
10
  platform: ruby
7
- authors:
11
+ authors:
8
12
  - Christopher Klapp
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
- date: 2012-06-04 00:00:00.000000000 Z
16
+
17
+ date: 2012-06-05 00:00:00 -04:00
18
+ default_executable:
13
19
  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.
20
+
21
+ description: gpio allows for devices such as raspberry pi or systems with 1wire usb adapters to speak to the system's input/output pins. The end goal is for people to contribute code for specific devices, sensors and outputs.
17
22
  email: christopher@klapp.name
18
23
  executables: []
24
+
19
25
  extensions: []
26
+
20
27
  extra_rdoc_files: []
21
- files:
28
+
29
+ files:
22
30
  - lib/gpio/device.rb
23
31
  - lib/gpio/pin.rb
24
32
  - lib/gpio/raspberry_pi.rb
25
33
  - lib/gpio.rb
26
34
  - README.md
27
35
  - changelog
36
+ has_rdoc: true
28
37
  homepage: http://klappy.github.com/gpio
29
38
  licenses: []
39
+
30
40
  post_install_message:
31
41
  rdoc_options: []
32
- require_paths:
42
+
43
+ require_paths:
33
44
  - 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'
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
46
59
  requirements: []
60
+
47
61
  rubyforge_project:
48
- rubygems_version: 1.8.24
62
+ rubygems_version: 1.3.6
49
63
  signing_key:
50
64
  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.
65
+ summary: gpio allows for devices such as raspberry pi or systems with 1wire usb adapters to speak to the system's input/output pins.
53
66
  test_files: []
67
+