Skip to content

Commit 15f4658

Browse files
committed
set maximum SPI speed
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
1 parent aed276d commit 15f4658

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "A node.js library to control a WS2801 RGB LED stripe via SPI with your Raspberry Pi",
55
"main": "rpi-ws2801.js",
66
"dependencies": {
7-
"microtime": "*"
7+
"microtime": "*",
8+
"spi":"*"
89
},
910
"devDependencies": {},
1011
"scripts": {

rpi-ws2801.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var fs = require('fs');
21
var microtime = require('microtime');
2+
var SPI = require('spi');
33

44
/*
55
A node.js library to control a WS2801 RGB LED stripe via SPI with your Raspberry Pi
@@ -9,7 +9,7 @@ var microtime = require('microtime');
99
function RPiWS2801(){
1010
this.spiDevice = '/dev/spidev0.0';
1111
this.numLEDs = 32;
12-
this.spiFd = null; //filedescriptor for spidevice
12+
this.spi = null;
1313
this.inverted = false;
1414
this.reversed = false;
1515
this.gamma = 2.5;
@@ -24,6 +24,8 @@ function RPiWS2801(){
2424
// manual of WS2801 says 500 is enough, however we need at least 1000
2525
this.lastWriteTime = microtime.now()-this.rowResetTime-1; //last time something was written to SPI
2626
//required for save WS2801 reset
27+
// SPI max speed
28+
this.maxSpeed = 1000000;
2729
// clear buffer
2830
this.values.fill(0);
2931
}
@@ -41,13 +43,16 @@ RPiWS2801.prototype = {
4143
if (spiDevice){
4244
this.spiDevice = spiDevice;
4345
}
44-
// connect synchronously
46+
4547
try{
46-
this.spiFd = fs.openSync(this.spiDevice, 'w');
48+
this.spi = new SPI.Spi(this.spiDevice, {'maxSpeed' : this.maxSpeed}, function(s){
49+
s.open();
50+
});
4751
} catch (err) {
4852
console.error("error opening SPI device "+this.spiDevice, err);
4953
return false;
5054
}
55+
5156
this.numLEDs = numLEDs;
5257

5358
this.channelCount = this.numLEDs*this.bytePerPixel;
@@ -66,14 +71,14 @@ RPiWS2801.prototype = {
6671
* disconnect from SPI port
6772
*/
6873
disconnect : function(){
69-
if (this.spiFd) fs.closeSync(this.spiFd);
74+
if (this.spi) this.spi.close();
7075
},
7176

7277
/*
7378
* send stored buffer with RGB values to WS2801 stripe
7479
*/
7580
update: function(){
76-
if (this.spiFd) {
81+
if (this.spi) {
7782
this.sendRgbBuffer(this.values);
7883
}
7984
},
@@ -120,7 +125,9 @@ RPiWS2801.prototype = {
120125
for (var i=0; i < buffer.length; i++){
121126
adjustedBuffer[i]=this.gammatable[buffer[i]];
122127
}
123-
fs.writeSync(this.spiFd, adjustedBuffer, 0, buffer.length, null);
128+
129+
this.spi.write(adjustedBuffer);
130+
124131
this.lastWriteTime = microtime.now();
125132
return true;
126133
}
@@ -132,13 +139,13 @@ RPiWS2801.prototype = {
132139
* fill whole stripe with one color
133140
*/
134141
fill: function(r,g,b){
135-
if (this.spiFd) {
142+
if (this.spi) {
136143
var colors = this.getRGBArray(r,g,b);
137144
var colorBuffer = new Buffer(this.channelCount);
138145
for (var i=0; i<(this.channelCount); i+=3){
139146
colorBuffer[i+0]=colors[0];
140147
colorBuffer[i+1]=colors[1];
141-
colorBuffer[i+2]=colors[2];
148+
colorBuffer[i+2]=colors[2];
142149
}
143150
this.sendRgbBuffer(colorBuffer);
144151
}
@@ -148,7 +155,7 @@ RPiWS2801.prototype = {
148155
* set color of led index [red, green, blue] from 0 to 255
149156
*/
150157
setColor: function(ledIndex, color) {
151-
if (this.spiFd) {
158+
if (this.spi) {
152159
var colors = this.getRGBArray(color[0],color[1],color[2]);
153160
var r, g, b;
154161
r = colors[0] / 255;
@@ -165,7 +172,7 @@ RPiWS2801.prototype = {
165172
* set power of channel from 0 to 1
166173
*/
167174
setChannelPower: function(channelIndex, powerValue) {
168-
if (this.spiFd) {
175+
if (this.spi) {
169176
if(channelIndex > this.channelCount || channelIndex < 0) {
170177
return false;
171178
}
@@ -181,7 +188,7 @@ RPiWS2801.prototype = {
181188
* set RGB hexcolor to LED index
182189
*/
183190
setRGB: function(ledIndex, hexColor) {
184-
if (this.spiFd) {
191+
if (this.spi) {
185192
var rgb = this.getRGBfromHex(hexColor);
186193
var colors = this.getRGBArray(rgb.r,rgb.g,rgb.b);
187194
var redChannel = this.getRedChannelIndex(ledIndex);
@@ -222,9 +229,9 @@ RPiWS2801.prototype = {
222229

223230
getRGBArray: function(r, g, b){
224231
var colorArray = new Array(3);
225-
colorArray[this.redIndex] = r;
226-
colorArray[this.greenIndex] = g;
227-
colorArray[this.blueIndex] = b;
232+
colorArray[this.redIndex] = r & 0xff;
233+
colorArray[this.greenIndex] = g & 0xff;
234+
colorArray[this.blueIndex] = b & 0xff;
228235
if(this.inverted) {
229236
colorArray[0] = (1 - colorArray[0]/255)*255;
230237
colorArray[1] = (1 - colorArray[1]/255)*255;

0 commit comments

Comments
 (0)