python - 尝试使用 nodejs lib gpIO 控制移位寄存器在树莓派上不起作用

标签 python node.js raspberry-pi gpio

我正在尝试使用 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 代码

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);

关于python - 尝试使用 nodejs lib gpIO 控制移位寄存器在树莓派上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17129389/

相关文章:

javascript - JSON.stringify 中的注释字段被忽略

javascript - 如何使用 dropbox.js 将文件上传到 Dropbox?

python - 如何在 Flask 中处理 JSON?

python - Raspbian python代码卡住

python - Windows 下 Django 的 syncdb 错误(OpenKey 错误?)

python - concat 后如何避免 pandas DataFrame 中的重复索引?

Python 正则表达式 - 从右到左

Python邮件脚本并将变量插入html代码

java - 如何使用 Java 中的几行代码将给定的 Before 链接转换为 After 链接?

opencv - 将视频从 Raspberry Pi 传输到运行 OpenCV 的桌面