piface 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/CHANGELOG.md +20 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +146 -0
- data/Rakefile +1 -0
- data/lib/piface/native.rb +22 -0
- data/lib/piface/version.rb +3 -0
- data/lib/piface.rb +49 -0
- data/piface.gemspec +25 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0b43f10c9a88d6a0c608ca43d4e1860f0420bf45
|
4
|
+
data.tar.gz: 26d96b13751d18c479ac1ace336b064881b0e8e1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 04e6ff4046cc0d866b0342452bf2a69292a3d7bf4e311f94049063d5192b4108894bdb4cf7b0060dd579aac868ef7d1948e4dbeb558a4aac8df305378f9001af
|
7
|
+
data.tar.gz: ddc296d7592048ddf663b353ac780b9aa05d237a770e76f7589072c36dafddd7d3c070ba8b5a8775146abae8eb3993b34db42b3d865c456a841033167de12639
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
## Upcoming v1.0
|
4
|
+
- Interrupts (change, high, low)
|
5
|
+
|
6
|
+
## v0.3
|
7
|
+
- added new pfio.c native methods using ffi
|
8
|
+
- updated ffi to ~> v1.7.dev
|
9
|
+
- implemented Piface.read_input and Piface.read_output
|
10
|
+
- changed all piface code references to lowercase (excluding the primary module)
|
11
|
+
- changed gem name to lowercase
|
12
|
+
- added Piface::HIGH and Piface::LOW
|
13
|
+
- updated README.md
|
14
|
+
- added ffi_lib path fallbacks
|
15
|
+
|
16
|
+
## v0.2
|
17
|
+
- Initial Public Release
|
18
|
+
|
19
|
+
## v0.1
|
20
|
+
- Internal Release
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Blake
|
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,146 @@
|
|
1
|
+
# Ruby Bindings for [pfio.c](https://github.com/thomasmacpherson/piface/tree/master/c)
|
2
|
+
|
3
|
+
The [pfio C Library](https://github.com/thomasmacpherson/piface/blob/master/c/) is used to control the [Pi-Face Digital Interface](http://pi.cs.man.ac.uk/interface.htm) for [Raspberry Pi](http://www.raspberrypi.org/). This gem uses the [Ruby-FFI](https://github.com/ffi/ffi) gem to bind the C functions to ruby.
|
4
|
+
|
5
|
+
## !! Project is currently in ALPHA (Things may change)
|
6
|
+
Ruby method names, as well as other aspects, may change until version 1.0 is released.
|
7
|
+
|
8
|
+
## Basic Usage
|
9
|
+
|
10
|
+
The PiFace has 8 inputs and 8 outputs. Both the inputs and outputs are linked to connectors 1-8 respectfully.
|
11
|
+
|
12
|
+
Note: The pfio C library will set all outputs to LOW (zero) when first initialized.
|
13
|
+
|
14
|
+
### Using Outputs
|
15
|
+
Writing to outputs 1 to 8 with all turn on the LEDs. Write to outputs using Piface::LOW (or 0) for off/disable and Piface::HIGH (or 1) for on/enable.
|
16
|
+
|
17
|
+
Please note that the PiFace outputs are all open-collectors (meaning that they do not output any voltage). Please read the [PiFace Manual](http://www.farnell.com/datasheets/1684425.pdf) for more information.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'piface'
|
21
|
+
|
22
|
+
# Enable output 4
|
23
|
+
Piface.write 4, 1
|
24
|
+
|
25
|
+
sleep 1 # wait one second
|
26
|
+
|
27
|
+
# Disable output 4
|
28
|
+
Piface.write 4, 0
|
29
|
+
```
|
30
|
+
|
31
|
+
### Using Relays
|
32
|
+
Write to outputs 1 and 2 to control the relays (if jumpers JP5 and JP6 are enabled on the PiFace).
|
33
|
+
```ruby
|
34
|
+
require 'piface'
|
35
|
+
|
36
|
+
# Turn on the first relay
|
37
|
+
Piface.write 1, 1
|
38
|
+
|
39
|
+
sleep 1 # wait one second
|
40
|
+
|
41
|
+
# Turn off first relay
|
42
|
+
Piface.write 1, 0
|
43
|
+
```
|
44
|
+
|
45
|
+
### Using Classes
|
46
|
+
Sometimes it's nice to represent an output using a class. One common example is when you want to track the current state of the output.
|
47
|
+
```ruby
|
48
|
+
require 'piface'
|
49
|
+
|
50
|
+
# Relay class implementing toggle functionality
|
51
|
+
class Relay
|
52
|
+
def initialize(relay_number)
|
53
|
+
@relay_number = relay_number
|
54
|
+
@state = 0
|
55
|
+
end
|
56
|
+
|
57
|
+
def turn_on
|
58
|
+
Piface.write @relay_number, 1
|
59
|
+
@state = 1
|
60
|
+
end
|
61
|
+
|
62
|
+
def turn_off
|
63
|
+
Piface.write @relay_number, 0
|
64
|
+
@state = 0
|
65
|
+
end
|
66
|
+
|
67
|
+
def toggle
|
68
|
+
@state == 1 ? turn_off : turn_on
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
relay1 = Relay.new(1)
|
73
|
+
|
74
|
+
# Turn the relay on and off 3 times
|
75
|
+
6.times do
|
76
|
+
relay1.toggle
|
77
|
+
sleep 1 # wait one second
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
### Using Inputs
|
82
|
+
The PiFace uses digital inputs. Reading the value with return either a 0 or 1. Since the PiFace uses pull-up resistors for inputs, by default they will read 1. This means to check for input (such as button presses), you must compare the read input value with 0 for on.
|
83
|
+
```ruby
|
84
|
+
require 'piface'
|
85
|
+
|
86
|
+
# Read value from input 6 (without active input)
|
87
|
+
Piface.read 6
|
88
|
+
# => 1
|
89
|
+
|
90
|
+
# Read value from input 6 (with active input)
|
91
|
+
Piface.read 6
|
92
|
+
# => 0
|
93
|
+
```
|
94
|
+
|
95
|
+
### Using Buttons
|
96
|
+
The PiFace has 4 built-in buttons that correspond to inputs 1 to 4.
|
97
|
+
```ruby
|
98
|
+
require 'piface'
|
99
|
+
|
100
|
+
# Listen for button 4 press
|
101
|
+
loop do
|
102
|
+
# Check if the button has been pressed
|
103
|
+
if (Piface.read(4) == Piface::LOW)
|
104
|
+
puts "Button 4 pressed"
|
105
|
+
end
|
106
|
+
sleep 0.1 # sleep to be kind to the CPU
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
## Additional Resources
|
111
|
+
* [The PiFace Digital PDF Manual](http://www.farnell.com/datasheets/1684425.pdf)
|
112
|
+
* [pfio C Library](https://github.com/thomasmacpherson/piface/blob/master/c/)
|
113
|
+
* [WiriingPi Library](https://github.com/WiringPi/WiringPi)
|
114
|
+
|
115
|
+
## Installation
|
116
|
+
|
117
|
+
First install the pfio C Library
|
118
|
+
|
119
|
+
[https://github.com/thomasmacpherson/piface](https://github.com/thomasmacpherson/piface)
|
120
|
+
|
121
|
+
Enable RaspberryPi's SPI module
|
122
|
+
|
123
|
+
$ sudo modprobe spi-bcm2708
|
124
|
+
or
|
125
|
+
$ gpio load spi
|
126
|
+
|
127
|
+
Add this line to your application's Gemfile:
|
128
|
+
|
129
|
+
gem 'piface'
|
130
|
+
|
131
|
+
And then execute:
|
132
|
+
|
133
|
+
$ bundle
|
134
|
+
|
135
|
+
Or install it yourself as:
|
136
|
+
|
137
|
+
$ gem install piface
|
138
|
+
|
139
|
+
|
140
|
+
## Contributing
|
141
|
+
|
142
|
+
1. Fork it
|
143
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
144
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
145
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
146
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "ffi"
|
2
|
+
|
3
|
+
module Piface
|
4
|
+
module Native
|
5
|
+
extend FFI::Library
|
6
|
+
ffi_lib ['libpiface', 'libpiface-1.0', 'libpiface-1.0.so.1', '/usr/local/lib/libpiface-1.0']
|
7
|
+
|
8
|
+
attach_function :pfio_init, [], :char
|
9
|
+
attach_function :pfio_deinit, [], :char
|
10
|
+
|
11
|
+
attach_function :pfio_digital_read, [ :char ], :char
|
12
|
+
attach_function :pfio_digital_write, [ :char, :char ], :void
|
13
|
+
|
14
|
+
attach_function :pfio_read_input, [], :char
|
15
|
+
attach_function :pfio_read_output, [], :char
|
16
|
+
|
17
|
+
# attach_function :pfio_write_output, [ :char ], :void
|
18
|
+
|
19
|
+
attach_function :pfio_get_pin_bit_mask, [ :char ], :char
|
20
|
+
attach_function :pfio_get_pin_number, [ :char ], :char
|
21
|
+
end
|
22
|
+
end
|
data/lib/piface.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require "piface/version"
|
2
|
+
require "piface/native"
|
3
|
+
|
4
|
+
module Piface
|
5
|
+
LOW = 0
|
6
|
+
HIGH = 1
|
7
|
+
|
8
|
+
def self.read(pin_number)
|
9
|
+
Native.pfio_digital_read(pin_number)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.write(pin_number, value)
|
13
|
+
Native.pfio_digital_write(pin_number, value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.read_input(pin_number)
|
17
|
+
current_state = Native.pfio_read_input
|
18
|
+
pin_state(current_state, pin_number)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.read_output(pin_number)
|
22
|
+
current_state = Native.pfio_read_output
|
23
|
+
pin_state(current_state, pin_number)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def self.init
|
28
|
+
Native.pfio_init
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.deinit
|
32
|
+
Native.pfio_init
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.pin_number_to_flag(pin_number)
|
36
|
+
2**(pin_number - 1)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.flag_active?(mask, pin_number)
|
40
|
+
mask & pin_number_to_flag(pin_number) > 0
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.pin_state(mask, pin_number)
|
44
|
+
flag_active?(mask, pin_number) ? HIGH : LOW
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
at_exit { Piface.deinit }
|
49
|
+
Piface.init
|
data/piface.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'piface/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "piface"
|
8
|
+
spec.version = Piface::VERSION
|
9
|
+
spec.authors = ["Blake Jakopovic"]
|
10
|
+
spec.email = ["blake.jakopovic@gmail.com"]
|
11
|
+
spec.description = %q{Ruby gem for using PiFace Extension Board (and Raspberry Pi)}
|
12
|
+
spec.summary = %q{Ruby Bindings for the pfio.c C Library for use with the PiFace Digital extension board and your Raspberry Pi}
|
13
|
+
spec.homepage = "http://twitter.com/blakejakopovic"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_dependency "ffi", "~> 1.7.dev"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: piface
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Blake Jakopovic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-03 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: ffi
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.7.dev
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.7.dev
|
55
|
+
description: Ruby gem for using PiFace Extension Board (and Raspberry Pi)
|
56
|
+
email:
|
57
|
+
- blake.jakopovic@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- CHANGELOG.md
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/piface.rb
|
69
|
+
- lib/piface/native.rb
|
70
|
+
- lib/piface/version.rb
|
71
|
+
- piface.gemspec
|
72
|
+
homepage: http://twitter.com/blakejakopovic
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Ruby Bindings for the pfio.c C Library for use with the PiFace Digital extension
|
96
|
+
board and your Raspberry Pi
|
97
|
+
test_files: []
|
98
|
+
has_rdoc:
|