Attempting to control shift register with nodejs lib gpIO does not work on raspberry pi

I am trying to control the shift register in nodejs using the enotionz / gpiO library ..

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

I can not get it to work for some reason.

The expected result for the shift register is 74hc595n for a 0 on loop output. The pins identified to control the shift register are set as variables in both sets of code.

The python code I developed works fine:

I believe that it cyclically passes through each zone and depending on what you set in setShiftRegister(<arr key>) , in which "zone" it needs to be included.

I also included a working example in python.

here is my js code:

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

This is python code that works

 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() 
+4
source share
1 answer

I am not 100% sure what your program should do, but at least I can help you with asynchronous issues.

All i / o operations are actually asynchronous, so you cannot just expect them to happen in order.

As an example, your code should look something like this:

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

Source: https://habr.com/ru/post/1486442/


All Articles