piface 0.4 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 62414175fab73d83fa196972a27b8178bc4646af
4
- data.tar.gz: 85b434904389380c0574cc7023037eeaec645fdd
3
+ metadata.gz: 0e5ef9896d5b037df0557be6dc961b426d9c4c23
4
+ data.tar.gz: 508b965d1458584719adf2f8481af7d46f7d8b02
5
5
  SHA512:
6
- metadata.gz: 4559d83b88665b759c2de81849bf859e9733bdd80551dbafc54662d7b9587c3348eb7e6f5ccb54d6b2468fe3c48cc5e940001ef9651ec9c9843db727d2d571b5
7
- data.tar.gz: 4f1dea3ef063bdb39ef88d70bc1a215ff06a4cbb4e46fdcdf83940b2696c4304ce00c71614b629748c0ae35b42045124167d0b971b040e82b2508421a702169a
6
+ metadata.gz: 5dff8cdda631a359f9662283a243f23abf244d7bad78cb0e8db4988213fb6600e52d44f99c813e9491ce080773df48ffd6abbb1a522c113fb81f9b04dcd01df2
7
+ data.tar.gz: 32cb149d20cbed53f14653ccc570dc0b927dc83d6e7261c3e588965f4d13c7f9a0913f69e4abcc65ff95f72d8600cfe374c5f9ecb780d260a6d557d46e2a8e34
data/CHANGELOG.md CHANGED
@@ -3,12 +3,13 @@
3
3
  ## Upcoming v1.0
4
4
  - Interrupts (change, high, low)
5
5
 
6
- ## v0.4.beta
6
+ ## v0.4
7
7
  - update ffi runtime dependancy to ~> v1.7
8
8
  - updated README.md
9
9
  - added pfio C library as gem extension
10
10
  - updated pfio C library to allow optional pin setup (false by default)
11
11
  - fixed deinit bug
12
+ - added ruby examples
12
13
 
13
14
  ## v0.31
14
15
  - updated the ffi load paths (bugfix for issue #1)
data/README.md CHANGED
@@ -109,18 +109,6 @@ end
109
109
 
110
110
  ## Advanced Usage
111
111
 
112
- ### Piface Initialization
113
- By requiring the gem, your Piface will automatically be initialized. By default, your pins will not be reset and will remain in the existing state. This is useful when running multiple scripts and you don't want to reset the outputs (turning them all off). If you want to setup the pins you can do so by calling the `init` method with `true`.
114
- ```ruby
115
- require 'piface'
116
-
117
- # Initialize the piface (without setting up the pins)
118
- Piface.init # same as Piface.init(false)
119
-
120
- # Initialize Piface and setup the pins
121
- Piface.init(true)
122
- ```
123
-
124
112
  ### Reading Output
125
113
  If you need to check whether or not an output is active, you can easily read an output state with the `read_output` method.
126
114
  ```ruby
@@ -153,17 +141,22 @@ class Relay
153
141
  Piface.write @relay_number, 0
154
142
  end
155
143
 
144
+ def state
145
+ Piface.read_output(@relay_number)
146
+ end
147
+
156
148
  def toggle
157
- Piface.write @relay_number, Piface.read_output(1) ^ 1
149
+ new_state = state ^ 1
150
+ Piface.write @relay_number, new_state
158
151
  end
159
152
  end
160
153
 
161
154
  relay2 = Relay.new(2)
162
- relay.toggle # toggle the light without using a stored state
155
+ relay2.toggle # toggle the light without using a stored state
163
156
  ```
164
157
 
165
158
  ## Examples
166
- For more examples, check out the `example` directory.
159
+ For more examples, check out the `examples` directory.
167
160
  * led
168
161
  * pir sensor
169
162
  * relay (same as stateless relay)
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
data/examples/relay.rb CHANGED
@@ -26,4 +26,4 @@ class Relay
26
26
  end
27
27
 
28
28
  relay2 = Relay.new(2)
29
- relay.toggle # toggle the light without using a stored state
29
+ relay2.toggle # toggle the light without using a stored state
data/ext/piface/pfio.c CHANGED
@@ -4,8 +4,6 @@
4
4
  */
5
5
  #include "pfio.h"
6
6
 
7
- #undef VERBOSE_MODE
8
-
9
7
  static Spi * spi;
10
8
 
11
9
  static void spi_transfer(char * txbuffer, char * rxbuffer);
@@ -13,8 +11,7 @@ static void spi_write(char port, char value);
13
11
  static char spi_read(char port);
14
12
 
15
13
 
16
- // char pfio_init(void)
17
- char pfio_init(bool setup_pins)
14
+ char pfio_init(void)
18
15
  {
19
16
  if ((spi = malloc(sizeof(Spi))) == NULL)
20
17
  return -1;
@@ -61,21 +58,16 @@ char pfio_init(bool setup_pins)
61
58
  }
62
59
  spi->maxspeed = maxspeed;
63
60
 
64
- if (setup_pins == true) {
65
- // set up some ports
66
- spi_write(IOCON, 8); // enable hardware addressing
67
- spi_write(IODIRA, 0); // set port A as outputs
68
- spi_write(IODIRB, 0xFF); // set port B as inputs
69
- spi_write(GPIOA, 0xFF); // set port A on
70
- //spi_write(GPIOB, 0xFF); // set port B on
71
- spi_write(GPPUA, 0xFF); // set port A pullups on
72
- spi_write(GPPUB, 0xFF); // set port B pullups on
73
-
74
- // initialise all outputs to 0
75
- int i;
76
- for (i = 1; i <= 8; i++)
77
- pfio_digital_write(i, 0);
78
- }
61
+ spi_write(IOCON, 8); // enable hardware addressing
62
+ spi_write(GPIOA, 0x00); // turn on port A
63
+ spi_write(IODIRA, 0); // set port A as an output
64
+ spi_write(IODIRB, 0xFF); // set port B as an input
65
+ spi_write(GPPUB, 0xFF); // turn on port B pullups
66
+
67
+ // initialise all outputs to 0
68
+ int i;
69
+ for (i = 1; i <= 8; i++)
70
+ pfio_digital_write(i, 0);
79
71
 
80
72
  return 0;
81
73
  }
@@ -105,20 +97,13 @@ void pfio_digital_write(char pin_number, char value)
105
97
  else
106
98
  new_pin_values = old_pin_values & ~pin_bit_mask;
107
99
 
108
- #ifdef VERBOSE_MODE
109
- printf("digital_write: pin number %d, value %d\n", pin_number, value);
110
- printf("pin bit mask: 0x%x\n", pin_bit_mask);
111
- printf("old pin values: 0x%x\n", old_pin_values);
112
- printf("new pin values: 0x%x\n", new_pin_values);
113
- printf("\n");
114
- #endif
115
-
116
100
  pfio_write_output(new_pin_values);
117
101
  }
118
102
 
119
103
  char pfio_read_input(void)
120
104
  {
121
- return spi_read(INPUT_PORT);
105
+ // inverse read values (0 is off, 1 is on), due to pullups
106
+ return spi_read(INPUT_PORT) ^ 0xFF; //!!! UPDATE DOCUMENTATION
122
107
  }
123
108
 
124
109
  char pfio_read_output(void)
@@ -126,23 +111,57 @@ char pfio_read_output(void)
126
111
  return spi_read(OUTPUT_PORT);
127
112
  }
128
113
 
114
+ // char pfio_read_output_pin(char pin_number)
115
+ // {
116
+ // return
117
+ // }
118
+
129
119
  void pfio_write_output(char value)
130
120
  {
131
121
  spi_write(OUTPUT_PORT, value);
132
122
  }
133
123
 
124
+ void pfio_input_pullup(char pin_number, char value)
125
+ {
126
+ char pin_bit_mask = pfio_get_pin_bit_mask(pin_number);
127
+ char old_pin_values = spi_read(INPUT_PULLUP_PORT);
128
+
129
+ char new_pin_values;
130
+ if (value > 0)
131
+ new_pin_values = old_pin_values | pin_bit_mask;
132
+ else
133
+ new_pin_values = old_pin_values & ~pin_bit_mask;
134
+
135
+ spi_write(INPUT_PULLUP_PORT, new_pin_values);
136
+ }
137
+
138
+ // void pfio_output_pullup(char pin_number, char value)
139
+ // {
140
+ // char pin_bit_mask = pfio_get_pin_bit_mask(pin_number);
141
+ // char old_pin_values = spi_read(OUTPUT_PULLUP_PORT);
142
+
143
+ // char new_pin_values;
144
+ // if (value > 0)
145
+ // new_pin_values = old_pin_values | pin_bit_mask;
146
+ // else
147
+ // new_pin_values = old_pin_values & ~pin_bit_mask;
148
+
149
+ // spi_write(OUTPUT_PULLUP_PORT, new_pin_values);
150
+ // }
151
+
134
152
  char pfio_get_pin_bit_mask(char pin_number)
135
153
  {
136
- return 1 << (pin_number-1);
154
+ // changed interface to use 0-8
155
+ return 1 << pin_number;
137
156
  }
138
157
 
139
158
  char pfio_get_pin_number(char bit_pattern)
140
159
  {
141
- char pin_number = 1; // assume pin 1
160
+ char pin_number = 0; // assume pin 0
142
161
  while ((bit_pattern & 1) == 0)
143
162
  {
144
163
  bit_pattern >>= 1;
145
- if (++pin_number > 8)
164
+ if (++pin_number > 7)
146
165
  {
147
166
  pin_number = 0;
148
167
  break;
@@ -151,7 +170,6 @@ char pfio_get_pin_number(char bit_pattern)
151
170
  return pin_number;
152
171
  }
153
172
 
154
-
155
173
  static void spi_transfer(char * txbuffer, char * rxbuffer)
156
174
  {
157
175
  // set up some transfer information
data/ext/piface/pfio.h CHANGED
@@ -34,6 +34,8 @@
34
34
  #define GPPUB 0x0D // port B pullups
35
35
  #define OUTPUT_PORT GPIOA
36
36
  #define INPUT_PORT GPIOB
37
+ #define OUTPUT_PULLUP_PORT GPPUA
38
+ #define INPUT_PULLUP_PORT GPIOB
37
39
 
38
40
  #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
39
41
 
@@ -49,15 +51,18 @@ typedef struct
49
51
  typedef enum { false, true } bool;
50
52
 
51
53
  // extern char pfio_init(void);
52
- extern char pfio_init(bool setup_pins);
54
+ extern char pfio_init(void);
53
55
  extern char pfio_deinit(void);
54
56
  extern char pfio_digital_read(char pin_number);
55
57
  extern void pfio_digital_write(char pin_number, char value);
56
58
  extern char pfio_read_input(void);
57
59
  extern char pfio_read_output(void);
60
+ // extern char pfio_read_output_pin(char pin_number);
58
61
  extern void pfio_write_output(char value);
59
62
  extern char pfio_get_pin_bit_mask(char pin_number);
60
63
  extern char pfio_get_pin_number(char bit_mask);
64
+ extern void pfio_input_pullup(char pin_number, char value);
65
+ extern void pfio_output_pullup(char pin_number, char value);
61
66
 
62
67
  /*
63
68
  static void spi_transfer(char * txbuffer, char * rxbuffer);
data/lib/piface.rb CHANGED
@@ -2,47 +2,83 @@ require "piface/version"
2
2
  require "piface/native"
3
3
 
4
4
  module Piface
5
+ # extend Forwardable
6
+
7
+ # def_delegator Native, :pfio_digital_read, :read
8
+ # def_delegator Native, :pfio_digital_write, :write
9
+ # def_delegator Native, :pfio_deinit, :deinit
10
+ # may need to be in quotes
11
+
5
12
  LOW = 0
6
13
  HIGH = 1
7
14
 
15
+ # DEPRECIATE
8
16
  def self.read(pin_number)
9
17
  Native.pfio_digital_read(pin_number)
10
18
  end
19
+ # alias :[] :read
11
20
 
21
+ # DEPRECIATE
12
22
  def self.write(pin_number, value)
13
23
  Native.pfio_digital_write(pin_number, value)
14
24
  end
25
+ # alias :[]= :write
15
26
 
16
- def self.read_input(pin_number)
17
- current_state = Native.pfio_read_input
18
- pin_state(current_state, pin_number)
19
- end
27
+ # def self.read_input(pin_number)
28
+ # current_state = Native.pfio_read_input
29
+ # pin_state(current_state, pin_number)
30
+ # end
20
31
 
21
32
  def self.read_output(pin_number)
33
+ # Native.pfio_read_output_pin(pin_number)
22
34
  current_state = Native.pfio_read_output
23
35
  pin_state(current_state, pin_number)
24
36
  end
25
37
 
26
- private
27
- def self.init(setup_pins = false)
28
- Native.pfio_init(setup_pins)
38
+ def self.init
39
+ # begin
40
+ # Native.pfio_init(setup_pins)
41
+ # rescue LoadError
42
+ #
43
+ exit "Error: Piface initialisation failed." unless Native.pfio_init == 0
44
+ # end
29
45
  end
30
46
 
47
+ # def set_pullup(pin_number, value)
48
+ #
49
+ # end
50
+
51
+ # DEPRECIATE
31
52
  def self.deinit
32
53
  Native.pfio_deinit
33
54
  end
34
55
 
56
+ # Native.pfio_get_pin_bit_mask pin_number
57
+ # DEPRECIATE
35
58
  def self.pin_number_to_flag(pin_number)
36
- 2**(pin_number - 1)
59
+ # Native.pfio_get_pin_bit_mask(pin_number)
60
+ 2**(pin_number - 1) #!!! still need the - 1?
37
61
  end
38
62
 
63
+ # DEPRECIATE
64
+ # Native.pfio_get_pin_number bit_mask
39
65
  def self.flag_active?(mask, pin_number)
40
- mask & pin_number_to_flag(pin_number) > 0
66
+ # char pfio_digital_read(char pin_number)
67
+ # {
68
+ # char current_pin_values = pfio_read_input();
69
+ # char pin_bit_mask = pfio_get_pin_bit_mask(pin_number);
70
+ # return (current_pin_values & pin_bit_mask) > 0;
71
+ # }
72
+ # mask & pin_number_to_flag(pin_number) > 0
73
+ mask & pin_number_to_flag(pin_number) == pin_number
41
74
  end
42
75
 
76
+ # DEPRECIATE
43
77
  def self.pin_state(mask, pin_number)
44
78
  flag_active?(mask, pin_number) ? HIGH : LOW
45
79
  end
80
+
81
+ # module_function :read, :write, :[], :[]=
46
82
  end
47
83
 
48
84
  at_exit { Piface.deinit }
data/lib/piface/native.rb CHANGED
@@ -5,18 +5,28 @@ module Piface
5
5
  extend FFI::Library
6
6
  ffi_lib File.expand_path("../pfio.so", __FILE__)
7
7
 
8
- attach_function :pfio_init, [ :bool ], :char
8
+ typedef :bool, :setup_pins
9
+ typedef :char, :pin_number
10
+ typedef :char, :pin_value
11
+ typedef :char, :pullup_value
12
+ typedef :char, :bit_mask
13
+
14
+ attach_function :pfio_init, [], :char
9
15
  attach_function :pfio_deinit, [], :char
10
16
 
11
- attach_function :pfio_digital_read, [ :char ], :char
12
- attach_function :pfio_digital_write, [ :char, :char ], :void
17
+ attach_function :pfio_digital_read, [ :pin_number ], :pin_value
18
+ attach_function :pfio_digital_write, [ :pin_number, :pin_value ], :void
19
+
20
+ attach_function :pfio_read_input, [], :bit_mask
21
+ attach_function :pfio_read_output, [], :bit_mask
22
+ # attach_function :pfio_read_output_pin, [ :char ], :char
13
23
 
14
- attach_function :pfio_read_input, [], :char
15
- attach_function :pfio_read_output, [], :char
24
+ # attach_function :pfio_write_output, [ :bit_mask ], :void
16
25
 
17
- # attach_function :pfio_write_output, [ :char ], :void
26
+ attach_function :pfio_input_pullup, [ :pin_number, :pullup_value ], :void
27
+ # attach_function :pfio_output_pullup, [ :pin_number, :pullup_value ], :void
18
28
 
19
- attach_function :pfio_get_pin_bit_mask, [ :char ], :char
20
- attach_function :pfio_get_pin_number, [ :char ], :char
29
+ attach_function :pfio_get_pin_bit_mask, [ :pin_value ], :bit_mask
30
+ attach_function :pfio_get_pin_number, [ :bit_mask ], :pin_value
21
31
  end
22
32
  end
@@ -1,3 +1,3 @@
1
1
  module Piface
2
- VERSION = "0.4"
2
+ VERSION = "0.4.1"
3
3
  end
data/piface.gemspec CHANGED
@@ -14,11 +14,13 @@ Gem::Specification.new do |spec|
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
+ spec.test_files = Dir.glob("spec/**/*.rb")
17
18
  spec.extensions = ['ext/piface/extconf.rb']
18
19
  spec.require_paths = ["lib"]
19
20
 
20
21
  spec.add_development_dependency "bundler", "~> 1.3"
21
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec', '~> 2.13'
22
24
 
23
25
  spec.add_dependency "ffi", "~> 1.7"
24
26
  end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Piface do
4
+ # init
5
+ # deinit
6
+ # write
7
+ # read
8
+ # read_output
9
+ # pin_number_to_flag
10
+ # flag_active?
11
+ # pin_state
12
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ RSpec.configure do |config|
5
+ # some (optional) config here
6
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piface
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Jakopovic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-13 00:00:00.000000000 Z
11
+ date: 2013-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.13'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: ffi
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -77,6 +91,8 @@ files:
77
91
  - lib/piface/native.rb
78
92
  - lib/piface/version.rb
79
93
  - piface.gemspec
94
+ - spec/piface_spec.rb
95
+ - spec/spec_helper.rb
80
96
  homepage: https://github.com/blakejakopovic/piface
81
97
  licenses:
82
98
  - MIT
@@ -102,5 +118,7 @@ signing_key:
102
118
  specification_version: 4
103
119
  summary: Ruby Bindings for the pfio.c C Library for use with the PiFace Digital extension
104
120
  board and your Raspberry Pi
105
- test_files: []
121
+ test_files:
122
+ - spec/piface_spec.rb
123
+ - spec/spec_helper.rb
106
124
  has_rdoc: