2 Copyright 2019 Ryan Caltabiano <https://github.com/XScorpion2>
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.
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.
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/>.
17 #include "i2c_master.h"
18 #include "oled_driver.h"
27 #include <avr/pgmspace.h>
28 #elif defined(ESP8266)
30 #else // defined(ESP8266)
32 #define memcpy_P(des, src, len) memcpy(des, src, len)
33 #endif // defined(__AVR__)
35 // Used commands from spec sheet: https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf
36 // Fundamental Commands
38 #define DISPLAY_ALL_ON 0xA5
39 #define DISPLAY_ALL_ON_RESUME 0xA4
40 #define NORMAL_DISPLAY 0xA6
41 #define DISPLAY_ON 0xAF
42 #define DISPLAY_OFF 0xAE
45 #define ACTIVATE_SCROLL 0x2F
46 #define DEACTIVATE_SCROLL 0x2E
47 #define SCROLL_RIGHT 0x26
48 #define SCROLL_LEFT 0x27
49 #define SCROLL_RIGHT_UP 0x29
50 #define SCROLL_LEFT_UP 0x2A
52 // Addressing Setting Commands
53 #define MEMORY_MODE 0x20
54 #define COLUMN_ADDR 0x21
55 #define PAGE_ADDR 0x22
57 // Hardware Configuration Commands
58 #define DISPLAY_START_LINE 0x40
59 #define SEGMENT_REMAP 0xA0
60 #define SEGMENT_REMAP_INV 0xA1
61 #define MULTIPLEX_RATIO 0xA8
62 #define COM_SCAN_INC 0xC0
63 #define COM_SCAN_DEC 0xC8
64 #define DISPLAY_OFFSET 0xD3
67 // Timing & Driving Commands
68 #define DISPLAY_CLOCK 0xD5
69 #define PRE_CHARGE_PERIOD 0xD9
70 #define VCOM_DETECT 0xDB
72 // Charge Pump Commands
73 #define CHARGE_PUMP 0x8D
76 #define OLED_TIMEOUT 60000
77 #define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8)
78 #define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT)
84 // already defined on ARM
85 #define I2C_TIMEOUT 100
86 #define I2C_TRANSMIT_P(data) i2c_transmit_P((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), I2C_TIMEOUT)
87 #else // defined(__AVR__)
88 #define I2C_TRANSMIT_P(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), I2C_TIMEOUT)
89 #endif // defined(__AVR__)
90 #define I2C_TRANSMIT(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), I2C_TIMEOUT)
91 #define I2C_WRITE_REG(mode, data, size) i2c_writeReg((OLED_DISPLAY_ADDRESS << 1), mode, data, size, I2C_TIMEOUT)
93 #define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
95 // Display buffer's is the same as the OLED memory layout
96 // this is so we don't end up with rounding errors with
97 // parts of the display unusable or don't get cleared correctly
98 // and also allows for drawing & inverting
99 uint8_t oled_buffer[OLED_MATRIX_SIZE];
100 uint8_t* oled_cursor;
101 OLED_BLOCK_TYPE oled_dirty = 0;
102 bool oled_initialized = false;
103 bool oled_active = false;
104 bool oled_scrolling = false;
105 uint8_t oled_rotation = 0;
106 uint8_t oled_rotation_width = 0;
107 #if !defined(OLED_DISABLE_TIMEOUT)
108 uint16_t oled_last_activity;
111 // Internal variables to reduce math instructions
114 // identical to i2c_transmit, but for PROGMEM since all initialization is in PROGMEM arrays currently
115 // probably should move this into i2c_master...
116 static i2c_status_t i2c_transmit_P(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
117 i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
119 for (uint16_t i = 0; i < length && status >= 0; i++) {
120 status = i2c_write(pgm_read_byte((const char*)data++), timeout);
130 // Flips the rendering bits for a character at the current cursor position
131 static void InvertCharacter(uint8_t *cursor)
133 const uint8_t *end = cursor + OLED_FONT_WIDTH;
134 while (cursor < end) {
135 *cursor = ~(*cursor);
140 bool oled_init(uint8_t rotation) {
141 oled_rotation = oled_init_user(rotation);
142 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
143 oled_rotation_width = OLED_DISPLAY_WIDTH;
145 oled_rotation_width = OLED_DISPLAY_HEIGHT;
149 static const uint8_t PROGMEM display_setup1[] = {
153 MULTIPLEX_RATIO, OLED_DISPLAY_HEIGHT - 1,
154 DISPLAY_OFFSET, 0x00,
155 DISPLAY_START_LINE | 0x00,
157 MEMORY_MODE, 0x00, }; // Horizontal addressing mode
158 if (I2C_TRANSMIT_P(display_setup1) != I2C_STATUS_SUCCESS) {
159 print("oled_init cmd set 1 failed\n");
163 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) {
164 static const uint8_t PROGMEM display_normal[] = {
168 if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) {
169 print("oled_init cmd normal rotation failed\n");
173 static const uint8_t PROGMEM display_flipped[] = {
177 if (I2C_TRANSMIT_P(display_flipped) != I2C_STATUS_SUCCESS) {
178 print("display_flipped failed\n");
183 static const uint8_t PROGMEM display_setup2[] = {
187 PRE_CHARGE_PERIOD, 0xF1,
189 DISPLAY_ALL_ON_RESUME,
193 if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) {
194 print("display_setup2 failed\n");
199 oled_initialized = true;
201 oled_scrolling = false;
205 __attribute__((weak))
206 oled_rotation_t oled_init_user(oled_rotation_t rotation) {
210 void oled_clear(void) {
211 memset(oled_buffer, 0, sizeof(oled_buffer));
212 oled_cursor = &oled_buffer[0];
213 oled_dirty = -1; // -1 will be max value as long as display_dirty is unsigned type
216 static void calc_bounds(uint8_t update_start, uint8_t* cmd_array)
218 cmd_array[1] = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_WIDTH;
219 cmd_array[4] = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_WIDTH;
220 cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) % OLED_DISPLAY_WIDTH + cmd_array[1];
221 cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) / OLED_DISPLAY_WIDTH - 1;
224 static void calc_bounds_90(uint8_t update_start, uint8_t* cmd_array)
226 cmd_array[1] = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8;
227 cmd_array[4] = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT;
228 cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8 - 1 + cmd_array[1];;
229 cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) % OLED_DISPLAY_HEIGHT / 8;
232 uint8_t crot(uint8_t a, int8_t n)
234 const uint8_t mask = 0x7;
236 return a << n | a >> (-n & mask);
239 static void rotate_90(const uint8_t* src, uint8_t* dest)
241 for (uint8_t i = 0, shift = 7; i < 8; ++i, --shift) {
242 uint8_t selector = (1 << i);
243 for (uint8_t j = 0; j < 8; ++j) {
244 dest[i] |= crot(src[j] & selector, shift - (int8_t)j);
249 void oled_render(void) {
250 // Do we have work to do?
251 if (!oled_dirty || oled_scrolling) {
255 // Find first dirty block
256 uint8_t update_start = 0;
257 while (!(oled_dirty & (1 << update_start))) { ++update_start; }
259 // Set column & page position
260 static uint8_t display_start[] = {
262 COLUMN_ADDR, 0, OLED_DISPLAY_WIDTH - 1,
263 PAGE_ADDR, 0, OLED_DISPLAY_HEIGHT / 8 - 1 };
264 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
265 calc_bounds(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
267 calc_bounds_90(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
270 // Send column & page position
271 if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) {
272 print("oled_render offset command failed\n");
276 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
277 // Send render data chunk as is
278 if (I2C_WRITE_REG(I2C_DATA, &oled_buffer[OLED_BLOCK_SIZE * update_start], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
279 print("oled_render data failed\n");
283 // Rotate the render chunks
284 const static uint8_t source_map[] = OLED_SOURCE_MAP;
285 const static uint8_t target_map[] = OLED_TARGET_MAP;
287 static uint8_t temp_buffer[OLED_BLOCK_SIZE];
288 memset(temp_buffer, 0, sizeof(temp_buffer));
289 for(uint8_t i = 0; i < sizeof(source_map); ++i) {
290 rotate_90(&oled_buffer[OLED_BLOCK_SIZE * update_start + source_map[i]], &temp_buffer[target_map[i]]);
293 // Send render data chunk after rotating
294 if (I2C_WRITE_REG(I2C_DATA, &temp_buffer[0], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
295 print("oled_render data failed\n");
300 // Turn on display if it is off
304 oled_dirty &= ~(1 << update_start);
307 void oled_set_cursor(uint8_t col, uint8_t line) {
308 uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH;
311 if (index >= OLED_MATRIX_SIZE) {
315 oled_cursor = &oled_buffer[index];
318 void oled_advance_page(bool clearPageRemainder) {
319 uint16_t index = oled_cursor - &oled_buffer[0];
320 uint8_t remaining = oled_rotation_width - (index % oled_rotation_width);
322 if (clearPageRemainder) {
323 // Remaining Char count
324 remaining = remaining / OLED_FONT_WIDTH;
326 // Write empty character until next line
328 oled_write_char(' ', false);
330 // Next page index out of bounds?
331 if (index + remaining >= OLED_MATRIX_SIZE) {
336 oled_cursor = &oled_buffer[index + remaining];
340 void oled_advance_char(void) {
341 uint16_t nextIndex = oled_cursor - &oled_buffer[0] + OLED_FONT_WIDTH;
342 uint8_t remainingSpace = oled_rotation_width - (nextIndex % oled_rotation_width);
344 // Do we have enough space on the current line for the next character
345 if (remainingSpace < OLED_FONT_WIDTH) {
346 nextIndex += remainingSpace;
349 // Did we go out of bounds
350 if (nextIndex >= OLED_MATRIX_SIZE) {
354 // Update cursor position
355 oled_cursor = &oled_buffer[nextIndex];
358 // Main handler that writes character data to the display buffer
359 void oled_write_char(const char data, bool invert) {
360 // Advance to the next line if newline
362 // Old source wrote ' ' until end of line...
363 oled_advance_page(true);
367 // copy the current render buffer to check for dirty after
368 static uint8_t oled_temp_buffer[OLED_FONT_WIDTH];
369 memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH);
371 // set the reder buffer data
372 uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
373 if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) {
374 memset(oled_cursor, 0x00, OLED_FONT_WIDTH);
376 const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH];
377 memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH);
382 InvertCharacter(oled_cursor);
386 if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) {
387 uint16_t index = oled_cursor - &oled_buffer[0];
388 oled_dirty |= (1 << (index / OLED_BLOCK_SIZE));
389 // Edgecase check if the written data spans the 2 chunks
390 oled_dirty |= (1 << ((index + OLED_FONT_WIDTH) / OLED_BLOCK_SIZE));
393 // Finally move to the next char
397 void oled_write(const char *data, bool invert) {
398 const char *end = data + strlen(data);
400 oled_write_char(*data, invert);
405 void oled_write_ln(const char *data, bool invert) {
406 oled_write(data, invert);
407 oled_advance_page(true);
411 void oled_write_P(const char *data, bool invert) {
412 uint8_t c = pgm_read_byte(data);
414 oled_write_char(c, invert);
415 c = pgm_read_byte(++data);
419 void oled_write_ln_P(const char *data, bool invert) {
420 oled_write_P(data, invert);
421 oled_advance_page(true);
423 #endif // defined(__AVR__)
426 #if !defined(OLED_DISABLE_TIMEOUT)
427 oled_last_activity = timer_read();
430 static const uint8_t PROGMEM display_on[] = { I2C_CMD, DISPLAY_ON };
432 if (I2C_TRANSMIT_P(display_on) != I2C_STATUS_SUCCESS) {
433 print("oled_on cmd failed\n");
441 bool oled_off(void) {
442 static const uint8_t PROGMEM display_off[] = { I2C_CMD, DISPLAY_OFF };
444 if (I2C_TRANSMIT_P(display_off) != I2C_STATUS_SUCCESS) {
445 print("oled_off cmd failed\n");
453 bool oled_scroll_right(void) {
454 // Dont enable scrolling if we need to update the display
455 // This prevents scrolling of bad data from starting the scroll too early after init
456 if (!oled_dirty && !oled_scrolling) {
457 static const uint8_t PROGMEM display_scroll_right[] = {
458 I2C_CMD, SCROLL_RIGHT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL };
459 if (I2C_TRANSMIT_P(display_scroll_right) != I2C_STATUS_SUCCESS) {
460 print("oled_scroll_right cmd failed\n");
461 return oled_scrolling;
463 oled_scrolling = true;
465 return oled_scrolling;
468 bool oled_scroll_left(void) {
469 // Dont enable scrolling if we need to update the display
470 // This prevents scrolling of bad data from starting the scroll too early after init
471 if (!oled_dirty && !oled_scrolling) {
472 static const uint8_t PROGMEM display_scroll_left[] = {
473 I2C_CMD, SCROLL_LEFT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL };
474 if (I2C_TRANSMIT_P(display_scroll_left) != I2C_STATUS_SUCCESS) {
475 print("oled_scroll_left cmd failed\n");
476 return oled_scrolling;
478 oled_scrolling = true;
480 return oled_scrolling;
483 bool oled_scroll_off(void) {
484 if (oled_scrolling) {
485 static const uint8_t PROGMEM display_scroll_off[] = { I2C_CMD, DEACTIVATE_SCROLL };
486 if (I2C_TRANSMIT_P(display_scroll_off) != I2C_STATUS_SUCCESS) {
487 print("oled_scroll_off cmd failed\n");
488 return oled_scrolling;
490 oled_scrolling = false;
492 return !oled_scrolling;
495 uint8_t oled_max_chars(void) {
496 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
497 return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH;
499 return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH;
502 uint8_t oled_max_lines(void) {
503 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
504 return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT;
506 return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT;
509 void oled_task(void) {
510 if (!oled_initialized) {
514 oled_set_cursor(0, 0);
518 // Smart render system, no need to check for dirty
521 // Display timeout check
522 #if !defined(OLED_DISABLE_TIMEOUT)
523 if (oled_active && timer_elapsed(oled_last_activity) > OLED_TIMEOUT) {
529 __attribute__((weak))
530 void oled_task_user(void) {