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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/*
Copyright 2021
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "st7565.h"
#include <string.h>
#include "keyboard.h"
#include "progmem.h"
#include "timer.h"
#include "wait.h"
#include ST7565_FONT_H
// Fundamental Commands
#define CONTRAST 0x81
#define DISPLAY_ALL_ON 0xA5
#define DISPLAY_ALL_ON_RESUME 0xA4
#define NORMAL_DISPLAY 0xA6
#define DISPLAY_ON 0xAF
#define DISPLAY_OFF 0xAE
#define NOP 0xE3
// Addressing Setting Commands
#define PAM_SETCOLUMN_LSB 0x00
#define PAM_SETCOLUMN_MSB 0x10
#define PAM_PAGE_ADDR 0xB0 // 0xb0 -- 0xb7
// Hardware Configuration Commands
#define DISPLAY_START_LINE 0x40
#define SEGMENT_REMAP 0xA0
#define SEGMENT_REMAP_INV 0xA1
#define COM_SCAN_INC 0xC0
#define COM_SCAN_DEC 0xC8
#define LCD_BIAS_7 0xA3
#define LCD_BIAS_9 0xA2
#define RESISTOR_RATIO 0x20
#define POWER_CONTROL 0x28
// Misc defines
#ifndef ST7565_BLOCK_COUNT
# define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8)
#endif
#ifndef ST7565_BLOCK_SIZE
# define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT)
#endif
#define ST7565_ALL_BLOCKS_MASK (((((ST7565_BLOCK_TYPE)1 << (ST7565_BLOCK_COUNT - 1)) - 1) << 1) | 1)
#define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
// Display buffer's is the same as the display memory layout
// this is so we don't end up with rounding errors with
// parts of the display unusable or don't get cleared correctly
// and also allows for drawing & inverting
uint8_t st7565_buffer[ST7565_MATRIX_SIZE];
uint8_t * st7565_cursor;
ST7565_BLOCK_TYPE st7565_dirty = 0;
bool st7565_initialized = false;
bool st7565_active = false;
display_rotation_t st7565_rotation = DISPLAY_ROTATION_0;
#if ST7565_TIMEOUT > 0
uint32_t st7565_timeout;
#endif
#if ST7565_UPDATE_INTERVAL > 0
uint16_t st7565_update_timeout;
#endif
// Flips the rendering bits for a character at the current cursor position
static void InvertCharacter(uint8_t *cursor) {
const uint8_t *end = cursor + ST7565_FONT_WIDTH;
while (cursor < end) {
*cursor = ~(*cursor);
cursor++;
}
}
bool st7565_init(display_rotation_t rotation) {
setPinOutput(ST7565_A0_PIN);
writePinHigh(ST7565_A0_PIN);
setPinOutput(ST7565_RST_PIN);
writePinHigh(ST7565_RST_PIN);
st7565_rotation = st7565_init_user(rotation);
spi_init();
spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
st7565_reset();
st7565_send_cmd(LCD_BIAS_7);
if (!HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
st7565_send_cmd(SEGMENT_REMAP);
st7565_send_cmd(COM_SCAN_DEC);
} else {
st7565_send_cmd(SEGMENT_REMAP_INV);
st7565_send_cmd(COM_SCAN_INC);
}
st7565_send_cmd(DISPLAY_START_LINE | 0x00);
st7565_send_cmd(CONTRAST);
st7565_send_cmd(ST7565_CONTRAST);
st7565_send_cmd(RESISTOR_RATIO | 0x01);
st7565_send_cmd(POWER_CONTROL | 0x04);
wait_ms(50);
st7565_send_cmd(POWER_CONTROL | 0x06);
wait_ms(50);
st7565_send_cmd(POWER_CONTROL | 0x07);
wait_ms(10);
st7565_send_cmd(DISPLAY_ON);
st7565_send_cmd(DISPLAY_ALL_ON_RESUME);
st7565_send_cmd(NORMAL_DISPLAY);
spi_stop();
#if ST7565_TIMEOUT > 0
st7565_timeout = timer_read32() + ST7565_TIMEOUT;
#endif
st7565_clear();
st7565_initialized = true;
st7565_active = true;
return true;
}
__attribute__((weak)) display_rotation_t st7565_init_user(display_rotation_t rotation) { return rotation; }
void st7565_clear(void) {
memset(st7565_buffer, 0, sizeof(st7565_buffer));
st7565_cursor = &st7565_buffer[0];
st7565_dirty = ST7565_ALL_BLOCKS_MASK;
}
uint8_t crot(uint8_t a, int8_t n) {
const uint8_t mask = 0x7;
n &= mask;
return a << n | a >> (-n & mask);
}
void st7565_render(void) {
if (!st7565_initialized) {
return;
}
// Do we have work to do?
st7565_dirty &= ST7565_ALL_BLOCKS_MASK;
if (!st7565_dirty) {
return;
}
// Find first dirty block
uint8_t update_start = 0;
while (!(st7565_dirty & ((ST7565_BLOCK_TYPE)1 << update_start))) {
++update_start;
}
// Calculate commands to set memory addressing bounds.
uint8_t start_page = ST7565_BLOCK_SIZE * update_start / ST7565_DISPLAY_WIDTH;
uint8_t start_column = ST7565_BLOCK_SIZE * update_start % ST7565_DISPLAY_WIDTH;
// IC has 132 segment drivers, for panels with less width we need to offset the starting column
if (HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
start_column += (132 - ST7565_DISPLAY_WIDTH);
}
spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
st7565_send_cmd(PAM_PAGE_ADDR | start_page);
st7565_send_cmd(PAM_SETCOLUMN_LSB | ((ST7565_COLUMN_OFFSET + start_column) & 0x0f));
st7565_send_cmd(PAM_SETCOLUMN_MSB | ((ST7565_COLUMN_OFFSET + start_column) >> 4 & 0x0f));
st7565_send_data(&st7565_buffer[ST7565_BLOCK_SIZE * update_start], ST7565_BLOCK_SIZE);
// Turn on display if it is off
st7565_on();
// Clear dirty flag
st7565_dirty &= ~((ST7565_BLOCK_TYPE)1 << update_start);
}
void st7565_set_cursor(uint8_t col, uint8_t line) {
uint16_t index = line * ST7565_DISPLAY_WIDTH + col * ST7565_FONT_WIDTH;
// Out of bounds?
if (index >= ST7565_MATRIX_SIZE) {
index = 0;
}
st7565_cursor = &st7565_buffer[index];
}
void st7565_advance_page(bool clearPageRemainder) {
uint16_t index = st7565_cursor - &st7565_buffer[0];
uint8_t remaining = ST7565_DISPLAY_WIDTH - (index % ST7565_DISPLAY_WIDTH);
if (clearPageRemainder) {
// Remaining Char count
remaining = remaining / ST7565_FONT_WIDTH;
// Write empty character until next line
while (remaining--) st7565_write_char(' ', false);
} else {
// Next page index out of bounds?
if (index + remaining >= ST7565_MATRIX_SIZE) {
index = 0;
remaining = 0;
}
st7565_cursor = &st7565_buffer[index + remaining];
}
}
void st7565_advance_char(void) {
uint16_t nextIndex = st7565_cursor - &st7565_buffer[0] + ST7565_FONT_WIDTH;
uint8_t remainingSpace = ST7565_DISPLAY_WIDTH - (nextIndex % ST7565_DISPLAY_WIDTH);
// Do we have enough space on the current line for the next character
if (remainingSpace < ST7565_FONT_WIDTH) {
nextIndex += remainingSpace;
}
// Did we go out of bounds
if (nextIndex >= ST7565_MATRIX_SIZE) {
nextIndex = 0;
}
// Update cursor position
st7565_cursor = &st7565_buffer[nextIndex];
}
// Main handler that writes character data to the display buffer
void st7565_write_char(const char data, bool invert) {
// Advance to the next line if newline
if (data == '\n') {
// Old source wrote ' ' until end of line...
st7565_advance_page(true);
return;
}
if (data == '\r') {
st7565_advance_page(false);
return;
}
// copy the current render buffer to check for dirty after
static uint8_t st7565_temp_buffer[ST7565_FONT_WIDTH];
memcpy(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH);
_Static_assert(sizeof(font) >= ((ST7565_FONT_END + 1 - ST7565_FONT_START) * ST7565_FONT_WIDTH), "ST7565_FONT_END references outside array");
// set the reder buffer data
uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
if (cast_data < ST7565_FONT_START || cast_data > ST7565_FONT_END) {
memset(st7565_cursor, 0x00, ST7565_FONT_WIDTH);
} else {
const uint8_t *glyph = &font[(cast_data - ST7565_FONT_START) * ST7565_FONT_WIDTH];
memcpy_P(st7565_cursor, glyph, ST7565_FONT_WIDTH);
}
// Invert if needed
if (invert) {
InvertCharacter(st7565_cursor);
}
// Dirty check
if (memcmp(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH)) {
uint16_t index = st7565_cursor - &st7565_buffer[0];
st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
// Edgecase check if the written data spans the 2 chunks
st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << ((index + ST7565_FONT_WIDTH - 1) / ST7565_BLOCK_SIZE));
}
// Finally move to the next char
st7565_advance_char();
}
void st7565_write(const char *data, bool invert) {
const char *end = data + strlen(data);
while (data < end) {
st7565_write_char(*data, invert);
data++;
}
}
void st7565_write_ln(const char *data, bool invert) {
st7565_write(data, invert);
st7565_advance_page(true);
}
void st7565_pan(bool left) {
uint16_t i = 0;
for (uint16_t y = 0; y < ST7565_DISPLAY_HEIGHT / 8; y++) {
if (left) {
for (uint16_t x = 0; x < ST7565_DISPLAY_WIDTH - 1; x++) {
i = y * ST7565_DISPLAY_WIDTH + x;
st7565_buffer[i] = st7565_buffer[i + 1];
}
} else {
for (uint16_t x = ST7565_DISPLAY_WIDTH - 1; x > 0; x--) {
i = y * ST7565_DISPLAY_WIDTH + x;
st7565_buffer[i] = st7565_buffer[i - 1];
}
}
}
st7565_dirty = ST7565_ALL_BLOCKS_MASK;
}
display_buffer_reader_t st7565_read_raw(uint16_t start_index) {
if (start_index > ST7565_MATRIX_SIZE) start_index = ST7565_MATRIX_SIZE;
display_buffer_reader_t ret_reader;
ret_reader.current_element = &st7565_buffer[start_index];
ret_reader.remaining_element_count = ST7565_MATRIX_SIZE - start_index;
return ret_reader;
}
void st7565_write_raw_byte(const char data, uint16_t index) {
if (index > ST7565_MATRIX_SIZE) index = ST7565_MATRIX_SIZE;
if (st7565_buffer[index] == data) return;
st7565_buffer[index] = data;
st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
}
void st7565_write_raw(const char *data, uint16_t size) {
uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
uint8_t c = *data++;
if (st7565_buffer[i] == c) continue;
st7565_buffer[i] = c;
st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
}
}
void st7565_write_pixel(uint8_t x, uint8_t y, bool on) {
if (x >= ST7565_DISPLAY_WIDTH) {
return;
}
uint16_t index = x + (y / 8) * ST7565_DISPLAY_WIDTH;
if (index >= ST7565_MATRIX_SIZE) {
return;
}
uint8_t data = st7565_buffer[index];
if (on) {
data |= (1 << (y % 8));
} else {
data &= ~(1 << (y % 8));
}
if (st7565_buffer[index] != data) {
st7565_buffer[index] = data;
st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
}
}
#if defined(__AVR__)
void st7565_write_P(const char *data, bool invert) {
uint8_t c = pgm_read_byte(data);
while (c != 0) {
st7565_write_char(c, invert);
c = pgm_read_byte(++data);
}
}
void st7565_write_ln_P(const char *data, bool invert) {
st7565_write_P(data, invert);
st7565_advance_page(true);
}
void st7565_write_raw_P(const char *data, uint16_t size) {
uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
uint8_t c = pgm_read_byte(data++);
if (st7565_buffer[i] == c) continue;
st7565_buffer[i] = c;
st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
}
}
#endif // defined(__AVR__)
bool st7565_on(void) {
if (!st7565_initialized) {
return st7565_active;
}
#if ST7565_TIMEOUT > 0
st7565_timeout = timer_read32() + ST7565_TIMEOUT;
#endif
if (!st7565_active) {
spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
st7565_send_cmd(DISPLAY_ON);
spi_stop();
st7565_active = true;
st7565_on_user();
}
return st7565_active;
}
__attribute__((weak)) void st7565_on_user(void) {}
bool st7565_off(void) {
if (!st7565_initialized) {
return !st7565_active;
}
if (st7565_active) {
spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
st7565_send_cmd(DISPLAY_OFF);
spi_stop();
st7565_active = false;
st7565_off_user();
}
return !st7565_active;
}
__attribute__((weak)) void st7565_off_user(void) {}
bool st7565_is_on(void) { return st7565_active; }
uint8_t st7565_max_chars(void) { return ST7565_DISPLAY_WIDTH / ST7565_FONT_WIDTH; }
uint8_t st7565_max_lines(void) { return ST7565_DISPLAY_HEIGHT / ST7565_FONT_HEIGHT; }
void st7565_task(void) {
if (!st7565_initialized) {
return;
}
#if ST7565_UPDATE_INTERVAL > 0
if (timer_elapsed(st7565_update_timeout) >= ST7565_UPDATE_INTERVAL) {
st7565_update_timeout = timer_read();
st7565_set_cursor(0, 0);
st7565_task_user();
}
#else
st7565_set_cursor(0, 0);
st7565_task_user();
#endif
// Smart render system, no need to check for dirty
st7565_render();
// Display timeout check
#if ST7565_TIMEOUT > 0
if (st7565_active && timer_expired32(timer_read32(), st7565_timeout)) {
st7565_off();
}
#endif
}
__attribute__((weak)) void st7565_task_user(void) {}
void st7565_reset(void) {
writePinLow(ST7565_RST_PIN);
wait_ms(20);
writePinHigh(ST7565_RST_PIN);
wait_ms(20);
}
spi_status_t st7565_send_cmd(uint8_t cmd) {
writePinLow(ST7565_A0_PIN);
return spi_write(cmd);
}
spi_status_t st7565_send_data(uint8_t *data, uint16_t length) {
writePinHigh(ST7565_A0_PIN);
return spi_transmit(data, length);
}
|