jperkin / node-rpio

Raspberry Pi GPIO library for node.js
858 stars 124 forks source link

Add mode option to close method #30

Closed robertlarue closed 7 years ago

robertlarue commented 7 years ago

Overview

Add mode option to rpio.close() method to prevent GPIO defaulting to INPUT on close()

I would like to keep a particular GPIO pin as an output after closing the pin, and it was not possible with the current method because it always set it to an INPUT when closing. This change would allow the user to explicitly define the closing mode through the optional mode parameter.

Problem

Here we write a particular output to LOW and then close. I would like the output to stay LOW after closing. In the current method, the GPIO would be set to INPUT and the output would not stay LOW, but rather go LOW momentarily, and then go HIGH.

rpio.open(pinNumber, rpio.OUTPUT, rpio.HIGH);
rpio.write(pinNumber, rpio.LOW);
rpio.close(pinNumber);

Solution

Instead of assuming that the GPIO should be set to an INPUT when closing, allow the user to specify what the ending mode should be. In the example below, one can explicitly define the closing mode.

rpio.open(pinNumber, rpio.OUTPUT, rpio.LOW);
rpio.write(pinNumber, rpio.HIGH);
rpio.close(pinNumber, rpio.OUTPUT);
jperkin commented 7 years ago

Thanks for this. Can you think of a reason why we shouldn't just leave the mode alone by default?

jperkin commented 7 years ago

How about this (untested)?

diff --git a/README.md b/README.md
index f5ad787..13ce2aa 100644
--- a/README.md
+++ b/README.md
@@ -467 +467 @@ rpio.poll(12, nuke_button, rpio.POLL_HIGH);
-#### `rpio.close(pin)`
+#### `rpio.close(pin[, reset])`
@@ -469,2 +469,9 @@ rpio.poll(12, nuke_button, rpio.POLL_HIGH);
-Reset `pin` to `rpio.INPUT` and clear any pullup/pulldown resistors and poll
-events.
+Indicate that the pin will no longer be used, and clear any poll events
+associated with it.
+
+The optional `reset` argument can be used to configure the state that `pin`
+will be left in after close:
+
+* `rpio.PIN_RESET`: return pin to `rpio.INPUT` and clear any pullup/pulldown
+  resistors.  This is the default.
+* `rpio.PIN_PRESERVE`: leave pin in its currently configured state.
@@ -476,2 +483,2 @@ rpio.close(11);
-rpio.close(12);
-rpio.close(13);
+rpio.close(12, rpio.PIN_RESET);
+rpio.close(13, rpio.PIN_PRESERVE);
diff --git a/lib/rpio.js b/lib/rpio.js
index c6935e4..46a28fb 100644
--- a/lib/rpio.js
+++ b/lib/rpio.js
@@ -59,0 +60,6 @@ rpio.prototype.POLL_BOTH = 0x3;   /* POLL_LOW | POLL_HIGH */
+ * Reset pin status on close (default), or preserve current status.
+ */
+rpio.prototype.PIN_PRESERVE = 0x0;
+rpio.prototype.PIN_RESET = 0x1;
+
+/*
@@ -503 +509 @@ rpio.prototype.poll = function(pin, cb, direction)
-rpio.prototype.close = function(pin)
+rpio.prototype.close = function(pin, reset)
@@ -507,4 +513,2 @@ rpio.prototype.close = function(pin)
-   if (!rpio_options.gpiomem)
-       rpio.prototype.pud(pin, rpio.prototype.PULL_OFF);
-
-   rpio.prototype.mode(pin, rpio.prototype.INPUT);
+   if (reset === undefined)
+       reset = rpio.prototype.PIN_RESET;
@@ -513,0 +518,6 @@ rpio.prototype.close = function(pin)
+
+   if (reset) {
+       if (!rpio_options.gpiomem)
+           rpio.prototype.pud(pin, rpio.prototype.PULL_OFF);
+       rpio.prototype.mode(pin, rpio.prototype.INPUT);
+   }
robertlarue commented 7 years ago

I think that is a good solution. I have made the patches you suggested to my master branch.

jperkin commented 7 years ago

I've published 0.9.15 to include this, thanks!