-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathBlinkt.js
157 lines (136 loc) · 4.18 KB
/
Blinkt.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use strict";
var wpi = require('wiring-pi'),
DAT = 23,
CLK = 24,
Blinkt;
Blinkt = function () {};
/**
* Connects to the GPIO and sets the GPIO pin modes. Must be called
* before any other commands. All pixels will start off white at
* full brightness by default.
*/
Blinkt.prototype.setup = function setup () {
// Set WPI to GPIO mode
wpi.setup('gpio');
// Set pin mode to output
wpi.pinMode(DAT, wpi.OUTPUT);
wpi.pinMode(CLK, wpi.OUTPUT);
this._numPixels = 8;
this._pixels = [];
// Init pixels
for (var i = 0; i < this._numPixels; i++) {
this.setPixel(i, 255, 255, 255, 1.0);
}
};
/**
* Sets all pixels to the passed RGB and brightness values.
* @param {Number} r The pixel red value between 0 and 255.
* @param {Number} g The pixel green value between 0 and 255.
* @param {Number} b The pixel blue value between 0 and 255.
* @param {Number} a The pixel brightness value between 0.0 and 1.0.
*/
Blinkt.prototype.setAllPixels = function setAllPixels (r, g, b, a) {
for (var i = 0; i < this._numPixels; i++) {
this.setPixel(i, r, g, b, a);
}
};
/**
* Sets the specified pixel to the passed rgb and brightness level.
* The pixelNum is an integer between 0 and 7 to indicate the pixel
* to change.
* @param {Number} pixelNum The pixel to set RGB and brightness for.
* An integer value between 0 and 7. Zero is the first pixel, 7 is
* the last one.
* @param {Number} r The pixel red value between 0 and 255.
* @param {Number} g The pixel green value between 0 and 255.
* @param {Number} b The pixel blue value between 0 and 255.
* @param {Number} a The pixel brightness value between 0.0 and 1.0.
*/
Blinkt.prototype.setPixel = function setPixel (pixelNum, r, g, b, a) {
if (a === undefined) {
if (this._pixels[pixelNum]) {
// Set a to current level or 1.0 if none exists
a = this._pixels[pixelNum][3] !== undefined ? this._pixels[pixelNum][3] : 1.0;
}
} else {
a = parseInt((31.0 * a), 10) & 0b11111; // jshint ignore:line
}
this._pixels[pixelNum] = [
parseInt(r, 10) & 255, // jshint ignore:line
parseInt(g, 10) & 255, // jshint ignore:line
parseInt(b, 10) & 255, // jshint ignore:line
a
];
};
/**
* Sets the brightness of the pixel specified by pixelNum.
* @param {Number} pixelNum The pixel to set RGB and brightness for.
* An integer value between 0 and 7. Zero is the first pixel, 7 is
* the last one.
* @param {Number} brightness The pixel brightness value between 0.0
* and 1.0.
*/
Blinkt.prototype.setBrightness = function setBrightness (pixelNum, brightness) {
this._pixels[pixelNum][3] = parseInt((31.0 * brightness), 10) & 0b11111; // jshint ignore:line
};
/**
* Clears the pixel buffer.
* This is the same as setting all pixels to black.
* You must also call sendUpdate() if you want to turn Blinkt! off.
*/
Blinkt.prototype.clearAll = function clearAll () {
for (var i = 0; i < this._numPixels; i++) {
this.setPixel(i, 0, 0, 0);
}
};
/**
* Sends the current pixel settings to the Blinkt! device. Once you
* have set each pixel RGB and brightness, you MUST call this for the
* pixels to change on the Blinkt! device.
*/
Blinkt.prototype.sendUpdate = function sendUpdate () {
var i,
pixel;
for (i = 0; i < 4; i++) {
this._writeByte(0);
}
for (i = 0; i < this._numPixels; i++) {
pixel = this._pixels[i];
// Brightness
this._writeByte(0b11100000 | pixel[3]); // jshint ignore:line
// Blue
this._writeByte(pixel[2]);
// Green
this._writeByte(pixel[1]);
// Red
this._writeByte(pixel[0]);
}
this._writeByte(0xff);
this._latch();
};
/**
* Writes byte data to the GPIO pins.
* @param {Number} byte The byte value to write.
* @private
*/
Blinkt.prototype._writeByte = function writeByte (byte) {
var bit;
for (var i = 0 ; i < this._numPixels; i++) {
bit = ((byte & (1 << (7 - i))) > 0) === true ? wpi.HIGH : wpi.LOW; // jshint ignore:line
wpi.digitalWrite(DAT, bit);
wpi.digitalWrite(CLK, 1);
wpi.digitalWrite(CLK, 0);
}
};
/**
* Emit exactly enough clock pulses to latch the small dark die APA102s which are weird.
* @private
*/
Blinkt.prototype._latch = function latch() {
wpi.digitalWrite(DAT, 0);
for (var i = 0 ; i < 36; i++) {
wpi.digitalWrite(CLK, 1);
wpi.digitalWrite(CLK, 0);
}
};
module.exports = Blinkt;