diff options
Diffstat (limited to 'users/drashna/oled/sh110x.c')
-rw-r--r-- | users/drashna/oled/sh110x.c | 860 |
1 files changed, 860 insertions, 0 deletions
diff --git a/users/drashna/oled/sh110x.c b/users/drashna/oled/sh110x.c new file mode 100644 index 000000000..c850a4753 --- /dev/null +++ b/users/drashna/oled/sh110x.c | |||
@@ -0,0 +1,860 @@ | |||
1 | /* | ||
2 | Copyright 2019 Ryan Caltabiano <https://github.com/XScorpion2> | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation, either version 2 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | #include "i2c_master.h" | ||
18 | #include "oled_driver.h" | ||
19 | #include OLED_FONT_H | ||
20 | #include "timer.h" | ||
21 | #include "print.h" | ||
22 | |||
23 | #include <string.h> | ||
24 | |||
25 | #include "progmem.h" | ||
26 | |||
27 | #include "keyboard.h" | ||
28 | |||
29 | // Used commands from spec sheet: https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf | ||
30 | // for SH1106: https://www.velleman.eu/downloads/29/infosheets/sh1106_datasheet.pdf | ||
31 | |||
32 | // Fundamental Commands | ||
33 | #define CONTRAST 0x81 | ||
34 | #define DISPLAY_ALL_ON 0xA5 | ||
35 | #define DISPLAY_ALL_ON_RESUME 0xA4 | ||
36 | #define NORMAL_DISPLAY 0xA6 | ||
37 | #define INVERT_DISPLAY 0xA7 | ||
38 | #define DISPLAY_ON 0xAF | ||
39 | #define DISPLAY_OFF 0xAE | ||
40 | #define NOP 0xE3 | ||
41 | |||
42 | // Scrolling Commands | ||
43 | #define ACTIVATE_SCROLL 0x2F | ||
44 | #define DEACTIVATE_SCROLL 0x2E | ||
45 | #define SCROLL_RIGHT 0x26 | ||
46 | #define SCROLL_LEFT 0x27 | ||
47 | #define SCROLL_RIGHT_UP 0x29 | ||
48 | #define SCROLL_LEFT_UP 0x2A | ||
49 | |||
50 | // Addressing Setting Commands | ||
51 | #define MEMORY_MODE 0x20 | ||
52 | #define COLUMN_ADDR 0x21 | ||
53 | #define PAGE_ADDR 0x22 | ||
54 | #define PAM_SETCOLUMN_LSB 0x00 | ||
55 | #define PAM_SETCOLUMN_MSB 0x10 | ||
56 | #define PAM_PAGE_ADDR 0xB0 // 0xb0 -- 0xb7 | ||
57 | |||
58 | // Hardware Configuration Commands | ||
59 | #define DISPLAY_START_LINE 0x40 | ||
60 | #define SEGMENT_REMAP 0xA0 | ||
61 | #define SEGMENT_REMAP_INV 0xA1 | ||
62 | #define MULTIPLEX_RATIO 0xA8 | ||
63 | #define COM_SCAN_INC 0xC0 | ||
64 | #define COM_SCAN_DEC 0xC8 | ||
65 | #define DISPLAY_OFFSET 0xD3 | ||
66 | #define COM_PINS 0xDA | ||
67 | #define COM_PINS_SEQ 0x02 | ||
68 | #define COM_PINS_ALT 0x12 | ||
69 | #define COM_PINS_SEQ_LR 0x22 | ||
70 | #define COM_PINS_ALT_LR 0x32 | ||
71 | |||
72 | // Timing & Driving Commands | ||
73 | #define DISPLAY_CLOCK 0xD5 | ||
74 | #define PRE_CHARGE_PERIOD 0xD9 | ||
75 | #define VCOM_DETECT 0xDB | ||
76 | |||
77 | // Advance Graphic Commands | ||
78 | #define FADE_BLINK 0x23 | ||
79 | #define ENABLE_FADE 0x20 | ||
80 | #define ENABLE_BLINK 0x30 | ||
81 | |||
82 | // Charge Pump Commands | ||
83 | #define CHARGE_PUMP 0x8D | ||
84 | |||
85 | // Commands specific to the SH1107 chip | ||
86 | #define SH1107_DISPLAY_START_LINE 0xDC | ||
87 | #define SH1107_MEMORY_MODE_PAGE 0x20 | ||
88 | #define SH1107_MEMORY_MODE_VERTICAL 0x21 | ||
89 | |||
90 | // Misc defines | ||
91 | #ifndef OLED_BLOCK_COUNT | ||
92 | # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) | ||
93 | #endif | ||
94 | #ifndef OLED_BLOCK_SIZE | ||
95 | # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) | ||
96 | #endif | ||
97 | |||
98 | #define OLED_ALL_BLOCKS_MASK (((((OLED_BLOCK_TYPE)1 << (OLED_BLOCK_COUNT - 1)) - 1) << 1) | 1) | ||
99 | |||
100 | #define OLED_IC_HAS_HORIZONTAL_MODE (OLED_IC == OLED_IC_SSD1306) | ||
101 | #define OLED_IC_COM_PINS_ARE_COLUMNS (OLED_IC == OLED_IC_SH1107) | ||
102 | |||
103 | #ifndef OLED_COM_PIN_COUNT | ||
104 | # if OLED_IC == OLED_IC_SH1106 | ||
105 | # define OLED_COM_PIN_COUNT 64 | ||
106 | # elif OLED_IC == OLED_IC_SH1107 | ||
107 | # define OLED_COM_PIN_COUNT 128 | ||
108 | # else | ||
109 | # error Invalid OLED_IC value | ||
110 | # endif | ||
111 | #endif | ||
112 | |||
113 | #ifndef OLED_COM_PIN_OFFSET | ||
114 | # define OLED_COM_PIN_OFFSET 0 | ||
115 | #endif | ||
116 | |||
117 | // i2c defines | ||
118 | #define I2C_CMD 0x00 | ||
119 | #define I2C_DATA 0x40 | ||
120 | #if defined(__AVR__) | ||
121 | # define I2C_TRANSMIT_P(data) i2c_transmit_P((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT) | ||
122 | #else // defined(__AVR__) | ||
123 | # define I2C_TRANSMIT_P(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT) | ||
124 | #endif // defined(__AVR__) | ||
125 | #define I2C_TRANSMIT(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT) | ||
126 | #define I2C_WRITE_REG(mode, data, size) i2c_writeReg((OLED_DISPLAY_ADDRESS << 1), mode, data, size, OLED_I2C_TIMEOUT) | ||
127 | |||
128 | #define HAS_FLAGS(bits, flags) ((bits & flags) == flags) | ||
129 | |||
130 | // Display buffer's is the same as the OLED memory layout | ||
131 | // this is so we don't end up with rounding errors with | ||
132 | // parts of the display unusable or don't get cleared correctly | ||
133 | // and also allows for drawing & inverting | ||
134 | uint8_t oled_buffer[OLED_MATRIX_SIZE]; | ||
135 | uint8_t * oled_cursor; | ||
136 | OLED_BLOCK_TYPE oled_dirty = 0; | ||
137 | bool oled_initialized = false; | ||
138 | bool oled_active = false; | ||
139 | bool oled_scrolling = false; | ||
140 | bool oled_inverted = false; | ||
141 | uint8_t oled_brightness = OLED_BRIGHTNESS; | ||
142 | oled_rotation_t oled_rotation = 0; | ||
143 | uint8_t oled_rotation_width = 0; | ||
144 | uint8_t oled_scroll_speed = 0; // this holds the speed after being remapped to ssd1306 internal values | ||
145 | uint8_t oled_scroll_start = 0; | ||
146 | uint8_t oled_scroll_end = 7; | ||
147 | #if OLED_TIMEOUT > 0 | ||
148 | uint32_t oled_timeout; | ||
149 | #endif | ||
150 | #if OLED_SCROLL_TIMEOUT > 0 | ||
151 | uint32_t oled_scroll_timeout; | ||
152 | #endif | ||
153 | #if OLED_UPDATE_INTERVAL > 0 | ||
154 | uint16_t oled_update_timeout; | ||
155 | #endif | ||
156 | |||
157 | // Internal variables to reduce math instructions | ||
158 | |||
159 | #if defined(__AVR__) | ||
160 | // identical to i2c_transmit, but for PROGMEM since all initialization is in PROGMEM arrays currently | ||
161 | // probably should move this into i2c_master... | ||
162 | static i2c_status_t i2c_transmit_P(uint8_t address, const uint8_t *data, uint16_t length, uint16_t timeout) { | ||
163 | i2c_status_t status = i2c_start(address | I2C_WRITE, timeout); | ||
164 | |||
165 | for (uint16_t i = 0; i < length && status >= 0; i++) { | ||
166 | status = i2c_write(pgm_read_byte((const char *)data++), timeout); | ||
167 | if (status) break; | ||
168 | } | ||
169 | |||
170 | i2c_stop(); | ||
171 | |||
172 | return status; | ||
173 | } | ||
174 | #endif | ||
175 | |||
176 | // Flips the rendering bits for a character at the current cursor position | ||
177 | static void InvertCharacter(uint8_t *cursor) { | ||
178 | const uint8_t *end = cursor + OLED_FONT_WIDTH; | ||
179 | while (cursor < end) { | ||
180 | *cursor = ~(*cursor); | ||
181 | cursor++; | ||
182 | } | ||
183 | } | ||
184 | |||
185 | bool oled_init(oled_rotation_t rotation) { | ||
186 | #if defined(USE_I2C) && defined(SPLIT_KEYBOARD) | ||
187 | if (!is_keyboard_master()) { | ||
188 | return true; | ||
189 | } | ||
190 | #endif | ||
191 | |||
192 | oled_rotation = oled_init_user(oled_init_kb(rotation)); | ||
193 | if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) { | ||
194 | oled_rotation_width = OLED_DISPLAY_WIDTH; | ||
195 | } else { | ||
196 | oled_rotation_width = OLED_DISPLAY_HEIGHT; | ||
197 | } | ||
198 | i2c_init(); | ||
199 | |||
200 | static const uint8_t PROGMEM display_setup1[] = { | ||
201 | I2C_CMD, | ||
202 | DISPLAY_OFF, | ||
203 | DISPLAY_CLOCK, | ||
204 | 0x80, | ||
205 | MULTIPLEX_RATIO, | ||
206 | #if OLED_IC_COM_PINS_ARE_COLUMNS | ||
207 | OLED_DISPLAY_WIDTH - 1, | ||
208 | #else | ||
209 | OLED_DISPLAY_HEIGHT - 1, | ||
210 | #endif | ||
211 | DISPLAY_OFFSET, | ||
212 | 0x00, | ||
213 | DISPLAY_START_LINE | 0x00, | ||
214 | CHARGE_PUMP, | ||
215 | 0x14, | ||
216 | #if (OLED_IC != OLED_IC_SH1106) | ||
217 | // MEMORY_MODE is unsupported on SH1106 (Page Addressing only) | ||
218 | MEMORY_MODE, | ||
219 | 0x00, // Horizontal addressing mode | ||
220 | #elif OLED_IC == OLED_IC_SH1107 | ||
221 | // Page addressing mode | ||
222 | SH1107_MEMORY_MODE_PAGE, | ||
223 | #endif | ||
224 | }; | ||
225 | if (I2C_TRANSMIT_P(display_setup1) != I2C_STATUS_SUCCESS) { | ||
226 | print("oled_init cmd set 1 failed\n"); | ||
227 | return false; | ||
228 | } | ||
229 | |||
230 | if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) { | ||
231 | static const uint8_t PROGMEM display_normal[] = { | ||
232 | I2C_CMD, SEGMENT_REMAP_INV, COM_SCAN_DEC, DISPLAY_OFFSET, OLED_COM_PIN_OFFSET, | ||
233 | }; | ||
234 | if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) { | ||
235 | print("oled_init cmd normal rotation failed\n"); | ||
236 | return false; | ||
237 | } | ||
238 | } else { | ||
239 | static const uint8_t PROGMEM display_flipped[] = { | ||
240 | I2C_CMD, SEGMENT_REMAP, COM_SCAN_INC, DISPLAY_OFFSET, (OLED_COM_PIN_COUNT - OLED_COM_PIN_OFFSET) % OLED_COM_PIN_COUNT, | ||
241 | }; | ||
242 | if (I2C_TRANSMIT_P(display_flipped) != I2C_STATUS_SUCCESS) { | ||
243 | print("display_flipped failed\n"); | ||
244 | return false; | ||
245 | } | ||
246 | } | ||
247 | |||
248 | static const uint8_t PROGMEM display_setup2[] = {I2C_CMD, COM_PINS, OLED_COM_PINS, CONTRAST, OLED_BRIGHTNESS, PRE_CHARGE_PERIOD, 0x22, VCOM_DETECT, 0x35, DISPLAY_ALL_ON_RESUME, NORMAL_DISPLAY, DEACTIVATE_SCROLL, DISPLAY_ON}; if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) { | ||
249 | print("display_setup2 failed\n"); | ||
250 | return false; | ||
251 | } | ||
252 | |||
253 | #if OLED_TIMEOUT > 0 | ||
254 | oled_timeout = timer_read32() + OLED_TIMEOUT; | ||
255 | #endif | ||
256 | #if OLED_SCROLL_TIMEOUT > 0 | ||
257 | oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT; | ||
258 | #endif | ||
259 | |||
260 | oled_clear(); | ||
261 | oled_initialized = true; | ||
262 | oled_active = true; | ||
263 | oled_scrolling = false; | ||
264 | return true; | ||
265 | } | ||
266 | |||
267 | __attribute__((weak)) oled_rotation_t oled_init_kb(oled_rotation_t rotation) { return rotation; } | ||
268 | __attribute__((weak)) oled_rotation_t oled_init_user(oled_rotation_t rotation) { return rotation; } | ||
269 | |||
270 | void oled_clear(void) { | ||
271 | memset(oled_buffer, 0, sizeof(oled_buffer)); | ||
272 | oled_cursor = &oled_buffer[0]; | ||
273 | oled_dirty = OLED_ALL_BLOCKS_MASK; | ||
274 | } | ||
275 | |||
276 | static void calc_bounds(uint8_t update_start, uint8_t *cmd_array) { | ||
277 | // Calculate commands to set memory addressing bounds. | ||
278 | uint8_t start_page = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_WIDTH; | ||
279 | uint8_t start_column = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_WIDTH; | ||
280 | #if !OLED_IC_HAS_HORIZONTAL_MODE | ||
281 | // Commands for Page Addressing Mode. Sets starting page and column; has no end bound. | ||
282 | // Column value must be split into high and low nybble and sent as two commands. | ||
283 | cmd_array[0] = PAM_PAGE_ADDR | start_page; | ||
284 | cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f); | ||
285 | cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f); | ||
286 | cmd_array[3] = NOP; | ||
287 | cmd_array[4] = NOP; | ||
288 | cmd_array[5] = NOP; | ||
289 | #else | ||
290 | // Commands for use in Horizontal Addressing mode. | ||
291 | cmd_array[1] = start_column + OLED_COLUMN_OFFSET; | ||
292 | cmd_array[4] = start_page; | ||
293 | cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) % OLED_DISPLAY_WIDTH + cmd_array[1]; | ||
294 | cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) / OLED_DISPLAY_WIDTH - 1 + cmd_array[4]; | ||
295 | #endif | ||
296 | } | ||
297 | |||
298 | static void calc_bounds_90(uint8_t update_start, uint8_t *cmd_array) { | ||
299 | // Block numbering starts from the bottom left corner, going up and then to | ||
300 | // the right. The controller needs the page and column numbers for the top | ||
301 | // left and bottom right corners of that block. | ||
302 | |||
303 | // Total number of pages across the screen height. | ||
304 | const uint8_t height_in_pages = OLED_DISPLAY_HEIGHT / 8; | ||
305 | |||
306 | // Difference of starting page numbers for adjacent blocks; may be 0 if | ||
307 | // blocks are large enough to occupy one or more whole 8px columns. | ||
308 | const uint8_t page_inc_per_block = OLED_BLOCK_SIZE % OLED_DISPLAY_HEIGHT / 8; | ||
309 | |||
310 | // Top page number for a block which is at the bottom edge of the screen. | ||
311 | const uint8_t bottom_block_top_page = (height_in_pages - page_inc_per_block) % height_in_pages; | ||
312 | |||
313 | #if !OLED_IC_HAS_HORIZONTAL_MODE | ||
314 | // Only the Page Addressing Mode is supported | ||
315 | uint8_t start_page = bottom_block_top_page - (OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT / 8); | ||
316 | uint8_t start_column = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8; | ||
317 | cmd_array[0] = PAM_PAGE_ADDR | start_page; | ||
318 | cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f); | ||
319 | cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f); | ||
320 | #else | ||
321 | cmd_array[1] = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8 + OLED_COLUMN_OFFSET; | ||
322 | cmd_array[4] = bottom_block_top_page - (OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT / 8); | ||
323 | cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8 - 1 + cmd_array[1]; | ||
324 | cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) % OLED_DISPLAY_HEIGHT / 8 + cmd_array[4]; | ||
325 | #endif | ||
326 | } | ||
327 | |||
328 | uint8_t crot(uint8_t a, int8_t n) { | ||
329 | const uint8_t mask = 0x7; | ||
330 | n &= mask; | ||
331 | return a << n | a >> (-n & mask); | ||
332 | } | ||
333 | |||
334 | static void rotate_90(const uint8_t *src, uint8_t *dest) { | ||
335 | for (uint8_t i = 0, shift = 7; i < 8; ++i, --shift) { | ||
336 | uint8_t selector = (1 << i); | ||
337 | for (uint8_t j = 0; j < 8; ++j) { | ||
338 | dest[i] |= crot(src[j] & selector, shift - (int8_t)j); | ||
339 | } | ||
340 | } | ||
341 | } | ||
342 | |||
343 | void oled_render(void) { | ||
344 | if (!oled_initialized) { | ||
345 | return; | ||
346 | } | ||
347 | |||
348 | // Do we have work to do? | ||
349 | oled_dirty &= OLED_ALL_BLOCKS_MASK; | ||
350 | if (!oled_dirty || oled_scrolling) { | ||
351 | return; | ||
352 | } | ||
353 | |||
354 | // Find first dirty block | ||
355 | uint8_t update_start = 0; | ||
356 | while (!(oled_dirty & ((OLED_BLOCK_TYPE)1 << update_start))) { | ||
357 | ++update_start; | ||
358 | } | ||
359 | |||
360 | // Set column & page position | ||
361 | #if OLED_IC_HAS_HORIZONTAL_MODE | ||
362 | static uint8_t display_start[] = {I2C_CMD, COLUMN_ADDR, 0, OLED_DISPLAY_WIDTH - 1, PAGE_ADDR, 0, OLED_DISPLAY_HEIGHT / 8 - 1}; | ||
363 | #else | ||
364 | static uint8_t display_start[] = {I2C_CMD, PAM_PAGE_ADDR, PAM_SETCOLUMN_LSB, PAM_SETCOLUMN_MSB}; | ||
365 | #endif | ||
366 | if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) { | ||
367 | calc_bounds(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start | ||
368 | } else { | ||
369 | calc_bounds_90(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start | ||
370 | } | ||
371 | |||
372 | // Send column & page position | ||
373 | if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) { | ||
374 | print("oled_render offset command failed\n"); | ||
375 | return; | ||
376 | } | ||
377 | |||
378 | if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) { | ||
379 | // Send render data chunk as is | ||
380 | if (I2C_WRITE_REG(I2C_DATA, &oled_buffer[OLED_BLOCK_SIZE * update_start], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) { | ||
381 | print("oled_render data failed\n"); | ||
382 | return; | ||
383 | } | ||
384 | } else { | ||
385 | // Rotate the render chunks | ||
386 | const static uint8_t source_map[] = OLED_SOURCE_MAP; | ||
387 | const static uint8_t target_map[] = OLED_TARGET_MAP; | ||
388 | |||
389 | static uint8_t temp_buffer[OLED_BLOCK_SIZE]; | ||
390 | memset(temp_buffer, 0, sizeof(temp_buffer)); | ||
391 | for (uint8_t i = 0; i < sizeof(source_map); ++i) { | ||
392 | rotate_90(&oled_buffer[OLED_BLOCK_SIZE * update_start + source_map[i]], &temp_buffer[target_map[i]]); | ||
393 | } | ||
394 | |||
395 | #if OLED_IC_HAS_HORIZONTAL_MODE | ||
396 | // Send render data chunk after rotating | ||
397 | if (I2C_WRITE_REG(I2C_DATA, &temp_buffer[0], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) { | ||
398 | print("oled_render90 data failed\n"); | ||
399 | return; | ||
400 | } | ||
401 | #else | ||
402 | // For SH1106 or SH1107 the data chunk must be split into separate pieces for each page | ||
403 | const uint8_t columns_in_block = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8; | ||
404 | const uint8_t num_pages = OLED_BLOCK_SIZE / columns_in_block; | ||
405 | for (uint8_t i = 0; i < num_pages; ++i) { | ||
406 | // Send column & page position for all pages except the first one | ||
407 | if (i > 0) { | ||
408 | display_start[1]++; | ||
409 | if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) { | ||
410 | print("oled_render offset command failed\n"); | ||
411 | return; | ||
412 | } | ||
413 | } | ||
414 | // Send data for the page | ||
415 | if (I2C_WRITE_REG(I2C_DATA, &temp_buffer[columns_in_block * i], columns_in_block) != I2C_STATUS_SUCCESS) { | ||
416 | print("oled_render90 data failed\n"); | ||
417 | return; | ||
418 | } | ||
419 | } | ||
420 | #endif | ||
421 | } | ||
422 | |||
423 | // Turn on display if it is off | ||
424 | oled_on(); | ||
425 | |||
426 | // Clear dirty flag | ||
427 | oled_dirty &= ~((OLED_BLOCK_TYPE)1 << update_start); | ||
428 | } | ||
429 | |||
430 | void oled_set_cursor(uint8_t col, uint8_t line) { | ||
431 | uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH; | ||
432 | |||
433 | // Out of bounds? | ||
434 | if (index >= OLED_MATRIX_SIZE) { | ||
435 | index = 0; | ||
436 | } | ||
437 | |||
438 | oled_cursor = &oled_buffer[index]; | ||
439 | } | ||
440 | |||
441 | void oled_advance_page(bool clearPageRemainder) { | ||
442 | uint16_t index = oled_cursor - &oled_buffer[0]; | ||
443 | uint8_t remaining = oled_rotation_width - (index % oled_rotation_width); | ||
444 | |||
445 | if (clearPageRemainder) { | ||
446 | // Remaining Char count | ||
447 | remaining = remaining / OLED_FONT_WIDTH; | ||
448 | |||
449 | // Write empty character until next line | ||
450 | while (remaining--) oled_write_char(' ', false); | ||
451 | } else { | ||
452 | // Next page index out of bounds? | ||
453 | if (index + remaining >= OLED_MATRIX_SIZE) { | ||
454 | index = 0; | ||
455 | remaining = 0; | ||
456 | } | ||
457 | |||
458 | oled_cursor = &oled_buffer[index + remaining]; | ||
459 | } | ||
460 | } | ||
461 | |||
462 | void oled_advance_char(void) { | ||
463 | uint16_t nextIndex = oled_cursor - &oled_buffer[0] + OLED_FONT_WIDTH; | ||
464 | uint8_t remainingSpace = oled_rotation_width - (nextIndex % oled_rotation_width); | ||
465 | |||
466 | // Do we have enough space on the current line for the next character | ||
467 | if (remainingSpace < OLED_FONT_WIDTH) { | ||
468 | nextIndex += remainingSpace; | ||
469 | } | ||
470 | |||
471 | // Did we go out of bounds | ||
472 | if (nextIndex >= OLED_MATRIX_SIZE) { | ||
473 | nextIndex = 0; | ||
474 | } | ||
475 | |||
476 | // Update cursor position | ||
477 | oled_cursor = &oled_buffer[nextIndex]; | ||
478 | } | ||
479 | |||
480 | // Main handler that writes character data to the display buffer | ||
481 | void oled_write_char(const char data, bool invert) { | ||
482 | // Advance to the next line if newline | ||
483 | if (data == '\n') { | ||
484 | // Old source wrote ' ' until end of line... | ||
485 | oled_advance_page(true); | ||
486 | return; | ||
487 | } | ||
488 | |||
489 | if (data == '\r') { | ||
490 | oled_advance_page(false); | ||
491 | return; | ||
492 | } | ||
493 | |||
494 | // copy the current render buffer to check for dirty after | ||
495 | static uint8_t oled_temp_buffer[OLED_FONT_WIDTH]; | ||
496 | memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH); | ||
497 | |||
498 | _Static_assert(sizeof(font) >= ((OLED_FONT_END + 1 - OLED_FONT_START) * OLED_FONT_WIDTH), "OLED_FONT_END references outside array"); | ||
499 | |||
500 | // set the reder buffer data | ||
501 | uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index | ||
502 | if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) { | ||
503 | memset(oled_cursor, 0x00, OLED_FONT_WIDTH); | ||
504 | } else { | ||
505 | const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH]; | ||
506 | memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH); | ||
507 | } | ||
508 | |||
509 | // Invert if needed | ||
510 | if (invert) { | ||
511 | InvertCharacter(oled_cursor); | ||
512 | } | ||
513 | |||
514 | // Dirty check | ||
515 | if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) { | ||
516 | uint16_t index = oled_cursor - &oled_buffer[0]; | ||
517 | oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE)); | ||
518 | // Edgecase check if the written data spans the 2 chunks | ||
519 | oled_dirty |= ((OLED_BLOCK_TYPE)1 << ((index + OLED_FONT_WIDTH - 1) / OLED_BLOCK_SIZE)); | ||
520 | } | ||
521 | |||
522 | // Finally move to the next char | ||
523 | oled_advance_char(); | ||
524 | } | ||
525 | |||
526 | void oled_write(const char *data, bool invert) { | ||
527 | const char *end = data + strlen(data); | ||
528 | while (data < end) { | ||
529 | oled_write_char(*data, invert); | ||
530 | data++; | ||
531 | } | ||
532 | } | ||
533 | |||
534 | void oled_write_ln(const char *data, bool invert) { | ||
535 | oled_write(data, invert); | ||
536 | oled_advance_page(true); | ||
537 | } | ||
538 | |||
539 | void oled_pan(bool left) { | ||
540 | uint16_t i = 0; | ||
541 | for (uint16_t y = 0; y < OLED_DISPLAY_HEIGHT / 8; y++) { | ||
542 | if (left) { | ||
543 | for (uint16_t x = 0; x < OLED_DISPLAY_WIDTH - 1; x++) { | ||
544 | i = y * OLED_DISPLAY_WIDTH + x; | ||
545 | oled_buffer[i] = oled_buffer[i + 1]; | ||
546 | } | ||
547 | } else { | ||
548 | for (uint16_t x = OLED_DISPLAY_WIDTH - 1; x > 0; x--) { | ||
549 | i = y * OLED_DISPLAY_WIDTH + x; | ||
550 | oled_buffer[i] = oled_buffer[i - 1]; | ||
551 | } | ||
552 | } | ||
553 | } | ||
554 | oled_dirty = OLED_ALL_BLOCKS_MASK; | ||
555 | } | ||
556 | |||
557 | oled_buffer_reader_t oled_read_raw(uint16_t start_index) { | ||
558 | if (start_index > OLED_MATRIX_SIZE) start_index = OLED_MATRIX_SIZE; | ||
559 | oled_buffer_reader_t ret_reader; | ||
560 | ret_reader.current_element = &oled_buffer[start_index]; | ||
561 | ret_reader.remaining_element_count = OLED_MATRIX_SIZE - start_index; | ||
562 | return ret_reader; | ||
563 | } | ||
564 | |||
565 | void oled_write_raw_byte(const char data, uint16_t index) { | ||
566 | if (index > OLED_MATRIX_SIZE) index = OLED_MATRIX_SIZE; | ||
567 | if (oled_buffer[index] == data) return; | ||
568 | oled_buffer[index] = data; | ||
569 | oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE)); | ||
570 | } | ||
571 | |||
572 | void oled_write_raw(const char *data, uint16_t size) { | ||
573 | uint16_t cursor_start_index = oled_cursor - &oled_buffer[0]; | ||
574 | if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index; | ||
575 | for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) { | ||
576 | uint8_t c = *data++; | ||
577 | if (oled_buffer[i] == c) continue; | ||
578 | oled_buffer[i] = c; | ||
579 | oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE)); | ||
580 | } | ||
581 | } | ||
582 | |||
583 | void oled_write_pixel(uint8_t x, uint8_t y, bool on) { | ||
584 | if (x >= oled_rotation_width) { | ||
585 | return; | ||
586 | } | ||
587 | uint16_t index = x + (y / 8) * oled_rotation_width; | ||
588 | if (index >= OLED_MATRIX_SIZE) { | ||
589 | return; | ||
590 | } | ||
591 | uint8_t data = oled_buffer[index]; | ||
592 | if (on) { | ||
593 | data |= (1 << (y % 8)); | ||
594 | } else { | ||
595 | data &= ~(1 << (y % 8)); | ||
596 | } | ||
597 | if (oled_buffer[index] != data) { | ||
598 | oled_buffer[index] = data; | ||
599 | oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE)); | ||
600 | } | ||
601 | } | ||
602 | |||
603 | #if defined(__AVR__) | ||
604 | void oled_write_P(const char *data, bool invert) { | ||
605 | uint8_t c = pgm_read_byte(data); | ||
606 | while (c != 0) { | ||
607 | oled_write_char(c, invert); | ||
608 | c = pgm_read_byte(++data); | ||
609 | } | ||
610 | } | ||
611 | |||
612 | void oled_write_ln_P(const char *data, bool invert) { | ||
613 | oled_write_P(data, invert); | ||
614 | oled_advance_page(true); | ||
615 | } | ||
616 | |||
617 | void oled_write_raw_P(const char *data, uint16_t size) { | ||
618 | uint16_t cursor_start_index = oled_cursor - &oled_buffer[0]; | ||
619 | if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index; | ||
620 | for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) { | ||
621 | uint8_t c = pgm_read_byte(data++); | ||
622 | if (oled_buffer[i] == c) continue; | ||
623 | oled_buffer[i] = c; | ||
624 | oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE)); | ||
625 | } | ||
626 | } | ||
627 | #endif // defined(__AVR__) | ||
628 | |||
629 | bool oled_on(void) { | ||
630 | if (!oled_initialized) { | ||
631 | return oled_active; | ||
632 | } | ||
633 | |||
634 | #if OLED_TIMEOUT > 0 | ||
635 | oled_timeout = timer_read32() + OLED_TIMEOUT; | ||
636 | #endif | ||
637 | |||
638 | static const uint8_t PROGMEM display_on[] = | ||
639 | #ifdef OLED_FADE_OUT | ||
640 | {I2C_CMD, FADE_BLINK, 0x00}; | ||
641 | #else | ||
642 | {I2C_CMD, DISPLAY_ON}; | ||
643 | #endif | ||
644 | |||
645 | if (!oled_active) { | ||
646 | if (I2C_TRANSMIT_P(display_on) != I2C_STATUS_SUCCESS) { | ||
647 | print("oled_on cmd failed\n"); | ||
648 | return oled_active; | ||
649 | } | ||
650 | oled_active = true; | ||
651 | } | ||
652 | return oled_active; | ||
653 | } | ||
654 | |||
655 | bool oled_off(void) { | ||
656 | if (!oled_initialized) { | ||
657 | return !oled_active; | ||
658 | } | ||
659 | |||
660 | static const uint8_t PROGMEM display_off[] = | ||
661 | #ifdef OLED_FADE_OUT | ||
662 | {I2C_CMD, FADE_BLINK, ENABLE_FADE | OLED_FADE_OUT_INTERVAL}; | ||
663 | #else | ||
664 | {I2C_CMD, DISPLAY_OFF}; | ||
665 | #endif | ||
666 | |||
667 | if (oled_active) { | ||
668 | if (I2C_TRANSMIT_P(display_off) != I2C_STATUS_SUCCESS) { | ||
669 | print("oled_off cmd failed\n"); | ||
670 | return oled_active; | ||
671 | } | ||
672 | oled_active = false; | ||
673 | } | ||
674 | return !oled_active; | ||
675 | } | ||
676 | |||
677 | bool is_oled_on(void) { return oled_active; } | ||
678 | |||
679 | uint8_t oled_set_brightness(uint8_t level) { | ||
680 | if (!oled_initialized) { | ||
681 | return oled_brightness; | ||
682 | } | ||
683 | |||
684 | uint8_t set_contrast[] = {I2C_CMD, CONTRAST, level}; | ||
685 | if (oled_brightness != level) { | ||
686 | if (I2C_TRANSMIT(set_contrast) != I2C_STATUS_SUCCESS) { | ||
687 | print("set_brightness cmd failed\n"); | ||
688 | return oled_brightness; | ||
689 | } | ||
690 | oled_brightness = level; | ||
691 | } | ||
692 | return oled_brightness; | ||
693 | } | ||
694 | |||
695 | uint8_t oled_get_brightness(void) { return oled_brightness; } | ||
696 | |||
697 | // Set the specific 8 lines rows of the screen to scroll. | ||
698 | // 0 is the default for start, and 7 for end, which is the entire | ||
699 | // height of the screen. For 128x32 screens, rows 4-7 are not used. | ||
700 | void oled_scroll_set_area(uint8_t start_line, uint8_t end_line) { | ||
701 | oled_scroll_start = start_line; | ||
702 | oled_scroll_end = end_line; | ||
703 | } | ||
704 | |||
705 | void oled_scroll_set_speed(uint8_t speed) { | ||
706 | // Sets the speed for scrolling... does not take effect | ||
707 | // until scrolling is either started or restarted | ||
708 | // the ssd1306 supports 8 speeds | ||
709 | // FrameRate2 speed = 7 | ||
710 | // FrameRate3 speed = 4 | ||
711 | // FrameRate4 speed = 5 | ||
712 | // FrameRate5 speed = 0 | ||
713 | // FrameRate25 speed = 6 | ||
714 | // FrameRate64 speed = 1 | ||
715 | // FrameRate128 speed = 2 | ||
716 | // FrameRate256 speed = 3 | ||
717 | // for ease of use these are remaped here to be in order | ||
718 | static const uint8_t scroll_remap[8] = {7, 4, 5, 0, 6, 1, 2, 3}; | ||
719 | oled_scroll_speed = scroll_remap[speed]; | ||
720 | } | ||
721 | |||
722 | bool oled_scroll_right(void) { | ||
723 | if (!oled_initialized) { | ||
724 | return oled_scrolling; | ||
725 | } | ||
726 | |||
727 | // Dont enable scrolling if we need to update the display | ||
728 | // This prevents scrolling of bad data from starting the scroll too early after init | ||
729 | if (!oled_dirty && !oled_scrolling) { | ||
730 | uint8_t display_scroll_right[] = {I2C_CMD, SCROLL_RIGHT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL}; | ||
731 | if (I2C_TRANSMIT(display_scroll_right) != I2C_STATUS_SUCCESS) { | ||
732 | print("oled_scroll_right cmd failed\n"); | ||
733 | return oled_scrolling; | ||
734 | } | ||
735 | oled_scrolling = true; | ||
736 | } | ||
737 | return oled_scrolling; | ||
738 | } | ||
739 | |||
740 | bool oled_scroll_left(void) { | ||
741 | if (!oled_initialized) { | ||
742 | return oled_scrolling; | ||
743 | } | ||
744 | |||
745 | // Dont enable scrolling if we need to update the display | ||
746 | // This prevents scrolling of bad data from starting the scroll too early after init | ||
747 | if (!oled_dirty && !oled_scrolling) { | ||
748 | uint8_t display_scroll_left[] = {I2C_CMD, SCROLL_LEFT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL}; | ||
749 | if (I2C_TRANSMIT(display_scroll_left) != I2C_STATUS_SUCCESS) { | ||
750 | print("oled_scroll_left cmd failed\n"); | ||
751 | return oled_scrolling; | ||
752 | } | ||
753 | oled_scrolling = true; | ||
754 | } | ||
755 | return oled_scrolling; | ||
756 | } | ||
757 | |||
758 | bool oled_scroll_off(void) { | ||
759 | if (!oled_initialized) { | ||
760 | return !oled_scrolling; | ||
761 | } | ||
762 | |||
763 | if (oled_scrolling) { | ||
764 | static const uint8_t PROGMEM display_scroll_off[] = {I2C_CMD, DEACTIVATE_SCROLL}; | ||
765 | if (I2C_TRANSMIT_P(display_scroll_off) != I2C_STATUS_SUCCESS) { | ||
766 | print("oled_scroll_off cmd failed\n"); | ||
767 | return oled_scrolling; | ||
768 | } | ||
769 | oled_scrolling = false; | ||
770 | oled_dirty = OLED_ALL_BLOCKS_MASK; | ||
771 | } | ||
772 | return !oled_scrolling; | ||
773 | } | ||
774 | |||
775 | bool is_oled_scrolling(void) { return oled_scrolling; } | ||
776 | |||
777 | bool oled_invert(bool invert) { | ||
778 | if (!oled_initialized) { | ||
779 | return oled_inverted; | ||
780 | } | ||
781 | |||
782 | if (invert && !oled_inverted) { | ||
783 | static const uint8_t PROGMEM display_inverted[] = {I2C_CMD, INVERT_DISPLAY}; | ||
784 | if (I2C_TRANSMIT_P(display_inverted) != I2C_STATUS_SUCCESS) { | ||
785 | print("oled_invert cmd failed\n"); | ||
786 | return oled_inverted; | ||
787 | } | ||
788 | oled_inverted = true; | ||
789 | } else if (!invert && oled_inverted) { | ||
790 | static const uint8_t PROGMEM display_normal[] = {I2C_CMD, NORMAL_DISPLAY}; | ||
791 | if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) { | ||
792 | print("oled_invert cmd failed\n"); | ||
793 | return oled_inverted; | ||
794 | } | ||
795 | oled_inverted = false; | ||
796 | } | ||
797 | |||
798 | return oled_inverted; | ||
799 | } | ||
800 | |||
801 | uint8_t oled_max_chars(void) { | ||
802 | if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) { | ||
803 | return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH; | ||
804 | } | ||
805 | return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH; | ||
806 | } | ||
807 | |||
808 | uint8_t oled_max_lines(void) { | ||
809 | if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) { | ||
810 | return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT; | ||
811 | } | ||
812 | return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT; | ||
813 | } | ||
814 | |||
815 | void oled_task(void) { | ||
816 | if (!oled_initialized) { | ||
817 | return; | ||
818 | } | ||
819 | |||
820 | #if OLED_UPDATE_INTERVAL > 0 | ||
821 | if (timer_elapsed(oled_update_timeout) >= OLED_UPDATE_INTERVAL) { | ||
822 | oled_update_timeout = timer_read(); | ||
823 | oled_set_cursor(0, 0); | ||
824 | oled_task_kb(); | ||
825 | } | ||
826 | #else | ||
827 | oled_set_cursor(0, 0); | ||
828 | oled_task_kb(); | ||
829 | #endif | ||
830 | |||
831 | #if OLED_SCROLL_TIMEOUT > 0 | ||
832 | if (oled_dirty && oled_scrolling) { | ||
833 | oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT; | ||
834 | oled_scroll_off(); | ||
835 | } | ||
836 | #endif | ||
837 | |||
838 | // Smart render system, no need to check for dirty | ||
839 | oled_render(); | ||
840 | |||
841 | // Display timeout check | ||
842 | #if OLED_TIMEOUT > 0 | ||
843 | if (oled_active && timer_expired32(timer_read32(), oled_timeout)) { | ||
844 | oled_off(); | ||
845 | } | ||
846 | #endif | ||
847 | |||
848 | #if OLED_SCROLL_TIMEOUT > 0 | ||
849 | if (!oled_scrolling && timer_expired32(timer_read32(), oled_scroll_timeout)) { | ||
850 | # ifdef OLED_SCROLL_TIMEOUT_RIGHT | ||
851 | oled_scroll_right(); | ||
852 | # else | ||
853 | oled_scroll_left(); | ||
854 | # endif | ||
855 | } | ||
856 | #endif | ||
857 | } | ||
858 | |||
859 | __attribute__((weak)) bool oled_task_kb(void) { return oled_task_user(); } | ||
860 | __attribute__((weak)) bool oled_task_user(void) { return true; } | ||