pi_facer 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab54d4f2592cd0e45473a1b959a15da85309d11c
4
+ data.tar.gz: dda5407c3966f3bf22e47eb766c19c136e1e7a2e
5
+ SHA512:
6
+ metadata.gz: b20b6b0d435e4c2a7bd633bf44cde09ff0eb2569b1e0e9133afaab171ad031ff6cb9951b73f9e9c3960e8547252d0648283f895b3b5854e136e21bf8f4946628
7
+ data.tar.gz: 65c47843eddb2f1e6713e11cfeb909d82feffde61006d1aa1f4452cece24ca22fa688c76c5f2ebb3a9c5d0d6ecd85182a271bf9a72f2f124b7b80583aabe71c2
data/Manifest ADDED
@@ -0,0 +1,24 @@
1
+ .
2
+ ./examples
3
+ ./examples/2_bit_counter
4
+ ./examples/2_bit_counter/2_bit_counter.rb
5
+ ./examples/7-segment
6
+ ./examples/7-segment/7-segment.rb
7
+ ./examples/dsl_switch
8
+ ./examples/dsl_switch/dsl_switch.rb
9
+ ./examples/morse_code
10
+ ./examples/morse_code/circuit.png
11
+ ./examples/morse_code/morse_code.rb
12
+ ./examples/simple_switch
13
+ ./examples/simple_switch/circuit.png
14
+ ./examples/simple_switch/simple_switch.rb
15
+ ./lib
16
+ ./lib/pi_facer
17
+ ./lib/pi_facer/io.rb
18
+ ./lib/pi_facer.rb
19
+ ./Manifest
20
+ ./pi_facer.gemspec
21
+ ./Rakefile
22
+ ./README.md
23
+
24
+ 8 directories, 13 files
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ ## Overview
2
+
3
+ Pi Facer brings event driven programming to the Raspberry Pi's PiFace accessory.
4
+ Pi Facer was designed for Ruby 2.0!
5
+ Pi Facer was based and inspired on <a href="https://github.com/jwhitehorn/pi_piper">Pi Piper</a> and uses <a href="https://github.com/blakejakopovic/piface">piface</a>, and such is still alpha as piface is.
6
+
7
+ To get started:
8
+
9
+ To install Pi Facer:
10
+
11
+ sudo gem install pi_facer
12
+
13
+ ### PIFACE
14
+ PiFace Digital plugs directly onto the top of your Raspberry Pi, and allows you to sense and control the real world.
15
+ 2 Changeover relays
16
+ 8 Open-collector outputs
17
+ 8 Digital inputs
18
+ 8 LED indicators
19
+ 4 Switches
20
+ Graphical emulator and simulator
21
+
22
+ ```ruby
23
+ require 'pi_facer'
24
+ include PiFacer
25
+
26
+ watch :io => 3 do
27
+ puts "FIO changed from #{last_value} to #{value}"
28
+ end
29
+
30
+ PiFacer.wait
31
+ ```
32
+
33
+ Your block will be called when a change to the pin's state is detected.
34
+
35
+ Additionally you can use the outputs too:
36
+
37
+ ```ruby
38
+ pin = PiFacer::FIO.new(:io => 1, :direction => :out)
39
+ pin.on
40
+ sleep 1
41
+ pin.off
42
+ ```
43
+
44
+
45
+ ***
46
+ <img src="http://www.raspberrypi.org/wp-content/uploads/2012/03/Raspi_Colour_R.png" width="90" />
47
+
48
+ Proudly developed exclusively on a [Raspberry Pi](http://www.raspberrypi.org)
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('pi_facer', '0.0.1') do |p|
6
+ p.description = "Event driven Raspberry PIFace library"
7
+ p.url = "http://github.com/awls99/pi_piper"
8
+ p.author = "Arthur Silva"
9
+ p.email = "awls99@gmail.com"
10
+ p.ignore_pattern = ["examples/**/*"]
11
+ p.dependencies = ['piface']
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/lib/pi_facer.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'piface'
2
+ Dir[File.dirname(__FILE__) + '/pi_facer/*.rb'].each {|file| require file }
3
+
4
+ module PiFacer
5
+ extend self
6
+
7
+ #Defines an event block to be executed when an event occurs.
8
+ #
9
+ # == Parameters:
10
+ # options:
11
+ # Options hash. Options include `:pin`, `:invert` and `:direction`.
12
+ #
13
+ def watch(options, &block)
14
+ Thread.new do
15
+ pin = PiFacer::FIO.new(options)
16
+ loop do
17
+ pin.wait_for_change
18
+ yield pin
19
+ end
20
+ end.abort_on_exception = true
21
+ end
22
+
23
+ #Prevents the main thread from exiting. Required when using PiFacer.watch
24
+ def wait
25
+ loop do sleep 1 end
26
+ end
27
+
28
+ end
@@ -0,0 +1,76 @@
1
+ module PiFacer
2
+ # Represents a Piface io on the Raspberry Pi
3
+ class FIO
4
+ attr_reader :io, :last_value, :value, :direction, :invert
5
+
6
+ #Initializes a new Piface io.
7
+ #
8
+ # @param [Hash] options A hash of options
9
+ # @option options [Fixnum] :io The io 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 io 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.
13
+ def initialize(direction: :in, invert: true, trigger: :both, **options)
14
+ raise "Invalid direction. Options are :in or :out" unless [:in, :out].include? direction
15
+ raise "Invalid IO, Piface IO numbers go from 1 to 8" unless (1..8).include? options[:io]
16
+ raise 'Invalid trigger' unless %I[rising falling both].include? trigger
17
+
18
+ @io = options[:io]
19
+ @direction = direction
20
+ @invert = invert
21
+ @trigger = trigger
22
+
23
+ read
24
+ end
25
+
26
+ # If the io has been initialized for output this method will set the logic level high.
27
+ def on
28
+ Piface.write( @io, 1 ) if direction == :out
29
+ end
30
+
31
+ # Tests if the logic level is high.
32
+ def on?
33
+ not off?
34
+ end
35
+
36
+ # If the io has been initialized for output this method will set the logic level low.
37
+ def off
38
+ Piface.write( @io, 0 ) if direction == :out
39
+ end
40
+
41
+ # Tests if the logic level is low.
42
+ def off?
43
+ value == 0
44
+ end
45
+
46
+ # If the io has been initialized for output this method will either raise or lower the logic level depending on `new_value`.
47
+ # @param [Object] new_value If false or 0 the io will be set to off, otherwise on.
48
+ def update_value(new_value)
49
+ !new_value || new_value == 0 ? off : on
50
+ end
51
+
52
+ # Tests if the logic level has changed since the io was last read.
53
+ def changed?
54
+ read
55
+ last_value != value
56
+ end
57
+
58
+ # blocks until a logic level change occurs. The initializer option `:trigger` modifies what edge this method will release on.
59
+ def wait_for_change
60
+ loop do
61
+ if changed?
62
+ next if @trigger == :rising and @value == 0
63
+ next if @trigger == :falling and @value == 1
64
+ break
65
+ end
66
+ end
67
+ end
68
+
69
+ def read
70
+ @last_value = @value
71
+ val = Piface.read @io
72
+ @value = invert ? (val ^ 1) : val
73
+ end
74
+
75
+ end
76
+ end
data/pi_facer.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "pi_facer"
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Arthur Silva"]
9
+ s.date = Time.now.strftime("%Y-%m-%d")
10
+ s.description = "Event driven Raspberry Pi Piface library based on pi_piper lib"
11
+ s.email = "awls99@gmail.com"
12
+ s.extra_rdoc_files = ["README.md", "lib/pi_facer.rb", "lib/pi_facer/io.rb"]
13
+ s.files = ["Manifest", "README.md", "Rakefile", "lib/pi_facer.rb", "lib/pi_facer/io.rb", "pi_facer.gemspec"]
14
+ s.homepage = "https://github.com/awls99/pi_piper"
15
+ s.require_paths = ["lib"]
16
+ s.rubygems_version = "1.8.23"
17
+ s.summary = "Event driven Raspberry Pi Piface library based on pi_piper lib"
18
+ s.add_dependency(%q<piface>, [">= 0"])
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pi_facer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arthur Silva
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: piface
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Event driven Raspberry Pi Piface library based on pi_piper lib
28
+ email: awls99@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - README.md
33
+ - lib/pi_facer.rb
34
+ - lib/pi_facer/io.rb
35
+ files:
36
+ - Manifest
37
+ - README.md
38
+ - Rakefile
39
+ - lib/pi_facer.rb
40
+ - lib/pi_facer/io.rb
41
+ - pi_facer.gemspec
42
+ homepage: https://github.com/awls99/pi_piper
43
+ licenses: []
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '1.2'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.0.3
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Event driven Raspberry Pi Piface library based on pi_piper lib
65
+ test_files: []