Skip to content

Commit

Permalink
set maximum SPI speed
Browse files Browse the repository at this point in the history
The raspberry linux kernel updated the default spi frequency to 125MHz:

kernel: BCM270X_DT: Set spidev spi-max-frequency to 125MHz.
See raspberrypi/linux#2165.

The WS2801 LEDs can only handle 10 MHz, but when not set the default value is used, which is too much.
This commit explicit includes the spi package and sets the max speed for the spidev
  • Loading branch information
Trekky12 committed Jan 3, 2018
1 parent aed276d commit 15f4658
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A node.js library to control a WS2801 RGB LED stripe via SPI with your Raspberry Pi",
"main": "rpi-ws2801.js",
"dependencies": {
"microtime": "*"
"microtime": "*",
"spi":"*"
},
"devDependencies": {},
"scripts": {
Expand Down
37 changes: 22 additions & 15 deletions rpi-ws2801.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var fs = require('fs');
var microtime = require('microtime');
var SPI = require('spi');

/*
A node.js library to control a WS2801 RGB LED stripe via SPI with your Raspberry Pi
Expand All @@ -9,7 +9,7 @@ var microtime = require('microtime');
function RPiWS2801(){
this.spiDevice = '/dev/spidev0.0';
this.numLEDs = 32;
this.spiFd = null; //filedescriptor for spidevice
this.spi = null;
this.inverted = false;
this.reversed = false;
this.gamma = 2.5;
Expand All @@ -24,6 +24,8 @@ function RPiWS2801(){
// manual of WS2801 says 500 is enough, however we need at least 1000
this.lastWriteTime = microtime.now()-this.rowResetTime-1; //last time something was written to SPI
//required for save WS2801 reset
// SPI max speed
this.maxSpeed = 1000000;
// clear buffer
this.values.fill(0);
}
Expand All @@ -41,13 +43,16 @@ RPiWS2801.prototype = {
if (spiDevice){
this.spiDevice = spiDevice;
}
// connect synchronously

try{
this.spiFd = fs.openSync(this.spiDevice, 'w');
this.spi = new SPI.Spi(this.spiDevice, {'maxSpeed' : this.maxSpeed}, function(s){
s.open();
});
} catch (err) {
console.error("error opening SPI device "+this.spiDevice, err);
return false;
}

this.numLEDs = numLEDs;

this.channelCount = this.numLEDs*this.bytePerPixel;
Expand All @@ -66,14 +71,14 @@ RPiWS2801.prototype = {
* disconnect from SPI port
*/
disconnect : function(){
if (this.spiFd) fs.closeSync(this.spiFd);
if (this.spi) this.spi.close();
},

/*
* send stored buffer with RGB values to WS2801 stripe
*/
update: function(){
if (this.spiFd) {
if (this.spi) {
this.sendRgbBuffer(this.values);
}
},
Expand Down Expand Up @@ -120,7 +125,9 @@ RPiWS2801.prototype = {
for (var i=0; i < buffer.length; i++){
adjustedBuffer[i]=this.gammatable[buffer[i]];
}
fs.writeSync(this.spiFd, adjustedBuffer, 0, buffer.length, null);

this.spi.write(adjustedBuffer);

this.lastWriteTime = microtime.now();
return true;
}
Expand All @@ -132,13 +139,13 @@ RPiWS2801.prototype = {
* fill whole stripe with one color
*/
fill: function(r,g,b){
if (this.spiFd) {
if (this.spi) {
var colors = this.getRGBArray(r,g,b);
var colorBuffer = new Buffer(this.channelCount);
for (var i=0; i<(this.channelCount); i+=3){
colorBuffer[i+0]=colors[0];
colorBuffer[i+1]=colors[1];
colorBuffer[i+2]=colors[2];
colorBuffer[i+2]=colors[2];
}
this.sendRgbBuffer(colorBuffer);
}
Expand All @@ -148,7 +155,7 @@ RPiWS2801.prototype = {
* set color of led index [red, green, blue] from 0 to 255
*/
setColor: function(ledIndex, color) {
if (this.spiFd) {
if (this.spi) {
var colors = this.getRGBArray(color[0],color[1],color[2]);
var r, g, b;
r = colors[0] / 255;
Expand All @@ -165,7 +172,7 @@ RPiWS2801.prototype = {
* set power of channel from 0 to 1
*/
setChannelPower: function(channelIndex, powerValue) {
if (this.spiFd) {
if (this.spi) {
if(channelIndex > this.channelCount || channelIndex < 0) {
return false;
}
Expand All @@ -181,7 +188,7 @@ RPiWS2801.prototype = {
* set RGB hexcolor to LED index
*/
setRGB: function(ledIndex, hexColor) {
if (this.spiFd) {
if (this.spi) {
var rgb = this.getRGBfromHex(hexColor);
var colors = this.getRGBArray(rgb.r,rgb.g,rgb.b);
var redChannel = this.getRedChannelIndex(ledIndex);
Expand Down Expand Up @@ -222,9 +229,9 @@ RPiWS2801.prototype = {

getRGBArray: function(r, g, b){
var colorArray = new Array(3);
colorArray[this.redIndex] = r;
colorArray[this.greenIndex] = g;
colorArray[this.blueIndex] = b;
colorArray[this.redIndex] = r & 0xff;
colorArray[this.greenIndex] = g & 0xff;
colorArray[this.blueIndex] = b & 0xff;
if(this.inverted) {
colorArray[0] = (1 - colorArray[0]/255)*255;
colorArray[1] = (1 - colorArray[1]/255)*255;
Expand Down

0 comments on commit 15f4658

Please sign in to comment.