-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper75HDB.ts
149 lines (129 loc) · 2.63 KB
/
paper75HDB.ts
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
import {
exit,
init,
INPUT,
LOW,
mode,
msleep,
OUTPUT,
read,
spiBegin,
spiChipSelect,
spiEnd,
spiSetClockDivider,
spiSetCSPolarity,
spiSetDataMode,
spiWrite,
write,
} from "rpio";
import { Device, Pixel8, PixelSize } from "./device";
import { Pin } from "./pin";
/**
* A 7.5inch HD (B) device implementation.
*/
export class Paper75HDB implements Device {
readonly width: PixelSize = 880;
readonly height: PixelSize = 528;
initialize(): void {
init({
gpiomem: false,
mapping: "gpio",
});
mode(Pin.RST, OUTPUT);
mode(Pin.DC, OUTPUT);
mode(Pin.CS, OUTPUT);
mode(Pin.BUSY, INPUT);
write(Pin.CS, 1);
spiBegin();
spiSetDataMode(0);
spiSetClockDivider(128);
spiChipSelect(0);
spiSetCSPolarity(0, LOW);
this.reset();
this.command(0x12);
this.waitUntilIdle();
this.command(0x46);
this.data(0xf7);
this.waitUntilIdle();
this.command(0x47);
this.data(0xf7);
this.waitUntilIdle();
this.command(0x0c);
this.data(0xae, 0xc7, 0xc3, 0xc0, 0x40);
this.command(0x01);
this.data(0xaf, 0x02, 0x01);
this.command(0x11);
this.data(0x01);
this.command(0x44);
this.data(0x00, 0x00, 0x6f, 0x03);
this.command(0x45);
this.data(0xaf, 0x02, 0x00, 0x00);
this.command(0x3c);
this.data(0x01);
this.command(0x18);
this.data(0x80);
this.command(0x22);
this.data(0xb1);
this.command(0x20);
this.waitUntilIdle();
this.command(0x4e);
this.data(0x00, 0x00);
this.command(0x4f);
this.data(0xaf, 0x02);
}
finalize(): void {
this.deviceSleep();
msleep(2000);
write(Pin.CS, LOW);
write(Pin.DC, LOW);
write(Pin.RST, LOW);
spiEnd();
exit();
}
draw(blackPixels: Pixel8[], redPixels: Pixel8[]): void {
this.command(0x4f);
this.data(0xaf, 0x02);
this.command(0x24);
this.data(...blackPixels);
this.command(0x26);
this.data(...redPixels);
this.displayOn();
}
private displayOn(): void {
this.command(0x22);
this.data(0xc7); // Load LUT from MCU(0x32)
this.command(0x20);
msleep(200); // !!!The rpio.msleep here is necessary, 200uS at least!!!
this.waitUntilIdle();
}
private deviceSleep(): void {
this.command(0x10);
this.data(0x01);
}
private reset(): void {
write(Pin.RST, 1);
msleep(200);
write(Pin.RST, 0);
msleep(2);
write(Pin.RST, 1);
msleep(200);
}
private command(id: number): void {
write(Pin.DC, 0);
write(Pin.CS, 0);
spiWrite(Buffer.alloc(1, id), 1);
write(Pin.CS, 1);
}
private data(...data: number[]): void {
write(Pin.CS, 0);
write(Pin.DC, 1);
spiWrite(Buffer.from(data), data.length);
write(Pin.CS, 1);
}
private waitUntilIdle(): void {
while (read(Pin.BUSY)) {
msleep(10);
}
msleep(200);
}
}