rupi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.md +29 -0
  2. data/bin/rupi +15 -0
  3. data/lib/rupi.rb +20 -0
  4. data/lib/rupi/pin.rb +102 -0
  5. data/lib/rupi/version.rb +3 -0
  6. metadata +68 -0
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rupi
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rupi'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rupi
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/bin/rupi ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..' , 'lib'))
4
+
5
+ require 'rubygems'
6
+ require 'rupi'
7
+
8
+ include Rupi
9
+
10
+ load ARGV[0]
11
+
12
+ if Rupi::Pin.watching?
13
+ trap("SIGINT") { Rupi::Pin.stop_watching }
14
+ Rupi::Pin.join_watch_thread
15
+ end
data/lib/rupi.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "rupi/version"
2
+ require 'rupi/pin'
3
+
4
+ module Rupi
5
+ PIN0 = Pin.new(0)
6
+ PIN1 = Pin.new(1)
7
+ PIN2 = Pin.new(2)
8
+ PIN3 = Pin.new(3)
9
+ PIN4 = Pin.new(4)
10
+ PIN5 = Pin.new(5)
11
+ PIN6 = Pin.new(6)
12
+ PIN7 = Pin.new(7)
13
+ PIN8 = Pin.new(8)
14
+ PIN9 = Pin.new(9)
15
+ PIN10 = Pin.new(10)
16
+ PIN11 = Pin.new(11)
17
+ PIN12 = Pin.new(12)
18
+ PIN13 = Pin.new(13)
19
+ PIN14 = Pin.new(14)
20
+ end
data/lib/rupi/pin.rb ADDED
@@ -0,0 +1,102 @@
1
+ require 'wiringpi'
2
+
3
+ module Rupi
4
+ class Pin
5
+ attr_reader :number, :up_handlers, :down_handlers
6
+
7
+ def initialize(number)
8
+ @number = number
9
+
10
+ @up_handlers = []
11
+ @down_handlers = []
12
+ end
13
+
14
+ def gpio
15
+ self.class.gpio
16
+ end
17
+
18
+ def self.gpio
19
+ @gpio ||= WiringPi::GPIO.new
20
+ end
21
+
22
+ def input!
23
+ gpio.mode(number, INPUT)
24
+ end
25
+
26
+ def output!
27
+ gpio.mode(number, OUTPUT)
28
+ end
29
+
30
+ def value
31
+ gpio.read(number)
32
+ end
33
+
34
+ def value=(value)
35
+ value = 1 if value == true
36
+ value = 0 if value == false
37
+
38
+ gpio.write(number, value)
39
+ end
40
+
41
+ def toggle
42
+ self.value = value != 1
43
+ end
44
+
45
+ def up(&block)
46
+ @up_handlers << block
47
+ self.class.watch(self)
48
+ end
49
+
50
+ def down(&block)
51
+ @down_handlers << block
52
+ self.class.watch(self)
53
+ end
54
+
55
+ def change(&block)
56
+ @up_handlers << block
57
+ @down_handlers << block
58
+ self.class.watch(self)
59
+ end
60
+
61
+ def self.watch(pin)
62
+ @watched_pins ||= {}
63
+ @watched_pins[pin] ||= nil
64
+ start_watching
65
+ end
66
+
67
+ def self.start_watching
68
+ @watching = true
69
+
70
+ @watch_thread ||= Thread.new do
71
+
72
+ while @watching && !@watched_pins.empty? do
73
+ @watched_pins.each do |pin, previous_value|
74
+ value = pin.value
75
+ if previous_value && previous_value != value
76
+ handlers = value == 1 ? pin.up_handlers : pin.down_handlers
77
+ handlers.each do |handler|
78
+ handler.call(value)
79
+ end
80
+ end
81
+ @watched_pins[pin] = value
82
+ end
83
+ sleep(0.1)
84
+
85
+ end
86
+ end
87
+ end
88
+
89
+ def self.stop_watching
90
+ @watching = false
91
+ end
92
+
93
+ def self.watching?
94
+ @watching
95
+ end
96
+
97
+ def self.join_watch_thread
98
+ @watch_thread.join
99
+ end
100
+
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module Rupi
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rupi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mick Staugaard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: wiringpi
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A avery simple ruby library for Raspberry Pi GPIO
31
+ email:
32
+ - mick@staugaard.com
33
+ executables:
34
+ - rupi
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - bin/rupi
39
+ - lib/rupi/pin.rb
40
+ - lib/rupi/version.rb
41
+ - lib/rupi.rb
42
+ - README.md
43
+ homepage: ''
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.24
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Provides a very natural ruby interface for interacting with GPIO on the Raspberry
67
+ Pi
68
+ test_files: []