1
- var fs = require ( 'fs' ) ;
2
1
var microtime = require ( 'microtime' ) ;
2
+ var SPI = require ( 'spi' ) ;
3
3
4
4
/*
5
5
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');
9
9
function RPiWS2801 ( ) {
10
10
this . spiDevice = '/dev/spidev0.0' ;
11
11
this . numLEDs = 32 ;
12
- this . spiFd = null ; //filedescriptor for spidevice
12
+ this . spi = null ;
13
13
this . inverted = false ;
14
14
this . reversed = false ;
15
15
this . gamma = 2.5 ;
@@ -24,6 +24,8 @@ function RPiWS2801(){
24
24
// manual of WS2801 says 500 is enough, however we need at least 1000
25
25
this . lastWriteTime = microtime . now ( ) - this . rowResetTime - 1 ; //last time something was written to SPI
26
26
//required for save WS2801 reset
27
+ // SPI max speed
28
+ this . maxSpeed = 1000000 ;
27
29
// clear buffer
28
30
this . values . fill ( 0 ) ;
29
31
}
@@ -41,13 +43,16 @@ RPiWS2801.prototype = {
41
43
if ( spiDevice ) {
42
44
this . spiDevice = spiDevice ;
43
45
}
44
- // connect synchronously
46
+
45
47
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
+ } ) ;
47
51
} catch ( err ) {
48
52
console . error ( "error opening SPI device " + this . spiDevice , err ) ;
49
53
return false ;
50
54
}
55
+
51
56
this . numLEDs = numLEDs ;
52
57
53
58
this . channelCount = this . numLEDs * this . bytePerPixel ;
@@ -66,14 +71,14 @@ RPiWS2801.prototype = {
66
71
* disconnect from SPI port
67
72
*/
68
73
disconnect : function ( ) {
69
- if ( this . spiFd ) fs . closeSync ( this . spiFd ) ;
74
+ if ( this . spi ) this . spi . close ( ) ;
70
75
} ,
71
76
72
77
/*
73
78
* send stored buffer with RGB values to WS2801 stripe
74
79
*/
75
80
update : function ( ) {
76
- if ( this . spiFd ) {
81
+ if ( this . spi ) {
77
82
this . sendRgbBuffer ( this . values ) ;
78
83
}
79
84
} ,
@@ -120,7 +125,9 @@ RPiWS2801.prototype = {
120
125
for ( var i = 0 ; i < buffer . length ; i ++ ) {
121
126
adjustedBuffer [ i ] = this . gammatable [ buffer [ i ] ] ;
122
127
}
123
- fs . writeSync ( this . spiFd , adjustedBuffer , 0 , buffer . length , null ) ;
128
+
129
+ this . spi . write ( adjustedBuffer ) ;
130
+
124
131
this . lastWriteTime = microtime . now ( ) ;
125
132
return true ;
126
133
}
@@ -132,13 +139,13 @@ RPiWS2801.prototype = {
132
139
* fill whole stripe with one color
133
140
*/
134
141
fill : function ( r , g , b ) {
135
- if ( this . spiFd ) {
142
+ if ( this . spi ) {
136
143
var colors = this . getRGBArray ( r , g , b ) ;
137
144
var colorBuffer = new Buffer ( this . channelCount ) ;
138
145
for ( var i = 0 ; i < ( this . channelCount ) ; i += 3 ) {
139
146
colorBuffer [ i + 0 ] = colors [ 0 ] ;
140
147
colorBuffer [ i + 1 ] = colors [ 1 ] ;
141
- colorBuffer [ i + 2 ] = colors [ 2 ] ;
148
+ colorBuffer [ i + 2 ] = colors [ 2 ] ;
142
149
}
143
150
this . sendRgbBuffer ( colorBuffer ) ;
144
151
}
@@ -148,7 +155,7 @@ RPiWS2801.prototype = {
148
155
* set color of led index [red, green, blue] from 0 to 255
149
156
*/
150
157
setColor : function ( ledIndex , color ) {
151
- if ( this . spiFd ) {
158
+ if ( this . spi ) {
152
159
var colors = this . getRGBArray ( color [ 0 ] , color [ 1 ] , color [ 2 ] ) ;
153
160
var r , g , b ;
154
161
r = colors [ 0 ] / 255 ;
@@ -165,7 +172,7 @@ RPiWS2801.prototype = {
165
172
* set power of channel from 0 to 1
166
173
*/
167
174
setChannelPower : function ( channelIndex , powerValue ) {
168
- if ( this . spiFd ) {
175
+ if ( this . spi ) {
169
176
if ( channelIndex > this . channelCount || channelIndex < 0 ) {
170
177
return false ;
171
178
}
@@ -181,7 +188,7 @@ RPiWS2801.prototype = {
181
188
* set RGB hexcolor to LED index
182
189
*/
183
190
setRGB : function ( ledIndex , hexColor ) {
184
- if ( this . spiFd ) {
191
+ if ( this . spi ) {
185
192
var rgb = this . getRGBfromHex ( hexColor ) ;
186
193
var colors = this . getRGBArray ( rgb . r , rgb . g , rgb . b ) ;
187
194
var redChannel = this . getRedChannelIndex ( ledIndex ) ;
@@ -222,9 +229,9 @@ RPiWS2801.prototype = {
222
229
223
230
getRGBArray : function ( r , g , b ) {
224
231
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 ;
228
235
if ( this . inverted ) {
229
236
colorArray [ 0 ] = ( 1 - colorArray [ 0 ] / 255 ) * 255 ;
230
237
colorArray [ 1 ] = ( 1 - colorArray [ 1 ] / 255 ) * 255 ;
0 commit comments