-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepaper.cpp
393 lines (313 loc) · 10.6 KB
/
epaper.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* -------------------------------------------------------------------------- */
/* NAME: Epaper Display CPP - Inky phat Pimoroni */
/* -------------------------------------------------------------------------- */
/* AUTHOR: Alessandro Rossetti */
/* VERSION: 0000 */
/* SOC: SSD1608 */
/* DOCUMENT REFERENCE: SSD1608_1.2.PDF */
/* -------------------------------------------------------------------------- */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "epaper.h"
/* --------------------------------------------------------------------------- */
/* TIMINGS DEFINE */
/* --------------------------------------------------------------------------- */
#define MAX_BUSY_WAIT_MS 20000
#define MAX_HARD_RESET_MS 500
#define MAX_SOFT_RESET_MS 500
/* --------------------------------------------------------------------------- */
/* DISPLAY PINS */
/* --------------------------------------------------------------------------- */
#define RESET_PIN 27
#define BUSY_PIN 17
#define DC_PIN 22 /* COMAND: 0, DATA: 1 */
#define CS_PIN 0
/* --------------------------------------------------------------------------- */
/* DISPLAY COMMANDS */
/* --------------------------------------------------------------------------- */
#define CMD_SOFT_RESET 0x12
#define CMD_DRIVER_CONTROL 0x01
#define CMD_WRITE_DUMMY 0x3A
#define CMD_WRITE_GATELINE 0x3B
#define CMD_DATA_MODE 0x11
#define CMD_SET_RAMXPOS 0x44
#define CMD_SET_RAMYPOS 0x45
#define CMD_WRITE_VCOM 0x2C
#define CMD_WRITE_LUT 0x32
#define CMD_SET_RAMXCOUNT 0x4E
#define CMD_SET_RAMYCOUNT 0x4F
#define CMD_WRITE_RAM 0x24
#define CMD_READ_RAM 0x25
#define CMD_WRITE_ALTRAM 0x26
#define CMD_MASTER_ACTIVATE 0x20
#define CMD_UPDATE_SEQUENCE 0x22
#define DATA_ENA_CLK_CP 0xC0
#define DATA_INTIAL_DISPLAY 0x08
#define DATA_DISPLAY_PATTEN 0x04
#define DATA_FULL_SEQUENCE 0xFF
/* --------------------------------------------------------------------------- */
/* LUTS */
/* --------------------------------------------------------------------------- */
const uint32_t lut_size = 30;
uint8_t lut_data[lut_size] = {
/* 00 */ 0x02,
/* 01 */ 0x02,
/* 02 */ 0x01,
/* 03 */ 0x11,
/* 04 */ 0x12,
/* 05 */ 0x12,
/* 06 */ 0x22,
/* 07 */ 0x22,
/* 08 */ 0x66,
/* 09 */ 0x69,
/* 10 */ 0x69,
/* 11 */ 0x59,
/* 12 */ 0x58,
/* 13 */ 0x99,
/* 14 */ 0x99,
/* 15 */ 0x88,
/* 16 */ 0x00,
/* 17 */ 0x00,
/* 18 */ 0x00,
/* 19 */ 0x00,
/* 20 */ 0xF0,
/* 21 */ 0xF0,
/* 22 */ 0xF0,
/* 23 */ 0xF0,
/* 24 */ 0xF0,
/* 25 */ 0xF0,
/* 26 */ 0xF0,
/* 27 */ 0xF0,
/* 28 */ 0xF0,
/* 29 */ 0xF0
};
/* --------------------------------------------------------------------------- */
/* HELPER FUNCTIONS */
/* --------------------------------------------------------------------------- */
static void assert(uint8_t cond, char* desc) {
if (!cond) {
fprintf(stderr, "\n\n[ERROR] %s\n\n\n", desc);
exit(1);
}
}
static void delay_ms(uint32_t ms) {
usleep(1000*ms);
}
/* --------------------------------------------------------------------------- */
/* PUBPLIC FUNCTIONS */
/* --------------------------------------------------------------------------- */
epaper::epaper() {
printf("[EPAP] Initialize SPI comunication.\n");
spi = new spi_device("/dev/spidev0.0", SPI_MODE_0, 8, 100000);
printf("[EPAP] Initialize GPIO comunication.\n");
gpio = new gpio_device();
printf("[EPAP] Set pins directions.\n");
gpio->direction(BUSY_PIN, gpio->INPUT);
gpio->direction(RESET_PIN, gpio->OUTPUT);
gpio->direction(DC_PIN, gpio->OUTPUT);
gpio->direction(CS_PIN, gpio->OUTPUT);
printf("[EPAP] Set pins default states.\n");
gpio->set(DC_PIN, LOW);
gpio->set(RESET_PIN, HIGH);
gpio->set(CS_PIN, HIGH);
clear(BLACK);
printf("[EPAP] Hard reset display.\n");
gpio->set(RESET_PIN, LOW);
delay_ms(MAX_HARD_RESET_MS);
gpio->set(RESET_PIN, HIGH);
delay_ms(MAX_HARD_RESET_MS);
printf("[EPAP] Soft reset display.\n");
send_command(CMD_SOFT_RESET);
delay_ms(MAX_SOFT_RESET_MS);
wait_while_busy();
printf("[EPAP] Read status.\n");
uint8_t data4 = recv_data();
printf("[EPAP] Status is %02X\n", data4);
wait_while_busy();
printf("[EPAP] Configure driver.\n");
send_command(CMD_DRIVER_CONTROL);
send_data(rows-1);
send_data((rows-1)>>8);
send_data(0x00);
wait_while_busy();
printf("[EPAP] Set Dummy line perio.\n");
send_both(CMD_WRITE_DUMMY, 0x1B);
wait_while_busy();
printf("[EPAP] Set Line Width.\n");
send_both(CMD_WRITE_GATELINE, 0x0B);
wait_while_busy();
printf("[EPAP] Set Data entry squence .\n");
send_both(CMD_DATA_MODE, 0x03); /* scan direction leftward and downward */
wait_while_busy();
printf("[EPAP] Set ram X start and end position.\n");
send_command(CMD_SET_RAMXPOS);
send_data(0x00);
send_data((cols/8)-1);
wait_while_busy();
printf("[EPAP] Set ram Y start and end position.\n");
send_command(CMD_SET_RAMYPOS);
send_data(0x00);
send_data(0x00);
send_data((rows - 1));
send_data((rows - 1) >> 8);
wait_while_busy();
printf("[EPAP] Set VCOM Voltage.\n");
send_both(CMD_WRITE_VCOM, 0x70);
wait_while_busy();
printf("[EPAP] Write LUT DATA.\n");
send_command(CMD_WRITE_LUT);
uint32_t i;
for (i = 0; i < lut_size; i++) {
send_data(lut_data[i]);
}
/* Self test */
printf("[EPAP] EPAP Self test:\n");
printf("[EPAP] Set RAM X address to 0.\n");
send_command(CMD_SET_RAMXCOUNT);
send_data(0x00);
wait_while_busy();
printf("[EPAP] Set RAM Y address to 0.\n");
send_command(CMD_SET_RAMYCOUNT);
send_data(0x00);
send_data(0x00);
wait_while_busy();
printf("[EPAP] Set first four values of RAM.\n");
send_command(CMD_WRITE_RAM);
send_data(0x0A);
send_data(0x1B);
send_data(0x2C);
send_data(0x3D);
send_command(CMD_WRITE_ALTRAM);
send_data(0x0A);
send_data(0x1B);
send_data(0x2C);
send_data(0x3D);
printf("[EPAP] Set RAM X address to 0.\n");
send_command(CMD_SET_RAMXCOUNT);
send_data(0x00);
wait_while_busy();
printf("[EPAP] Set RAM Y address to 0.\n");
send_command(CMD_SET_RAMYCOUNT);
send_data(0x00);
send_data(0x00);
wait_while_busy();
printf("[EPAP] Set first four values of RAM.\n");
send_command(CMD_READ_RAM);
uint8_t data0 = recv_data();
uint8_t data1 = recv_data();
uint8_t data2 = recv_data();
uint8_t data3 = recv_data();
printf("[EPAP] Set RAM X address to 0.\n");
send_command(CMD_SET_RAMXCOUNT);
send_data(0x00);
wait_while_busy();
printf("[EPAP] Set RAM Y address to 0.\n");
send_command(CMD_SET_RAMYCOUNT);
send_data(0x00);
send_data(0x00);
wait_while_busy();
if (data0 != 0x0A) printf("[EPAP] SELF TEST FAILED ON BYTE 0 {%02X,%02X} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", data0, 0x0A);
if (data1 != 0x1B) printf("[EPAP] SELF TEST FAILED ON BYTE 1 {%02X,%02X} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", data1, 0x1B);
if (data2 != 0x2C) printf("[EPAP] SELF TEST FAILED ON BYTE 2 {%02X,%02X} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", data2, 0x2C);
if (data3 != 0x3D) printf("[EPAP] SELF TEST FAILED ON BYTE 3 {%02X,%02X} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", data3, 0x3D);
printf("[EPAP] End of self test.\n");
}
epaper::~epaper() {
delete spi;
delete gpio;
}
void epaper::show(uint8_t wait) {
uint32_t i;
printf("[EPAP] Converting in to packet bits.\n");
buff_to_mem();
printf("[EPAP] Set RAM X address to 0.\n");
send_command(CMD_SET_RAMXCOUNT);
send_data(0x00);
wait_while_busy();
printf("[EPAP] Set RAM Y address to 0.\n");
send_command(CMD_SET_RAMYCOUNT);
send_data(0x00);
send_data(0x00);
wait_while_busy();
printf("[EPAP] Set Black/white buffer.\n");
send_command(CMD_WRITE_RAM);
for (i = 0; i < mem_len; i++) {
send_data(mem_data[0][i]);
}
printf("[EPAP] Set Yellow/white buffer.\n");
send_command(CMD_WRITE_ALTRAM);
for (i = 0; i < mem_len; i++) {
send_data(mem_data[1][i]);
}
printf("[EPAP] Master activate.\n");
send_command(CMD_MASTER_ACTIVATE);
if (wait == 1) {
wait_while_busy();
}
}
void epaper::clear(uint8_t color) {
printf("[EPAP] Clear buffers %s.\n", color == BLACK ? "BLACK": color == WHITE ? "WHITE": "YELLOW");
uint32_t x,y;
for (x = 0; x < width; x++) {
for (y = 0; y < heigth; y++) {
buff[x][y] = color;
}
}
}
/* --------------------------------------------------------------------------- */
/* PRIVATE FUNCTIONS */
/* --------------------------------------------------------------------------- */
void epaper::buff_to_mem(void) {
uint32_t currbit = 0;
uint32_t x,y;
for (y = 0; y < heigth; y++) {
for (x = 0; x < width; x++) {
uint32_t currbyte = currbit/8;
uint32_t curroffs = currbit%8;
curroffs = 7-curroffs;
assert(currbyte < mem_len, "[EPAP] buff_to_mem not working");
switch (buff[x][y]) {
case WHITE:
mem_data[0][currbyte] |= (1 << curroffs);
mem_data[1][currbyte] &= ~(1 << curroffs);
break;
case BLACK:
mem_data[0][currbyte] &= ~(1 << curroffs);
mem_data[1][currbyte] &= ~(1 << curroffs);
break;
case YELLOW:
mem_data[0][currbyte] &= ~(1 << curroffs);
mem_data[1][currbyte] |= (1 << curroffs);
break;
}
/* Increment idx */
currbit++;
}
}
}
void epaper::send_both(uint8_t cmd, uint8_t data) {
send_command(cmd);
send_data(data);
}
void epaper::send_command(uint8_t cmd) {
gpio->set(DC_PIN, LOW);
spi->writeSingle(cmd);
}
void epaper::send_data(uint8_t data) {
gpio->set(DC_PIN, HIGH);
spi->writeSingle(data);
}
uint8_t epaper::recv_data() {
gpio->set(DC_PIN, HIGH);
return spi->readSingle();
}
void epaper::wait_while_busy() {
uint32_t times = 0;
while (gpio->get(BUSY_PIN) == 1) {
delay_ms(10);
times++;
//assert((times*10) < MAX_BUSY_WAIT_MS, "Busy line high for too much.");
if ((times*10) > 10000) break;
}
}