pi_piper 1.2.1 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -6,4 +6,3 @@ lib/pi_piper/bcm2835.rb
6
6
  lib/pi_piper/libbcm2835.img
7
7
  lib/pi_piper/pin.rb
8
8
  lib/pi_piper/spi.rb
9
- pi_piper.gemspec
data/README.md CHANGED
@@ -7,9 +7,10 @@ Pi Piper brings event driven programming to the Raspberry Pi's GPIO pins. To get
7
7
  ### GPIO
8
8
  ```ruby
9
9
  require 'pi_piper'
10
+ include PiPiper
10
11
 
11
- PiPiper.watch :pin => 23 do |pin|
12
- puts "Pin changed from #{pin.last_value} to #{pin.value}"
12
+ watch :pin => 23 do
13
+ puts "Pin changed from #{last_value} to #{value}"
13
14
  end
14
15
 
15
16
  PiPiper.wait
@@ -36,6 +37,9 @@ PiPiper::Spi.begin do |spi|
36
37
  end
37
38
  ```
38
39
 
40
+ ## Documentation
41
+
42
+ API documentation for Pi Piper can be found at [rdoc.info](http://rdoc.info/github/jwhitehorn/pi_piper/frames/).
39
43
 
40
44
  ## Example projects
41
45
 
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('pi_piper', '1.2.1') do |p|
5
+ Echoe.new('pi_piper', '1.3') do |p|
6
6
  p.description = "Event driven Raspberry Pi GPIO library"
7
7
  p.url = "http://github.com/jwhitehorn/pi_piper"
8
8
  p.author = "Jason Whitehorn"
data/lib/pi_piper.rb CHANGED
@@ -5,18 +5,40 @@ end
5
5
  PiPiper::Bcm2835.init
6
6
 
7
7
  module PiPiper
8
-
9
- def PiPiper.watch(options)
8
+ extend self
9
+
10
+ #Defines an event block to be executed when an pin event occurs.
11
+ #
12
+ # == Parameters:
13
+ # options:
14
+ # Options hash. Options include `:pin`, `:invert` and `:trigger`.
15
+ #
16
+ def watch(options, &block)
10
17
  Thread.new do
11
18
  pin = PiPiper::Pin.new(options)
12
19
  loop do
13
20
  pin.wait_for_change
14
- yield pin
21
+ if block.arity > 0
22
+ block.call pin
23
+ else
24
+ pin.instance_exec &block
25
+ end
15
26
  end
16
27
  end.abort_on_exception = true
17
28
  end
29
+
30
+ #Defines an event block to be executed after a pin either goes high or low.
31
+ #
32
+ # @param [Hash] options A hash of options
33
+ # @option options [Fixnum] :pin The pin number to initialize. Required.
34
+ # @option options [Symbol] :goes The event to watch for. Either :high or :low. Required.
35
+ def after(options, &block)
36
+ options[:trigger] = options.delete(:goes) == :high ? :rising : :falling
37
+ watch options, &block
38
+ end
18
39
 
19
- def PiPiper.wait
40
+ #Prevents the main thread from exiting. Required when using PiPiper.watch
41
+ def wait
20
42
  loop do sleep 1 end
21
43
  end
22
44
 
@@ -1,6 +1,8 @@
1
1
  require 'ffi'
2
2
 
3
3
  module PiPiper
4
+ # The Bcm2835 module is not intended to be directly called.
5
+ # It serves as an FFI library for PiPiper::SPI.
4
6
  module Bcm2835
5
7
  extend FFI::Library
6
8
  ffi_lib File.dirname(__FILE__) + '/libbcm2835.img'
data/lib/pi_piper/pin.rb CHANGED
@@ -1,7 +1,15 @@
1
1
  module PiPiper
2
+ # Represents a GPIO pin on the Raspberry Pi
2
3
  class Pin
3
4
  attr_reader :pin, :last_value, :value, :direction, :invert
4
5
 
6
+ #Initializes a new GPIO pin.
7
+ #
8
+ # @param [Hash] options A hash of options
9
+ # @option options [Fixnum] :pin The pin number to initialize. Required.
10
+ # @option options [Symbol] :direction The direction of communication, either :in or :out. Defaults to :in.
11
+ # @option options [Boolean] :invert Indicates if the value read from the physical pin should be inverted. Defaults to false.
12
+ # @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.
5
13
  def initialize(options)
6
14
  options = {:direction => :in, :invert => false, :trigger => :both}.merge options
7
15
  @pin = options[:pin]
@@ -18,30 +26,38 @@ module PiPiper
18
26
  read
19
27
  end
20
28
 
29
+ # If the pin has been initialized for output this method will set the logic level high.
21
30
  def on
22
31
  File.open(value_file, 'w') {|f| f.write("1") } if direction == :out
23
32
  end
24
33
 
34
+ # Tests if the logic level is high.
25
35
  def on?
26
36
  not off?
27
37
  end
28
38
 
39
+ # If the pin has been initialized for output this method will set the logic level low.
29
40
  def off
30
41
  File.open(value_file, 'w') {|f| f.write("0") } if direction == :out
31
42
  end
32
43
 
44
+ # Tests if the logic level is low.
33
45
  def off?
34
46
  value == 0
35
47
  end
36
48
 
49
+ # If the pin has been initialized for output this method will either raise or lower the logic level depending on `new_value`.
50
+ # @param [Object] new_value If false or 0 the pin will be set to off, otherwise on.
37
51
  def update_value(new_value)
38
52
  !new_value || new_value == 0 ? off : on
39
53
  end
40
54
 
55
+ # Tests if the logic level has changed since the pin was last read.
41
56
  def changed?
42
57
  last_value != value
43
58
  end
44
59
 
60
+ # blocks until a logic level change occurs. The initializer option `:trigger` modifies what edge this method will release on.
45
61
  def wait_for_change
46
62
  fd = File.open(value_file, "r")
47
63
  File.open(edge_file, "w") { |f| f.write("both") }
@@ -56,7 +72,9 @@ module PiPiper
56
72
  end
57
73
  end
58
74
  end
59
-
75
+
76
+ # Reads the current value from the pin. Without calling this method first, `value`, `last_value` and `changed?` will not be updated.
77
+ # In short, you must call this method if you are curious about the current state of the pin.
60
78
  def read
61
79
  @last_value = @value
62
80
  val = File.read(value_file).to_i
data/lib/pi_piper/spi.rb CHANGED
@@ -1,32 +1,38 @@
1
- =begin
2
- Modifications Copyright 2013, Jason Whitehorn and released under the terms
3
- of the license included in README.md
4
-
5
- Based on works, Copyright (c) 2012 Joshua Nussbaum
6
-
7
- MIT License
8
-
9
- Permission is hereby granted, free of charge, to any person obtaining
10
- a copy of this software and associated documentation files (the
11
- "Software"), to deal in the Software without restriction, including
12
- without limitation the rights to use, copy, modify, merge, publish,
13
- distribute, sublicense, and/or sell copies of the Software, and to
14
- permit persons to whom the Software is furnished to do so, subject to
15
- the following conditions:
16
-
17
- The above copyright notice and this permission notice shall be
18
- included in all copies or substantial portions of the Software.
19
-
20
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
- =end
1
+ #--
2
+ #Modifications Copyright 2013, Jason Whitehorn and released under the terms
3
+ #of the license included in README.md
4
+ #
5
+ #Based on works, Copyright (c) 2012 Joshua Nussbaum
6
+ #
7
+ #MIT License
8
+ #
9
+ #Permission is hereby granted, free of charge, to any person obtaining
10
+ #a copy of this software and associated documentation files (the
11
+ #"Software"), to deal in the Software without restriction, including
12
+ #without limitation the rights to use, copy, modify, merge, publish,
13
+ #distribute, sublicense, and/or sell copies of the Software, and to
14
+ #
15
+ #permit persons to whom the Software is furnished to do so, subject to
16
+ #the following conditions:
17
+ #
18
+ #The above copyright notice and this permission notice shall be
19
+ #included in all copies or substantial portions of the Software.
20
+ #
21
+ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
+ #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
+ #MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
+ #NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
+ #LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
+ #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
+ #WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
+ #++
29
+
30
+
31
+
32
+
28
33
 
29
34
  module PiPiper
35
+ # class for SPI interfaces on the Raspberry Pi
30
36
  class Spi
31
37
  # 65536 = 256us = 4kHz
32
38
  CLOCK_DIVIDER_65536 = 0
@@ -69,6 +75,7 @@ module PiPiper
69
75
  # No CS, control it yourself
70
76
  CHIP_SELECT_NONE = 3
71
77
 
78
+ #Sets the SPI mode. Defaults to mode (0,0).
72
79
  def self.set_mode(cpol, cpha)
73
80
  mode = SPI_MODE0 #default
74
81
  mode = SPI_MODE1 if cpol == 0 and cpha == 1
@@ -77,6 +84,7 @@ module PiPiper
77
84
  Bcma2835.spi_set_data_mode mode
78
85
  end
79
86
 
87
+ #Begin an SPI block. All SPI communications should be wrapped in a block.
80
88
  def self.begin(chip=nil)
81
89
  Bcm2835.spi_begin
82
90
  chip = CHIP_SELECT_0 if !chip && block_given?
@@ -190,7 +198,7 @@ module PiPiper
190
198
  # @example Write multiple bytes
191
199
  # spi.write(0x22, 0x33, 0x44)
192
200
  #
193
- # @return [Number|Array|String] data that came out of MISO during write
201
+ # @return [Number|Array] data that came out of MISO during write
194
202
  def write(*args)
195
203
  case args.count
196
204
  when 0
data/pi_piper.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "pi_piper"
5
- s.version = "1.2.1"
5
+ s.version = "1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jason Whitehorn"]
9
- s.date = "2013-01-22"
9
+ s.date = "2013-01-28"
10
10
  s.description = "Event driven Raspberry Pi GPIO library"
11
11
  s.email = "jason.whitehorn@gmail.com"
12
12
  s.extra_rdoc_files = ["README.md", "lib/pi_piper.rb", "lib/pi_piper/bcm2835.rb", "lib/pi_piper/libbcm2835.img", "lib/pi_piper/pin.rb", "lib/pi_piper/spi.rb"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pi_piper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: '1.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-22 00:00:00.000000000 Z
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi