pi_piper 1.3.2 → 1.9.9

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 822b5150bab4929fca37ae9777209d9445cd663e
4
- data.tar.gz: 84a0cd3979800a0ed62ce15fdeab0e9722ffa9b4
3
+ metadata.gz: 13fdeb8e5268450e6f5e16b76fadd8c86c21d14e
4
+ data.tar.gz: 0cf0fc4609e69c32a9f2fe67eb6b581a9959d021
5
5
  SHA512:
6
- metadata.gz: d7b461f1e2ddae7fb25774cc66c9dcffcf9577dc8429c4020d8915fe96aa161713fa72e2db02d4a9169356b6036a982d86016721bd52e4f2c018b04a07140f1e
7
- data.tar.gz: 96d61bf27a05ad1f305058ff283272c56651b9d33594bc64d1dcfd1a69dcb4c4897f163cba1d9ea6fc8472d114a065e515907797efffeeb4955e2eaab94e536e
6
+ metadata.gz: 87894270c282f9368be74074bd7872cd5fd3ce9e26f961cb5cace37a8599cb8ac5aef5c4fcd03368274800cb37da9a10d21012cae8b2a8db0461e3577b77f6f8
7
+ data.tar.gz: 51bc8cec9b89c1005881a20277543242e458c56c7e2e495daa32d46bdff5af3ab5671b7553d7ce66904d6d121ad76bccae618d2f4d65790d6524b1d332920c6c
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.swp
3
+ *.gem
4
+ coverage/*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+
6
+ before_install:
7
+ - gem install bundler
8
+ - gem update
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'echoe'
4
+ gem 'rake'
5
+ gem 'ffi'
6
+ gem "eventmachine", "~> 1.0.9"
7
+
8
+ gemspec
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pi_piper (2.0.beta.11)
5
+ eventmachine (= 1.0.9)
6
+ ffi
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ allison (2.0.3)
12
+ diff-lcs (1.2.4)
13
+ echoe (4.6.3)
14
+ allison (>= 2.0.3)
15
+ gemcutter (>= 0.7.0)
16
+ rake (>= 0.9.2)
17
+ rdoc (>= 3.6.1)
18
+ rubyforge (>= 2.0.4)
19
+ eventmachine (1.0.9)
20
+ ffi (1.9.0)
21
+ gemcutter (0.7.1)
22
+ json (1.8.1)
23
+ json_pure (1.8.1)
24
+ metaclass (0.0.1)
25
+ mocha (0.14.0)
26
+ metaclass (~> 0.0.1)
27
+ multi_json (1.8.2)
28
+ rake (10.1.0)
29
+ rdoc (4.0.1)
30
+ json (~> 1.4)
31
+ rspec (3.4.0)
32
+ rspec-core (~> 3.4.0)
33
+ rspec-expectations (~> 3.4.0)
34
+ rspec-mocks (~> 3.4.0)
35
+ rspec-core (3.4.1)
36
+ rspec-support (~> 3.4.0)
37
+ rspec-expectations (3.4.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.4.0)
40
+ rspec-mocks (3.4.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.4.0)
43
+ rspec-support (3.4.1)
44
+ rubyforge (2.0.4)
45
+ json_pure (>= 1.1.7)
46
+ simplecov (0.7.1)
47
+ multi_json (~> 1.0)
48
+ simplecov-html (~> 0.7.1)
49
+ simplecov-html (0.7.1)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ echoe
56
+ eventmachine (~> 1.0.9)
57
+ ffi
58
+ mocha
59
+ pi_piper!
60
+ rake
61
+ rspec
62
+ simplecov
63
+
64
+ BUNDLED WITH
65
+ 1.11.2
data/Manifest CHANGED
@@ -1,9 +1,14 @@
1
+ Gemfile
2
+ Gemfile.lock
1
3
  Manifest
2
4
  README.md
3
5
  Rakefile
4
6
  lib/pi_piper.rb
5
7
  lib/pi_piper/bcm2835.rb
8
+ lib/pi_piper/frequency.rb
9
+ lib/pi_piper/i2c.rb
6
10
  lib/pi_piper/libbcm2835.img
7
11
  lib/pi_piper/pin.rb
12
+ lib/pi_piper/platform.rb
8
13
  lib/pi_piper/spi.rb
9
14
  pi_piper.gemspec
data/README.md CHANGED
@@ -1,14 +1,16 @@
1
1
  ## Overview
2
2
 
3
- Pi Piper brings event driven programming to the Raspberry Pi's GPIO pins. Pi Piper works with all revisions of the Raspberry Pi,
4
- and has been tested with Ruby 1.9.3 under both [Raspbian "wheezy"](http://www.raspberrypi.org/downloads) and [Occidentalis v0.2](http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-2).
3
+ [![Build Status](https://travis-ci.org/jwhitehorn/pi_piper.png)](https://travis-ci.org/jwhitehorn/pi_piper)
4
+
5
+ Pi Piper brings event driven programming to the Raspberry Pi's GPIO pins. Pi Piper works with all revisions of the Raspberry Pi,
6
+ and has been tested with Ruby 1.9.3 & 2.0 under both [Raspbian "wheezy"](http://www.raspberrypi.org/downloads) and [Occidentalis v0.2](http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-2).
5
7
 
6
8
  To get started:
7
9
 
8
10
  If you do not already have Ruby installed, first you'll need to:
9
-
11
+
10
12
  sudo apt-get install ruby ruby1.9.1-dev
11
-
13
+
12
14
  Despite one of the packages being titled "ruby1.9.1-dev", the above command will install Ruby 1.9.3 (as of January 2013) and the Ruby dev tools.
13
15
 
14
16
  To install Pi Piper:
@@ -38,6 +40,22 @@ PiPiper.wait
38
40
 
39
41
  Your block will be called when a change to the pin's state is detected.
40
42
 
43
+ When using pins as input, you can use internal resistors to pull the pin
44
+ up or pull down. This is important if you use open-collector sensors
45
+ which have floating output in some states.
46
+
47
+ You can set resistors when creating a pin passing a :pull parameter
48
+ (which can be :up, :down or :off, which is the default).
49
+
50
+ ```ruby
51
+ pin = PiPiper::Pin.new(:pin => 17, :direction => :in, :pull => :up)
52
+ ```
53
+
54
+ This way, the pin will always return 'on' if it is unconnected or of the
55
+ sensor has an open collector output.
56
+
57
+ You can later alter the pulling resistors using PiPiper#pull!
58
+
41
59
  Additionally you can use pins as output too:
42
60
 
43
61
  ```ruby
@@ -47,15 +65,26 @@ sleep 1
47
65
  pin.off
48
66
  ```
49
67
 
68
+ _please note, in the above context "pin" refers to the GPIO number of the Raspberry Pi._
69
+
50
70
  ### SPI
51
- Starting with version 1.2, Pi Piper offers SPI support.
71
+ Starting with version 1.2, Pi Piper offers SPI support.
52
72
 
53
73
  ```ruby
54
- PiPiper::Spi.begin do
55
- puts write [0x01, 0x80, 0x00]
74
+ PiPiper::Spi.begin do
75
+ puts write [0x01, 0x80, 0x00]
56
76
  end
57
77
  ```
58
78
 
79
+ If you are using an operating system that supports `/dev/spidev0.0` like the [adafruit
80
+ distro][adafruit-linux] you can also write to the spi using `PiPiper::Spi.spidev_out`
81
+
82
+ ```ruby
83
+ # Example writing red, green, blue to a string of WS2801 pixels
84
+ PiPiper::Spi.spidev_out([255,0,0,0,255,0,0,0,255])
85
+ ```
86
+ [adafruit-linux]:http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/overview
87
+
59
88
  ## Documentation
60
89
 
61
90
  API documentation for Pi Piper can be found at [rdoc.info](http://rdoc.info/github/jwhitehorn/pi_piper/frames/).
@@ -69,9 +98,26 @@ Looking for more examples/sample code for Pi Piper? Then check out the following
69
98
  * [Project 3: 2-bit counter](https://github.com/jwhitehorn/pi_piper/wiki/Project-3:-2-bit-counter)
70
99
  * [Project 4: MCP3008](https://github.com/jwhitehorn/pi_piper/wiki/Project-4:-MCP3008)
71
100
 
101
+ ## Under the hood
102
+
103
+ PiPiper use the libbcm2835 library from Mike McCauley at airspayce.
104
+
105
+ http://www.airspayce.com/mikem/bcm2835/index.html
106
+
107
+ if you want to upgrade or downgrade the library for compatibility reason, get it and make it a shared object library :
108
+
109
+ ```script
110
+ wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.49.tar.gz
111
+ tar zxvf bcm2835-1.49.tar.gz && cd bcm2835-1.49
112
+ ./configure && make
113
+ sudo make check
114
+ sudo make install
115
+ cd src && cc -shared bcm2835.o -o libbcm2835.so
116
+ cp libbcm2835.so ~/pi_piper/lib/pi_piper
117
+
72
118
  ## License
73
119
 
74
- Copyright (c) 2013, [Jason Whitehorn](https://github.com/jwhitehorn)
120
+ Copyright (c) 2013, [Jason Whitehorn](https://github.com/jwhitehorn)
75
121
  All rights reserved.
76
122
 
77
123
  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
data/Rakefile CHANGED
@@ -1,14 +1,24 @@
1
- require 'rubygems'
2
- require 'rake'
3
- require 'echoe'
4
-
5
- Echoe.new('pi_piper', '1.3.2') do |p|
6
- p.description = "Event driven Raspberry Pi GPIO library"
7
- p.url = "http://github.com/jwhitehorn/pi_piper"
8
- p.author = "Jason Whitehorn"
9
- p.email = "jason.whitehorn@gmail.com"
10
- p.ignore_pattern = ["examples/**/*"]
11
- p.dependencies = ['ffi']
12
- end
13
-
14
- Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+ require 'rspec/core/rake_task'
5
+
6
+ #rake manifest
7
+ #rake build_gemspec
8
+ #gem build pi_piper.gemspec
9
+ #gem push xxx.gem
10
+
11
+ Echoe.new('pi_piper', '2.0.beta.4') do |p|
12
+ p.description = "Event driven Raspberry Pi GPIO library"
13
+ p.url = "http://github.com/jwhitehorn/pi_piper"
14
+ p.author = "Jason Whitehorn"
15
+ p.email = "jason.whitehorn@gmail.com"
16
+ p.ignore_pattern = ["examples/**/*", "spec/**/*"]
17
+ p.dependencies = ['ffi', 'eventmachine 1.0.3']
18
+ end
19
+
20
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
21
+
22
+ RSpec::Core::RakeTask.new(:spec)
23
+
24
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ require 'pi_piper'
2
+
3
+ puts "Press the switch to get started"
4
+ pin17 = PiPiper::Pin.new(:pin => 17, :direction => :out)
5
+ pin27 = PiPiper::Pin.new(:pin => 27, :direction => :out)
6
+
7
+ pin17.off
8
+ pin27.off
9
+
10
+ sum = 0
11
+
12
+ PiPiper.watch :pin => 22, :trigger => :rising do |pin|
13
+ sum = sum + 1
14
+ display = sum % 4
15
+ puts sum
16
+
17
+ pin17.update_value(display == 2 || display == 3)
18
+ pin27.update_value(display == 1 || display == 3)
19
+ end
20
+
21
+ PiPiper.wait
22
+
@@ -0,0 +1,37 @@
1
+ require 'pi_piper'
2
+
3
+ s1 = PiPiper::Pin.new(:direction => :out, :pin => 27)
4
+ s2 = PiPiper::Pin.new(:direction => :out, :pin => 24)
5
+ s3 = PiPiper::Pin.new(:direction => :out, :pin => 23)
6
+ s4 = PiPiper::Pin.new(:direction => :out, :pin => 25)
7
+ s5 = PiPiper::Pin.new(:direction => :out, :pin => 18)
8
+ s6 = PiPiper::Pin.new(:direction => :out, :pin => 22)
9
+ s7 = PiPiper::Pin.new(:direction => :out, :pin => 17)
10
+
11
+
12
+ pins = [s1, s2, s3, s4, s5, s6, s7]
13
+
14
+ zero = Proc.new { s1.on; s2.on; s3.on; s4.on; s5.on; s6.on; s7.off; }
15
+ one = Proc.new { s1.off; s2.off; s3.on; s4.on; s5.off; s6.off; s7.off; }
16
+ two = Proc.new { s1.off; s2.on; s3.on; s4.off; s5.on; s6.on; s7.on; }
17
+ three = Proc.new { s1.off; s2.on; s3.on; s4.on; s5.on; s6.off; s7.on; }
18
+ four = Proc.new { s1.on; s2.off; s3.on; s4.on;; s5.off; s6.off; s7.on; }
19
+ five = Proc.new { s1.on; s2.on; s3.off; s4.on; s5.on; s6.off; s7.on; }
20
+ six = Proc.new { s1.on; s2.on; s3.off; s4.on; s5.on; s6.on; s7.on; }
21
+ seven = Proc.new { s1.off; s2.on; s3.on; s4.on; s5.off; s6.off; s7.off; }
22
+ eight = Proc.new { s1.on; s2.on; s3.on; s4.on; s5.on; s6.on; s7.on; }
23
+ nine = Proc.new { s1.on; s2.on; s3.on; s4.on; s5.off; s6.off; s7.on; }
24
+
25
+ numbers = [zero, one, two, three, four, five, six, seven, eight, nine]
26
+
27
+ pins.each { |p| p.off }
28
+
29
+ PiPiper.watch :pin => 4, :trigger => :rising do
30
+ (0..250).each do
31
+ pins.each { |p| p.update_value(Random.rand(2) == 1) }
32
+ end
33
+ number = Random.rand(10)
34
+ numbers[number].call
35
+ end
36
+
37
+ PiPiper.wait
@@ -0,0 +1,15 @@
1
+ require 'pi_piper'
2
+ include PiPiper
3
+
4
+ puts "Press the switch to get started"
5
+
6
+ watch :pin => 17, :invert => true do
7
+ puts "Pin changed from #{last_value} to #{value}"
8
+ end
9
+
10
+ after :pin => 17, :goes => :high do
11
+ puts "ouch!"
12
+ end
13
+
14
+ PiPiper.wait
15
+
@@ -0,0 +1,55 @@
1
+ require 'pi_piper'
2
+ #port of the Adafruit MCP3008 interface code found @ http://learn.adafruit.com/send-raspberry-pi-data-to-cosm/python-script
3
+
4
+ def read_adc(adc_pin, clockpin, adc_in, adc_out, cspin)
5
+ cspin.on
6
+ clockpin.off
7
+ cspin.off
8
+
9
+ command_out = adc_pin
10
+ command_out |= 0x18
11
+ command_out <<= 3
12
+
13
+ (0..4).each do
14
+ adc_in.update_value((command_out & 0x80) > 0)
15
+ command_out <<= 1
16
+ clockpin.on
17
+ clockpin.off
18
+ end
19
+ result = 0
20
+
21
+ (0..11).each do
22
+ clockpin.on
23
+ clockpin.off
24
+ result <<= 1
25
+ adc_out.read
26
+ if adc_out.on?
27
+ result |= 0x1
28
+ end
29
+ end
30
+
31
+ cspin.on
32
+
33
+ result >> 1
34
+ end
35
+
36
+ clock = PiPiper::Pin.new :pin => 18, :direction => :out
37
+ adc_out = PiPiper::Pin.new :pin => 23
38
+ adc_in = PiPiper::Pin.new :pin => 24, :direction => :out
39
+ cs = PiPiper::Pin.new :pin => 25, :direction => :out
40
+
41
+ adc_pin = 0
42
+
43
+ loop do
44
+ value = read_adc(adc_pin, clock, adc_in, adc_out, cs)
45
+ invert = 1023 - value
46
+ mvolts = invert * (3300.0 / 1023.0)
47
+ if mvolts < 2700
48
+ temp = (mvolts - 380.0) / (2320.0 / 84.0)
49
+ else
50
+ temp = (mvolts - 2700.0) / (390.0 / 92.0) + 84.0
51
+ end
52
+ temp_f = (temp * 9.0 / 5.0) + 32
53
+ puts "Value = #{value}, invert = #{invert}, mvolts = #{mvolts}, temp = #{temp} C | #{temp_f} F"
54
+ sleep 1
55
+ end
@@ -0,0 +1,24 @@
1
+ require 'pi_piper'
2
+ #special thanks to Jeremy Blythe, and his article @ http://jeremyblythe.blogspot.com/2012/09/raspberry-pi-hardware-spi-analog-inputs.html
3
+ #it greatly helped in getting the MCP3008 setup with SPI
4
+
5
+ adc_num = 0
6
+
7
+ loop do
8
+ value = 0
9
+ PiPiper::Spi.begin do |spi|
10
+ raw = spi.write [1, (8+adc_num)<<4, 0]
11
+ value = ((raw[1]&3) << 8) + raw[2]
12
+ end
13
+
14
+ invert = 1023 - value
15
+ mvolts = invert * (3300.0 / 1023.0)
16
+ if mvolts < 2700
17
+ temp = (mvolts - 380.0) / (2320.0 / 84.0)
18
+ else
19
+ temp = (mvolts - 2700.0) / (390.0 / 92.0) + 84.0
20
+ end
21
+ temp_f = (temp * 9.0 / 5.0) + 32
22
+ puts "Value = #{value}, invert = #{invert}, mvolts = #{mvolts}, temp = #{temp} C | #{temp_f} F"
23
+ sleep 1
24
+ end
@@ -0,0 +1,43 @@
1
+ require 'pi_piper'
2
+
3
+ unit = 0.1
4
+ dot = unit
5
+ dash = unit * 3
6
+ inter_element_gap = unit
7
+ short_gap = unit * 3
8
+ medium_gap = unit * 7
9
+
10
+ #http://en.wikipedia.org/wiki/Morse_code
11
+ character_timing = { "a" => [dot, dash], "b" => [dash, dot, dot, dot], "c" => [dash, dot, dash, dot],
12
+ "d" => [dash, dot, dot], "e" => [dot], "f" => [dot, dot, dash, dot],
13
+ "g" => [dash, dash, dot], "h" => [dot, dot, dot, dot], "i" => [dot, dot],
14
+ "j" => [dot, dash, dash, dash], "k" => [dash, dot, dash], "l" => [dot, dash, dot, dot],
15
+ "m" => [dash, dash], "n" => [dash, dot], "o" => [dash, dash, dash],
16
+ "p" => [dot, dash, dash, dot], "q" => [dash, dash, dot, dash], "r" => [dot, dash, dot],
17
+ "s" => [dot, dot, dot], "t" => [dash], "u" => [dot, dot, dash],
18
+ "v" => [dot, dot, dot, dash], "w" => [dot, dash, dash], "x" => [dash, dot, dot, dash],
19
+ "y" => [dash, dot, dash, dash], "z" => [dash, dash, dot, dot]}
20
+
21
+ pin = PiPiper::Pin.new(:pin => 17, :direction => :out)
22
+ pin.off
23
+
24
+ loop do
25
+ puts "Please type something"
26
+ something = gets.chomp.downcase
27
+
28
+ something.each_char do |letter|
29
+ if letter == " "
30
+ pin.off
31
+ sleep medium_gap
32
+ else
33
+ character_timing[letter].each do |timing|
34
+ pin.on
35
+ sleep timing
36
+ pin.off
37
+ sleep inter_element_gap
38
+ end
39
+ sleep short_gap - inter_element_gap
40
+ end
41
+ end
42
+
43
+ end