试图用nodejs库gpIO控制移位寄存器在树莓派上不起作用



我试图控制一个移位寄存器在nodejs使用库的enotionz/gpiO..

库:https://github.com/EnotionZ/GpiO

我不知道为什么不能让它工作。

预期结果是74hc595n移位寄存器在引脚0上循环。用于控制移位寄存器的引脚在两组代码中都被设置为变量。

我开发的python代码工作得很好:

我相信它循环通过每个区域,取决于你在setShiftRegister(<arr key>)中设置的是它需要启用哪个"区域"。

我在python中也包含了一个工作示例…

这是我的js代码:

var gpio = require('gpio');
var sr_data, sr_clock, sr_latch, sr_oe,
    stations = [0,0,0,0,0,0,0,0];
console.log('hello there');
//shift register DS data (GPIO27, PIN 13)
sr_data = gpio.export(13, {
    direction: 'out',
    ready:function(){ cb('exported sr_data'); }
});
//shift register SH_CP clock (GPIO4, PIN 7)
sr_clock = gpio.export(7, {
    direction: 'out',
    ready:function(){ cb('exported sr_clock'); }
});
//shift register ST_CP latch pin (GPIO22, PIN 15)
sr_latch = gpio.export(15, {
    direction: 'out',
    ready:function(){ cb('exported sr_latch'); }
});
//shift register OE output enable, goes to ground (GPIO17, PIN 11)
sr_oe = gpio.export(11, {
    direction: 'out',
    ready:function(){
        cb('exported sr_oe');
        sr_oe.set(0);
    }
});
setTimeout(function(){
    console.log('Enabling SR Pin: ', 0);
    //set the latch pin low for as long as we are clocking through the shift register
    console.log('-----------------------------------');
    //shift pins up using bitwise, i = pin #
    setShiftRegister(7);
    enableShiftRegisterOutput();
}, 5000);
//set the latch pin high to signal chip that it no longer needs to listen for information

function setShiftRegister(p){
    sr_clock.set(0);
    sr_latch.set(0);
    var num_stations = stations.length;
    stations[p] = 1;
    console.log('num_stations: ', num_stations);
    for (var i = stations.length - 1; i >= 0; i--) {
        var station = stations[num_stations-1-i];
        console.log('SR PIN: ' + (num_stations-1-i) + ' STATION ARR ID: ' + i  + ' STATION VALUE: ' + station);
        sr_clock.set(0);
        //sets pin to high or low depending on pinState
        sr_data.set(station);
        //register shift bits on upstroke of clock pin
        sr_clock.set(1);
    }
    sr_latch.set(1, function(){
        console.log('latch set');
    });
}
function enableShiftRegisterOutput(){
    sr_oe.set(1, function(){
        cb('enabling shift register');
    });
}
function cleanup(){
    sr_clock.unexport();
    sr_data.unexport();
    sr_latch.unexport();
    sr_oe.unexport();
    console.log('pin cleanup done');
}
function cb(message){
    console.log(message);
}
// function setShiftRegister(srvals, zones, cb){
//  GPIO.write(pin_sr_clk, false);
//  GPIO.write(pin_sr_lat, false);
//  for (var i = zones.length - 1; i >= 0; i--) {
//      console.log('zones.length: ', zones.length);
//      console.log('i: ', i);
//      var zone = zones[i];
//      GPIO.write(pin_sr_clk, false);
//      GPIO.write(pin_sr_dat, srvals[zones.length - 1 - i]); //have to decrement result by 1 as Shift Register Gate starts at 0
//      GPIO.write(pin_sr_clk, true);
//  };
//  GPIO.write(pin_sr_lat, true);           
//  cb();
// }
// setShiftRegister(srvals, zones);

这是运行

的python代码
import RPi.GPIO as GPIO
import atexit
#GPIO PIN DEFINES
pin_sr_clk =  4
pin_sr_noe = 17
pin_sr_dat = 27 # NOTE: if you have a RPi rev.2, need to change this to 27
pin_sr_lat = 22
# NUMBER OF STATIONS
num_stations = 8
# STATION BITS
values = [0]*num_stations
def enableShiftRegisterOutput():
    GPIO.output(pin_sr_noe, False)
def disableShiftRegisterOutput():
    GPIO.output(pin_sr_noe, True)
def setShiftRegister(values):
    GPIO.output(pin_sr_clk, False)
    GPIO.output(pin_sr_lat, False)
    for s in range(0,num_stations):
        print num_stations-1-s
    print values[num_stations-1-s]
    GPIO.output(pin_sr_clk, False)
        GPIO.output(pin_sr_dat, values[num_stations-1-s])
        GPIO.output(pin_sr_clk, True)
    GPIO.output(pin_sr_lat, True)
def run():
    GPIO.cleanup()
    # setup GPIO pins to interface with shift register
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(pin_sr_clk, GPIO.OUT)
    GPIO.setup(pin_sr_noe, GPIO.OUT)
    disableShiftRegisterOutput()
    GPIO.setup(pin_sr_dat, GPIO.OUT)
    GPIO.setup(pin_sr_lat, GPIO.OUT)
    values[0]=1 #this is the equivalent of setting array key 0 = 1 (or true)
    print values
    setShiftRegister(values)
    enableShiftRegisterOutput()
def progexit():
    global values
    values = [0]*num_stations
    setShiftRegister(values)
    GPIO.cleanup()
if __name__ == '__main__':
    atexit.register(progexit)
    run()

我不能100%确定您的程序应该做什么,但至少我可以帮助您解决异步性中的问题。

所有的i/o操作实际上都是异步的,所以你不能指望它们按顺序发生。

作为一个例子,你的代码应该看起来像这样:
var async = require('async');
var gpio = require('gpio');
var stations = [0,0,0,0,0,0,0,0];
var pins = {
    data: 13,
    clock: 7,
    latch: 15,
    oe: 11
};
var lastLatch = 1;
var sr = {};
function resetClock(cb) {
    sr.clock.set(0, cb);
}
function setLatch(cb) {
    lastLatch = lastLatch ? 0 : 1;
    sr.latch.set(lastLatch, cb);
}
function exportPin(name, cb) {
    sr[name] = gpio.export(pins[name], {
        direction: 'out',
        ready: cb
    });
}
function stationIterator(station, callback) {
    console.log('setting station', stations.indexOf(station), station);
    async.series([
        resetClock,
        function setStation(cb) {
            sr.data.set(station, cb);
        },
        function setClock(cb) {
            sr.clock.set(1, cb)
        }
    ], callback);
}
function setShiftRegister(p, mainCallback) {
    async.series([resetClock, setLatch], function setupCallback() {
        var num_stations = stations.length;
        stations[p] = 1;
        console.log('num_stations: ', num_stations);
        async.mapSeries(stations, stationIterator,  function mapSeriesCallback() {
            console.log('setting latch');
            setLatch(function enableShiftRegisterOutput() {
                sr.oe.set(1, mainCallback);
            });
        });
    });
}
function startUp() {
    console.log('Enabling SR Pin: ', 0);
    sr.oe.set(0, function () {    
        console.log('-----------------------------------');
        setShiftRegister(7, function registerSetCallback() {
            console.log('all set');
        });
    });
}
async.map(Object.keys(pins), exportPin, startUp);

最新更新