gpio 0.0.2 → 0.0.3
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/README.md +73 -3
- data/changelog +7 -0
- data/examples/motion_example.rb +20 -0
- data/lib/gpio.rb +1 -1
- data/lib/gpio/device.rb +8 -8
- data/lib/gpio/motion_detector.rb +7 -0
- data/lib/gpio/raspberry_pi.rb +2 -15
- data/lib/gpio/sensor.rb +15 -0
- metadata +7 -4
data/README.md
CHANGED
@@ -1,6 +1,76 @@
|
|
1
|
-
|
1
|
+
# description
|
2
2
|
====
|
3
3
|
|
4
|
-
|
4
|
+
a ruby gem and repository to contribute gpio code for devices such as Raspberry Pi to read sensors and control outputs.
|
5
5
|
|
6
|
-
|
6
|
+
gpio allows for devices such as raspberry pi or systems with 1wire usb adapters to speak to the system's input/output pins.
|
7
|
+
|
8
|
+
# installation
|
9
|
+
====
|
10
|
+
|
11
|
+
### bundler
|
12
|
+
|
13
|
+
using bundler, add this to your Gemfile in the root of your app
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'gpio'
|
17
|
+
```
|
18
|
+
install bundler and then run bundle install
|
19
|
+
```bash
|
20
|
+
gem install bundler
|
21
|
+
bundle install
|
22
|
+
```
|
23
|
+
|
24
|
+
|
25
|
+
### gem install
|
26
|
+
|
27
|
+
from command line
|
28
|
+
|
29
|
+
```bash
|
30
|
+
gem install gpio
|
31
|
+
```
|
32
|
+
|
33
|
+
require in your ruby file
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'gpio'
|
37
|
+
```
|
38
|
+
|
39
|
+
# example
|
40
|
+
====
|
41
|
+
|
42
|
+
let's setup a motion sensor on GPIO pin 17 on a RaspberryPi (default).
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
motion = GPIO::MotionDetector.new(pin: 17)
|
46
|
+
```
|
47
|
+
|
48
|
+
do we detect and motion yet?
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
motion.detect
|
52
|
+
```
|
53
|
+
|
54
|
+
let's print a message corresponding to the motion.detect?
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
puts motion.detect ? "Motion detected!" : "No motion detected."
|
58
|
+
```
|
59
|
+
|
60
|
+
was the value any different than the last time it detected?
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
motion.changed?
|
64
|
+
puts "last: #{motion.last_reading}, current: #{motion.reading}"
|
65
|
+
```
|
66
|
+
|
67
|
+
wait until the value changes before we move on.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
until motion.changed? do
|
71
|
+
motion.detect
|
72
|
+
sleep 0.5
|
73
|
+
end
|
74
|
+
puts "Something changed!"
|
75
|
+
puts "last: #{motion.last_reading}, current: #{motion.reading}"
|
76
|
+
```
|
data/changelog
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
version 0.0.3
|
2
|
+
- updated readme with example
|
3
|
+
- example usage code added
|
4
|
+
- example sensor of motion_detector added
|
5
|
+
- sensor class added
|
6
|
+
- raspberry_pi module extends device module
|
7
|
+
|
1
8
|
version 0.0.2
|
2
9
|
- device/rasberrypi now modules instead of class
|
3
10
|
- pin.new(params) now pass hash of params to pin.new {pin: 1, mode: :in, device: :RaspberryPi}
|
@@ -0,0 +1,20 @@
|
|
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}"
|
data/lib/gpio.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
%w[device raspberry_pi pin].each{|file| require File.dirname(__FILE__)+'/gpio/'+file}
|
1
|
+
%w[device raspberry_pi pin sensor motion_detector].each{|file| require File.dirname(__FILE__)+'/gpio/'+file}
|
data/lib/gpio/device.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
module GPIO
|
2
2
|
module Device
|
3
|
-
def
|
4
|
-
raise "That software pin doesn't exist." unless
|
5
|
-
mapping == :software ? pin :
|
3
|
+
def software_pin(pin)
|
4
|
+
raise "That software pin doesn't exist." unless software_pins.include? pin
|
5
|
+
mapping == :software ? pin : software_pins[hardware_pins.index(pin)]
|
6
6
|
end
|
7
|
-
def
|
8
|
-
raise "That hardware pin doesn't exist." unless
|
9
|
-
mapping == :hardware ? pin :
|
7
|
+
def hardware_pin(pin)
|
8
|
+
raise "That hardware pin doesn't exist." unless hardware_pins.include? pin
|
9
|
+
mapping == :hardware ? pin : hardware_pins[software_pins.index(pin)]
|
10
10
|
end
|
11
|
-
def
|
11
|
+
def load_pins
|
12
12
|
get_pins(:hardware).map{|pin| Pin.new(pin,get_direction(pin),self)}
|
13
13
|
end
|
14
|
-
def
|
14
|
+
def unexport_all!
|
15
15
|
load_pins.map(&unexport!)
|
16
16
|
end
|
17
17
|
end
|
data/lib/gpio/raspberry_pi.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module GPIO
|
2
2
|
module RaspberryPi
|
3
|
+
extend Device
|
4
|
+
|
3
5
|
def self.mapping
|
4
6
|
:hardware
|
5
7
|
end
|
@@ -43,20 +45,5 @@ module GPIO
|
|
43
45
|
raise "This pin is an input." if get_direction(software_pin) == 'in'
|
44
46
|
`sudo bash -c "echo #{value} > /sys/class/gpio/gpio#{software_pin}/value && echo true || false"`.chomp == 'true'
|
45
47
|
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
|
61
48
|
end
|
62
49
|
end
|
data/lib/gpio/sensor.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module GPIO
|
2
|
+
class Sensor
|
3
|
+
attr_reader :reading, :last_reading
|
4
|
+
def initialize(params)
|
5
|
+
@pin = Pin.new(:pin => params[:pin], :mode => :in, :device => params[:device])
|
6
|
+
end
|
7
|
+
def changed?
|
8
|
+
@last_reading != @reading
|
9
|
+
end
|
10
|
+
def read
|
11
|
+
@last_reading = @reading
|
12
|
+
@reading = @pin.read
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Christopher Klapp
|
@@ -18,7 +18,7 @@ date: 2012-06-05 00:00:00 -04:00
|
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
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.
|
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.
|
22
22
|
email: christopher@klapp.name
|
23
23
|
executables: []
|
24
24
|
|
@@ -28,9 +28,12 @@ extra_rdoc_files: []
|
|
28
28
|
|
29
29
|
files:
|
30
30
|
- lib/gpio/device.rb
|
31
|
+
- lib/gpio/motion_detector.rb
|
31
32
|
- lib/gpio/pin.rb
|
32
33
|
- lib/gpio/raspberry_pi.rb
|
34
|
+
- lib/gpio/sensor.rb
|
33
35
|
- lib/gpio.rb
|
36
|
+
- examples/motion_example.rb
|
34
37
|
- README.md
|
35
38
|
- changelog
|
36
39
|
has_rdoc: true
|
@@ -62,6 +65,6 @@ rubyforge_project:
|
|
62
65
|
rubygems_version: 1.3.6
|
63
66
|
signing_key:
|
64
67
|
specification_version: 3
|
65
|
-
summary:
|
68
|
+
summary: a ruby gem and repository to contribute gpio code for devices such as Raspberry Pi to read sensors and control outputs.
|
66
69
|
test_files: []
|
67
70
|
|