ruby-gpio 0.0.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b8e7c0d13e7899530c314d45f0736974831e936b
4
+ data.tar.gz: d961f0533c215504664cf218dd3c813fe87e972f
5
+ SHA512:
6
+ metadata.gz: 2b2c9b74f133ea4cf7346963a1ac1f21622a77dc4bfb496c5dcb10fd222f0f97121f1ff8832d4a71ce9f3e92661432d2d2c44b85c38befa46bbfb294b427600a
7
+ data.tar.gz: d8d8b1e2308491468dd848845ee825d08015d41554fdbee70fcafc0e88ae7aa5896ae72f536f94a8660b57176eb06c213086d814404db134ea6c89607f5a54e7
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gpio.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Chang Sau Sheong
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Ruby-GPIO
2
+
3
+ A Linux sysfs-based GPIO interface wrapper
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ruby-gpio'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ruby-gpio
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/sausheong/ruby-gpio/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,21 @@
1
+ require 'gpio'
2
+
3
+ GPIO.access(blue: 23, yellow: 27, led: 22) do
4
+ blue.as :in
5
+ yellow.as :in
6
+ led.as :out
7
+
8
+ # watch GPIO23 and turn the led on when it is pulled high
9
+ # use async to spin it off asynchronously, if you don't use
10
+ # async, you will wait indefinitely watching for blue
11
+ blue.async.watch_for(1) do
12
+ led.on
13
+ end
14
+ # watch GPIO23 and turn the led off when it is pulled high
15
+ yellow.async.watch_for(1) do
16
+ white.off
17
+ end
18
+
19
+ # sleep is only necessary if you're watching pins
20
+ sleep
21
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gpio/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-gpio"
8
+ spec.version = Gpio::VERSION
9
+ spec.authors = ["Chang Sau Sheong"]
10
+ spec.email = ["sausheong@gmail.com"]
11
+ spec.summary = %q{A Linux sysfs-based GPIO interface wrapper for Ruby}
12
+ spec.description = %q{Wraps around the Linux sysfs GPIO interface to control GPIO pins. Also watches pins and trigger handlers accordingly.}
13
+ spec.homepage = "https://github.com/sausheong/ruby-gpio"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_runtime_dependency "concurrent-ruby", "~> 0.8.0"
24
+ spec.add_runtime_dependency "concurrent-ruby-ext", "~> 0.8.0"
25
+ end
@@ -0,0 +1,3 @@
1
+ module Gpio
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,116 @@
1
+ require "gpio/version"
2
+
3
+ require 'concurrent/async'
4
+
5
+ class Hash
6
+ def method_missing(name, *args, &block)
7
+ return self[name.to_sym]
8
+ end
9
+ end
10
+
11
+
12
+ module GPIO
13
+ class Pin
14
+ include Concurrent::Async
15
+ attr_accessor :name, :number, :direction
16
+
17
+ def initialize(name, number)
18
+ init_mutex
19
+ @name = name
20
+ @number = number
21
+ end
22
+
23
+ # set input or output
24
+ # gpio_num is the GPIO pin number, dir is the direction - either :out or :in
25
+ def as(dir=:out)
26
+ @direction = dir.to_s
27
+ GPIO.write "gpio#{@number}/direction", @direction
28
+ end
29
+
30
+ # send a high value to the pin
31
+ def on
32
+ GPIO.write "gpio#{@number}/value", "1"
33
+ end
34
+
35
+ # send a low value to the pin
36
+ def off
37
+ GPIO.write "gpio#{@number}/value", "0"
38
+ end
39
+
40
+ # send a PWM (pulse width modulation) signal to the pin
41
+ def pwm(value)
42
+ GPIO.write "gpio#{@number}/value", value
43
+ end
44
+
45
+ # read from the pin
46
+ def read
47
+ GPIO.read @number
48
+ end
49
+
50
+ # watch the pin for change and trigger the block once
51
+ def watch_once_for(value, &block)
52
+ watching = true
53
+ while watching
54
+ if read == value
55
+ GPIO.instance_eval &block
56
+ watching = false
57
+ end
58
+ end
59
+ end
60
+
61
+ # watch the pin for change and trigger the block over and over again
62
+ def watch_for(value, &block)
63
+ while true
64
+ if read == value
65
+ GPIO.instance_eval &block
66
+ end
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ class << self
73
+ # main entry for the DSL
74
+ def access(hash, &block)
75
+ @pins = {} # a hash of available pins
76
+ # set up pins
77
+ hash.each do |k, v|
78
+ @pins[k] = Pin.new(k.to_s, v)
79
+ export v
80
+ end
81
+ begin
82
+ instance_eval &block
83
+ ensure
84
+ # make sure all pins are unexported before we end the program
85
+ hash.each do |k, v|
86
+ unexport v
87
+ end
88
+ end
89
+ end
90
+
91
+ # method missing used to return the pin named earlier in the hash parameter to access
92
+ def method_missing(name, *args, &block)
93
+ if @pins.keys.include?(name)
94
+ return @pins[name]
95
+ end
96
+ end
97
+
98
+ def export(gpio_num)
99
+ write "export", gpio_num
100
+ end
101
+
102
+ def unexport(gpio_num)
103
+ write "unexport", gpio_num
104
+ end
105
+
106
+ def read(gpio_num)
107
+ File.read("/sys/class/gpio/gpio#{gpio_num}/value").to_i
108
+ end
109
+
110
+ def write(command, value)
111
+ File.open("/sys/class/gpio/#{command}", "w") do |f|
112
+ f.write value
113
+ end
114
+ end
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-gpio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Chang Sau Sheong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: concurrent-ruby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: concurrent-ruby-ext
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.0
69
+ description: Wraps around the Linux sysfs GPIO interface to control GPIO pins. Also
70
+ watches pins and trigger handlers accordingly.
71
+ email:
72
+ - sausheong@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - examples/led.rb
83
+ - gpio.gemspec
84
+ - lib/gpio/version.rb
85
+ - lib/ruby-gpio.rb
86
+ homepage: https://github.com/sausheong/ruby-gpio
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: A Linux sysfs-based GPIO interface wrapper for Ruby
110
+ test_files: []