-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (162 loc) · 6.98 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
/*
LCDWIDTH = 84
LCDHEIGHT = 48
ROWPIXELS = LCDHEIGHT//6
PCD8544_POWERDOWN = 0x04
PCD8544_ENTRYMODE = 0x02
PCD8544_EXTENDEDINSTRUCTION = 0x01
PCD8544_DISPLAYBLANK = 0x0
PCD8544_DISPLAYNORMAL = 0x4
PCD8544_DISPLAYALLON = 0x1
PCD8544_DISPLAYINVERTED = 0x5
PCD8544_FUNCTIONSET = 0x20
PCD8544_DISPLAYCONTROL = 0x08
PCD8544_SETYADDR = 0x40
PCD8544_SETXADDR = 0x80
PCD8544_SETTEMP = 0x04
PCD8544_SETBIAS = 0x10
PCD8544_SETVOP = 0x80
*/
const SPI = require('spi');
const gpio = require('rpi-gpio');
const debug = require('debug')('nokia-screen');
class NokiaScreen {
constructor(dc, rst, spi) {
this.spi = spi;
this.dc = dc;
this.rst = rst;
let logo = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFF, 0xFC, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, 0xC0, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x87, 0x8F, 0x9F, 0x9F, 0xFF, 0xFF, 0xFF, 0xC1, 0xC0, 0xE0, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xE0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF1, 0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x87, 0xE7, 0xFF, 0xFF, 0xFF, 0x1F, 0x1F, 0x3F, 0xF9, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x7E, 0x3F, 0x3F, 0x0F, 0x1F, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xE0, 0xF1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0F, 0x1F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x1F, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ];
this.buffer = new Buffer(logo);
debug("Class constructed");
}
command(c) {
debug(`Executing command ${c}`);
return new Promise( ( resolve, reject ) => {
gpio.write(this.dc, false, err => {
if (err) return reject(err);
this.spi.transfer(new Buffer([ c ]),new Buffer([ 0x00 ]),function(device, buf) {
var s = "";
for (var i=0; i < buf.length; i++) {
s = s + buf[i] + " ";
//console.log(s);
}
resolve(buf);
});
});
});
}
extended_command(c) {
debug(`Executing extended command ${c}`);
return this.command( 0x20 | 0x01 )
.then( ()=> this.command(c) )
.then( ()=> this.command(0x20) )
.then( ()=> this.command(0x08 | 0x4) )
}
data(c) {
debug("Writing data");
return new Promise( ( resolve, reject ) => {
gpio.write(this.dc, true, err => {
return reject(err);
this.spi.transfer(new Buffer([ c ]),new Buffer([ 0x00 ]),function(device, buf) {
var s = "";
for (var i=0; i < buf.length; i++) {
s = s + buf[i] + " ";
//console.log(s);
}
resolve(buf);
});
});
});
}
begin(contrast, bias) {
//gpio.setMode(gpio.MODE_BCM);
debug("Starting class");
let prs = [];
prs.push(
new Promise( (resolve, reject) => {
debug(`D/C: Pin ${this.dc}`);
gpio.setup(this.dc, gpio.DIR_OUT, err => {
if (err) {
debug(`D/C pin (${this.dc}) is not ready`);
return reject(err);
}
debug(`D/C pin (${this.dc}) is ready`);
resolve(this.dc);
});
})
);
prs.push(
new Promise( (resolve, reject) => {
debug(`RESET Pin ${this.rst}`);
gpio.setup(this.rst, gpio.DIR_OUT, err => {
if (err) {
debug(`Reset pin (${this.rst}) is not ready`);
return reject(err);
}
debug(`Reset pin (${this.rst}) is ready`);
resolve(this.rst);
});
})
);
return Promise.all(prs)
.then( pins => {
debug(`Set pins ${pins}`);
})
.then( () => this.reset() )
.then( () => this.set_bias(bias) )
.then( () => this.set_contrast(contrast) );
}
reset() {
// Resets the display
debug("Resetting the display");
return new Promise( (resolve, reject) => {
debug(`Resetting display on pin ${this.rst}`);
gpio.write(this.rst, false, err => {
if (err) return reject(err);
setTimeout( () => {
gpio.write(this.rst, true, err => {
if (err) return reject(err);
resolve();
});
}, 100);
});
});
}
display() {
debug("Pushing display buffer to display");
return this.command(0x40)
.then( () => this.command(0x80) )
.then( () => {
return new Promise( (resolve, reject) => {
gpio.write(this.dc, true, err => {
if (err) return reject(err);
try {
this.spi.transfer(this.buffer,new Buffer( new Array(this.buffer.length).fill( 0x00 ) ),function(device, buf) {
var s = "";
for (var i=0; i < buf.length; i++) {
s = s + buf[i] + " ";
//console.log(s);
}
});
} catch (e) {
reject(e);
}
});
});
});
}
clear() {
debug("Clearing display buffer");
this.buffer = new Buffer( new Array((84 * 48 / 8)).fill(0x00) );
}
set_contrast(contrast) {
contrast = Math.max(0, Math.min(contrast, 127));
debug(`Setting contrast to ${contrast} (Clamped 0-127)`);
return this.extended_command( 0x80 | contrast );
}
set_bias(bias) {
debug(`Setting bias to ${bias}`);
return this.extended_command( 0x10 | bias );
}
}
module.exports = NokiaScreen;