em-gpio 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in em-gpio.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Anup Narkhede
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.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # EM::Gpio
2
+
3
+ EM::Gpio is an eventmachine compatible gpio library for raspberry-pi.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'em-gpio'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install em-gpio
18
+
19
+ ## Usage
20
+
21
+ EM.run do
22
+ pin = EM::Gpio::Pin.new(10, :direction => :in)
23
+
24
+ pin.callback do
25
+ # called when pin value changes
26
+ end
27
+
28
+ pin.callback :on => :high do
29
+ # called on :low to :high transition
30
+ end
31
+
32
+ pin.callback :on => :low do
33
+ # called on :high to :low transition
34
+ end
35
+ end
36
+
37
+ ## TODO
38
+
39
+ 1. SerialPort support
40
+ 2. Example rack app
41
+ 3. Find time for 1 and 2
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/em-gpio.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'em-gpio/version'
7
+
8
+ Gem::Specification.new do |gem|
9
+ gem.name = "em-gpio"
10
+ gem.version = EventMachine::Gpio::VERSION
11
+ gem.authors = ["Anup Narkhede"]
12
+ gem.email = ["anup.narkhede@gmail.com"]
13
+ gem.description = %q{Eventmachine based GPIO library for Raspberry Pi}
14
+ gem.summary = %q{Evented GPIO library for Raspberry Pi}
15
+ gem.homepage = ""
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_dependency "eventmachine", "1.0.1"
23
+ gem.add_dependency "mocha"
24
+ end
data/lib/em-gpio.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'eventmachine'
2
+
3
+ require_relative 'em-gpio/adapter'
4
+ require_relative 'em-gpio/callbacks'
5
+ require_relative 'em-gpio/pin'
@@ -0,0 +1,30 @@
1
+ module EventMachine
2
+ module Gpio
3
+ class Adapter
4
+
5
+ attr_accessor :pin
6
+
7
+ BASE_PATH = "/sys/class/gpio/"
8
+
9
+ def initialize(pin)
10
+ @pin = pin
11
+ end
12
+
13
+ def read
14
+ File.read(BASE_PATH + "gpio#{pin}/value").to_i
15
+ end
16
+
17
+ def write(value)
18
+ File.open(BASE_PATH + "gpio#{pin}/value", "w"){|f| f.write(value)}
19
+ end
20
+
21
+ def set_mode(direction)
22
+ File.open(BASE_PATH + "gpio#{pin}/direction", "w"){|f| f.write(direction)}
23
+ end
24
+
25
+ def export!
26
+ File.open(BASE_PATH + "export", "w"){|f| f.write(pin)}
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ module EventMachine
2
+ module Gpio
3
+ module Callbacks
4
+
5
+ VALID_ON_OPTIONS = [:change, :high, :low]
6
+
7
+ def callback(options={}, &handler)
8
+ callback_wrapper do
9
+ event = options.fetch(:on, :change)
10
+ raise(ArgumentError, "Unknown event #{on.inspect} specified") unless VALID_ON_OPTIONS.include?(event)
11
+ instance_variable_set("@on_#{event}", handler)
12
+ end
13
+ self
14
+ end
15
+
16
+ private
17
+
18
+ def callback_wrapper
19
+ EM.next_tick do
20
+ yield
21
+ eventable_read
22
+ end
23
+ end
24
+
25
+ def eventable_read
26
+ read
27
+ trigger_callbacks
28
+ end
29
+
30
+ def trigger_callbacks
31
+ callback_wrapper do
32
+ if changed?
33
+ _trigger :change if @value && @last_value
34
+ _trigger :low if @value == Pin::LOW
35
+ _trigger :high if @value == Pin::HIGH
36
+ end
37
+ end
38
+ end
39
+
40
+ def _trigger(event)
41
+ handler = instance_variable_get("@on_#{event}")
42
+ handler.call(value) if handler
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,51 @@
1
+ module EventMachine
2
+ module Gpio
3
+ class Pin
4
+ include Callbacks
5
+
6
+ PINS = 0..22
7
+ MODES = [:in, :out]
8
+ HIGH = 1
9
+ LOW = 0
10
+
11
+ attr_accessor :pin, :value, :last_value, :direction
12
+
13
+ def initialize(pin, options={})
14
+ @direction = options[:direction] || :out
15
+ @pin = pin
16
+ validate
17
+ setup_mode
18
+ end
19
+
20
+ def read
21
+ @last_value = value
22
+ @value = adapter.read
23
+ end
24
+
25
+ def write(value)
26
+ adapter.write(value)
27
+ end
28
+
29
+ def changed?
30
+ value != last_value
31
+ end
32
+
33
+ private
34
+
35
+ def validate
36
+ raise "Invalid pin: #{pin}" unless PINS.include?(pin.to_i)
37
+ raise "Invalid direction: #{direction}" unless MODES.include?(direction)
38
+ end
39
+
40
+ def setup_mode
41
+ adapter.export!
42
+ adapter.set_mode(direction)
43
+ @last_value = adapter.read
44
+ end
45
+
46
+ def adapter
47
+ @adapter ||= Adapter.new(pin)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ module EventMachine
2
+ module Gpio
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/test/pin_test.rb ADDED
@@ -0,0 +1,26 @@
1
+ require_relative 'test_helper'
2
+ require 'mocha/setup'
3
+
4
+ describe "Pin" do
5
+ def setup
6
+ EM::Gpio::Adapter.any_instance.stubs(:export!).returns(true)
7
+ EM::Gpio::Adapter.any_instance.stubs(:set_mode).returns('in')
8
+ EM::Gpio::Adapter.any_instance.stubs(:read).returns(1)
9
+ end
10
+
11
+ it "should detect an invalid GPIO pin" do
12
+ EM::Gpio::Pin.any_instance.stubs(:setup_mode).returns(nil)
13
+ (0..22).each do |n|
14
+ EM::Gpio::Pin.new(n).must_be_instance_of EM::Gpio::Pin
15
+ end
16
+
17
+ [25, 100].each do |n|
18
+ proc{ EM::Gpio::Pin.new(n) }.must_raise RuntimeError
19
+ end
20
+ end
21
+
22
+ it "should read the initial state after initialization" do
23
+ pin = EM::Gpio::Pin.new 5
24
+ pin.last_value.must_be :==, 1
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ require_relative '../lib/em-gpio'
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-gpio
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Anup Narkhede
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ none: false
21
+ name: eventmachine
22
+ type: :runtime
23
+ prerelease: false
24
+ requirement: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - '='
27
+ - !ruby/object:Gem::Version
28
+ version: 1.0.1
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ none: false
37
+ name: mocha
38
+ type: :runtime
39
+ prerelease: false
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ description: Eventmachine based GPIO library for Raspberry Pi
47
+ email:
48
+ - anup.narkhede@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - em-gpio.gemspec
59
+ - lib/em-gpio.rb
60
+ - lib/em-gpio/adapter.rb
61
+ - lib/em-gpio/callbacks.rb
62
+ - lib/em-gpio/pin.rb
63
+ - lib/em-gpio/version.rb
64
+ - test/pin_test.rb
65
+ - test/test_helper.rb
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ none: false
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ none: false
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.23
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Evented GPIO library for Raspberry Pi
90
+ test_files:
91
+ - test/pin_test.rb
92
+ - test/test_helper.rb