Git Product home page Git Product logo

Comments (8)

fivdi avatar fivdi commented on July 22, 2024

On which platform? They are configured differently on each platform. As onoff plays well with the quick2wire gpio-admin and the WiringPi gpio utilities, either of these tools can be used to configure pull-up and pull-down resistors on th Pi.

from onoff.

Muximize avatar Muximize commented on July 22, 2024

What exactly do you mean by "Plays well"? Running a unix command from node to turn them on?

Like so?

var exec = require('child_process').exec;
var Gpio = require('onoff').Gpio,

exec('gpio-admin export 18 pulldown', function (error, stdout, stderr) {
  console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
});

button = new Gpio(18, 'in', 'both');

I understand that it might be difficult if they are configured differently on each platform, but it would be nice if this could be abstracted away in the library, maybe as extra direction parameters, as counterparts to the 'high' and 'low' values. Also, these could be more clear. How about using constants?

Gpio.OUT
Gpio.OUT_LOW
Gpio.OUT_HIGH
Gpio.IN
Gpio.IN_PULLUP
Gpio.IN_PULLDOWN

from onoff.

fivdi avatar fivdi commented on July 22, 2024

@Muximize

What exactly do you mean by "Plays well"?

The intended meaning here is that the quick2wire gpio-admin or the WiringPi gpio utilities can be used to export gpios and configure pull-up or pull-down resistors. Thereafter, a Node.js application using onoff can be executed without superuser privileges. The onoff Gpio constructor is implemented to take this into account.

Does the code above that exports pin 18 using gpio-admin require superuser privileges to execute successfully?

The first four of the six constants you suggest are a good idea. I'll add those. I'd prefer not to add pull-up/pull-down configuration capabilities to onoff if it means that superuser privileges are required.

from onoff.

Muximize avatar Muximize commented on July 22, 2024

I'd rather not use those utilities seperately from/manually with my node.js code, I like my applications self-contained.

The code I posted works without superuser privileges, just running node as user pi. That user needs to be in the gpio group that gpio-admin creates on install, as stated in their instructions. Slightly refined:

function exportPin(pin, pull, callback) {
    var command = "gpio-admin export " + pin + (pull ? " " + pull : "");
    exec(command, function(error, stdout, stderr) {
        if (error !== null) {
            console.log(error);
        } else {
            setTimeout(callback, 500);  // avoid EACCES
        }
    });
}

Without the timeout I sometimes get EACCES issues when using new Gpio() in the callback. Seems to have something to do with issue #20.

About the constants, I think Gpio.OUT might not be a good idea after all, as outputs have to be either high or low, and just setting it to ouput it will make it go to some unknown default state (which may differ per pin) until it is written to. Forcing the user to choose either one makes for clearer, less error-prone code and circuits.

from onoff.

fireblade983 avatar fireblade983 commented on July 22, 2024

I would like to use this with raspberry pi with the new version 1.0.0. Can you give na exemple how to configure the pullup/down.

Thanks

from onoff.

fivdi avatar fivdi commented on July 22, 2024

@fireblade983 sorry for the delay, I somehow missed your post.

Here's how to configure pullup/down resistors using the WiringPi gpio utility.
First, install WiringPi as described here.
After a successful install, the gpio utility will be available. It's described here.

Lets say you want to configure the pulldown resistor on GPIO8. This can be acheived with the following command sequence:

pi@raspberrypi ~ $ gpio -g mode 8 down
pi@raspberrypi ~ $ 

If nothing is wired up to GPIO8, reading it's value should return 0. To see this cd into /sys/class/gpio try the following:

pi@raspberrypi /sys/class/gpio $ echo 8 > export 
pi@raspberrypi /sys/class/gpio $ cat gpio8/value 
0
pi@raspberrypi /sys/class/gpio $ 

To configure the pullup resistor on GPIO8:

pi@raspberrypi ~ $ gpio -g mode 8 up
pi@raspberrypi ~ $ 

If nothing is wired up to GPIO8, reading it's value should return 1. To see this cd into /sys/class/gpio try the following:

pi@raspberrypi /sys/class/gpio $ cat gpio8/value 
1
pi@raspberrypi /sys/class/gpio $ 

So, if you write a bash script to configure the pullup/down resistors the way you want them, and execute this bash script before you start your Node.js program, the pullup/down resistors will be configured as desired.

from onoff.

fivdi avatar fivdi commented on July 22, 2024

In the not too distant future Raspbian will support the Linux device tree. When this is the case, onoff should be able to configure pullup/down resistors without requiring additional tools such as WiringPi.

from onoff.

fivdi avatar fivdi commented on July 22, 2024

The January 2015 release of Raspbian with Linux kernel 3.18.5 supports the device tree and has it enabled by default. This means that it's now possible to use device tree overlays to configure pullup and pulldown resistors for GPIOs. The onoff Wiki page Enabling Pullup and Pulldown Resistors on The Raspberry Pi describes how this can be acheived on the Raspberry Pi.

Using device tree overlays to configure pullup and pulldown resistors goes with the grain of how Linux would like things to be done. Device tree overlays are loaded at boot time. There are techniques that can be used to configure pullup and pulldown resistors at runtime, but these generally require root privileges and this is a road that I don't want to go down with onoff.

from onoff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.