rpi_gpio 0.1.3 → 0.1.4

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: 4059220d806257737dc29e65f4539e82e36a8c44
4
- data.tar.gz: 1b0c4294c22a09525f19f050fc2a2bdbccc4f374
3
+ metadata.gz: bb3eca91931a655322781bb97858870321541c69
4
+ data.tar.gz: 283cd9f48b7af7d11db72fd3d38118ebd9d12a19
5
5
  SHA512:
6
- metadata.gz: 710e9e5d14f5397473c63a855af0f6d0755ef252eb9e6372c5f1dc1081bb62ea01813da466e2baac4c7d552d5b2d5108e5bddb0411e2cabd3ba0e252fbc34ad1
7
- data.tar.gz: 9d2dda1389655992ebf9c3406011fc881b622d449a9e9196093e3c3f5b762e63688c310f468f564af471eaf94d8d20eb2052adc255bbef8ae56f7f2968b97514
6
+ metadata.gz: e603d6197144360f18af04c48dc3c9dcb8bc5ffc63e8050817e30d8ff4fa14728cb63b60506f879eb3f078ef5e9dfb7ee34158378c47317b3e9c6c46d9bbd2fe
7
+ data.tar.gz: 7042bb0dbed8696695e63f86449d81167f2e1f1ac9ee166a10b6b6a73aa3abf308947b253926c57df66a970d5a0da891e2edc1829468827d8d33eb67b5d9dccc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rpi_gpio (0.1.2)
4
+ rpi_gpio (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- #rpi_gpio v0.1.3
1
+ #rpi_gpio v0.1.4
2
2
 
3
3
  Ruby conversion of [RPi.GPIO Python module](https://pypi.python.org/pypi/RPi.GPIO)
4
4
 
@@ -166,9 +166,9 @@ void set_pullupdn(int gpio, int pud)
166
166
  int shift = (gpio%32);
167
167
 
168
168
  if (pud == PUD_DOWN)
169
- *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_DOWN;
169
+ *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET)&~3)|PUD_DOWN;
170
170
  else if (pud == PUD_UP)
171
- *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_UP;
171
+ *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET)&~3)|PUD_UP;
172
172
  else // pud == PUD_OFF
173
173
  *(gpio_map+PULLUPDN_OFFSET) &= ~3;
174
174
 
@@ -102,7 +102,8 @@ int gpio_set_direction(unsigned int gpio, unsigned int in_flag)
102
102
  int fd;
103
103
  char filename[33];
104
104
 
105
- snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/direction", gpio);
105
+ snprintf(filename, sizeof(filename),
106
+ "/sys/class/gpio/gpio%d/direction", gpio);
106
107
  if ((fd = open(filename, O_WRONLY)) < 0)
107
108
  return -1;
108
109
 
@@ -527,12 +528,16 @@ int blocking_wait_for_edge(unsigned int gpio, unsigned int edge, int bouncetime)
527
528
  epoll_ctl(epfd_blocking, EPOLL_CTL_DEL, g->value_fd, &ev);
528
529
  return 2;
529
530
  }
530
- if (g->initial_wait) { // first time triggers with current state, so ignore
531
- g->initial_wait = 0;
531
+ // first time triggers with current state, so ignore
532
+ if (g->initial_wait) {
533
+ g->initial_wait = 0;
532
534
  } else {
533
535
  gettimeofday(&tv_timenow, NULL);
534
536
  timenow = tv_timenow.tv_sec*1E6 + tv_timenow.tv_usec;
535
- if (g->bouncetime == -666 || timenow - g->lastcall > g->bouncetime*1000 || g->lastcall == 0 || g->lastcall > timenow) {
537
+ if (g->bouncetime == -666 ||
538
+ timenow - g->lastcall > g->bouncetime*1000 ||
539
+ g->lastcall == 0 ||
540
+ g->lastcall > timenow) {
536
541
  g->lastcall = timenow;
537
542
  finished = 1;
538
543
  }
@@ -33,15 +33,14 @@ void define_gpio_module_stuff(void)
33
33
  {
34
34
  int i;
35
35
 
36
- rb_define_singleton_method(m_GPIO, "setup", GPIO_setup, 2);
37
- rb_define_singleton_method(m_GPIO, "clean_up", GPIO_clean_up, -1);
38
- rb_define_singleton_method(m_GPIO, "set_numbering", GPIO_set_numbering, 1);
39
- rb_define_singleton_method(m_GPIO, "set_high", GPIO_set_high, 1);
40
- rb_define_singleton_method(m_GPIO, "set_low", GPIO_set_low, 1);
41
- rb_define_singleton_method(m_GPIO, "high?", GPIO_test_high, 1);
42
- rb_define_singleton_method(m_GPIO, "low?", GPIO_test_low, 1);
43
- rb_define_singleton_method(m_GPIO, "set_warnings", GPIO_set_warnings, 1);
44
- define_constants(m_GPIO);
36
+ rb_define_module_function(m_GPIO, "setup", GPIO_setup, 2);
37
+ rb_define_module_function(m_GPIO, "clean_up", GPIO_clean_up, -1);
38
+ rb_define_module_function(m_GPIO, "set_numbering", GPIO_set_numbering, 1);
39
+ rb_define_module_function(m_GPIO, "set_high", GPIO_set_high, 1);
40
+ rb_define_module_function(m_GPIO, "set_low", GPIO_set_low, 1);
41
+ rb_define_module_function(m_GPIO, "high?", GPIO_test_high, 1);
42
+ rb_define_module_function(m_GPIO, "low?", GPIO_test_low, 1);
43
+ rb_define_module_function(m_GPIO, "set_warnings", GPIO_set_warnings, 1);
45
44
 
46
45
  for (i=0; i<54; i++)
47
46
  gpio_direction[i] = -1;
@@ -95,6 +94,51 @@ int mmap_gpio_mem(void)
95
94
  }
96
95
  }
97
96
 
97
+ int is_gpio_input(unsigned int gpio)
98
+ {
99
+ if (gpio_direction[gpio] != INPUT)
100
+ {
101
+ if (gpio_direction[gpio] != OUTPUT)
102
+ {
103
+ rb_raise(rb_eRuntimeError, "you must setup() the GPIO channel first");
104
+ return 0;
105
+ }
106
+
107
+ rb_raise(rb_eRuntimeError, "GPIO channel not setup as input");
108
+ return 0;
109
+ }
110
+
111
+ return 1;
112
+ }
113
+
114
+ int is_gpio_output(unsigned int gpio)
115
+ {
116
+ if (gpio_direction[gpio] != OUTPUT)
117
+ {
118
+ if (gpio_direction[gpio] != INPUT)
119
+ {
120
+ rb_raise(rb_eRuntimeError, "you must setup() the GPIO channel first");
121
+ return 0;
122
+ }
123
+
124
+ rb_raise(rb_eRuntimeError, "GPIO channel not setup as output");
125
+ return 0;
126
+ }
127
+
128
+ return 1;
129
+ }
130
+
131
+ int is_rpi(void)
132
+ {
133
+ if (setup_error)
134
+ {
135
+ rb_raise(rb_eRuntimeError, "this gem can only be run on a Raspberry Pi");
136
+ return 0;
137
+ }
138
+
139
+ return 1;
140
+ }
141
+
98
142
  // RPi::GPIO.clean_up(channel=nil)
99
143
  // clean up everything by default; otherwise, clean up given channel
100
144
  VALUE GPIO_clean_up(int argc, VALUE *argv, VALUE self)
@@ -108,7 +152,8 @@ VALUE GPIO_clean_up(int argc, VALUE *argv, VALUE self)
108
152
  channel = NUM2INT(argv[0]);
109
153
  else if (argc > 1)
110
154
  {
111
- rb_raise(rb_eArgError, "wrong number of arguments; 0 for all pins, 1 for a specific pin");
155
+ rb_raise(rb_eArgError, "wrong number of arguments; 0 for all pins, "
156
+ "1 for a specific pin");
112
157
  return Qnil;
113
158
  }
114
159
 
@@ -164,7 +209,7 @@ VALUE GPIO_setup(VALUE self, VALUE channel, VALUE hash)
164
209
  int direction;
165
210
  VALUE pud_val = Qnil;
166
211
  const char *pud_str = NULL;
167
- int pud = PUD_OFF + PY_PUD_CONST_OFFSET;
212
+ int pud = PUD_OFF;
168
213
  int func;
169
214
 
170
215
  // func to set up channel stored in channel variable
@@ -215,11 +260,11 @@ VALUE GPIO_setup(VALUE self, VALUE channel, VALUE hash)
215
260
 
216
261
  pud_str = rb_id2name(rb_to_id(pud_val));
217
262
  if (strcmp("down", pud_str) == 0)
218
- pud = PUD_DOWN + PY_PUD_CONST_OFFSET;
263
+ pud = PUD_DOWN;
219
264
  else if (strcmp("up", pud_str) == 0)
220
- pud = PUD_UP + PY_PUD_CONST_OFFSET;
265
+ pud = PUD_UP;
221
266
  else if (strcmp("off", pud_str) == 0)
222
- pud = PUD_OFF + PY_PUD_CONST_OFFSET;
267
+ pud = PUD_OFF;
223
268
  else
224
269
  {
225
270
  rb_raise(rb_eArgError, "invalid pin pull direction; must be :up, :down, "
@@ -228,16 +273,9 @@ VALUE GPIO_setup(VALUE self, VALUE channel, VALUE hash)
228
273
  }
229
274
  }
230
275
  else
231
- pud = PUD_OFF + PY_PUD_CONST_OFFSET;
232
-
233
- // check module has been imported cleanly
234
- if (setup_error)
235
- {
236
- rb_raise(rb_eRuntimeError, "gem not imported correctly");
237
- return Qnil;
238
- }
276
+ pud = PUD_OFF;
239
277
 
240
- if (mmap_gpio_mem())
278
+ if (!is_rpi() || mmap_gpio_mem())
241
279
  return Qnil;
242
280
 
243
281
  if (direction != INPUT && direction != OUTPUT)
@@ -247,8 +285,7 @@ VALUE GPIO_setup(VALUE self, VALUE channel, VALUE hash)
247
285
  }
248
286
 
249
287
  if (direction == OUTPUT)
250
- pud = PUD_OFF + PY_PUD_CONST_OFFSET;
251
- pud -= PY_PUD_CONST_OFFSET;
288
+ pud = PUD_OFF;
252
289
 
253
290
  if (!setup_one())
254
291
  return Qnil;
@@ -273,11 +310,8 @@ VALUE GPIO_set_numbering(VALUE self, VALUE mode)
273
310
  else
274
311
  rb_raise(rb_eArgError, "invalid numbering mode; must be :board or :bcm");
275
312
 
276
- if (setup_error)
277
- {
278
- rb_raise(rb_eRuntimeError, "gem not imported correctly");
313
+ if (!is_rpi())
279
314
  return Qnil;
280
- }
281
315
 
282
316
  if (gpio_mode != BOARD && gpio_mode != BCM)
283
317
  {
@@ -299,31 +333,14 @@ VALUE GPIO_set_numbering(VALUE self, VALUE mode)
299
333
  VALUE GPIO_set_high(VALUE self, VALUE channel)
300
334
  {
301
335
  unsigned int gpio;
302
- int chan = -1;
336
+ int chan = NUM2INT(channel);
303
337
 
304
- // func to output value var on channel var
305
- int output(void)
306
- {
307
- if (get_gpio_number(chan, &gpio))
308
- return 0;
309
-
310
- if (gpio_direction[gpio] != OUTPUT)
311
- {
312
- rb_raise(rb_eRuntimeError, "GPIO channel not set as output");
313
- return 0;
314
- }
315
-
316
- if (check_gpio_priv())
317
- return 0;
318
-
319
- output_gpio(gpio, 1);
320
- return 1;
321
- }
322
-
323
- chan = NUM2INT(channel);
324
- if (!output())
325
- return Qnil;
338
+ if (get_gpio_number(chan, &gpio) ||
339
+ !is_gpio_output(gpio) ||
340
+ check_gpio_priv())
341
+ return Qnil;
326
342
 
343
+ output_gpio(gpio, 1);
327
344
  return self;
328
345
  }
329
346
 
@@ -331,31 +348,14 @@ VALUE GPIO_set_high(VALUE self, VALUE channel)
331
348
  VALUE GPIO_set_low(VALUE self, VALUE channel)
332
349
  {
333
350
  unsigned int gpio;
334
- int chan = -1;
351
+ int chan = NUM2INT(channel);
335
352
 
336
- // func to output value var on channel var
337
- int output(void)
338
- {
339
- if (get_gpio_number(chan, &gpio))
340
- return 0;
341
-
342
- if (gpio_direction[gpio] != OUTPUT)
343
- {
344
- rb_raise(rb_eRuntimeError, "GPIO channel not set as output");
345
- return 0;
346
- }
347
-
348
- if (check_gpio_priv())
349
- return 0;
350
-
351
- output_gpio(gpio, 0);
352
- return 1;
353
- }
354
-
355
- chan = NUM2INT(channel);
356
- if (!output())
357
- return Qnil;
353
+ if (get_gpio_number(chan, &gpio) ||
354
+ !is_gpio_output(gpio) ||
355
+ check_gpio_priv())
356
+ return Qnil;
358
357
 
358
+ output_gpio(gpio, 0);
359
359
  return self;
360
360
  }
361
361
 
@@ -364,41 +364,25 @@ VALUE GPIO_test_high(VALUE self, VALUE channel)
364
364
  {
365
365
  unsigned int gpio;
366
366
 
367
- if (get_gpio_number(NUM2INT(channel), &gpio))
367
+ if (get_gpio_number(NUM2INT(channel), &gpio) ||
368
+ !is_gpio_input(gpio) ||
369
+ check_gpio_priv())
368
370
  return Qnil;
369
-
370
- if (gpio_direction[gpio] != INPUT && gpio_direction[gpio] != OUTPUT)
371
- {
372
- rb_raise(rb_eRuntimeError, "you must setup() the GPIO channel first");
373
- return Qnil;
374
- }
375
-
376
- if (check_gpio_priv())
377
- return Qnil;
378
-
379
- if (input_gpio(gpio))
380
- return Qtrue;
381
- else
382
- return Qfalse;
371
+
372
+ return input_gpio(gpio) ? Qtrue : Qfalse;
383
373
  }
384
374
 
385
375
  // RPi::GPIO.low?(channel)
386
376
  VALUE GPIO_test_low(VALUE self, VALUE channel)
387
377
  {
388
- VALUE val = GPIO_test_high(self, channel);
389
- if (val == Qtrue) return Qfalse;
390
- else if (val == Qfalse) return Qtrue;
391
- else return Qnil;
378
+ return GPIO_test_high(self, channel) ? Qfalse : Qtrue;
392
379
  }
393
380
 
394
381
  // RPi::GPIO.set_warnings(state)
395
382
  VALUE GPIO_set_warnings(VALUE self, VALUE setting)
396
383
  {
397
- if (setup_error)
398
- {
399
- rb_raise(rb_eRuntimeError, "gem not imported correctly");
384
+ if (!is_rpi())
400
385
  return Qnil;
401
- }
402
386
 
403
387
  gpio_warnings = NUM2INT(setting);
404
388
  return self;
@@ -29,11 +29,13 @@ SOFTWARE.
29
29
  #include "event_gpio.h"
30
30
  #include "cpuinfo.h"
31
31
  #include "common.h"
32
- #include "constants.h"
33
32
  #include "rb_pwm.h"
34
33
 
35
34
  void define_gpio_module_stuff(void);
36
35
  int mmap_gpio_mem(void);
36
+ int is_gpio_input(unsigned int gpio);
37
+ int is_gpio_output(unsigned int gpio);
38
+ int is_rpi(void);
37
39
  VALUE GPIO_clean_up(int argc, VALUE *argv, VALUE self);
38
40
  VALUE GPIO_setup(VALUE self, VALUE channel, VALUE hash);
39
41
  VALUE GPIO_set_numbering(VALUE self, VALUE mode);
@@ -42,3 +44,4 @@ VALUE GPIO_set_low(VALUE self, VALUE channel);
42
44
  VALUE GPIO_test_high(VALUE self, VALUE channel);
43
45
  VALUE GPIO_test_low(VALUE self, VALUE channel);
44
46
  VALUE GPIO_set_warnings(VALUE self, VALUE setting);
47
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpi_gpio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Lowery
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-08 00:00:00.000000000 Z
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -40,8 +40,6 @@ files:
40
40
  - ext/rpi_gpio/c_gpio.h
41
41
  - ext/rpi_gpio/common.c
42
42
  - ext/rpi_gpio/common.h
43
- - ext/rpi_gpio/constants.c
44
- - ext/rpi_gpio/constants.h
45
43
  - ext/rpi_gpio/cpuinfo.c
46
44
  - ext/rpi_gpio/cpuinfo.h
47
45
  - ext/rpi_gpio/event_gpio.c
@@ -55,7 +53,6 @@ files:
55
53
  - ext/rpi_gpio/rpi_gpio.h
56
54
  - ext/rpi_gpio/soft_pwm.c
57
55
  - ext/rpi_gpio/soft_pwm.h
58
- - lib/rpi_gpio.so
59
56
  homepage: https://github.com/ClockVapor/rpi_gpio
60
57
  licenses:
61
58
  - MIT
@@ -1,61 +0,0 @@
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
- }
@@ -1,40 +0,0 @@
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 PY_PUD_CONST_OFFSET 20
28
- #define PY_EVENT_CONST_OFFSET 30
29
-
30
- VALUE pwm;
31
- VALUE serial;
32
- VALUE i2c;
33
- VALUE spi;
34
- VALUE unknown;
35
- VALUE rising_edge;
36
- VALUE falling_edge;
37
- VALUE both_edge;
38
- VALUE version;
39
-
40
- void define_constants(VALUE module);
data/lib/rpi_gpio.so DELETED
Binary file