I am trying to control a shift register in nodejs using the library by enotionz/gpiO..
library here: https://github.com/EnotionZ/GpiO
I cant get it to work for some reason.
The expected result is for a 74hc595n shift register to cycle pin 0 on. The pins identified to control the shift register are set as variables in both sets of code.
The python code that i have developed works great:
I believe it cycles through each zone and depending on what you set in setShiftRegister(<arr key>)
is which "zone" it needs to enable.
I have included a working example in python as well..
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 the python code which 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()
I am trying to control a shift register in nodejs using the library by enotionz/gpiO..
library here: https://github.com/EnotionZ/GpiO
I cant get it to work for some reason.
The expected result is for a 74hc595n shift register to cycle pin 0 on. The pins identified to control the shift register are set as variables in both sets of code.
The python code that i have developed works great:
I believe it cycles through each zone and depending on what you set in setShiftRegister(<arr key>)
is which "zone" it needs to enable.
I have included a working example in python as well..
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 the python code which 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()
0 commentaires:
Enregistrer un commentaire