pcduino 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWU1YWQ4MGRkYWNkODk2MWM0ZmM2YzA5ZjBiOTUwNTFlZTJjNWY2Mw==
5
+ data.tar.gz: !binary |-
6
+ OTllMjU5NGRmYTc0M2Q4OTkzY2VjZjQ4Yjk3M2U1YmM4YjgxNmMzYQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NzU3YmJmOGE2NjE3ZmZiYWU0ODg1YzA2MDEzNTI1ZGE3YjAxZGRlNDdkZjUy
10
+ YTMxM2ZiOWVjMmI4MDgxMDA3NjU4YjNjYzZlMWFmNzc5YzEzM2MzNTY1OTE0
11
+ YjY2NjI5MzY4NWI0OWNlMWUxZmRiZTQ0MGUzODI4YmYyYzVhNDc=
12
+ data.tar.gz: !binary |-
13
+ ZGMyMGJmYmVjMzhlM2U4NmNmMjhkODlkOTQ4NTU1M2ZkYzlmMDdkNjFiM2I3
14
+ N2I3MGQ5ZjE3Njk1OGQ2YmJjOGJiYWU0MmI5YzIzOGVhYzA2ZjRjYzUyNDNl
15
+ MWZiYzNjNjE4NWNhNDU0NDQ3OGExM2Y0YzE2MDMwMzg1MTRkNmY=
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 pcduino.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 willoftw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
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,36 @@
1
+ <<<<<<< HEAD
2
+ # Pcduino
3
+
4
+ TODO: Write a gem description
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'pcduino'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install pcduino
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
31
+ =======
32
+ pcduino
33
+ =======
34
+
35
+ Ruby Gem to talk to pcduino's GPIO pins
36
+ >>>>>>> ee30f6e89a8310aae21264e176b2b6fa005164c6
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+ task :test => :spec
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pcduino'
4
+
5
+ blink_pin = Pin.new 13
6
+
7
+ while true
8
+ blink_pin.toggle
9
+ sleep 1
10
+ end
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'sinatra'
4
+ require 'haml'
5
+
6
+ require 'pcduino'
7
+
8
+ get '/' do
9
+ haml :index
10
+ end
11
+
12
+ get '/control' do
13
+ haml :control
14
+ end
15
+
16
+ post '/control' do
17
+ @host = params[:host]
18
+ @my_pin = Pin.new 13
19
+ @results = @my_pin.write(@host.to_i)
20
+
21
+
22
+ haml :control
23
+ end
24
+
25
+
26
+ __END__
27
+ @@ layout
28
+ %html
29
+ %head
30
+ %title Network Tools
31
+ %body
32
+ #header
33
+ %h1 Network Tools
34
+ #content
35
+ =yield
36
+ %footer
37
+ %a(href='/') Back to index
38
+
39
+ @@ index
40
+ %p
41
+ Welcome to Wills mini home automation server!
42
+ %ul
43
+ %li
44
+ %h3
45
+ %a(href='/control') Control Stuff!
46
+
47
+ @@ control
48
+ %h3 Controll Stuff yo!
49
+ %form(action='/control' method='POST')
50
+ %input(type='text' name='host' value=@output)
51
+ %input(type='submit')
52
+ - if defined?(@results)
53
+ %pre= @results
54
+
@@ -0,0 +1,107 @@
1
+
2
+ class Pin
3
+
4
+ attr_accessor :mode_URI,:status_URI,:pin,:input_value,:input_pu_value,:output_value,:mode
5
+ def initialize(pin_num)
6
+ @mode_URI = "/sys/devices/virtual/misc/gpio/mode/"
7
+ @status_URI = "/sys/devices/virtual/misc/gpio/pin/"
8
+ @pin = pin_num
9
+ @mode_URI = @mode_URI << "gpio" << @pin.to_s
10
+ @status_URI = @status_URI << "gpio" << @pin.to_s
11
+ @input_value = 0
12
+ @input_value_pu = 8
13
+ @output_value = 1
14
+ @state = 0;
15
+
16
+ if @pin < 0 or @pin > 17
17
+ raise "inaccessable pin specified"
18
+ end
19
+ end
20
+
21
+ def check_connection(path)
22
+ if File.exist?(path)
23
+ true
24
+ else
25
+ raise "Error file:" + path + " doesnt exist"
26
+ end
27
+ end
28
+
29
+ def set_as_output
30
+ if write_to_file(@output_value,@mode_URI)
31
+ @mode=@output_value
32
+ self
33
+ else
34
+ raise "Error: couldnt write output_value to file"
35
+ end
36
+ end
37
+
38
+ def set_as_input(file = @mode_URI)
39
+ if write_to_file(@input_value,file)
40
+ @mode=@input_value
41
+ self
42
+ else
43
+ raise "Error: couldnt write input_value to file"
44
+ end
45
+ end
46
+
47
+ def set_as_input_pu
48
+ if write_to_file(@input_value_pu,@mode_URI)
49
+ @mode=@input_pu_value
50
+ self
51
+ else
52
+ raise "Error: couldnt write input_value to file"
53
+ end
54
+ end
55
+
56
+ def write(value)
57
+ if value.is_a? Integer and value == 0 or value == 1
58
+ if @mode==@input_pu_value
59
+ set_as_output
60
+ end
61
+ write_to_file(value.to_s,@status_URI)
62
+ else
63
+ raise "invalid value passed"
64
+ end
65
+ end
66
+
67
+ def write_to_file (value,path)
68
+ begin
69
+ file = File.open(path, "w")
70
+ file.write(value)
71
+ rescue IOError => e
72
+ raise "error writing to file"
73
+ ensure
74
+ file.close unless file == nil
75
+ end
76
+ true
77
+ end
78
+
79
+ def read_from_file(file)
80
+ begin
81
+ file = File.open(file)
82
+ line = file.gets
83
+ rescue IOError => e
84
+ raise "error reading from file: " + file
85
+ ensure
86
+ file.close unless file == nil
87
+ end
88
+ line.to_i
89
+
90
+ end
91
+
92
+ def read
93
+ set_as_input
94
+ read_from_file(@status_URI)
95
+ end
96
+
97
+ def toggle
98
+ if @state == 0
99
+ @state = 1
100
+ else
101
+ @state = 0
102
+ end
103
+ write @state
104
+
105
+ end
106
+ end
107
+
@@ -0,0 +1,3 @@
1
+ module Pcduino
2
+ VERSION = "0.0.1"
3
+ end
data/lib/pcduino.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "pcduino/version"
2
+ require "pcduino/pin"
3
+
4
+ module Pcduino
5
+ def self.hi
6
+ p "hello world"
7
+ end
8
+ require "pcduino/pin"
9
+ end
10
+
11
+ module Logic
12
+ HIGH = 1
13
+ LOW = 0
14
+ end
data/pcduino.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'pcduino/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "pcduino"
9
+ spec.version = Pcduino::VERSION
10
+ spec.authors = "Will Olner"
11
+ spec.email = "William.olner@gmail.com"
12
+ spec.description = "Control GPIO of Pcduino"
13
+ spec.summary = "Control GPIO of Pcduino using Ruby"
14
+ spec.homepage = "http://github.com/willoftw/pcduino"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
data/spec/pin_spec.rb ADDED
@@ -0,0 +1,156 @@
1
+ require "spec_helper"
2
+
3
+ describe Pin do
4
+ @os = RUBY_PLATFORM
5
+ before :all do
6
+ @pin = Pin.new 1
7
+ $on_pcduino = false
8
+ end
9
+
10
+ if @os.include?("darwin")
11
+ $on_pcduino = false
12
+ else
13
+ $on_pcduino = true
14
+ end
15
+ describe "#new" do
16
+
17
+ it "should return a pin object" do
18
+ @pin.should be_an_instance_of Pin
19
+ end
20
+
21
+ it "should raise an error for an incorrect pin number" do
22
+ lambda { Pin.new(99) }.should raise_error
23
+ end
24
+
25
+ it "should not raise an error for an incorrect pin" do
26
+ lambda { Pin.new(9) }.should_not raise_error
27
+ end
28
+
29
+ it "should have the correct mode URI" do
30
+ @pin.mode_URI.should == "/sys/devices/virtual/misc/gpio/mode/gpio1"
31
+ end
32
+
33
+ it "should have the correct status URI" do
34
+ @pin.status_URI.should == "/sys/devices/virtual/misc/gpio/pin/gpio1"
35
+ end
36
+
37
+ end
38
+ describe "#write_to_file" do
39
+ it "should return true for a succesful file write" do
40
+ test_file_name = "out.txt"
41
+ out_file = File.new(test_file_name, "w")
42
+ @pin.write_to_file(0,test_file_name).should be_true
43
+ lambda{ @pin.write_to_file(0,test_file_name)} .should_not raise_error
44
+ File.delete(test_file_name)
45
+ end
46
+ end
47
+
48
+ describe "#set_as_input" do
49
+ if $on_pcduino
50
+ it "should return a pin object on pcduino" do
51
+ @pin.set_as_input.should be_an_instance_of Pin
52
+ end
53
+
54
+ it "should sucessfully write to its pin mode input file on pcduino" do
55
+ lambda { @pin.set_as_input }.should_not raise_error
56
+ end
57
+ else
58
+ it "should not sucessfully write to its pin mode file when not on pcduino" do
59
+ lambda { @pin.set_as_input }.should raise_error
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "#set_as_input_pu" do
65
+ if $on_pcduino
66
+ it "should return a pin object" do
67
+ @pin.set_as_input_pu.should be_an_instance_of Pin
68
+ end
69
+
70
+ it "should sucessfully write to its pin mode pu file" do
71
+ lambda { @pin.set_as_input_pu }.should_not raise_error
72
+ end
73
+ else
74
+ it "should not sucessfully write to its pin mode input_pu file when not on pcduino" do
75
+ lambda { @pin.set_as_input_pu }.should raise_error
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "#set_as_output" do
81
+ if $on_pcduino
82
+ it "should return a pin object" do
83
+ @pin.set_as_input.should be_an_instance_of Pin
84
+ end
85
+
86
+ it "should sucessfully write to its pin mode file" do
87
+ lambda { @pin.set_as_output }.should_not raise_error
88
+ end
89
+ else
90
+ it "should not sucessfully write to its pin mode output file when not on pcduino" do
91
+ lambda { @pin.set_as_output }.should raise_error
92
+ end
93
+ end
94
+ end
95
+
96
+ describe "#write" do
97
+ if $on_pcduino
98
+ it "should not raise an error" do
99
+ lambda { @pin.write(Logic::HIGH) }.should_not raise_error
100
+ end
101
+
102
+ it "should not allow invalid values" do
103
+ lambda { @pin.write(12) }.should raise_error
104
+ lambda { @pin.write(-12) }.should raise_error
105
+ end
106
+
107
+ else
108
+ it "should raise an error" do
109
+ lambda { @pin.write(Logic::HIGH) }.should raise_error
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+ describe "#read_from_file" do
116
+ it "should raise error for a non exsistant file" do
117
+ lambda { @pin.read_from_file("nonexisitantfile.txt") }.should raise_error
118
+ end
119
+ end
120
+
121
+ describe "#read" do
122
+ if $on_pcduino
123
+ it "should return a value for sucessful read when set as input" do
124
+ @pin.set_as_input
125
+ @pin.read.should be_an(Integer)
126
+ end
127
+
128
+ else
129
+ it "should return an error for unsucessful read when set as input" do
130
+ #@pin.set_as_input
131
+ lambda { @pin.read }.should raise_error
132
+ end
133
+ end
134
+
135
+ end
136
+
137
+ describe "#toggle" do
138
+ if $on_pcduino
139
+ it "should return true for succesful toggle" do
140
+ @pin.read.should be_true
141
+ end
142
+
143
+ it "should not raise an error" do
144
+ lambda { @pin.toggle }.should_not raise_error
145
+ end
146
+
147
+ else
148
+ it "should return an error for toggle" do
149
+ #@pin.set_as_input
150
+ lambda { @pin.toggle }.should raise_error
151
+ end
152
+ end
153
+
154
+ end
155
+ end
156
+
@@ -0,0 +1,3 @@
1
+ require 'pcduino'
2
+
3
+
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pcduino
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Will Olner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-16 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Control GPIO of Pcduino
56
+ email: William.olner@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - .gitignore
62
+ - Gemfile
63
+ - LICENSE
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - examples/io_example/blink.rb
68
+ - examples/sinatra_example/app.rb
69
+ - lib/pcduino.rb
70
+ - lib/pcduino/pin.rb
71
+ - lib/pcduino/version.rb
72
+ - pcduino.gemspec
73
+ - spec/pin_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: http://github.com/willoftw/pcduino
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.7
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Control GPIO of Pcduino using Ruby
99
+ test_files:
100
+ - spec/pin_spec.rb
101
+ - spec/spec_helper.rb