-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8x8matrix.js
127 lines (91 loc) · 2.25 KB
/
8x8matrix.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
const rpio = require('rpio');
var Matrix = function(data) {
if (typeof data == 'undefined') data = {}
this.brightness = data.brightness || 15;
this.write_buffer = [];
this.current_array = [];
rpio.i2cBegin();
rpio.i2cSetSlaveAddress(data.slaveAddress || 0x70);
rpio.i2cSetBaudRate(data.bautrate || 10000);
// Turn on the oscillator
rpio.i2cWrite(Buffer([(0x20 | 0x01)]));
// Turn display on
rpio.i2cWrite(Buffer([(0x01 | 0x80)]));
// Initial Clear
for (var x = 0; x < 16; x++) {
rpio.i2cWrite(Buffer([x, 0]));
}
// Set display to full brightness.
rpio.i2cWrite(Buffer([(0xE0 | this.brightness)]));
}
Matrix.prototype.setBrightness = function(b) {
if (b > 15) b = 15;
if (b < 0) b = 0;
rpio.i2cWrite(Buffer([(0xE0 | b)]));
}
Matrix.prototype.setLED = function(y, x, value) {
var led = y * 16 + ((x + 7) % 8);
var pos = Math.floor(led / 8);
var offset = led % 8;
if (value)
this.write_buffer[pos] |= (1 << offset);
else
this.write_buffer[pos] &= ~(1 << offset);
}
Matrix.prototype.writeArray = function(_array) {
this.current_array = _array;
this.clearBuffer();
var x = 0;
var y = 0;
for (var i in _array) {
this.setLED(y, x, _array[i]);
x++;
if (x >= 8) {
y++;
x = 0;
}
}
this.writeBuffer();
}
Matrix.prototype.writeAnimation = function(_array, speed) {
var self = this;
var old_buffer = this.write_buffer.slice();
for (var i in _array) {
self.writeAnimation2(i, _array[i], speed);
}
setTimeout(function() {
self.clearBuffer();
self.writeBuffer();
}, _array.length * speed + speed);
setTimeout(function() {
self.write_buffer = old_buffer.slice();
self.writeBuffer();
}, _array.length * speed + 1000);
}
Matrix.prototype.writeAnimation2 = function(i, data, speed) {
var self = this;
setTimeout(function() {
self.writeArray(data);
}, speed * i);
}
Matrix.prototype.writeBuffer = function() {
for (var i in this.write_buffer) {
rpio.i2cWrite(Buffer([i, this.write_buffer[i]]));
}
}
Matrix.prototype.clearBuffer = function() {
for (var i in this.write_buffer) {
this.write_buffer[i] = 0;
}
}
Matrix.prototype.smily = [
0,0,1,1,1,1,0,0,
0,1,0,0,0,0,1,0,
1,0,1,0,0,1,0,1,
1,0,0,0,0,0,0,1,
1,0,1,0,0,1,0,1,
1,0,0,1,1,0,0,1,
0,1,0,0,0,0,1,0,
0,0,1,1,1,1,0,0,
];
module.exports = Matrix;