rpi_gpio 0.5.0 → 0.7.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c751d8fda46ee296e1df93542416ba668f7f6d9d05142f7647ce63310be42873
4
- data.tar.gz: 471e4e0dc50ed6b265c9f9dd3ff01a4cb332ac288c8210b161c4bd69ee9cd4d1
3
+ metadata.gz: e85a39df6149e36f76739cbb642208a635cc20fa3a7e201ea0207c2070fc4765
4
+ data.tar.gz: 36b825467b90ebe19c1bcd431847bbc596ea94fdd31e7c5b99fdda31f3efc87c
5
5
  SHA512:
6
- metadata.gz: 00ef0c4360e5f9c41aec4cf7274249c7dae11fcadc06ad54e27b474a2ea95397fb6727a1a8360e8261fe88f3aa765d904739314da8bc5b1730cc8319c5db2042
7
- data.tar.gz: 76a6df3e90bfc0ab0ee47030c1c18cb36098b981b72ac5ff855ca5e5abc424a9c5e7452a8e8f1c47507dc846db6ee694de6a49d06fe7bd10bae4529b7c98a9cf
6
+ metadata.gz: fa6f1aa7414cdcfc349b2f06c4108fc6819634f8f6a1d3dd5aa70cf8f6e040e0c0a06d32342af3d36c5207673c86640895918e4c6651e74205b6da62d8608056
7
+ data.tar.gz: bed6f48051ae7eb6c62eb3394cd18c7f1e4f8b5b543a50a38b9ebe091260b0f5a512b46aa89b69f0e3a011c58f51e9a0921b0d2f76acccaf5640a3ed072ff1bc
data/LICENSE CHANGED
@@ -1,7 +1,7 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2014-2020 Nick Lowery
4
- Copyright (c) 2012-2014 Ben Croston
3
+ Copyright (c) 2014-2026 Nick Lowery
4
+ Copyright (c) 2012-2021 Ben Croston
5
5
 
6
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # rpi_gpio v0.5.0
1
+ # rpi_gpio v0.7.1
2
2
 
3
3
  Ruby conversion of [RPi.GPIO Python module](https://pypi.python.org/pypi/RPi.GPIO)
4
4
 
@@ -10,7 +10,7 @@ Manipulate your Raspberry Pi's GPIO pins from Ruby!
10
10
  - Software-driven PWM (written in C for speed)
11
11
  - Event-driven input (blocking and non-blocking)
12
12
 
13
- Up-to-date with RPi.GPIO Python module version 0.7.0, so it works on all Raspberry Pi models!
13
+ Up-to-date with RPi.GPIO Python module version 0.7.1, so it works on all Raspberry Pi models!
14
14
 
15
15
  ## Sample Usage
16
16
 
@@ -180,7 +180,7 @@ to clean up all pins and to also reset the selected numbering mode.
180
180
 
181
181
  Original Python code by Ben Croston modified for Ruby by Nick Lowery
182
182
 
183
- Copyright (c) 2014-2020 [Nick Lowery](https://github.com/ClockVapor)
183
+ Copyright (c) 2014-2026 [Nick Lowery](https://github.com/ClockVapor)
184
184
 
185
185
  View LICENSE for full license.
186
186
 
@@ -0,0 +1,345 @@
1
+ /*
2
+ Original code by Ben Croston modified for Ruby by Nick Lowery
3
+ (github.com/clockvapor)
4
+ Copyright (c) 2014-2026 Nick Lowery
5
+
6
+ Copyright (c) 2012-2021 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 <stdio.h>
28
+ #include <stdint.h>
29
+ #include <stdlib.h>
30
+ #include <fcntl.h>
31
+ #include <sys/mman.h>
32
+ #include <string.h>
33
+ #include "c_gpio.h"
34
+
35
+ #define BCM2708_PERI_BASE_DEFAULT 0x20000000
36
+ #define BCM2709_PERI_BASE_DEFAULT 0x3f000000
37
+ #define BCM2710_PERI_BASE_DEFAULT 0x3f000000
38
+ #define BCM2711_PERI_BASE_DEFAULT 0xfe000000
39
+ #define GPIO_BASE_OFFSET 0x200000
40
+ #define FSEL_OFFSET 0 // 0x0000
41
+ #define SET_OFFSET 7 // 0x001c / 4
42
+ #define CLR_OFFSET 10 // 0x0028 / 4
43
+ #define PINLEVEL_OFFSET 13 // 0x0034 / 4
44
+ #define EVENT_DETECT_OFFSET 16 // 0x0040 / 4
45
+ #define RISING_ED_OFFSET 19 // 0x004c / 4
46
+ #define FALLING_ED_OFFSET 22 // 0x0058 / 4
47
+ #define HIGH_DETECT_OFFSET 25 // 0x0064 / 4
48
+ #define LOW_DETECT_OFFSET 28 // 0x0070 / 4
49
+ #define PULLUPDN_OFFSET 37 // 0x0094 / 4
50
+ #define PULLUPDNCLK_OFFSET 38 // 0x0098 / 4
51
+
52
+ #define PULLUPDN_OFFSET_2711_0 57
53
+ #define PULLUPDN_OFFSET_2711_1 58
54
+ #define PULLUPDN_OFFSET_2711_2 59
55
+ #define PULLUPDN_OFFSET_2711_3 60
56
+
57
+ #define PAGE_SIZE (4*1024)
58
+ #define BLOCK_SIZE (4*1024)
59
+
60
+ static volatile uint32_t *gpio_map;
61
+
62
+ void short_wait(void)
63
+ {
64
+ int i;
65
+
66
+ for (i=0; i<150; i++) { // wait 150 cycles
67
+ asm volatile("nop");
68
+ }
69
+ }
70
+
71
+ int setup(void)
72
+ {
73
+ int mem_fd;
74
+ uint8_t *gpio_mem;
75
+ uint32_t peri_base = 0;
76
+ uint32_t gpio_base;
77
+ uint8_t ranges[12] = { 0 };
78
+ uint8_t rev[4] = { 0 };
79
+ uint32_t cpu = 0;
80
+ FILE *fp;
81
+ char buffer[1024];
82
+ char hardware[1024];
83
+ int found = 0;
84
+
85
+ // try /dev/gpiomem first - this does not require root privs
86
+ if ((mem_fd = open("/dev/gpiomem", O_RDWR|O_SYNC)) > 0)
87
+ {
88
+ if ((gpio_map = (uint32_t *)mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, 0)) == MAP_FAILED) {
89
+ return SETUP_MMAP_FAIL;
90
+ } else {
91
+ return SETUP_OK;
92
+ }
93
+ }
94
+
95
+ // revert to /dev/mem method - requires root privileges
96
+
97
+ if ((fp = fopen("/proc/device-tree/soc/ranges", "rb")) != NULL)
98
+ {
99
+ // get peri base from device tree
100
+ if (fread(ranges, 1, sizeof(ranges), fp) >= 8) {
101
+ peri_base = ranges[4] << 24 | ranges[5] << 16 | ranges[6] << 8 | ranges[7] << 0;
102
+ if (!peri_base) {
103
+ peri_base = ranges[8] << 24 | ranges[9] << 16 | ranges[10] << 8 | ranges[11] << 0;
104
+ }
105
+ }
106
+ if ((ranges[0] != 0x7e) ||
107
+ (ranges[1] != 0x00) ||
108
+ (ranges[2] != 0x00) ||
109
+ (ranges[3] != 0x00) ||
110
+ ((peri_base != BCM2708_PERI_BASE_DEFAULT) &&
111
+ (peri_base != BCM2709_PERI_BASE_DEFAULT) &&
112
+ (peri_base != BCM2711_PERI_BASE_DEFAULT))) {
113
+ // invalid ranges file
114
+ peri_base = 0;
115
+ }
116
+ fclose(fp);
117
+ }
118
+
119
+ // guess peri_base based on /proc/device-tree/system/linux,revision
120
+ if (!peri_base) {
121
+ if ((fp = fopen("/proc/device-tree/system/linux,revision", "rb")) != NULL) {
122
+ if (fread(rev, 1, sizeof(rev), fp) == 4) {
123
+ cpu = (rev[2] >> 4) & 0xf;
124
+ switch (cpu) {
125
+ case 0 : peri_base = BCM2708_PERI_BASE_DEFAULT;
126
+ break;
127
+ case 1 :
128
+ case 2 : peri_base = BCM2709_PERI_BASE_DEFAULT;
129
+ break;
130
+ case 3 : peri_base = BCM2711_PERI_BASE_DEFAULT;
131
+ break;
132
+ }
133
+ }
134
+ fclose(fp);
135
+ }
136
+ }
137
+
138
+ // guess peri_base based on /proc/cpuinfo hardware field
139
+ if (!peri_base) {
140
+ if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
141
+ return SETUP_CPUINFO_FAIL;
142
+
143
+ while(!feof(fp) && !found && fgets(buffer, sizeof(buffer), fp)) {
144
+ sscanf(buffer, "Hardware : %s", hardware);
145
+ if (strcmp(hardware, "BCM2708") == 0 || strcmp(hardware, "BCM2835") == 0) {
146
+ // pi 1 hardware
147
+ peri_base = BCM2708_PERI_BASE_DEFAULT;
148
+ } else if (strcmp(hardware, "BCM2709") == 0 || strcmp(hardware, "BCM2836") == 0) {
149
+ // pi 2 hardware
150
+ peri_base = BCM2709_PERI_BASE_DEFAULT;
151
+ } else if (strcmp(hardware, "BCM2710") == 0 || strcmp(hardware, "BCM2837") == 0) {
152
+ // pi 3 hardware
153
+ peri_base = BCM2710_PERI_BASE_DEFAULT;
154
+ } else if (strcmp(hardware, "BCM2711") == 0) {
155
+ // pi 4 hardware
156
+ peri_base = BCM2711_PERI_BASE_DEFAULT;
157
+ }
158
+ }
159
+ fclose(fp);
160
+ }
161
+
162
+ if (!peri_base)
163
+ return SETUP_NO_PERI_ADDR;
164
+
165
+ gpio_base = peri_base + GPIO_BASE_OFFSET;
166
+
167
+ // mmap the GPIO memory registers
168
+ if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0)
169
+ return SETUP_DEVMEM_FAIL;
170
+
171
+ if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
172
+ return SETUP_MALLOC_FAIL;
173
+
174
+ if ((uint32_t)gpio_mem % PAGE_SIZE)
175
+ gpio_mem += PAGE_SIZE - ((uint32_t)gpio_mem % PAGE_SIZE);
176
+
177
+ if ((gpio_map = (uint32_t *)mmap( (void *)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, gpio_base)) == MAP_FAILED)
178
+ return SETUP_MMAP_FAIL;
179
+
180
+ return SETUP_OK;
181
+ }
182
+
183
+ void clear_event_detect(int gpio)
184
+ {
185
+ int offset = EVENT_DETECT_OFFSET + (gpio/32);
186
+ int shift = (gpio%32);
187
+
188
+ *(gpio_map+offset) |= (1 << shift);
189
+ short_wait();
190
+ *(gpio_map+offset) = 0;
191
+ }
192
+
193
+ int eventdetected(int gpio)
194
+ {
195
+ int offset, value, bit;
196
+
197
+ offset = EVENT_DETECT_OFFSET + (gpio/32);
198
+ bit = (1 << (gpio%32));
199
+ value = *(gpio_map+offset) & bit;
200
+ if (value)
201
+ clear_event_detect(gpio);
202
+ return value;
203
+ }
204
+
205
+ void set_rising_event(int gpio, int enable)
206
+ {
207
+ int offset = RISING_ED_OFFSET + (gpio/32);
208
+ int shift = (gpio%32);
209
+
210
+ if (enable)
211
+ *(gpio_map+offset) |= 1 << shift;
212
+ else
213
+ *(gpio_map+offset) &= ~(1 << shift);
214
+ clear_event_detect(gpio);
215
+ }
216
+
217
+ void set_falling_event(int gpio, int enable)
218
+ {
219
+ int offset = FALLING_ED_OFFSET + (gpio/32);
220
+ int shift = (gpio%32);
221
+
222
+ if (enable) {
223
+ *(gpio_map+offset) |= (1 << shift);
224
+ *(gpio_map+offset) = (1 << shift);
225
+ } else {
226
+ *(gpio_map+offset) &= ~(1 << shift);
227
+ }
228
+ clear_event_detect(gpio);
229
+ }
230
+
231
+ void set_high_event(int gpio, int enable)
232
+ {
233
+ int offset = HIGH_DETECT_OFFSET + (gpio/32);
234
+ int shift = (gpio%32);
235
+
236
+ if (enable)
237
+ *(gpio_map+offset) |= (1 << shift);
238
+ else
239
+ *(gpio_map+offset) &= ~(1 << shift);
240
+ clear_event_detect(gpio);
241
+ }
242
+
243
+ void set_low_event(int gpio, int enable)
244
+ {
245
+ int offset = LOW_DETECT_OFFSET + (gpio/32);
246
+ int shift = (gpio%32);
247
+
248
+ if (enable)
249
+ *(gpio_map+offset) |= 1 << shift;
250
+ else
251
+ *(gpio_map+offset) &= ~(1 << shift);
252
+ clear_event_detect(gpio);
253
+ }
254
+
255
+ void set_pullupdn(int gpio, int pud)
256
+ {
257
+ // Check GPIO register
258
+ int is2711 = *(gpio_map+PULLUPDN_OFFSET_2711_3) != 0x6770696f;
259
+ if (is2711) {
260
+ // Pi 4 Pull-up/down method
261
+ int pullreg = PULLUPDN_OFFSET_2711_0 + (gpio >> 4);
262
+ int pullshift = (gpio & 0xf) << 1;
263
+ unsigned int pullbits;
264
+ unsigned int pull = 0;
265
+ switch (pud) {
266
+ case PUD_OFF: pull = 0; break;
267
+ case PUD_UP: pull = 1; break;
268
+ case PUD_DOWN: pull = 2; break;
269
+ default: pull = 0; // switch PUD to OFF for other values
270
+ }
271
+ pullbits = *(gpio_map + pullreg);
272
+ pullbits &= ~(3 << pullshift);
273
+ pullbits |= (pull << pullshift);
274
+ *(gpio_map + pullreg) = pullbits;
275
+ } else {
276
+ // Legacy Pull-up/down method
277
+ int clk_offset = PULLUPDNCLK_OFFSET + (gpio/32);
278
+ int shift = (gpio%32);
279
+
280
+ if (pud == PUD_DOWN) {
281
+ *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_DOWN;
282
+ } else if (pud == PUD_UP) {
283
+ *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_UP;
284
+ } else { // pud == PUD_OFF
285
+ *(gpio_map+PULLUPDN_OFFSET) &= ~3;
286
+ }
287
+ short_wait();
288
+ *(gpio_map+clk_offset) = 1 << shift;
289
+ short_wait();
290
+ *(gpio_map+PULLUPDN_OFFSET) &= ~3;
291
+ *(gpio_map+clk_offset) = 0;
292
+ }
293
+ }
294
+
295
+ void setup_gpio(int gpio, int direction, int pud)
296
+ {
297
+ int offset = FSEL_OFFSET + (gpio/10);
298
+ int shift = (gpio%10)*3;
299
+
300
+ set_pullupdn(gpio, pud);
301
+ if (direction == OUTPUT)
302
+ *(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift)) | (1<<shift);
303
+ else // direction == INPUT
304
+ *(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift));
305
+ }
306
+
307
+ // Contribution by Eric Ptak <trouch@trouch.com>
308
+ int gpio_function(int gpio)
309
+ {
310
+ int offset = FSEL_OFFSET + (gpio/10);
311
+ int shift = (gpio%10)*3;
312
+ int value = *(gpio_map+offset);
313
+ value >>= shift;
314
+ value &= 7;
315
+ return value; // 0=input, 1=output, 4=alt0
316
+ }
317
+
318
+ void output_gpio(int gpio, int value)
319
+ {
320
+ int offset, shift;
321
+
322
+ if (value) // value == HIGH
323
+ offset = SET_OFFSET + (gpio/32);
324
+ else // value == LOW
325
+ offset = CLR_OFFSET + (gpio/32);
326
+
327
+ shift = (gpio%32);
328
+
329
+ *(gpio_map+offset) = 1 << shift;
330
+ }
331
+
332
+ int input_gpio(int gpio)
333
+ {
334
+ int offset, value, mask;
335
+
336
+ offset = PINLEVEL_OFFSET + (gpio/32);
337
+ mask = (1 << gpio%32);
338
+ value = *(gpio_map+offset) & mask;
339
+ return value;
340
+ }
341
+
342
+ void cleanup(void)
343
+ {
344
+ munmap((void *)gpio_map, BLOCK_SIZE);
345
+ }
@@ -0,0 +1,55 @@
1
+ /*
2
+ Original code by Ben Croston modified for Ruby by Nick Lowery
3
+ (github.com/clockvapor)
4
+ Copyright (c) 2014-2026 Nick Lowery
5
+
6
+ Copyright (c) 2012-2021 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
+ #define SETUP_CPUINFO_FAIL 4
44
+ #define SETUP_NO_PERI_ADDR 5
45
+
46
+ #define INPUT 1 // is really 0 for control register!
47
+ #define OUTPUT 0 // is really 1 for control register!
48
+ #define ALT0 4
49
+
50
+ #define HIGH 1
51
+ #define LOW 0
52
+
53
+ #define PUD_OFF 0
54
+ #define PUD_DOWN 1
55
+ #define PUD_UP 2
@@ -0,0 +1,96 @@
1
+ /*
2
+ Original code by Ben Croston modified for Ruby by Nick Lowery
3
+ (github.com/clockvapor)
4
+ Copyright (c) 2014-2026 Nick Lowery
5
+
6
+ Copyright (c) 2013-2021 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, 18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
33
+ const int pin_to_gpio_rev2[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
34
+ const int pin_to_gpio_rev3[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12, 13, -1, 19, 16, 26, 20, -1, 21 };
35
+ const int (*pin_to_gpio)[41];
36
+ int gpio_direction[54];
37
+ rpi_info rpiinfo;
38
+ int setup_error = 0;
39
+ int module_setup = 0;
40
+
41
+ int check_gpio_priv(void)
42
+ {
43
+ // check module has been imported cleanly
44
+ if (setup_error)
45
+ {
46
+ rb_raise(rb_eRuntimeError, "gem not imported correctly!");
47
+ return 1;
48
+ }
49
+
50
+ // check mmap setup has worked
51
+ if (!module_setup)
52
+ {
53
+ rb_raise(rb_eRuntimeError, "no access to /dev/mem. try running as root!");
54
+ return 2;
55
+ }
56
+ return 0;
57
+ }
58
+
59
+ int get_gpio_number(int channel, unsigned int *gpio)
60
+ {
61
+ // check setmode() has been run
62
+ if (gpio_mode != BOARD && gpio_mode != BCM)
63
+ {
64
+ rb_raise(rb_eRuntimeError, "please set pin numbering mode "
65
+ "using RPi::GPIO.set_numbering :board or "
66
+ "RPi::GPIO.set_numbering :bcm");
67
+ return 3;
68
+ }
69
+
70
+ // check channel number is in range
71
+ if ( (gpio_mode == BCM && (channel < 0 || channel > 53))
72
+ || (gpio_mode == BOARD && (channel < 1 || channel > 26) && rpiinfo.p1_revision != 3)
73
+ || (gpio_mode == BOARD && (channel < 1 || channel > 40) && rpiinfo.p1_revision == 3) )
74
+ {
75
+ rb_raise(rb_eArgError, "the channel sent is invalid on a Raspberry Pi");
76
+ return 4;
77
+ }
78
+
79
+ // convert channel to gpio
80
+ if (gpio_mode == BOARD)
81
+ {
82
+ if (*(*pin_to_gpio+channel) == -1)
83
+ {
84
+ rb_raise(rb_eArgError, "the channel sent is invalid on a Raspberry Pi");
85
+ return 5;
86
+ } else {
87
+ *gpio = *(*pin_to_gpio+channel);
88
+ }
89
+ }
90
+ else // gpio_mode == BCM
91
+ {
92
+ *gpio = channel;
93
+ }
94
+
95
+ return 0;
96
+ }
@@ -0,0 +1,48 @@
1
+ /*
2
+ Original code by Ben Croston modified for Ruby by Nick Lowery
3
+ (github.com/clockvapor)
4
+ Copyright (c) 2014-2026 Nick Lowery
5
+
6
+ Copyright (c) 2013-2021 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 "cpuinfo.h"
28
+
29
+ #define MODE_UNKNOWN -1
30
+ #define BOARD 10
31
+ #define BCM 11
32
+ #define SERIAL 40
33
+ #define SPI 41
34
+ #define I2C 42
35
+ #define PWM 43
36
+
37
+ extern int gpio_mode;
38
+ extern const int pin_to_gpio_rev1[41];
39
+ extern const int pin_to_gpio_rev2[41];
40
+ extern const int pin_to_gpio_rev3[41];
41
+ extern const int (*pin_to_gpio)[41];
42
+ extern int gpio_direction[54];
43
+ extern rpi_info rpiinfo;
44
+ extern int setup_error;
45
+ extern int module_setup;
46
+
47
+ int check_gpio_priv(void);
48
+ int get_gpio_number(int channel, unsigned int *gpio);