尝试用nodejs lib gpIO控制移位寄存器在raspberry pi上不起作用

2024-03-28 13:57:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用enotionz/gpiO的库控制nodejs中的移位寄存器。。在

图书馆在这里: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代码,可以工作

^{pr2}$

Tags: registerloggpioshiftvarpinfunctionconsole
1条回答
网友
1楼 · 发布于 2024-03-28 13:57:02

我不是百分之百确定你的程序应该做什么,但至少我可以帮助你解决异步性的问题。在

所有的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);

相关问题 更多 >