wiringpi 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,233 @@
1
+ require 'wiringpi/wiringpi'
2
+
3
+ WPI_MODE_PINS = 0 # Use sane pin numbering
4
+ WPI_MODE_GPIO = 1 # Use Broadcom barmy GPIO pin numbering
5
+
6
+ # Constants for mode()
7
+ INPUT = 0
8
+ OUTPUT = 1
9
+ PWM_OUTPUT = 2
10
+
11
+ # Constants for digitalWrite()
12
+ HIGH = 1
13
+ LOW = 0
14
+
15
+ PUD_OFF = 0
16
+ PUD_DOWN = 1
17
+ PUD_UP = 2
18
+
19
+ # Bit-order for shiftOut and shiftIn
20
+ LSBFIRST = 0 # Least Significant Bit First
21
+ MSBFIRST = 1 # Most Significant Bit First
22
+
23
+ module WiringPi
24
+
25
+ class Serial
26
+
27
+ @id = 0
28
+ @device = '/dev/ttyAMA0'
29
+ @baud = 9600
30
+
31
+ def initialize(device='/dev/ttyAMA0',baud=9600)
32
+
33
+ @device = device
34
+ @baud = baud
35
+
36
+ @id = Wiringpi.serialOpen( @device,@baud )
37
+
38
+ end
39
+
40
+ def serialClose
41
+
42
+ Wiringpi.serialClose( @id )
43
+ @id = 0
44
+
45
+ end
46
+
47
+ def serialPutchar( char )
48
+
49
+ Wiringpi.serialPutchar( @id, char )
50
+
51
+ end
52
+
53
+ def serialPuts( string )
54
+
55
+ Wiringpi.serialPuts( @id, string )
56
+
57
+ end
58
+
59
+ def serialDataAvail
60
+
61
+ Wiringpi.serialDataAvail( @id )
62
+
63
+ end
64
+
65
+ def serialGetchar
66
+
67
+ Wiringpi.serialGetchar( @id )
68
+
69
+ end
70
+
71
+ end
72
+
73
+ class GPIO
74
+
75
+ GPIO_PINS = [
76
+ 0,1,4,7,8,9,10,11,14,15,17,18,21,22,23,24,25 # seemingly random indeed!
77
+ ]
78
+
79
+ PINS = [
80
+ 0,1,2,3,4,5,6,7, # basic IO pins
81
+ 8,9, # i2c with 1k8 pull up resistor
82
+ 10,11,12,13,14, # SPI pins, can also be used for IO
83
+ 15,16,17
84
+ ]
85
+
86
+ @mode = WPI_MODE_PINS
87
+
88
+ @@init = false # once wiringPiSetup has been called, we don't have to do it again
89
+
90
+ def initialize( mode=WPI_MODE_PINS )
91
+
92
+ @mode = mode
93
+ self.wiringPiSetup unless @@init
94
+
95
+ end
96
+
97
+ def wiringPiMode( mode )
98
+
99
+ @mode = mode
100
+ Wiringpi.wiringPiGpioMode( @mode )
101
+
102
+ end
103
+
104
+ def wiringPiSetup
105
+
106
+ begin
107
+ Wiringpi.wiringPiSetup
108
+ rescue Exception=>e
109
+ raise e
110
+ end
111
+
112
+ Wiringpi.wiringPiGpioMode( @mode )
113
+ @@init = true
114
+
115
+ end
116
+
117
+ def checkPin(pin)
118
+
119
+ ( @mode = WPI_MODE_PINS and PINS.include?(pin) ) or ( @mode = WPI_MODE_GPIO and GPIO_PINS.include?(pin) )
120
+
121
+ end
122
+
123
+ def pinError(pin)
124
+ "invalid #{pin}, available gpio pins: #{PINS}" if @mode == WPI_MODE_PINS
125
+ "invalid #{pin}, available gpio pins: #{GPIO_PINS}" if @mode == WPI_MODE_GPIO
126
+ end
127
+
128
+ def read(pin)
129
+
130
+ raise ArgumentError, pinError(pin) unless checkPin(pin)
131
+
132
+ Wiringpi.digitalRead(pin)
133
+
134
+ end
135
+
136
+ def pwmWrite(pin,value)
137
+
138
+ raise ArgumentError, pinError(pin) unless checkPin(pin)
139
+
140
+ Wiringpi.pwmWrite(pin,value)
141
+
142
+ end
143
+
144
+ def write(pin,value)
145
+
146
+ raise ArgumentError, pinError(pin) unless checkPin(pin)
147
+ raise ArgumentError, 'invalid value' unless [0,1].include?(value)
148
+
149
+ Wiringpi.digitalWrite(pin,value)
150
+
151
+ end
152
+
153
+ def mode(pin,mode)
154
+
155
+ raise ArgumentError, pinError(pin) unless checkPin(pin)
156
+ raise ArgumentError, "invalid mode" unless [INPUT,OUTPUT,PWM_OUTPUT].include?(mode)
157
+
158
+ Wiringpi.pinMode(pin, mode)
159
+
160
+ end
161
+
162
+ =begin
163
+ shiftOutArray int dataPin, int clockPin, int latchPin, int[] bits
164
+ Shifts out an array of ints by converting them into bytes
165
+ and handing to Wiringpi.shiftOut, must contain only 1s or 0s
166
+ =end
167
+ def shiftOutArray(dataPin, clockPin, latchPin, bits)
168
+
169
+ raise ArgumentError, "invalid data pin, available gpio pins: #{PINS}" unless checkPin(dataPin)
170
+ raise ArgumentError, "invalid clock pin, available gpio pins: #{PINS}" unless checkPin(clockPin)
171
+ raise ArgumentError, "invalid latch pin, available gpio pins: #{PINS}" unless checkPin(latchPin)
172
+
173
+ WiringPi.write( latchPin, LOW )
174
+
175
+ bits.each_slice(8) do |slice|
176
+ Wiringpi.shiftOut(dataPin, clockPin, LSBFIRST, slice.reverse.join.to_i(2))
177
+ end
178
+
179
+ WiringPi.write( latchPin, HIGH )
180
+
181
+ end
182
+
183
+ =begin
184
+ shiftOut int dataPin, int clockPin, int latchPin, char
185
+ Shift out a single 8-bit integer 0-255
186
+ =end
187
+ def shiftOut(dataPin, clockPin, latchPin, char)
188
+
189
+ raise ArgumentError, "invalid data pin, available gpio pins: #{PINS}" unless checkPin(dataPin)
190
+ raise ArgumentError, "invalid clock pin, available gpio pins: #{PINS}" unless checkPin(clockPin)
191
+ raise ArgumentError, "invalid latch pin, available gpio pins: #{PINS}" unless checkPin(latchPin)
192
+
193
+ WiringPi.write( latchPin, LOW )
194
+
195
+ Wiringpi.shiftOut(dataPin, clockPin, LSBFIRST, char)
196
+
197
+ WiringPi.write( latchPin, HIGH )
198
+
199
+ end
200
+
201
+ =begin
202
+ readAll
203
+ Reads values of all pins and returns them as a hash
204
+ =end
205
+ def readAll
206
+
207
+ pinValues = Hash.new
208
+
209
+ if @mode == WPI_MODE_GPIO
210
+
211
+ GPIO_PINS.each do |pin|
212
+
213
+ pinValues[pin] = self.read(pin)
214
+
215
+ end
216
+
217
+ else
218
+
219
+ PINS.each do |pin|
220
+
221
+ pinValues[pin] = self.read(pin)
222
+
223
+ end
224
+
225
+ end
226
+
227
+ pinValues
228
+
229
+ end
230
+
231
+ end
232
+
233
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wiringpi
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gordon
9
+ - Phil
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-06-22 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: ! 'WiringPi library wrapper for the Raspberry Pi only. Wraps up the Arduino
16
+ wiring-like WiringPi library into a convinient Ruby gem. Currently includes GPIO
17
+ functionality, serial and shiftOut/shiftIn support. Credit to Gordon for the WiringPi
18
+ library, which can be found here: http://projects.drogon.net/raspberry-pi/wiringpi/'
19
+ email: phil@gadgetoid.com
20
+ executables: []
21
+ extensions:
22
+ - ext/wiringpi/extconf.rb
23
+ extra_rdoc_files: []
24
+ files:
25
+ - lib/wiringpi.rb
26
+ - ext/wiringpi/wiringShift.c
27
+ - ext/wiringpi/wiringPi.c
28
+ - ext/wiringpi/serial.c
29
+ - ext/wiringpi/wiringpi_wrap.c
30
+ - ext/wiringpi/wiringShift.h
31
+ - ext/wiringpi/serial.h
32
+ - ext/wiringpi/wiringPi.h
33
+ - ext/wiringpi/extconf.rb
34
+ homepage: http://rubygems.org/gems/wiringpi
35
+ licenses:
36
+ - GNU General Public License
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Arduino wiring-like library for Raspberry Pi GPIO. Will only work on a Pi.
59
+ Alpha version.
60
+ test_files: []