-
Notifications
You must be signed in to change notification settings - Fork 2
/
display.cc
225 lines (202 loc) · 6.77 KB
/
display.cc
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Tool to display images on a Waveshare 7.5" e-ink display.
// Compile: g++ -o display-waveshare -lpng -llgpio -lm display.cc
// Requires png++, libpng, lg (https://abyz.me.uk/lg/)
#include <algorithm>
#include <array>
#include <iostream>
#include <lgpio.h>
#include <png++/gray_pixel.hpp>
#include <png++/png.hpp>
#include <vector>
constexpr int PIN_RST = 17;
constexpr int PIN_DATA_COMMAND = 25;
constexpr int PIN_CHIP_SELECT = 8;
constexpr int PIN_PWR = 18;
constexpr int PIN_BUSY = 24;
constexpr int WIDTH = 800;
constexpr int HEIGHT = 480;
enum Commands {
SET_COLOR = 0x10,
SET_COLOR_INVERSE = 0x13,
};
static void display_wait_idle(int gpio) {
std::cout << "busy. waiting for idle" << std::endl;
do {
lguSleep(0.020);
} while (lgGpioRead(gpio, PIN_BUSY) >= 1);
lguSleep(0.020);
std::cout << "idle" << std::endl;
}
static void display_reset(int gpio) {
lgGpioWrite(gpio, PIN_RST, 1);
lguSleep(0.020);
lgGpioWrite(gpio, PIN_RST, 0);
lguSleep(0.002);
lgGpioWrite(gpio, PIN_RST, 1);
lguSleep(0.020);
}
template <class It>
static void display_command(int gpio, int spi, uint8_t command, It first,
It last) {
lgGpioWrite(gpio, PIN_DATA_COMMAND, 0);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
lgSpiWrite(spi, reinterpret_cast<char *>(&command), 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
lgGpioWrite(gpio, PIN_DATA_COMMAND, 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
std::for_each(first, last, [&](uint8_t byte) {
lgSpiWrite(spi, reinterpret_cast<char *>(&byte), 1);
});
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
}
static void display_command(int gpio, int spi, uint8_t command,
std::initializer_list<uint8_t> args) {
display_command(gpio, spi, command, std::begin(args), std::end(args));
}
static void display_command(int gpio, int spi, uint8_t command) {
display_command(gpio, spi, command, {});
}
static void display_sleep(int gpio, int spi) {
// Power Off
display_command(gpio, spi, 0x02);
display_wait_idle(gpio);
// Deep Sleep
display_command(gpio, spi, 0x07, {0xA5});
}
static void display_refresh(int gpio, int spi) {
display_command(gpio, spi, 0x12);
lguSleep(0.100);
display_wait_idle(gpio);
}
static void display_clear(int gpio, int spi, bool color) {
const int pixels = WIDTH * HEIGHT / 8;
const char color_byte = color ? 0xFF : 0x00;
{
const char command = Commands::SET_COLOR;
lgGpioWrite(gpio, PIN_DATA_COMMAND, 0);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
lgSpiWrite(spi, &command, 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
lgGpioWrite(gpio, PIN_DATA_COMMAND, 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
for (int i = 0; i < pixels; ++i) {
lgSpiWrite(spi, &color_byte, 1);
}
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
}
{
const char command = Commands::SET_COLOR_INVERSE;
lgGpioWrite(gpio, PIN_DATA_COMMAND, 0);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
lgSpiWrite(spi, &command, 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
lgGpioWrite(gpio, PIN_DATA_COMMAND, 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
const char inverse_color_byte = ~color_byte;
for (int i = 0; i < pixels; ++i) {
lgSpiWrite(spi, &inverse_color_byte, 1);
}
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
}
}
static void display_image(int gpio, int spi, const std::string &filename) {
png::image<png::gray_pixel_1> image(
filename, png::require_color_space<png::gray_pixel_1>());
if (image.get_width() != WIDTH || image.get_height() != HEIGHT) {
std::cerr << "Image size must be " << WIDTH << "x" << HEIGHT << std::endl;
return;
}
// Note: display expects bytes, where each represents 8 pixels
{
const char command = Commands::SET_COLOR;
lgGpioWrite(gpio, PIN_DATA_COMMAND, 0);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
lgSpiWrite(spi, &command, 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
lgGpioWrite(gpio, PIN_DATA_COMMAND, 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
for (int x = 0; x < image.get_height(); x++) {
lgSpiWrite(spi, reinterpret_cast<char *>(image.get_row(x).get_data()),
image.get_width() / 8);
}
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
}
{
const char command = Commands::SET_COLOR_INVERSE;
lgGpioWrite(gpio, PIN_DATA_COMMAND, 0);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
lgSpiWrite(spi, &command, 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
lgGpioWrite(gpio, PIN_DATA_COMMAND, 1);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
for (int x = 0; x < image.get_height(); x++) {
std::array<char, WIDTH / 8> row_inverse;
std::transform(image.get_row(x).get_data(),
image.get_row(x).get_data() + WIDTH / 8,
row_inverse.begin(), [](char c) { return ~c; });
lgSpiWrite(spi, row_inverse.data(), image.get_width() / 8);
}
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
}
}
static void display_init(int gpio, int spi) {
// general init
lgGpioClaimInput(gpio, 0, PIN_BUSY);
lgGpioClaimOutput(gpio, 0, PIN_RST, LG_LOW);
lgGpioClaimOutput(gpio, 0, PIN_DATA_COMMAND, LG_LOW);
lgGpioClaimOutput(gpio, 0, PIN_CHIP_SELECT, LG_LOW);
lgGpioClaimOutput(gpio, 0, PIN_PWR, LG_LOW);
lgGpioWrite(gpio, PIN_CHIP_SELECT, 1);
lgGpioWrite(gpio, PIN_PWR, 1);
// 7.5 inch display init
display_reset(gpio);
// Power Setting
display_command(gpio, spi, 0x01, {0x07, 0x07, 0x3f, 0x3f});
// Booster Soft Start
display_command(gpio, spi, 0x06, {0x17, 0x17, 0x28, 0x17});
// Power On
display_command(gpio, spi, 0x04);
// Wait till on
lguSleep(0.100);
display_wait_idle(gpio);
// Panel Setting
display_command(gpio, spi, 0x00, {0x1f});
// Resolution Setting
display_command(gpio, spi, 0x61, {0x03, 0x20, 0x01, 0xe0});
// Dual SPI
display_command(gpio, spi, 0x15, {0x00});
// VCOM and Data Interval Setting
display_command(gpio, spi, 0x50, {0x10, 0x07});
// TCON Setting
display_command(gpio, spi, 0x60, {0x22});
}
static void display_shutdown(int gpio, int spi) {
lgGpioWrite(gpio, PIN_CHIP_SELECT, 0);
lgGpioWrite(gpio, PIN_PWR, 0);
lgGpioWrite(gpio, PIN_DATA_COMMAND, 0);
lgGpioWrite(gpio, PIN_RST, 0);
}
int main(int argc, char **argv) {
const auto gpio = lgGpiochipOpen(0);
if (gpio < 0) {
std::cerr << "Failed to open gpiochip 0" << std::endl;
return 1;
}
const auto spi = lgSpiOpen(0, 0, 10000000, 0);
if (spi < 0) {
std::cerr << "Failed to open SPI0.0" << std::endl;
return 1;
}
std::cout << "display init" << std::endl;
display_init(gpio, spi);
std::cout << "display image" << std::endl;
display_image(gpio, spi, argv[1]);
std::cout << "refresh" << std::endl;
display_refresh(gpio, spi);
std::cout << "shutdown" << std::endl;
lguSleep(5.0);
display_sleep(gpio, spi);
display_shutdown(gpio, spi);
lgSpiClose(spi);
lgGpiochipClose(gpio);
}