rpi_gpio 0.3.0 → 0.3.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.
Files changed (3) hide show
  1. data/README.md +8 -1
  2. data/ext/rpi_gpio/rb_gpio.c +33 -13
  3. metadata +2 -2
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # rpi_gpio v0.3.0
1
+ # rpi_gpio v0.3.1
2
2
 
3
3
  Ruby conversion of [RPi.GPIO Python module](https://pypi.python.org/pypi/RPi.GPIO)
4
4
 
@@ -70,6 +70,13 @@ RPi::GPIO.set_low PIN_NUM
70
70
  ```
71
71
  to set the pin either high or low.
72
72
 
73
+ You can use the additional hash argument `:initialize` to set the pin's initial state like so:
74
+ ```ruby
75
+ RPi::GPIO.setup PIN_NUM, :as => :output, :initialize => :high
76
+ # or
77
+ RPi::GPIO.setup PIN_NUM, :as => :output, :initialize => :low
78
+ ```
79
+
73
80
  #### PWM (pulse-width modulation)
74
81
 
75
82
  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](#output).
@@ -97,7 +97,7 @@ int is_gpio_input(unsigned int gpio)
97
97
  {
98
98
  if (gpio_direction[gpio] != INPUT) {
99
99
  if (gpio_direction[gpio] != OUTPUT) {
100
- rb_raise(rb_eRuntimeError,
100
+ rb_raise(rb_eRuntimeError,
101
101
  "you must setup the GPIO channel first with "
102
102
  "RPi::GPIO.setup CHANNEL, :as => :input or "
103
103
  "RPi::GPIO.setup CHANNEL, :as => :output");
@@ -232,16 +232,16 @@ VALUE GPIO_setup(VALUE self, VALUE channel, VALUE hash)
232
232
  func = gpio_function(gpio);
233
233
  if (gpio_warnings &&
234
234
  ((func != 0 && func != 1) ||
235
- (gpio_direction[gpio] == -1 && func == 1))) {
236
- rb_warn("this channel is already in use... continuing anyway. "
237
- "use RPi::GPIO.set_warnings(false) to disable warnings");
235
+ (gpio_direction[gpio] == -1 && func == 1)))
236
+ {
237
+ rb_warn("this channel is already in use... continuing anyway. use RPi::GPIO.set_warnings(false) to disable warnings");
238
238
  }
239
239
 
240
240
  if (gpio_warnings) {
241
241
  if (rpiinfo.p1_revision == 0) { // compute module - do nothing
242
- } else if ((rpiinfo.p1_revision == 1 &&
243
- (gpio == 0 || gpio == 1)) ||
244
- (gpio == 2 || gpio == 3)) {
242
+ } else if ((rpiinfo.p1_revision == 1 &&
243
+ (gpio == 0 || gpio == 1)) ||
244
+ (gpio == 2 || gpio == 3)) {
245
245
  if (pud == PUD_UP || pud == PUD_DOWN) {
246
246
  rb_warn("a physical pull up resistor is fitted on "
247
247
  "this channel");
@@ -260,15 +260,14 @@ VALUE GPIO_setup(VALUE self, VALUE channel, VALUE hash)
260
260
  chan = NUM2INT(channel);
261
261
 
262
262
  // pin direction
263
- direction_str = rb_id2name(rb_to_id(rb_hash_aref(hash,
264
- ID2SYM(rb_intern("as")))));
263
+ direction_str = rb_id2name(rb_to_id(rb_hash_aref(hash, ID2SYM(rb_intern("as")))));
265
264
  if (strcmp("input", direction_str) == 0) {
266
265
  direction = INPUT;
267
266
  } else if (strcmp("output", direction_str) == 0) {
268
267
  direction = OUTPUT;
269
268
  } else {
270
- rb_raise(rb_eArgError,
271
- "invalid pin direction; must be :input or :output");
269
+ rb_raise(rb_eArgError,
270
+ "invalid pin direction; must be :input or :output");
272
271
  }
273
272
 
274
273
  // pull up, down, or off
@@ -294,6 +293,27 @@ VALUE GPIO_setup(VALUE self, VALUE channel, VALUE hash)
294
293
  } else {
295
294
  pud = PUD_OFF;
296
295
  }
296
+ // initialize high or low
297
+ initialize_val = rb_hash_aref(hash, ID2SYM(rb_intern("initialize")));
298
+ if (initialize_val != Qnil) {
299
+ if (direction == INPUT) {
300
+ rb_raise(rb_eArgError, "input pins cannot use initial argument");
301
+ return Qnil;
302
+ }
303
+
304
+ initialize_str = rb_id2name(rb_to_id(initialize_val));
305
+ if (strcmp("high", initialize_str) == 0) {
306
+ initialize = HIGH;
307
+ } else if (strcmp("low", initialize_str) == 0) {
308
+ initialize = LOW;
309
+ } else {
310
+ rb_raise(rb_eArgError,
311
+ "invalid pin initialize state; must be :high or :low");
312
+ return Qnil;
313
+ }
314
+ } else {
315
+ initialize = HIGH;
316
+ }
297
317
 
298
318
  if (!is_rpi() || mmap_gpio_mem()) {
299
319
  return Qnil;
@@ -332,7 +352,7 @@ VALUE GPIO_set_numbering(VALUE self, VALUE mode)
332
352
  } else if (strcmp(mode_str, "bcm") == 0) {
333
353
  new_mode = BCM;
334
354
  } else {
335
- rb_raise(rb_eArgError,
355
+ rb_raise(rb_eArgError,
336
356
  "invalid numbering mode; must be :board or :bcm");
337
357
  }
338
358
 
@@ -351,7 +371,7 @@ VALUE GPIO_set_numbering(VALUE self, VALUE mode)
351
371
  return Qnil;
352
372
  }
353
373
 
354
- gpio_mode = new_mode;
374
+ gpio_mode = new_mode;
355
375
  return self;
356
376
  }
357
377
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpi_gpio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-07 00:00:00.000000000 Z
12
+ date: 2016-10-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake-compiler