frankenpins 0.2.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
+ NjJiYmEyZmVlNjc5NjVmMmFlYmVlZTkwODY0N2YyNWQ2N2VkN2I2NQ==
5
+ data.tar.gz: !binary |-
6
+ MTRmMGZiYjNiMDhmMDY4MmMzNDUyMjc3NDhjM2IyODA1ZDI3YjQzNg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZDc4MGNjNDUyOTE3NjNkY2RiYTI2NTk5MThlMTQ2ZTYyZmU2NjJhZDBlZmUx
10
+ NWY1NjVkNjUzZDQ3ZjlkZmE0ZTczMDJhNTZlYjM4N2RhMGI2YjU2YWI4MmE4
11
+ OGI0NWFlYjYwNjdhMDk3MmExMzBmYmM5MjhmODY3ZjkxM2M2YjM=
12
+ data.tar.gz: !binary |-
13
+ YjE1NzdkYjI5YTExZTgxMzM4MjUxZDg1Y2ZhMWM4ZGRiOThjZDU1N2RiMjlk
14
+ ZDViZGNhMzU4ZjRlMDY0Njk5MGViZjc3MGVlMTI1MTExNGIyOWY4NTc0MTc4
15
+ YmFhOTE3MTQ3YTRhYzQzY2NhYmI3MDczN2MyMzkzZWMzM2NkNjQ=
data/AUTHORS ADDED
@@ -0,0 +1 @@
1
+ - Andrew Nicolaou (andrew.nicolaou@bbc.co.uk)
data/COPYING ADDED
@@ -0,0 +1,225 @@
1
+ (c) 2013 British Broadcasting Corporation and contributors
2
+
3
+ See "AUTHORS" file for full details.
4
+
5
+ All code here, except where otherwise indicated (see below), is licensed under
6
+ the Apache Licence version 2.0.
7
+
8
+ ----------------------------------------------------------------------------
9
+
10
+ Apache License
11
+ Version 2.0, January 2004
12
+ http://www.apache.org/licenses/
13
+
14
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
15
+
16
+ 1. Definitions.
17
+
18
+ "License" shall mean the terms and conditions for use, reproduction,
19
+ and distribution as defined by Sections 1 through 9 of this document.
20
+
21
+ "Licensor" shall mean the copyright owner or entity authorized by
22
+ the copyright owner that is granting the License.
23
+
24
+ "Legal Entity" shall mean the union of the acting entity and all
25
+ other entities that control, are controlled by, or are under common
26
+ control with that entity. For the purposes of this definition,
27
+ "control" means (i) the power, direct or indirect, to cause the
28
+ direction or management of such entity, whether by contract or
29
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
30
+ outstanding shares, or (iii) beneficial ownership of such entity.
31
+
32
+ "You" (or "Your") shall mean an individual or Legal Entity
33
+ exercising permissions granted by this License.
34
+
35
+ "Source" form shall mean the preferred form for making modifications,
36
+ including but not limited to software source code, documentation
37
+ source, and configuration files.
38
+
39
+ "Object" form shall mean any form resulting from mechanical
40
+ transformation or translation of a Source form, including but
41
+ not limited to compiled object code, generated documentation,
42
+ and conversions to other media types.
43
+
44
+ "Work" shall mean the work of authorship, whether in Source or
45
+ Object form, made available under the License, as indicated by a
46
+ copyright notice that is included in or attached to the work
47
+ (an example is provided in the Appendix below).
48
+
49
+ "Derivative Works" shall mean any work, whether in Source or Object
50
+ form, that is based on (or derived from) the Work and for which the
51
+ editorial revisions, annotations, elaborations, or other modifications
52
+ represent, as a whole, an original work of authorship. For the
53
+ purposes
54
+ of this License, Derivative Works shall not include works that remain
55
+ separable from, or merely link (or bind by name) to the interfaces of,
56
+ the Work and Derivative Works thereof.
57
+
58
+ "Contribution" shall mean any work of authorship, including
59
+ the original version of the Work and any modifications or additions
60
+ to that Work or Derivative Works thereof, that is intentionally
61
+ submitted to Licensor for inclusion in the Work by the copyright owner
62
+ or by an individual or Legal Entity authorized to submit on behalf of
63
+ the copyright owner. For the purposes of this definition, "submitted"
64
+ means any form of electronic, verbal, or written communication sent
65
+ to the Licensor or its representatives, including but not limited to
66
+ communication on electronic mailing lists, source code control
67
+ systems,
68
+ and issue tracking systems that are managed by, or on behalf of, the
69
+ Licensor for the purpose of discussing and improving the Work, but
70
+ excluding communication that is conspicuously marked or otherwise
71
+ designated in writing by the copyright owner as "Not a Contribution."
72
+
73
+ "Contributor" shall mean Licensor and any individual or Legal Entity
74
+ on behalf of whom a Contribution has been received by Licensor and
75
+ subsequently incorporated within the Work.
76
+
77
+ 2. Grant of Copyright License. Subject to the terms and conditions of
78
+ this License, each Contributor hereby grants to You a perpetual,
79
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
80
+ copyright license to reproduce, prepare Derivative Works of,
81
+ publicly display, publicly perform, sublicense, and distribute the
82
+ Work and such Derivative Works in Source or Object form.
83
+
84
+ 3. Grant of Patent License. Subject to the terms and conditions of
85
+ this License, each Contributor hereby grants to You a perpetual,
86
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
87
+ (except as stated in this section) patent license to make, have made,
88
+ use, offer to sell, sell, import, and otherwise transfer the Work,
89
+ where such license applies only to those patent claims licensable
90
+ by such Contributor that are necessarily infringed by their
91
+ Contribution(s) alone or by combination of their Contribution(s)
92
+ with the Work to which such Contribution(s) was submitted. If You
93
+ institute patent litigation against any entity (including a
94
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
95
+ or a Contribution incorporated within the Work constitutes direct
96
+ or contributory patent infringement, then any patent licenses
97
+ granted to You under this License for that Work shall terminate
98
+ as of the date such litigation is filed.
99
+
100
+ 4. Redistribution. You may reproduce and distribute copies of the
101
+ Work or Derivative Works thereof in any medium, with or without
102
+ modifications, and in Source or Object form, provided that You
103
+ meet the following conditions:
104
+
105
+ (a) You must give any other recipients of the Work or
106
+ Derivative Works a copy of this License; and
107
+
108
+ (b) You must cause any modified files to carry prominent notices
109
+ stating that You changed the files; and
110
+
111
+ (c) You must retain, in the Source form of any Derivative Works
112
+ that You distribute, all copyright, patent, trademark, and
113
+ attribution notices from the Source form of the Work,
114
+ excluding those notices that do not pertain to any part of
115
+ the Derivative Works; and
116
+
117
+ (d) If the Work includes a "NOTICE" text file as part of its
118
+ distribution, then any Derivative Works that You distribute must
119
+ include a readable copy of the attribution notices contained
120
+ within such NOTICE file, excluding those notices that do not
121
+ pertain to any part of the Derivative Works, in at least one
122
+ of the following places: within a NOTICE text file distributed
123
+ as part of the Derivative Works; within the Source form or
124
+ documentation, if provided along with the Derivative Works; or,
125
+ within a display generated by the Derivative Works, if and
126
+ wherever such third-party notices normally appear. The contents
127
+ of the NOTICE file are for informational purposes only and
128
+ do not modify the License. You may add Your own attribution
129
+ notices within Derivative Works that You distribute, alongside
130
+ or as an addendum to the NOTICE text from the Work, provided
131
+ that such additional attribution notices cannot be construed
132
+ as modifying the License.
133
+
134
+ You may add Your own copyright statement to Your modifications and
135
+ may provide additional or different license terms and conditions
136
+ for use, reproduction, or distribution of Your modifications, or
137
+ for any such Derivative Works as a whole, provided Your use,
138
+ reproduction, and distribution of the Work otherwise complies with
139
+ the conditions stated in this License.
140
+
141
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
142
+ any Contribution intentionally submitted for inclusion in the Work
143
+ by You to the Licensor shall be under the terms and conditions of
144
+ this License, without any additional terms or conditions.
145
+ Notwithstanding the above, nothing herein shall supersede or modify
146
+ the terms of any separate license agreement you may have executed
147
+ with Licensor regarding such Contributions.
148
+
149
+ 6. Trademarks. This License does not grant permission to use the trade
150
+ names, trademarks, service marks, or product names of the Licensor,
151
+ except as required for reasonable and customary use in describing the
152
+ origin of the Work and reproducing the content of the NOTICE file.
153
+
154
+ 7. Disclaimer of Warranty. Unless required by applicable law or
155
+ agreed to in writing, Licensor provides the Work (and each
156
+ Contributor provides its Contributions) on an "AS IS" BASIS,
157
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
158
+ implied, including, without limitation, any warranties or conditions
159
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
160
+ PARTICULAR PURPOSE. You are solely responsible for determining the
161
+ appropriateness of using or redistributing the Work and assume any
162
+ risks associated with Your exercise of permissions under this License.
163
+
164
+ 8. Limitation of Liability. In no event and under no legal theory,
165
+ whether in tort (including negligence), contract, or otherwise,
166
+ unless required by applicable law (such as deliberate and grossly
167
+ negligent acts) or agreed to in writing, shall any Contributor be
168
+ liable to You for damages, including any direct, indirect, special,
169
+ incidental, or consequential damages of any character arising as a
170
+ result of this License or out of the use or inability to use the
171
+ Work (including but not limited to damages for loss of goodwill,
172
+ work stoppage, computer failure or malfunction, or any and all
173
+ other commercial damages or losses), even if such Contributor
174
+ has been advised of the possibility of such damages.
175
+
176
+ 9. Accepting Warranty or Additional Liability. While redistributing
177
+ the Work or Derivative Works thereof, You may choose to offer,
178
+ and charge a fee for, acceptance of support, warranty, indemnity,
179
+ or other liability obligations and/or rights consistent with this
180
+ License. However, in accepting such obligations, You may act only
181
+ on Your own behalf and on Your sole responsibility, not on behalf
182
+ of any other Contributor, and only if You agree to indemnify,
183
+ defend, and hold each Contributor harmless for any liability
184
+ incurred by, or claims asserted against, such Contributor by reason
185
+ of your accepting any such warranty or additional liability.
186
+
187
+ END OF TERMS AND CONDITIONS
188
+
189
+ APPENDIX: How to apply the Apache License to your work.
190
+
191
+ To apply the Apache License to your work, attach the following
192
+ boilerplate notice, with the fields enclosed by brackets "[]"
193
+ replaced with your own identifying information. (Don't include
194
+ the brackets!) The text should be enclosed in the appropriate
195
+ comment syntax for the file format. We also recommend that a
196
+ file or class name and description of purpose be included on the
197
+ same "printed page" as the copyright notice for easier
198
+ identification within third-party archives.
199
+
200
+ Copyright [yyyy] [name of copyright owner]
201
+
202
+ Licensed under the Apache License, Version 2.0 (the "License");
203
+ you may not use this file except in compliance with the License.
204
+ You may obtain a copy of the License at
205
+
206
+ http://www.apache.org/licenses/LICENSE-2.0
207
+
208
+ Unless required by applicable law or agreed to in writing, software
209
+ distributed under the License is distributed on an "AS IS" BASIS,
210
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
211
+ See the License for the specific language governing permissions and
212
+ limitations under the License.
213
+
214
+ ----------------------------------------------------------------------------
215
+
216
+ Some of the software is redistributed under the following license:
217
+ https://github.com/jwhitehorn/pi_piper/
218
+
219
+ Copyright (c) 2013, Jason Whitehorn All rights reserved.
220
+
221
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
222
+
223
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
224
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
225
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ frankenpins (0.2.0)
5
+ unobservable (>= 0.11.0)
6
+ wiringpi2 (>= 2.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ unobservable (0.11.0)
12
+ wiringpi2 (2.0.1)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ frankenpins!
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ Frankenpins
2
+ ===
3
+
4
+ A small library to make working with physical buttons on the Raspberry Pi easier.
5
+
6
+ Installation
7
+ ---
8
+
9
+ To use Frankenpins using Rubygems:
10
+
11
+ gem install frankenpins
12
+
13
+ If you're using Bundler, put the following in your `Gemfile`:
14
+
15
+ gem 'frankenpins'
16
+
17
+ Then run `sudo bundle install` to install the gem (see 'Sudo' below).
18
+
19
+ Sudo
20
+ ---
21
+
22
+ You need to run your code as `sudo` so it can access the GPIO pins.
23
+
24
+ $ sudo bundle exec bin/start
25
+
26
+ Pin numbering
27
+ ---
28
+
29
+ Frankenpins uses the WiringPi convention for pin numbering. The wiring pi set has a [useful chart](http://wiringpi.com/).
30
+
31
+ ![wiring pi pin numbering chart](http://wiringpi.com/wp-content/uploads/2013/03/gpio1.png)
32
+
33
+ Buttons
34
+ ---
35
+
36
+ Frankenpins is event-based.
37
+
38
+ require 'frankenpins'
39
+
40
+ button = Frankenpins::Button.new(:pin => 0, :pull => :up)
41
+ button.on :pressed do
42
+ puts "Button pressed"
43
+ end
44
+
45
+ Frankenpins.wait
46
+
47
+ First we create a new button, telling the library that the button is on wiring pi pin number `0`, and that the pin's internal pull-up resistor should be used:
48
+
49
+ button = Frankenpins::Button.new(:pin => 0, :pull => :up)
50
+
51
+ Then we register a block of code to be called whenever the button is pressed down:
52
+
53
+ button.on :pressed do
54
+ puts "Button pressed"
55
+ end
56
+
57
+ The button events are:
58
+
59
+ - `pressed` when the button is pushed down
60
+ - `released` when the button is released
61
+ - `changed` fires every time the button is pushed down or released
62
+
63
+ Finally, we tell Frankenpins to keep the program running until we quit it. Otherwise, it would exit and wouldn't be watching for our button presses.
64
+
65
+ Frankenpins.wait
66
+
67
+ Pins
68
+ ---
69
+
70
+ Frankenpins Pin event implementation is based on the great [Pi Piper](https://github.com/jwhitehorn/pi_piper) library, and uses a modified version of the Pin class which means you can register for events whenever a pin changes value. Check `examples/raw_pin.rb` for sample code.
71
+
72
+ Versioning
73
+ ---
74
+
75
+ We use [Semantic Versioning](http://semver.org/) and as this is pre-1.0.0 software, anything may change at any time.
76
+
77
+ Development
78
+ ---
79
+
80
+ It's very early days for this library so any help is gratefully received.
81
+
82
+ Raise an issue in Github to discuss what you'd like to see included.
@@ -0,0 +1,22 @@
1
+ require_relative '../lib/frankenpins'
2
+
3
+ # Create a new Button attached to Wiring Pi pin #0.
4
+ # By default, the built-in pull-up resistor will be used.
5
+ # To use your own external resistor, pass in :pull => :none
6
+ # as an option
7
+ button = Frankenpins::Button.new(:pin => 0)
8
+
9
+ button.on :pressed do
10
+ puts "Button pressed"
11
+ end
12
+
13
+ button.on :released do
14
+ puts "Button released"
15
+ end
16
+
17
+ button.on :changed do
18
+ puts "Button changed"
19
+ end
20
+
21
+ puts "Push the button..."
22
+ Frankenpins.wait
@@ -0,0 +1,15 @@
1
+ require_relative '../lib/frankenpins'
2
+
3
+ # Create a new Pin to control Wiring Pi pin #0
4
+ # We enable the built-in pull up/down resistors
5
+ # on the pin using the :pull => :up option, the
6
+ # default is :pull => :none
7
+ raw_pin = Frankenpins::Pin.new(:pin => 0, :pull => :up)
8
+ raw_pin.watch do |pin|
9
+ puts "Value changed #{pin.value}"
10
+ end
11
+
12
+ puts raw_pin
13
+
14
+ puts "Waiting...."
15
+ Frankenpins.wait
@@ -0,0 +1,22 @@
1
+ require_relative '../lib/frankenpins'
2
+
3
+ # Set up a rotary encoder that is connected to
4
+ # Wiring Pi pins #4 and #5 using a :pull => :up
5
+ # internal resistor by default. If you'd like to
6
+ # use external resistors on both pins, pass the
7
+ # option :pull => :none
8
+ encoder = Frankenpins::RotaryEncoder.new(:pin_a => 4, :pin_b => 5)
9
+
10
+ encoder.on :changed do |pos, direction|
11
+ puts "position: #{pos}, direction: #{direction}"
12
+ end
13
+
14
+ # Use the Button class to watch the switch part of the
15
+ # encoder, push down to activate
16
+ button = Frankenpins::Button.new(:pin => 1)
17
+ button.on :pressed do
18
+ puts "Button pressed"
19
+ end
20
+
21
+ puts "Rotate or push...."
22
+ Frankenpins.wait
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'frankenpins'
3
+ s.version = '0.2.1'
4
+ s.date = '2013-08-28'
5
+ s.summary = "Connect buttons etc to your Rapsberry Pi"
6
+ s.description = "Connect buttons etc to your Rapsberry Pi"
7
+ s.authors = ["Andrew Nicolaou"]
8
+ s.email = 'andrew.nicolaou@bbc.co.uk'
9
+ s.files = `git ls-files`.split($/)
10
+ s.homepage = 'https://github.com/radiodan/frankenpins'
11
+ s.license = 'Apache 2'
12
+
13
+ s.add_runtime_dependency "wiringpi2" , "~> 2.0.0"
14
+ s.add_runtime_dependency "unobservable", "~> 0.11.0"
15
+ end
@@ -0,0 +1,31 @@
1
+ require 'unobservable'
2
+
3
+ module Frankenpins
4
+ class Button
5
+ include Unobservable::Support
6
+
7
+ attr_event :pressed
8
+ attr_event :released
9
+ attr_event :changed
10
+
11
+ def initialize(options)
12
+ options[:pull] = :up unless options.has_key?(:pull)
13
+
14
+ @pin = Frankenpins::Pin.new(options)
15
+ @pin.watch do |pin|
16
+ if pin.value == 0
17
+ raise_event :pressed
18
+ elsif pin.value == 1
19
+ raise_event :released
20
+ end
21
+
22
+ # Always fire a changed event
23
+ raise_event :changed
24
+ end
25
+ end
26
+
27
+ def on(event_name, &block)
28
+ send(event_name).register(&block)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,126 @@
1
+ # A modified version of PiPiper's pin class
2
+ # https://github.com/jwhitehorn/pi_piper/blob/master/lib/pi_piper/pin.rb
3
+ #
4
+ require_relative './utils'
5
+ require_relative './wiring_pi_singleton'
6
+
7
+ module Frankenpins
8
+ # Represents a GPIO pin on the Raspberry Pi
9
+ class Pin
10
+ include Utils
11
+
12
+ INPUT = 0
13
+ PUD_OFF = 0
14
+ PUD_DOWN = 1
15
+ PUD_UP = 2
16
+
17
+ attr_reader :pin, :last_value, :value, :direction, :invert
18
+
19
+ def wiring_to_gpio(pin)
20
+ @io.wpi_pin_to_gpio(pin)
21
+ end
22
+
23
+ #Initializes a new GPIO pin.
24
+ #
25
+ # @param [Hash] options A hash of options
26
+ # @option options [Fixnum] :pin The pin number to initialize. Required.
27
+ # @option options [Symbol] :direction The direction of communication, either :in or :out. Defaults to :in.
28
+ # @option options [Boolean] :invert Indicates if the value read from the physical pin should be inverted. Defaults to false.
29
+ # @option options [Symbol] :trigger Indicates when the wait_for_change method will detect a change, either :rising, :falling, or :both edge triggers. Defaults to :both.
30
+ # @option options [Symbol] :pull Use an internal pull-up/down resistor, either :up, :down, :none
31
+ def initialize(options)
32
+ options = {:direction => :in, :invert => false, :trigger => :both, :pull => :none}.merge options
33
+ @io = WiringPiSingleton.gpio_instance
34
+
35
+ @wiring_pin = options[:pin]
36
+ @pin = wiring_to_gpio(options[:pin])
37
+ @direction = options[:direction]
38
+ @invert = options[:invert]
39
+ @trigger = options[:trigger]
40
+ @pull = options[:pull]
41
+
42
+ raise "Invalid direction. Options are :in or :out" unless [:in, :out].include? @direction
43
+ raise "Invalid trigger. Options are :rising, :falling, or :both" unless [:rising, :falling, :both].include? @trigger
44
+
45
+ File.open("/sys/class/gpio/export", "w") { |f| f.write("#{@pin}") }
46
+ File.open(direction_file, "w") { |f| f.write(@direction == :out ? "out" : "in") }
47
+
48
+ if @pull == :up || @pull == :down
49
+ direction = PUD_UP if @pull == :up
50
+ direction = PUD_DOWN if @pull == :down
51
+ @io.pull_up_dn_control(@wiring_pin, direction)
52
+ end
53
+
54
+ read
55
+ end
56
+
57
+ # If the pin has been initialized for output this method will set the logic level high.
58
+ def on
59
+ File.open(value_file, 'w') {|f| f.write("1") } if direction == :out
60
+ end
61
+
62
+ # Tests if the logic level is high.
63
+ def on?
64
+ not off?
65
+ end
66
+
67
+ # If the pin has been initialized for output this method will set the logic level low.
68
+ def off
69
+ File.open(value_file, 'w') {|f| f.write("0") } if direction == :out
70
+ end
71
+
72
+ # Tests if the logic level is low.
73
+ def off?
74
+ value == 0
75
+ end
76
+
77
+ # If the pin has been initialized for output this method will either raise or lower the logic level depending on `new_value`.
78
+ # @param [Object] new_value If false or 0 the pin will be set to off, otherwise on.
79
+ def update_value(new_value)
80
+ !new_value || new_value == 0 ? off : on
81
+ end
82
+
83
+ # Tests if the logic level has changed since the pin was last read.
84
+ def changed?
85
+ last_value != value
86
+ end
87
+
88
+ # blocks until a logic level change occurs. The initializer option `:trigger` modifies what edge this method will release on.
89
+ def wait_for_change
90
+ fd = File.open(value_file, "r")
91
+ File.open(edge_file, "w") { |f| f.write("both") }
92
+ loop do
93
+ fd.read
94
+ IO.select(nil, nil, [fd], nil)
95
+ read
96
+ if changed?
97
+ next if @trigger == :rising and value == 0
98
+ next if @trigger == :falling and value == 1
99
+ break
100
+ end
101
+ end
102
+ end
103
+
104
+ # Reads the current value from the pin. Without calling this method first, `value`, `last_value` and `changed?` will not be updated.
105
+ # In short, you must call this method if you are curious about the current state of the pin.
106
+ def read
107
+ @last_value = @value
108
+ val = File.read(value_file).to_i
109
+ @value = invert ? (val ^ 1) : val
110
+ end
111
+
112
+ private
113
+ def value_file
114
+ "/sys/class/gpio/gpio#{pin}/value"
115
+ end
116
+
117
+ def edge_file
118
+ "/sys/class/gpio/gpio#{pin}/edge"
119
+ end
120
+
121
+ def direction_file
122
+ "/sys/class/gpio/gpio#{pin}/direction"
123
+ end
124
+
125
+ end
126
+ end
@@ -0,0 +1,55 @@
1
+ require 'unobservable'
2
+
3
+ module Frankenpins
4
+ class RotaryEncoder
5
+ include Unobservable::Support
6
+
7
+ attr_event :changed
8
+
9
+ def initialize(options)
10
+ @pin_a, @pin_b = create_pins_from_options_hash!(options)
11
+
12
+ @pin_a.watch { update }
13
+ @pin_b.watch { update }
14
+
15
+ @position = 0
16
+ @last_val = 0
17
+ end
18
+
19
+ def update
20
+ @pin_a.read
21
+ msb = @pin_a.value
22
+
23
+ @pin_b.read
24
+ lsb = @pin_b.value
25
+
26
+ encoded = (msb << 1) | lsb;
27
+ sum = (@last_val << 2) | encoded;
28
+
29
+ if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011)
30
+ @position += 1
31
+ raise_event :changed, @position, :clockwise
32
+ end
33
+ if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000)
34
+ @position -= 1
35
+ raise_event :changed, @position, :anticlockwise
36
+ end
37
+
38
+ @last_val = encoded
39
+ end
40
+
41
+ def on(event_name, &block)
42
+ send(event_name).register(&block)
43
+ end
44
+
45
+ private
46
+ def create_pins_from_options_hash!(options)
47
+ options[:pull] = :up unless options.has_key?(:pull)
48
+ pin_a_num = options.delete(:pin_a)
49
+ pin_b_num = options.delete(:pin_b)
50
+ pin_a = Frankenpins::Pin.new(options.merge(:pin => pin_a_num))
51
+ pin_b = Frankenpins::Pin.new(options.merge(:pin => pin_b_num))
52
+ return pin_a, pin_b
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,17 @@
1
+ module Frankenpins
2
+ module Utils
3
+ #Prevents the main thread from exiting. Required when using PiPiper.watch
4
+ def wait
5
+ loop do sleep 1 end
6
+ end
7
+
8
+ def watch(&block)
9
+ Thread.new do
10
+ loop do
11
+ wait_for_change
12
+ block.call(self)
13
+ end
14
+ end.abort_on_exception = true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'wiringpi2'
2
+
3
+ module Frankenpins
4
+ class WiringPiSingleton
5
+ @@gpio_instance = WiringPi::GPIO.new
6
+
7
+ def self.gpio_instance
8
+ @@gpio_instance
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ require_relative 'frankenpins/utils'
3
+ require_relative 'frankenpins/wiring_pi_singleton'
4
+ require_relative 'frankenpins/pin'
5
+ require_relative 'frankenpins/button'
6
+ require_relative 'frankenpins/rotary_encoder'
7
+
8
+ module Frankenpins
9
+ include Utils
10
+ module_function :wait
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'bundler/setup'
2
+ require 'wiringpi2'
3
+ require_relative '../lib/things'
4
+
5
+ io = WiringPi::GPIO.new
6
+ io.pin_mode(0, 0)
7
+
8
+
9
+ io.interrupt(0, :rising_edge) do |value|
10
+ puts "Value changed #{value}"
11
+ end
12
+
13
+ Things.wait
@@ -0,0 +1,22 @@
1
+ require 'bundler/setup'
2
+ require 'wiringpi2'
3
+
4
+ INPUT = 0
5
+ PUD_UP = 2
6
+ INT_EDGE_BOTH = 3
7
+
8
+ pin = 0
9
+
10
+ io = WiringPi::GPIO.new
11
+ io.pin_mode(pin,INPUT)
12
+ io.pullUpDnControl(pin, PUD_UP)
13
+ io.wiringpi_isr(pin, INT_EDGE_BOTH) do
14
+ puts "in block"
15
+ end
16
+
17
+ puts "before while"
18
+
19
+ while (true)
20
+ puts io.digital_read(pin)
21
+ sleep(0.5)
22
+ end
data/scratch/simple.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler/setup'
2
+ require 'wiringpi2'
3
+
4
+ INPUT = 0
5
+ PUD_UP = 2
6
+
7
+ pin = 0
8
+
9
+ io = WiringPi::GPIO.new
10
+ io.pin_mode(pin,INPUT)
11
+ io.pullUpDnControl(pin, PUD_UP)
12
+
13
+ while (true)
14
+ puts io.digital_read(pin)
15
+ sleep(0.5)
16
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: frankenpins
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Nicolaou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: wiringpi2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: unobservable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.11.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.11.0
41
+ description: Connect buttons etc to your Rapsberry Pi
42
+ email: andrew.nicolaou@bbc.co.uk
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - AUTHORS
48
+ - COPYING
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - README.md
52
+ - examples/button.rb
53
+ - examples/raw_pin.rb
54
+ - examples/rotary_encoder.rb
55
+ - frankenpins.gemspec
56
+ - lib/frankenpins.rb
57
+ - lib/frankenpins/button.rb
58
+ - lib/frankenpins/pin.rb
59
+ - lib/frankenpins/rotary_encoder.rb
60
+ - lib/frankenpins/utils.rb
61
+ - lib/frankenpins/wiring_pi_singleton.rb
62
+ - scratch/button-wiring.rb
63
+ - scratch/interrupt.rb
64
+ - scratch/simple.rb
65
+ homepage: https://github.com/radiodan/frankenpins
66
+ licenses:
67
+ - Apache 2
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.1.10
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Connect buttons etc to your Rapsberry Pi
89
+ test_files: []
90
+ has_rdoc: