-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (161 loc) · 4.56 KB
/
Copy pathindex.js
File metadata and controls
168 lines (161 loc) · 4.56 KB
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
168
const graphics = require("graphics");
/**
* SSD1351 class
*/
class SSD1351 {
/**
* Setup SSD1351 for SPI connection
* @param {SPI} spi
* @param {Object} options
* .width {number=128}
* .height {number=128}
* .dc {number=-1}
* .rst {number=-1}
* .cs {number=-1}
* .rotation {number=0}
*/
setup(spi, options) {
this.spi = spi;
options = Object.assign(
{
width: 128,
height: 128,
dc: -1,
rst: -1,
cs: -1,
rotation: 0,
},
options
);
this.width = options.width;
this.height = options.height;
this.dc = options.dc;
this.rst = options.rst;
this.cs = options.cs;
this.rotation = options.rotation;
this.context = null;
if (this.dc > -1) pinMode(this.dc, OUTPUT);
if (this.rst > -1) pinMode(this.rst, OUTPUT);
if (this.cs > -1) pinMode(this.cs, OUTPUT);
digitalWrite(this.cs, HIGH); // deselect
// reset
digitalWrite(this.rst, LOW);
delay(20);
digitalWrite(this.rst, HIGH);
delay(50);
digitalWrite(this.cs, LOW); // select
this.cmd(0xfd, [0x12]); // unlock
this.cmd(0xfd, [0xb1]); // unlock
this.cmd(0xae); // display off
this.cmd(0xb3, [0xf1]); // clock div
this.cmd(0xca, [0x7f]); // Multiplex Ratio
this.cmd(0xa0, [0x74]); // remap
this.cmd(0x15, [0, 0x7f]); // col
this.cmd(0x65, [0, 0x7f]); // row
this.cmd(0xa1, [0]); // startline
this.cmd(0xa2, [0]); // display offset
this.cmd(0xb5, [0]); // GPIO
this.cmd(0xab, [1]); // func select
this.cmd(0xb1, [0x32]); // precharge
this.cmd(0xbe, [5]); // vcomh
this.cmd(0xa6); // normal display
this.cmd(0xc1, [0xc8, 0x80, 0xc8]); // contrast abc
this.cmd(0xc7, [0x0f]); // contrast master
this.cmd(0xb4, [0xa0, 0xb5, 0x55]); // set vsl
this.cmd(0xb6, [1]); // precharge2
this.cmd(0xaf); // display on
digitalWrite(this.cs, HIGH); // deselect
delay(50);
}
/**
* Send command
* @param {number} cmd
* @param {Array<number>} data
*/
cmd(cmd, data) {
digitalWrite(this.dc, LOW); // command
this.spi.send(new Uint8Array([cmd]));
if (data) {
digitalWrite(this.dc, HIGH); // data
this.spi.send(new Uint8Array(data));
}
}
/**
* Get a graphic context
* @param {string} type Type of graphic context.
* 'buffer' or 'callback'. Default is 'callback'
*/
getContext(type) {
if (!this.context) {
if (type === "buffer") {
this.context = new graphics.BufferedGraphicsContext(
this.width,
this.height,
{
rotation: this.rotation,
bpp: 16,
display: (buffer) => {
digitalWrite(this.cs, LOW); // select
this.cmd(0x15, [0, this.width - 1]);
this.cmd(0x75, [0, this.height - 1]);
digitalWrite(this.dc, LOW); // command
this.spi.send(new Uint8Array([0x5c]));
digitalWrite(this.dc, HIGH); // data
this.spi.send(buffer);
digitalWrite(this.cs, HIGH); // deselect
},
}
);
} else {
// 'callback'
this.context = new graphics.GraphicsContext(this.width, this.height, {
rotation: this.rotation,
setPixel: (x, y, color) => {
digitalWrite(this.cs, LOW); // select
this.cmd(0x15, [x, 127]);
this.cmd(0x75, [y, 127]);
this.cmd(0x5c, [color >> 8, color]);
digitalWrite(this.cs, HIGH); // deselect
},
fillRect: (x, y, w, h, color) => {
digitalWrite(this.cs, LOW); // select
this.cmd(0x15, [x, x + w - 1]);
this.cmd(0x75, [y, y + h - 1]);
digitalWrite(this.dc, LOW); // command
this.spi.send(new Uint8Array([0x5c]));
digitalWrite(this.dc, HIGH); // data
this.spi.send(new Uint8Array([color >> 8, color]), 5000, w * h);
digitalWrite(this.cs, HIGH); // deselect
},
});
}
}
return this.context;
}
/**
* Turn on display
*/
on() {
digitalWrite(this.cs, LOW); // select
this.cmd(0xaf);
digitalWrite(this.cs, HIGH); // deselect
}
/**
* Turn off display
*/
off() {
digitalWrite(this.cs, LOW); // select
this.cmd(0xae);
digitalWrite(this.cs, HIGH); // deselect
}
/**
* Set contrast
* @param {number} c
*/
setContrast(c) {
digitalWrite(this.cs, LOW); // select
this.spi.send(new Uint8Array([0x81, c]));
digitalWrite(this.cs, HIGH); // deselect
}
}
exports.SSD1351 = SSD1351;