gpio 0.0.5 → 0.0.6

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.
data/changelog CHANGED
@@ -1,3 +1,11 @@
1
+ version 0.0.6
2
+ - new reading method File.new and f.getc/rewind that gets into ~35 microsecond reads instead of ~55 microsecond IO.new io.getc/rewind and ~350 microsecond reads with IO.read
3
+ - typed input/output pins, mainly to prepare for pwm, bidirectional/1wire, and more
4
+ - organized folders for devices/pins/sensors
5
+
6
+ version 0.0.5
7
+ - date bump
8
+
1
9
  version 0.0.4
2
10
  - major changes to reading/writing from/to gpio including checking permissions with hands free fallback to older sudo method
3
11
  - path_write method checks permissions and writes using IO.write for performance and falls back to shelling out to sudo echo
@@ -1 +1 @@
1
- %w[device raspberry_pi pin sensor motion_detector].each{|file| require File.dirname(__FILE__)+'/gpio/'+file}
1
+ %w[device devices/raspberry_pi pin pins/input_pin pins/output_pin sensor sensors/motion_detector].each{|file| require File.dirname(__FILE__)+'/gpio/'+file}
@@ -1,12 +1,12 @@
1
1
  module GPIO
2
2
  module Device
3
3
  def software_pin(pin)
4
- raise "That software pin #{pin} doesn't exist." unless software_pins.include? pin
5
- mapping == :software ? pin : software_pins[hardware_pins.index(pin)]
4
+ raise "That software pin #{pin} doesn't exist." unless self::SOFTWARE_PINS.include? pin
5
+ self::MAPPING == :software ? pin : self::SOFTWARE_PINS[self::HARDWARE_PINS.index(pin)]
6
6
  end
7
7
  def hardware_pin(pin)
8
- raise "That hardware pin #{pin} doesn't exist." unless hardware_pins.include? pin
9
- mapping == :hardware ? pin : hardware_pins[software_pins.index(pin)]
8
+ raise "That hardware pin #{pin} doesn't exist." unless self::HARDWARE_PINS.include? pin
9
+ self::MAPPING == :hardware ? pin : self::HARDWARE_PINS[self::SOFTWARE_PINS.index(pin)]
10
10
  end
11
11
  def load_pins
12
12
  get_pins(:hardware).map{|pin| Pin.new(pin,get_direction(pin),self)}
@@ -16,7 +16,7 @@ module GPIO
16
16
  end
17
17
 
18
18
  def get_pins(mapping)
19
- pins = Dir.entries base_path.select!{|pin| pin[/(?:#{pin_prefix})(\d+)/]}.to_i
19
+ pins = Dir.entries self::BASE_PATH.select!{|pin| pin[/(?:#{PIN_PREFIX})(\d+)/]}.to_i
20
20
  pins.map!{|pin| Pin.new(pin,nil,self).pin}
21
21
  end
22
22
  def get_direction(software_pin)
@@ -29,18 +29,27 @@ module GPIO
29
29
  end
30
30
 
31
31
  def exported?(software_pin)
32
- Dir.exists? pin_path(software_pin)
32
+ File.exists? pin_path(software_pin)
33
33
  end
34
34
  def export!(software_pin,direction)
35
- path_write export_path, software_pin
35
+ path_write self::EXPORT_PATH, software_pin
36
36
  path_write direction_path(software_pin), direction
37
37
  exported?(software_pin)
38
38
  end
39
39
  def unexport!(software_pin)
40
- path_write unexport_path, software_pin
40
+ path_write self::UNEXPORT_PATH, software_pin
41
41
  !exported? software_pin
42
42
  end
43
43
 
44
+ def pin_file(software_pin, mode)
45
+ m = case mode.to_s
46
+ when 'in'; 'r'
47
+ when 'out'; 'w'
48
+ when 'bi'; 'r+'
49
+ end
50
+ File.new value_path(software_pin), m
51
+ end
52
+
44
53
  def read(software_pin)
45
54
  IO.read(value_path(software_pin), 1) == '1'
46
55
  end
@@ -49,10 +58,24 @@ module GPIO
49
58
  path_write value_path(software_pin), value
50
59
  end
51
60
  def path_write(path, value)
52
- File::Stat.writable?(path) ? IO.write(path, value) : path_write_sudo(path, value)
61
+ File::Stat.new(path).writable? ? IO.write(path, value) : path_write_sudo(path, value)
53
62
  end
54
63
  def path_write_sudo(path, value)
55
64
  `sudo bash -c "echo #{value} > #{path}"`.chomp!
56
65
  end
66
+
67
+ def pin_path(n)
68
+ "#{self::BASE_PATH}#{self::PIN_PREFIX}#{n}/"
69
+ end
70
+ def direction_path(n)
71
+ "#{pin_path(n)}#{self::DIRECTION_FILE}"
72
+ end
73
+ def value_path(n)
74
+ "#{pin_path(n)}#{self::VALUE_FILE}"
75
+ end
76
+
77
+ def valid?
78
+ IO.read(self::VALIDATE_FILE).chomp! == self::VALIDATE_VALUE
79
+ end
57
80
  end
58
81
  end
@@ -0,0 +1,18 @@
1
+ module GPIO
2
+ module Other
3
+ extend Device
4
+
5
+ VALIDATE_FILE = "/sys/class/gpio/gpiochip0/label"
6
+ VALIDATE_VALUE = "chipset_gpio"
7
+
8
+ MAPPING = :hardware
9
+ HARDWARE_PINS = [0,1,2]
10
+ SOFTWARE_PINS = [2,1,0]
11
+ BASE_PATH = '/sys/class/gpio/'
12
+ EXPORT_PATH = "#{BASE_PATH}export"
13
+ UNEXPORT_PATH = "#{BASE_PATH}unexport"
14
+ PIN_PREFIX = "gpio"
15
+ DIRECTION_FILE = "mode"
16
+ VALUE_FILE = "value"
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module GPIO
2
+ module RaspberryPi
3
+ extend Device
4
+
5
+ VALIDATE_FILE = "/sys/class/gpio/gpiochip0/label"
6
+ VALIDATE_VALUE = "bcm2708_gpio"
7
+
8
+ MAPPING = :hardware
9
+ HARDWARE_PINS = [3,5,7,8,10,11,12,13,15,16,17,19,21,22,23,24,26]
10
+ SOFTWARE_PINS = [0,1,4,14,15,17,18,21,22,23,24,10,9,25,11,8,7]
11
+ BASE_PATH = '/sys/class/gpio/'
12
+ EXPORT_PATH = "#{BASE_PATH}export"
13
+ UNEXPORT_PATH = "#{BASE_PATH}unexport"
14
+ PIN_PREFIX = "gpio"
15
+ DIRECTION_FILE = "direction"
16
+ VALUE_FILE = "value"
17
+ end
18
+ end
@@ -1,6 +1,6 @@
1
1
  module GPIO
2
2
  class Pin
3
- attr_reader :pin, :mode, :device, :hardware_pin, :software_pin
3
+ attr_reader :pin, :mode, :device, :hardware_pin, :software_pin, :file
4
4
  def initialize(params) #(pin, mode, device=:RaspberryPi)
5
5
  @device = GPIO.const_get(params[:device]||:RaspberryPi)
6
6
 
@@ -8,22 +8,16 @@ module GPIO
8
8
  @hardware_pin = device.hardware_pin(pin)
9
9
  @software_pin = device.software_pin(pin)
10
10
 
11
- @mode = params[:mode].to_s || device.get_direction
11
+ @mode = params[:mode].to_s
12
12
  raise "Mode should be :in, :out, :bi, :pwm." unless ['in','out'].include? @mode
13
13
 
14
14
  device.initialize_pin(software_pin, @mode)
15
+ @mode ||= get_direction
16
+ @file = @device.pin_file(pin, mode)
15
17
  end
16
18
 
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
19
  def read
26
- device.read(software_pin)
20
+ file.rewind; file.getc
27
21
  end
28
22
  end
29
23
  end
@@ -0,0 +1,17 @@
1
+ module GPIO
2
+ class InputPin < Pin
3
+ attr_reader :last_reading, :reading
4
+ def initialize(params) #(pin, mode, device=:RaspberryPi)
5
+ params.merge!(:mode => :in)
6
+ super(params)
7
+ end
8
+
9
+ def changed?
10
+ @last_reading != @reading
11
+ end
12
+ def read
13
+ @last_reading = @reading
14
+ @reading = super
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module GPIO
2
+ class OutputPin < Pin
3
+ def initialize(params) #(pin, mode, device=:RaspberryPi)
4
+ params.merge!(:mode => :out)
5
+ super(params)
6
+ end
7
+
8
+ def on
9
+ device.write software_pin, 1
10
+ read
11
+ end
12
+ def off
13
+ device.write software_pin, 0
14
+ read
15
+ end
16
+ end
17
+ end
@@ -1,15 +1,20 @@
1
1
  module GPIO
2
2
  class Sensor
3
- attr_reader :reading, :last_reading
3
+ attr_reader :pin
4
4
  def initialize(params)
5
- @pin = Pin.new(:pin => params[:pin], :mode => :in, :device => params[:device])
5
+ @pin = InputPin.new(:pin => params[:pin], :mode => :in, :device => params[:device])
6
6
  end
7
7
  def changed?
8
- @last_reading != @reading
8
+ pin.changed?
9
9
  end
10
10
  def read
11
- @last_reading = @reading
12
- @reading = @pin.read
11
+ pin.read
12
+ end
13
+ def reading
14
+ pin.reading
15
+ end
16
+ def last_reading
17
+ pin.last_reading
13
18
  end
14
19
  end
15
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gpio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-06 00:00:00.000000000 Z
12
+ date: 2012-06-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: gpio allows for devices such as raspberry pi or systems with 1wire usb
15
15
  adapters to speak to the system's input/output pins.
@@ -19,11 +19,14 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - lib/gpio.rb
22
- - lib/gpio/raspberry_pi.rb
22
+ - lib/gpio/sensors/motion_detector.rb
23
23
  - lib/gpio/sensor.rb
24
24
  - lib/gpio/pin.rb
25
- - lib/gpio/motion_detector.rb
25
+ - lib/gpio/pins/output_pin.rb
26
+ - lib/gpio/pins/input_pin.rb
26
27
  - lib/gpio/device.rb
28
+ - lib/gpio/devices/raspberry_pi.rb
29
+ - lib/gpio/devices/other.rb
27
30
  - README.md
28
31
  - changelog
29
32
  homepage: http://klappy.github.com/gpio
@@ -1,37 +0,0 @@
1
- module GPIO
2
- module RaspberryPi
3
- extend Device
4
-
5
- def self.mapping
6
- :hardware
7
- end
8
- def self.hardware_pins
9
- [3,5,7,8,10,11,12,13,15,16,17,19,21,22,23,24,26]
10
- end
11
- def self.software_pins
12
- [0,1,4,14,15,17,18,21,22,23,24,10,9,25,11,8,7]
13
- end
14
-
15
- def self.base_path
16
- '/sys/class/gpio/'
17
- end
18
- def self.pin_path(n)
19
- "#{base_path}#{pin_prefix}#{n}/"
20
- end
21
- def self.direction_path(n)
22
- "#{pin_path(n)}direction"
23
- end
24
- def self.value_path(n)
25
- "#{pin_path(n)}value"
26
- end
27
- def self.export_path
28
- "#{base_path}export"
29
- end
30
- def self.unexport_path
31
- "#{base_path}unexport"
32
- end
33
- def self.pin_prefix
34
- "gpio"
35
- end
36
- end
37
- end