EnotionZ / gpio

Talk to your Single Board Computer's gpio pins
MIT License
394 stars 57 forks source link

Fast Pulse #19

Open pacmac opened 11 years ago

pacmac commented 11 years ago

Hi;

Using a scope to monitor the pin, I seem to be unable to get a pulse faster than 10ms using the following code, but I really need the pulse width to be in microseconds rather than milliseconds, hence the setTimeout value of 0, which I was hoping would last a few clock cycles. Even if I remove the SetTimeout() function, the pulse width is still 10ms.

there is a 1K pullup on gpio.4.

The following code is intended to pull gpio4 low for 500us, then release it and then read whether the connected device has pulled the line low (presence) and log the result to the console, however I am unable to get a pulse width less than 10ms, any ideas why ??

var dq;
function dqinit(cb){
    dq = gpio.export(4, {
       ready: function() {
          dq.set(0);
          setTimeout(function() {
            dq.set();
            dq = gpio.export(4, {
               direction: "in",
               ready: function() {
                    cb(dq.value); 
               }
            });         
          },0);
       }
    }); 
}

dqinit(function(val){
    console.log(val);
});
EnotionZ commented 11 years ago

You actually just need to export the header once. In the ready callback, run dq.set(0, function(){ dq.set(1, function(){ cb(dq.value); }); }); I'm on my iPad so it's a bit hard to type, hope that makes sense.

vicary commented 11 years ago

Most browsers (including Chrome) has a minimum countdown around 5-8ms with setTimeout(..., 0), which is considered an artifact of some kind of complicated theory and is yet to be resolved.

I don't know if V8 has fixed this, better use the internal Timer class in nodejs IMO.

I have a fork that takes an array of 1 and 0s for SPI bit banging, which pipes them directly into C for a way more reasonable output.

@EnotionZ Do you think µs preciseness can be done in javascript?

kilianc commented 11 years ago

@vicary you can. Use process.hrtime() or the microtime module. You can run as fast as your cpu clock does + T(V8 runloop) + T(your code).

EnotionZ commented 11 years ago

@vicary the bottleneck is probably the fact that we're using node's filesystem to write header values. I bet if we wrote a node c++ module that wraps the wiringPi library, we'd get faster timing. As @kilianc said, we can take a high resolution (nanosecond) timestamp with process.hrtime() to validate.

vicary commented 11 years ago

I am not very sure about how gpiolib.c works, I guess it bypasses the filesystem writes? If that's true, can nodejs works directly with that layer instead of writing through the filesystem?