forked from SerafinTech/node-apa102-spi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapa102-spi.js
35 lines (28 loc) · 1012 Bytes
/
apa102-spi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var rpio = require('rpio')
rpio.init({
gpiomem: false
})
function Apa102spi (stringLength, clockDivider) {
clockDivider = typeof clockDivider !== 'undefined' ? clockDivider : 200
this.bufferLength = stringLength * 4
this.writeBuffer = Buffer.alloc(this.bufferLength, 'E0000000', 'hex')
this.bufferLength += 4
this.writeBuffer = Buffer.concat([Buffer.alloc(4, '00000000', 'hex'), this.writeBuffer], this.bufferLength)
rpio.spiBegin()
rpio.spiSetClockDivider(clockDivider)
}
Apa102spi.prototype.sendLeds = function () {
rpio.spiWrite(this.writeBuffer, this.bufferLength)
}
Apa102spi.prototype.setLedColor = function (n, brightness, r, g, b) {
n *= 4
n += 4
this.writeBuffer[n] = brightness | 0b11100000
this.writeBuffer[n + 1] = b
this.writeBuffer[n + 2] = g
this.writeBuffer[n + 3] = r
}
Apa102spi.prototype.setBuffer = function (ledBuffer) {
this.writeBuffer = Buffer.concat([Buffer.alloc(4, '00000000', 'hex'), ledBuffer], this.bufferLength)
}
module.exports = Apa102spi