rpi_gpio 0.1.2
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 +7 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +12 -0
- data/LICENSE +23 -0
- data/README.md +122 -0
- data/Rakefile +3 -0
- data/ext/rpi_gpio/c_gpio.c +233 -0
- data/ext/rpi_gpio/c_gpio.h +50 -0
- data/ext/rpi_gpio/common.c +100 -0
- data/ext/rpi_gpio/common.h +46 -0
- data/ext/rpi_gpio/constants.c +61 -0
- data/ext/rpi_gpio/constants.h +40 -0
- data/ext/rpi_gpio/cpuinfo.c +88 -0
- data/ext/rpi_gpio/cpuinfo.h +27 -0
- data/ext/rpi_gpio/event_gpio.c +556 -0
- data/ext/rpi_gpio/event_gpio.h +40 -0
- data/ext/rpi_gpio/extconf.rb +5 -0
- data/ext/rpi_gpio/rb_gpio.c +405 -0
- data/ext/rpi_gpio/rb_gpio.h +44 -0
- data/ext/rpi_gpio/rb_pwm.c +131 -0
- data/ext/rpi_gpio/rb_pwm.h +40 -0
- data/ext/rpi_gpio/rpi_gpio.c +45 -0
- data/ext/rpi_gpio/rpi_gpio.h +28 -0
- data/ext/rpi_gpio/soft_pwm.c +218 -0
- data/ext/rpi_gpio/soft_pwm.h +32 -0
- data/lib/rpi_gpio.so +0 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d85582851adc2450613f9dd4a2eb0a1ce1309628
|
4
|
+
data.tar.gz: 8cf454f69c69bdc2784ef384c9af276872915e73
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b763263bda2aee27efeef35dfe11230f3aca222fd7c04e88c2dc14ba2643335ce391b0d76823cd050c517ffddfc072f3524ab1407a03e7348ab5bacd6dbf9c80
|
7
|
+
data.tar.gz: aed83487fe1ac044983ec959a315b64ea6b9f4115f20939a12eac20f181090c235cc3ed90a912b0418c71b83b6714e4ac93ea6b23ff6c0655321ad1a0a72223e
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014-2015 Nick Lowery
|
4
|
+
Copyright (c) 2013-2014 Ben Croston
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
|
data/README.md
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
#rpi_gpio v0.1.2
|
2
|
+
|
3
|
+
Ruby conversion of [RPi.GPIO Python module](https://pypi.python.org/pypi/RPi.GPIO)
|
4
|
+
|
5
|
+
##Features
|
6
|
+
|
7
|
+
Manipulate your Raspberry Pi's GPIO pins from Ruby!
|
8
|
+
- Boolean input/output
|
9
|
+
- Software-driven PWM (written in C for speed)
|
10
|
+
|
11
|
+
##Sample Usage
|
12
|
+
|
13
|
+
I aimed to make the gem's usage exactly the same as its Python counterpart -- only with a few semantic differences to utilize Ruby's readability. If anything is confusing, you can always check [here](http://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/) for the original Python module's documentation.
|
14
|
+
|
15
|
+
####Download the gem
|
16
|
+
|
17
|
+
The easiest way to download the gem is to use [Bundler](http://bundler.io/) with a Gemfile. In your Gemfile, include the line
|
18
|
+
```
|
19
|
+
gem 'rpi_gpio'
|
20
|
+
```
|
21
|
+
Then you can run `bundle install` to automatically download and compile the gem for your system. To include the gem in a Ruby file, use the line `require 'rpi_gpio'`.
|
22
|
+
|
23
|
+
####Pin numbering
|
24
|
+
|
25
|
+
Before you can do anything with the GPIO pins, you need to specify how you want to number them.
|
26
|
+
```
|
27
|
+
RPi::GPIO.set_numbering :board
|
28
|
+
# or
|
29
|
+
RPi::GPIO.set_numbering :bcm
|
30
|
+
````
|
31
|
+
`:board` numbering refers to the physical pin numbers on the Pi, whereas `:bcm` numbering refers to the Broadcom SOC channel numbering. Note that `:bcm` numbering differs between Pi models, while `:board` numbering does not.
|
32
|
+
|
33
|
+
####Input
|
34
|
+
|
35
|
+
To receive input from a GPIO pin, you must first initialize it as an input pin:
|
36
|
+
```
|
37
|
+
RPi::GPIO.setup PIN_NUM, as: :input
|
38
|
+
```
|
39
|
+
The pin number will differ based on your selected numbering system and which pin you want to use.
|
40
|
+
|
41
|
+
Now you can use the calls
|
42
|
+
```
|
43
|
+
RPi::GPIO.high? PIN_NUM
|
44
|
+
RPi::GPIO.low? PIN_NUM
|
45
|
+
```
|
46
|
+
to receive either `true` or `false`.
|
47
|
+
|
48
|
+
You can use the additional hash argument `:pull` to apply a pull-up or pull-down resistor to the input pin like so:
|
49
|
+
```
|
50
|
+
RPi::GPIO.setup PIN_NUM, as: input, pull: :down
|
51
|
+
# or
|
52
|
+
RPi::GPIO.setup PIN_NUM, as: input, pull: :up
|
53
|
+
# or (not necessary; :off is the default value)
|
54
|
+
RPi::GPIO.setup PIN_NUM, as: input, pull: :off
|
55
|
+
```
|
56
|
+
|
57
|
+
####Output
|
58
|
+
|
59
|
+
To send output to a GPIO pin, you must first initialize it as an output pin:
|
60
|
+
```
|
61
|
+
RPi::GPIO.setup PIN_NUM, as: :output
|
62
|
+
```
|
63
|
+
Now you can use the calls
|
64
|
+
```
|
65
|
+
RPi::GPIO.set_high PIN_NUM
|
66
|
+
RPi::GPIO.set_low PIN_NUM
|
67
|
+
```
|
68
|
+
to set the pin either high or low.
|
69
|
+
|
70
|
+
####PWM (pulse-width modulation)
|
71
|
+
|
72
|
+
Pulse-width modulation is a useful tool for controlling things like LED brightness or motor speed. To utilize PWM, first create a PWM object for an output pin.
|
73
|
+
```
|
74
|
+
pwm = RPi::GPIO::PWM.new(PIN_NUM, PWM_FREQ)
|
75
|
+
```
|
76
|
+
The `PWM_FREQ` is a value in hertz that specifies the amount of pulse cycles per second.
|
77
|
+
|
78
|
+
Now you can call the following method to start PWM:
|
79
|
+
```
|
80
|
+
pwm.start DUTY_CYCLE
|
81
|
+
```
|
82
|
+
`DUTY_CYCLE` is a value from `0.0` to `100.0` indicating the percent of the time that the signal will be high.
|
83
|
+
|
84
|
+
Once running, you can get/set the PWM duty cycle with
|
85
|
+
```
|
86
|
+
pwm.duty_cycle # get
|
87
|
+
pwm.duty_cycle = NEW_DUTY_CYCLE # set
|
88
|
+
```
|
89
|
+
get/set the PWM frequency with
|
90
|
+
```
|
91
|
+
pwm.frequency # get
|
92
|
+
pwm.frequency = NEW_FREQUENCY # set
|
93
|
+
```
|
94
|
+
and get the PWM GPIO pin with
|
95
|
+
```
|
96
|
+
pwm.pin
|
97
|
+
```
|
98
|
+
|
99
|
+
To stop PWM, use
|
100
|
+
```
|
101
|
+
pwm.stop
|
102
|
+
```
|
103
|
+
|
104
|
+
####Cleaning up
|
105
|
+
|
106
|
+
After your program is finished using the GPIO pins, it's a good idea to release them so other programs can use them later. Simply call
|
107
|
+
```
|
108
|
+
RPi::GPIO.clean_up PIN_NUM
|
109
|
+
```
|
110
|
+
to release a specific pin, or
|
111
|
+
```
|
112
|
+
RPi::GPIO.clean_up
|
113
|
+
```
|
114
|
+
to release all allocated pins.
|
115
|
+
|
116
|
+
##Credits
|
117
|
+
|
118
|
+
Original Python code by Ben Croston modified for Ruby by Nick Lowery
|
119
|
+
|
120
|
+
Copyright (c) 2014-2015 [Nick Lowery](https://github.com/ClockVapor)
|
121
|
+
|
122
|
+
View LICENSE for full license.
|
data/Rakefile
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
/*
|
2
|
+
Original code by Ben Croston modified for Ruby by Nick Lowery
|
3
|
+
(github.com/clockvapor)
|
4
|
+
Copyright (c) 2014-2015 Nick Lowery
|
5
|
+
|
6
|
+
Copyright (c) 2013-2014 Ben Croston
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
9
|
+
this software and associated documentation files (the "Software"), to deal in
|
10
|
+
the Software without restriction, including without limitation the rights to
|
11
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
12
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
13
|
+
so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
25
|
+
*/
|
26
|
+
|
27
|
+
#include <stdint.h>
|
28
|
+
#include <stdlib.h>
|
29
|
+
#include <fcntl.h>
|
30
|
+
#include <sys/mman.h>
|
31
|
+
#include "c_gpio.h"
|
32
|
+
|
33
|
+
#define BCM2708_PERI_BASE 0x20000000
|
34
|
+
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
|
35
|
+
#define FSEL_OFFSET 0 // 0x0000
|
36
|
+
#define SET_OFFSET 7 // 0x001c / 4
|
37
|
+
#define CLR_OFFSET 10 // 0x0028 / 4
|
38
|
+
#define PINLEVEL_OFFSET 13 // 0x0034 / 4
|
39
|
+
#define EVENT_DETECT_OFFSET 16 // 0x0040 / 4
|
40
|
+
#define RISING_ED_OFFSET 19 // 0x004c / 4
|
41
|
+
#define FALLING_ED_OFFSET 22 // 0x0058 / 4
|
42
|
+
#define HIGH_DETECT_OFFSET 25 // 0x0064 / 4
|
43
|
+
#define LOW_DETECT_OFFSET 28 // 0x0070 / 4
|
44
|
+
#define PULLUPDN_OFFSET 37 // 0x0094 / 4
|
45
|
+
#define PULLUPDNCLK_OFFSET 38 // 0x0098 / 4
|
46
|
+
|
47
|
+
#define PAGE_SIZE (4*1024)
|
48
|
+
#define BLOCK_SIZE (4*1024)
|
49
|
+
|
50
|
+
static volatile uint32_t *gpio_map;
|
51
|
+
|
52
|
+
void short_wait(void)
|
53
|
+
{
|
54
|
+
int i;
|
55
|
+
|
56
|
+
for (i=0; i<150; i++) // wait 150 cycles
|
57
|
+
{
|
58
|
+
asm volatile("nop");
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
int setup(void)
|
63
|
+
{
|
64
|
+
int mem_fd;
|
65
|
+
uint8_t *gpio_mem;
|
66
|
+
|
67
|
+
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0)
|
68
|
+
{
|
69
|
+
return SETUP_DEVMEM_FAIL;
|
70
|
+
}
|
71
|
+
|
72
|
+
if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
|
73
|
+
return SETUP_MALLOC_FAIL;
|
74
|
+
|
75
|
+
if ((uint32_t)gpio_mem % PAGE_SIZE)
|
76
|
+
gpio_mem += PAGE_SIZE - ((uint32_t)gpio_mem % PAGE_SIZE);
|
77
|
+
|
78
|
+
gpio_map = (uint32_t *)mmap( (caddr_t)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, GPIO_BASE);
|
79
|
+
|
80
|
+
if ((uint32_t)gpio_map < 0)
|
81
|
+
return SETUP_MMAP_FAIL;
|
82
|
+
|
83
|
+
return SETUP_OK;
|
84
|
+
}
|
85
|
+
|
86
|
+
void clear_event_detect(int gpio)
|
87
|
+
{
|
88
|
+
int offset = EVENT_DETECT_OFFSET + (gpio/32);
|
89
|
+
int shift = (gpio%32);
|
90
|
+
|
91
|
+
*(gpio_map+offset) |= (1 << shift);
|
92
|
+
short_wait();
|
93
|
+
*(gpio_map+offset) = 0;
|
94
|
+
}
|
95
|
+
|
96
|
+
int eventdetected(int gpio)
|
97
|
+
{
|
98
|
+
int offset, value, bit;
|
99
|
+
|
100
|
+
offset = EVENT_DETECT_OFFSET + (gpio/32);
|
101
|
+
bit = (1 << (gpio%32));
|
102
|
+
value = *(gpio_map+offset) & bit;
|
103
|
+
if (value)
|
104
|
+
{
|
105
|
+
clear_event_detect(gpio);
|
106
|
+
}
|
107
|
+
return value;
|
108
|
+
}
|
109
|
+
|
110
|
+
void set_rising_event(int gpio, int enable)
|
111
|
+
{
|
112
|
+
int offset = RISING_ED_OFFSET + (gpio/32);
|
113
|
+
int shift = (gpio%32);
|
114
|
+
|
115
|
+
if (enable)
|
116
|
+
*(gpio_map+offset) |= 1 << shift;
|
117
|
+
else
|
118
|
+
*(gpio_map+offset) &= ~(1 << shift);
|
119
|
+
clear_event_detect(gpio);
|
120
|
+
}
|
121
|
+
|
122
|
+
void set_falling_event(int gpio, int enable)
|
123
|
+
{
|
124
|
+
int offset = FALLING_ED_OFFSET + (gpio/32);
|
125
|
+
int shift = (gpio%32);
|
126
|
+
|
127
|
+
if (enable)
|
128
|
+
{
|
129
|
+
*(gpio_map+offset) |= (1 << shift);
|
130
|
+
*(gpio_map+offset) = (1 << shift);
|
131
|
+
} else {
|
132
|
+
*(gpio_map+offset) &= ~(1 << shift);
|
133
|
+
}
|
134
|
+
clear_event_detect(gpio);
|
135
|
+
}
|
136
|
+
|
137
|
+
void set_high_event(int gpio, int enable)
|
138
|
+
{
|
139
|
+
int offset = HIGH_DETECT_OFFSET + (gpio/32);
|
140
|
+
int shift = (gpio%32);
|
141
|
+
|
142
|
+
if (enable)
|
143
|
+
{
|
144
|
+
*(gpio_map+offset) |= (1 << shift);
|
145
|
+
} else {
|
146
|
+
*(gpio_map+offset) &= ~(1 << shift);
|
147
|
+
}
|
148
|
+
clear_event_detect(gpio);
|
149
|
+
}
|
150
|
+
|
151
|
+
void set_low_event(int gpio, int enable)
|
152
|
+
{
|
153
|
+
int offset = LOW_DETECT_OFFSET + (gpio/32);
|
154
|
+
int shift = (gpio%32);
|
155
|
+
|
156
|
+
if (enable)
|
157
|
+
*(gpio_map+offset) |= 1 << shift;
|
158
|
+
else
|
159
|
+
*(gpio_map+offset) &= ~(1 << shift);
|
160
|
+
clear_event_detect(gpio);
|
161
|
+
}
|
162
|
+
|
163
|
+
void set_pullupdn(int gpio, int pud)
|
164
|
+
{
|
165
|
+
int clk_offset = PULLUPDNCLK_OFFSET + (gpio/32);
|
166
|
+
int shift = (gpio%32);
|
167
|
+
|
168
|
+
if (pud == PUD_DOWN)
|
169
|
+
*(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_DOWN;
|
170
|
+
else if (pud == PUD_UP)
|
171
|
+
*(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_UP;
|
172
|
+
else // pud == PUD_OFF
|
173
|
+
*(gpio_map+PULLUPDN_OFFSET) &= ~3;
|
174
|
+
|
175
|
+
short_wait();
|
176
|
+
*(gpio_map+clk_offset) = 1 << shift;
|
177
|
+
short_wait();
|
178
|
+
*(gpio_map+PULLUPDN_OFFSET) &= ~3;
|
179
|
+
*(gpio_map+clk_offset) = 0;
|
180
|
+
}
|
181
|
+
|
182
|
+
void setup_gpio(int gpio, int direction, int pud)
|
183
|
+
{
|
184
|
+
int offset = FSEL_OFFSET + (gpio/10);
|
185
|
+
int shift = (gpio%10)*3;
|
186
|
+
|
187
|
+
set_pullupdn(gpio, pud);
|
188
|
+
if (direction == OUTPUT)
|
189
|
+
*(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift)) | (1<<shift);
|
190
|
+
else // direction == INPUT
|
191
|
+
*(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift));
|
192
|
+
}
|
193
|
+
|
194
|
+
// Contribution by Eric Ptak <trouch@trouch.com>
|
195
|
+
int gpio_function(int gpio)
|
196
|
+
{
|
197
|
+
int offset = FSEL_OFFSET + (gpio/10);
|
198
|
+
int shift = (gpio%10)*3;
|
199
|
+
int value = *(gpio_map+offset);
|
200
|
+
value >>= shift;
|
201
|
+
value &= 7;
|
202
|
+
return value; // 0=input, 1=output, 4=alt0
|
203
|
+
}
|
204
|
+
|
205
|
+
void output_gpio(int gpio, int value)
|
206
|
+
{
|
207
|
+
int offset, shift;
|
208
|
+
|
209
|
+
if (value) // value == HIGH
|
210
|
+
offset = SET_OFFSET + (gpio/32);
|
211
|
+
else // value == LOW
|
212
|
+
offset = CLR_OFFSET + (gpio/32);
|
213
|
+
|
214
|
+
shift = (gpio%32);
|
215
|
+
|
216
|
+
*(gpio_map+offset) = 1 << shift;
|
217
|
+
}
|
218
|
+
|
219
|
+
int input_gpio(int gpio)
|
220
|
+
{
|
221
|
+
int offset, value, mask;
|
222
|
+
|
223
|
+
offset = PINLEVEL_OFFSET + (gpio/32);
|
224
|
+
mask = (1 << gpio%32);
|
225
|
+
value = *(gpio_map+offset) & mask;
|
226
|
+
return value;
|
227
|
+
}
|
228
|
+
|
229
|
+
void cleanup(void)
|
230
|
+
{
|
231
|
+
// fixme - set all gpios back to input
|
232
|
+
munmap((caddr_t)gpio_map, BLOCK_SIZE);
|
233
|
+
}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
/*
|
2
|
+
Original code by Ben Croston modified for Ruby by Nick Lowery
|
3
|
+
(github.com/clockvapor)
|
4
|
+
Copyright (c) 2014-2015 Nick Lowery
|
5
|
+
|
6
|
+
Copyright (c) 2013-2014 Ben Croston
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
9
|
+
this software and associated documentation files (the "Software"), to deal in
|
10
|
+
the Software without restriction, including without limitation the rights to
|
11
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
12
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
13
|
+
so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
25
|
+
*/
|
26
|
+
|
27
|
+
int setup(void);
|
28
|
+
void setup_gpio(int gpio, int direction, int pud);
|
29
|
+
int gpio_function(int gpio);
|
30
|
+
void output_gpio(int gpio, int value);
|
31
|
+
int input_gpio(int gpio);
|
32
|
+
void set_rising_event(int gpio, int enable);
|
33
|
+
void set_falling_event(int gpio, int enable);
|
34
|
+
void set_high_event(int gpio, int enable);
|
35
|
+
void set_low_event(int gpio, int enable);
|
36
|
+
int eventdetected(int gpio);
|
37
|
+
void cleanup(void);
|
38
|
+
|
39
|
+
#define SETUP_OK 0
|
40
|
+
#define SETUP_DEVMEM_FAIL 1
|
41
|
+
#define SETUP_MALLOC_FAIL 2
|
42
|
+
#define SETUP_MMAP_FAIL 3
|
43
|
+
|
44
|
+
#define INPUT 1 // is really 0 for control register!
|
45
|
+
#define OUTPUT 0 // is really 1 for control register!
|
46
|
+
#define ALT0 4
|
47
|
+
|
48
|
+
#define PUD_OFF 0
|
49
|
+
#define PUD_DOWN 1
|
50
|
+
#define PUD_UP 2
|
@@ -0,0 +1,100 @@
|
|
1
|
+
/*
|
2
|
+
Original code by Ben Croston modified for Ruby by Nick Lowery
|
3
|
+
(github.com/clockvapor)
|
4
|
+
Copyright (c) 2014-2015 Nick Lowery
|
5
|
+
|
6
|
+
Copyright (c) 2013-2014 Ben Croston
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
9
|
+
this software and associated documentation files (the "Software"), to deal in
|
10
|
+
the Software without restriction, including without limitation the rights to
|
11
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
12
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
13
|
+
so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
25
|
+
*/
|
26
|
+
|
27
|
+
#include "ruby.h"
|
28
|
+
#include "c_gpio.h"
|
29
|
+
#include "common.h"
|
30
|
+
|
31
|
+
int gpio_mode = MODE_UNKNOWN;
|
32
|
+
const int pin_to_gpio_rev1[41] = {-1, -1, -1, 0, -1, 1, -1, 4, 14, -1, 15, 17,
|
33
|
+
18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1,
|
34
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1 };
|
35
|
+
const int pin_to_gpio_rev2[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17,
|
36
|
+
18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1,
|
37
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1 };
|
38
|
+
const int pin_to_gpio_rev3[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17,
|
39
|
+
18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12,
|
40
|
+
13, -1, 19, 16, 26, 20, -1, 21 };
|
41
|
+
int setup_error = 0;
|
42
|
+
int module_setup = 0;
|
43
|
+
int revision = -1;
|
44
|
+
|
45
|
+
int check_gpio_priv(void)
|
46
|
+
{
|
47
|
+
// check module has been imported cleanly
|
48
|
+
if (setup_error)
|
49
|
+
{
|
50
|
+
rb_raise(rb_eRuntimeError, "Gem not imported correctly!");
|
51
|
+
return 1;
|
52
|
+
}
|
53
|
+
|
54
|
+
// check mmap setup has worked
|
55
|
+
if (!module_setup)
|
56
|
+
{
|
57
|
+
rb_raise(rb_eRuntimeError, "No access to /dev/mem. Try "
|
58
|
+
"running as root!");
|
59
|
+
return 2;
|
60
|
+
}
|
61
|
+
return 0;
|
62
|
+
}
|
63
|
+
|
64
|
+
int get_gpio_number(int channel, unsigned int *gpio)
|
65
|
+
{
|
66
|
+
// check setmode() has been run
|
67
|
+
if (gpio_mode != BOARD && gpio_mode != BCM)
|
68
|
+
{
|
69
|
+
rb_raise(rb_eRuntimeError, "Please set pin numbering mode "
|
70
|
+
"using RPi::GPIO.set_mode(RPi::GPIO::BOARD) or "
|
71
|
+
"RPi::GPIO.set_mode(RPi::GPIO::BCM)");
|
72
|
+
return 3;
|
73
|
+
}
|
74
|
+
|
75
|
+
// check channel number is in range
|
76
|
+
if ( (gpio_mode == BCM && (channel < 0 || channel > 53))
|
77
|
+
|| (gpio_mode == BOARD && (channel < 1 || channel > 26) && revision != 3)
|
78
|
+
|| (gpio_mode == BOARD && (channel < 1 || channel > 40) && revision == 3))
|
79
|
+
{
|
80
|
+
rb_raise(rb_eArgError, "The channel sent is invalid on a Raspberry Pi");
|
81
|
+
return 4;
|
82
|
+
}
|
83
|
+
|
84
|
+
// convert channel to gpio
|
85
|
+
if (gpio_mode == BOARD)
|
86
|
+
{
|
87
|
+
if (*(*pin_to_gpio+channel) == -1)
|
88
|
+
{
|
89
|
+
rb_raise(rb_eArgError, "The channel sent is invalid on a Raspberry "
|
90
|
+
"Pi");
|
91
|
+
return 5;
|
92
|
+
} else {
|
93
|
+
*gpio = *(*pin_to_gpio+channel);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
else // gpio_mode == BCM
|
97
|
+
*gpio = channel;
|
98
|
+
|
99
|
+
return 0;
|
100
|
+
}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
/*
|
2
|
+
Original code by Ben Croston modified for Ruby by Nick Lowery
|
3
|
+
(github.com/clockvapor)
|
4
|
+
Copyright (c) 2014-2015 Nick Lowery
|
5
|
+
|
6
|
+
Copyright (c) 2013-2014 Ben Croston
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
9
|
+
this software and associated documentation files (the "Software"), to deal in
|
10
|
+
the Software without restriction, including without limitation the rights to
|
11
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
12
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
13
|
+
so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
25
|
+
*/
|
26
|
+
|
27
|
+
#define MODE_UNKNOWN -1
|
28
|
+
#define BOARD 10
|
29
|
+
#define BCM 11
|
30
|
+
#define SERIAL 40
|
31
|
+
#define SPI 41
|
32
|
+
#define I2C 42
|
33
|
+
#define PWM 43
|
34
|
+
|
35
|
+
int gpio_mode;
|
36
|
+
const int pin_to_gpio_rev1[41];
|
37
|
+
const int pin_to_gpio_rev2[41];
|
38
|
+
const int pin_to_gpio_rev3[41];
|
39
|
+
const int (*pin_to_gpio)[41];
|
40
|
+
int gpio_direction[54];
|
41
|
+
int revision;
|
42
|
+
|
43
|
+
int check_gpio_priv(void);
|
44
|
+
int get_gpio_number(int channel, unsigned int *gpio);
|
45
|
+
int setup_error;
|
46
|
+
int module_setup;
|
@@ -0,0 +1,61 @@
|
|
1
|
+
/*
|
2
|
+
Original code by Ben Croston modified for Ruby by Nick Lowery
|
3
|
+
(github.com/clockvapor)
|
4
|
+
Copyright (c) 2014-2015 Nick Lowery
|
5
|
+
|
6
|
+
Copyright (c) 2013-2014 Ben Croston
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
9
|
+
this software and associated documentation files (the "Software"), to deal in
|
10
|
+
the Software without restriction, including without limitation the rights to
|
11
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
12
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
13
|
+
so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
25
|
+
*/
|
26
|
+
|
27
|
+
#include "ruby.h"
|
28
|
+
#include "constants.h"
|
29
|
+
#include "common.h"
|
30
|
+
#include "c_gpio.h"
|
31
|
+
#include "event_gpio.h"
|
32
|
+
|
33
|
+
void define_constants(VALUE module)
|
34
|
+
{
|
35
|
+
pwm = INT2NUM(PWM);
|
36
|
+
rb_define_const(module, "HARD_PWM", pwm);
|
37
|
+
|
38
|
+
serial = INT2NUM(SERIAL);
|
39
|
+
rb_define_const(module, "SERIAL", serial);
|
40
|
+
|
41
|
+
i2c = INT2NUM(I2C);
|
42
|
+
rb_define_const(module, "I2C", i2c);
|
43
|
+
|
44
|
+
spi = INT2NUM(SPI);
|
45
|
+
rb_define_const(module, "SPI", spi);
|
46
|
+
|
47
|
+
unknown = INT2NUM(MODE_UNKNOWN);
|
48
|
+
rb_define_const(module, "UNKNOWN", unknown);
|
49
|
+
|
50
|
+
rising_edge = INT2NUM(RISING_EDGE + PY_EVENT_CONST_OFFSET);
|
51
|
+
rb_define_const(module, "RISING", rising_edge);
|
52
|
+
|
53
|
+
falling_edge = INT2NUM(FALLING_EDGE + PY_EVENT_CONST_OFFSET);
|
54
|
+
rb_define_const(module, "FALLING", falling_edge);
|
55
|
+
|
56
|
+
both_edge = INT2NUM(BOTH_EDGE + PY_EVENT_CONST_OFFSET);
|
57
|
+
rb_define_const(module, "BOTH", both_edge);
|
58
|
+
|
59
|
+
version = rb_str_new2("0.5.8");
|
60
|
+
rb_define_const(module, "VERSION", version);
|
61
|
+
}
|