diff options
| author | Ryan Caltabiano <rcalt2vt@gmail.com> | 2019-04-15 22:32:57 -0500 |
|---|---|---|
| committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-04-20 08:05:10 -0700 |
| commit | 0a645225b9c863a106921185a6c2e0c340f10694 (patch) | |
| tree | 2bf8c295650e54fb4548a7ac4d348ccfc8caa307 /drivers/oled/oled_driver.c | |
| parent | b5cb5ec6ddb15cfe336b835055f546f72d440a66 (diff) | |
| download | qmk_firmware-0a645225b9c863a106921185a6c2e0c340f10694.tar.gz qmk_firmware-0a645225b9c863a106921185a6c2e0c340f10694.zip | |
OLED Driver Feature
Diffstat (limited to 'drivers/oled/oled_driver.c')
| -rw-r--r-- | drivers/oled/oled_driver.c | 528 |
1 files changed, 528 insertions, 0 deletions
diff --git a/drivers/oled/oled_driver.c b/drivers/oled/oled_driver.c new file mode 100644 index 000000000..aa025d7a4 --- /dev/null +++ b/drivers/oled/oled_driver.c | |||
| @@ -0,0 +1,528 @@ | |||
| 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 | #if defined(__AVR__) | ||
| 26 | #include <avr/io.h> | ||
| 27 | #include <avr/pgmspace.h> | ||
| 28 | #elif defined(ESP8266) | ||
| 29 | #include <pgmspace.h> | ||
| 30 | #else // defined(ESP8266) | ||
| 31 | #define PROGMEM | ||
| 32 | #define memcpy_P(des, src, len) memcpy(des, src, len) | ||
| 33 | #endif // defined(__AVR__) | ||
| 34 | |||
| 35 | // Used commands from spec sheet: https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf | ||
| 36 | // Fundamental Commands | ||
| 37 | #define CONTRAST 0x81 | ||
| 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 | ||
| 43 | |||
| 44 | // Scrolling Commands | ||
| 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 | ||
| 51 | |||
| 52 | // Addressing Setting Commands | ||
| 53 | #define MEMORY_MODE 0x20 | ||
| 54 | #define COLUMN_ADDR 0x21 | ||
| 55 | #define PAGE_ADDR 0x22 | ||
| 56 | |||
| 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 | ||
| 65 | #define COM_PINS 0xDA | ||
| 66 | |||
| 67 | // Timing & Driving Commands | ||
| 68 | #define DISPLAY_CLOCK 0xD5 | ||
| 69 | #define PRE_CHARGE_PERIOD 0xD9 | ||
| 70 | #define VCOM_DETECT 0xDB | ||
| 71 | |||
| 72 | // Charge Pump Commands | ||
| 73 | #define CHARGE_PUMP 0x8D | ||
| 74 | |||
| 75 | // Misc defines | ||
| 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) | ||
| 79 | |||
| 80 | // i2c defines | ||
| 81 | #define I2C_CMD 0x00 | ||
| 82 | #define I2C_DATA 0x40 | ||
| 83 | #if defined(__AVR__) | ||
| 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) | ||
| 92 | |||
| 93 | #define HAS_FLAGS(bits, flags) ((bits & flags) == flags) | ||
| 94 | |||
| 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; | ||
| 109 | #endif | ||
| 110 | |||
| 111 | // Internal variables to reduce math instructions | ||
| 112 | |||
| 113 | #if defined(__AVR__) | ||
| 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); | ||
| 118 | |||
| 119 | for (uint16_t i = 0; i < length && status >= 0; i++) { | ||
| 120 | status = i2c_write(pgm_read_byte((const char*)data++), timeout); | ||
| 121 | if (status) break; | ||
| 122 | } | ||
| 123 | |||
| 124 | i2c_stop(); | ||
| 125 | |||
| 126 | return status; | ||
| 127 | } | ||
| 128 | #endif | ||
| 129 | |||
| 130 | // Flips the rendering bits for a character at the current cursor position | ||
| 131 | static void InvertCharacter(uint8_t *cursor) | ||
| 132 | { | ||
| 133 | const uint8_t *end = cursor + OLED_FONT_WIDTH; | ||
| 134 | while (cursor < end) { | ||
| 135 | *cursor = ~(*cursor); | ||
| 136 | cursor++; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 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; | ||
| 144 | } else { | ||
| 145 | oled_rotation_width = OLED_DISPLAY_HEIGHT; | ||
| 146 | } | ||
| 147 | i2c_init(); | ||
| 148 | |||
| 149 | static const uint8_t PROGMEM display_setup1[] = { | ||
| 150 | I2C_CMD, | ||
| 151 | DISPLAY_OFF, | ||
| 152 | DISPLAY_CLOCK, 0x80, | ||
| 153 | MULTIPLEX_RATIO, OLED_DISPLAY_HEIGHT - 1, | ||
| 154 | DISPLAY_OFFSET, 0x00, | ||
| 155 | DISPLAY_START_LINE | 0x00, | ||
| 156 | CHARGE_PUMP, 0x14, | ||
| 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"); | ||
| 160 | return false; | ||
| 161 | } | ||
| 162 | |||
| 163 | if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) { | ||
| 164 | static const uint8_t PROGMEM display_normal[] = { | ||
| 165 | I2C_CMD, | ||
| 166 | SEGMENT_REMAP_INV, | ||
| 167 | COM_SCAN_DEC }; | ||
| 168 | if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) { | ||
| 169 | print("oled_init cmd normal rotation failed\n"); | ||
| 170 | return false; | ||
| 171 | } | ||
| 172 | } else { | ||
| 173 | static const uint8_t PROGMEM display_flipped[] = { | ||
| 174 | I2C_CMD, | ||
| 175 | SEGMENT_REMAP, | ||
| 176 | COM_SCAN_INC }; | ||
| 177 | if (I2C_TRANSMIT_P(display_flipped) != I2C_STATUS_SUCCESS) { | ||
| 178 | print("display_flipped failed\n"); | ||
| 179 | return false; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | static const uint8_t PROGMEM display_setup2[] = { | ||
| 184 | I2C_CMD, | ||
| 185 | COM_PINS, 0x02, | ||
| 186 | CONTRAST, 0x8F, | ||
| 187 | PRE_CHARGE_PERIOD, 0xF1, | ||
| 188 | VCOM_DETECT, 0x40, | ||
| 189 | DISPLAY_ALL_ON_RESUME, | ||
| 190 | NORMAL_DISPLAY, | ||
| 191 | DEACTIVATE_SCROLL, | ||
| 192 | DISPLAY_ON }; | ||
| 193 | if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) { | ||
| 194 | print("display_setup2 failed\n"); | ||
| 195 | return false; | ||
| 196 | } | ||
| 197 | |||
| 198 | oled_clear(); | ||
| 199 | oled_initialized = true; | ||
| 200 | oled_active = true; | ||
| 201 | oled_scrolling = false; | ||
| 202 | return true; | ||
| 203 | } | ||
| 204 | |||
| 205 | __attribute__((weak)) | ||
| 206 | uint8_t oled_init_user(uint8_t rotation) { | ||
| 207 | return rotation; | ||
| 208 | } | ||
| 209 | |||
| 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 | ||
| 214 | } | ||
| 215 | |||
| 216 | static void calc_bounds(uint8_t update_start, uint8_t* cmd_array) | ||
| 217 | { | ||
| 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; | ||
| 222 | } | ||
| 223 | |||
| 224 | static void calc_bounds_90(uint8_t update_start, uint8_t* cmd_array) | ||
| 225 | { | ||
| 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; | ||
| 230 | } | ||
| 231 | |||
| 232 | uint8_t crot(uint8_t a, int8_t n) | ||
| 233 | { | ||
| 234 | const uint8_t mask = 0x7; | ||
| 235 | n &= mask; | ||
| 236 | return a << n | a >> (-n & mask); | ||
| 237 | } | ||
| 238 | |||
| 239 | static void rotate_90(const uint8_t* src, uint8_t* dest) | ||
| 240 | { | ||
| 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); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | void oled_render(void) { | ||
| 250 | // Do we have work to do? | ||
| 251 | if (!oled_dirty || oled_scrolling) { | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | |||
| 255 | // Find first dirty block | ||
| 256 | uint8_t update_start = 0; | ||
| 257 | while (!(oled_dirty & (1 << update_start))) { ++update_start; } | ||
| 258 | |||
| 259 | // Set column & page position | ||
| 260 | static uint8_t display_start[] = { | ||
| 261 | I2C_CMD, | ||
| 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 | ||
| 266 | } else { | ||
| 267 | calc_bounds_90(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start | ||
| 268 | } | ||
| 269 | |||
| 270 | // Send column & page position | ||
| 271 | if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) { | ||
| 272 | print("oled_render offset command failed\n"); | ||
| 273 | return; | ||
| 274 | } | ||
| 275 | |||
| 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"); | ||
| 280 | return; | ||
| 281 | } | ||
| 282 | } else { | ||
| 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; | ||
| 286 | |||
| 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]]); | ||
| 291 | } | ||
| 292 | |||
| 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"); | ||
| 296 | return; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | // Turn on display if it is off | ||
| 301 | oled_on(); | ||
| 302 | |||
| 303 | // Clear dirty flag | ||
| 304 | oled_dirty &= ~(1 << update_start); | ||
| 305 | } | ||
| 306 | |||
| 307 | void oled_set_cursor(uint8_t col, uint8_t line) { | ||
| 308 | uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH; | ||
| 309 | |||
| 310 | // Out of bounds? | ||
| 311 | if (index >= OLED_MATRIX_SIZE) { | ||
| 312 | index = 0; | ||
| 313 | } | ||
| 314 | |||
| 315 | oled_cursor = &oled_buffer[index]; | ||
| 316 | } | ||
| 317 | |||
| 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); | ||
| 321 | |||
| 322 | if (clearPageRemainder) { | ||
| 323 | // Remaining Char count | ||
| 324 | remaining = remaining / OLED_FONT_WIDTH; | ||
| 325 | |||
| 326 | // Write empty character until next line | ||
| 327 | while (remaining--) | ||
| 328 | oled_write_char(' ', false); | ||
| 329 | } else { | ||
| 330 | // Next page index out of bounds? | ||
| 331 | if (index + remaining >= OLED_MATRIX_SIZE) { | ||
| 332 | index = 0; | ||
| 333 | remaining = 0; | ||
| 334 | } | ||
| 335 | |||
| 336 | oled_cursor = &oled_buffer[index + remaining]; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 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); | ||
| 343 | |||
| 344 | // Do we have enough space on the current line for the next character | ||
| 345 | if (remainingSpace < OLED_FONT_WIDTH) { | ||
| 346 | nextIndex += remainingSpace; | ||
| 347 | } | ||
| 348 | |||
| 349 | // Did we go out of bounds | ||
| 350 | if (nextIndex >= OLED_MATRIX_SIZE) { | ||
| 351 | nextIndex = 0; | ||
| 352 | } | ||
| 353 | |||
| 354 | // Update cursor position | ||
| 355 | oled_cursor = &oled_buffer[nextIndex]; | ||
| 356 | } | ||
| 357 | |||
| 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 | ||
| 361 | if (data == '\n') { | ||
| 362 | // Old source wrote ' ' until end of line... | ||
| 363 | oled_advance_page(true); | ||
| 364 | return; | ||
| 365 | } | ||
| 366 | |||
| 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); | ||
| 370 | |||
| 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); | ||
| 375 | } else { | ||
| 376 | const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH]; | ||
| 377 | memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH); | ||
| 378 | } | ||
| 379 | |||
| 380 | // Invert if needed | ||
| 381 | if (invert) { | ||
| 382 | InvertCharacter(oled_cursor); | ||
| 383 | } | ||
| 384 | |||
| 385 | // Dirty check | ||
| 386 | if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) { | ||
| 387 | oled_dirty |= (1 << ((oled_cursor - &oled_buffer[0]) / OLED_BLOCK_SIZE)); | ||
| 388 | } | ||
| 389 | |||
| 390 | // Finally move to the next char | ||
| 391 | oled_advance_char(); | ||
| 392 | } | ||
| 393 | |||
| 394 | void oled_write(const char *data, bool invert) { | ||
| 395 | const char *end = data + strlen(data); | ||
| 396 | while (data < end) { | ||
| 397 | oled_write_char(*data, invert); | ||
| 398 | data++; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | void oled_write_ln(const char *data, bool invert) { | ||
| 403 | oled_write(data, invert); | ||
| 404 | oled_advance_page(true); | ||
| 405 | } | ||
| 406 | |||
| 407 | #if defined(__AVR__) | ||
| 408 | void oled_write_P(const char *data, bool invert) { | ||
| 409 | uint8_t c = pgm_read_byte(data); | ||
| 410 | while (c != 0) { | ||
| 411 | oled_write_char(c, invert); | ||
| 412 | c = pgm_read_byte(++data); | ||
| 413 | } | ||
| 414 | } | ||
| 415 | |||
| 416 | void oled_write_ln_P(const char *data, bool invert) { | ||
| 417 | oled_write_P(data, invert); | ||
| 418 | oled_advance_page(true); | ||
| 419 | } | ||
| 420 | #endif // defined(__AVR__) | ||
| 421 | |||
| 422 | bool oled_on(void) { | ||
| 423 | #if !defined(OLED_DISABLE_TIMEOUT) | ||
| 424 | oled_last_activity = timer_read(); | ||
| 425 | #endif | ||
| 426 | |||
| 427 | static const uint8_t PROGMEM display_on[] = { I2C_CMD, DISPLAY_ON }; | ||
| 428 | if (!oled_active) { | ||
| 429 | if (I2C_TRANSMIT_P(display_on) != I2C_STATUS_SUCCESS) { | ||
| 430 | print("oled_on cmd failed\n"); | ||
| 431 | return oled_active; | ||
| 432 | } | ||
| 433 | oled_active = true; | ||
| 434 | } | ||
| 435 | return oled_active; | ||
| 436 | } | ||
| 437 | |||
| 438 | bool oled_off(void) { | ||
| 439 | static const uint8_t PROGMEM display_off[] = { I2C_CMD, DISPLAY_OFF }; | ||
| 440 | if (oled_active) { | ||
| 441 | if (I2C_TRANSMIT_P(display_off) != I2C_STATUS_SUCCESS) { | ||
| 442 | print("oled_off cmd failed\n"); | ||
| 443 | return oled_active; | ||
| 444 | } | ||
| 445 | oled_active = false; | ||
| 446 | } | ||
| 447 | return !oled_active; | ||
| 448 | } | ||
| 449 | |||
| 450 | bool oled_scroll_right(void) { | ||
| 451 | // Dont enable scrolling if we need to update the display | ||
| 452 | // This prevents scrolling of bad data from starting the scroll too early after init | ||
| 453 | if (!oled_dirty && !oled_scrolling) { | ||
| 454 | static const uint8_t PROGMEM display_scroll_right[] = { | ||
| 455 | I2C_CMD, SCROLL_RIGHT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL }; | ||
| 456 | if (I2C_TRANSMIT_P(display_scroll_right) != I2C_STATUS_SUCCESS) { | ||
| 457 | print("oled_scroll_right cmd failed\n"); | ||
| 458 | return oled_scrolling; | ||
| 459 | } | ||
| 460 | oled_scrolling = true; | ||
| 461 | } | ||
| 462 | return oled_scrolling; | ||
| 463 | } | ||
| 464 | |||
| 465 | bool oled_scroll_left(void) { | ||
| 466 | // Dont enable scrolling if we need to update the display | ||
| 467 | // This prevents scrolling of bad data from starting the scroll too early after init | ||
| 468 | if (!oled_dirty && !oled_scrolling) { | ||
| 469 | static const uint8_t PROGMEM display_scroll_left[] = { | ||
| 470 | I2C_CMD, SCROLL_LEFT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL }; | ||
| 471 | if (I2C_TRANSMIT_P(display_scroll_left) != I2C_STATUS_SUCCESS) { | ||
| 472 | print("oled_scroll_left cmd failed\n"); | ||
| 473 | return oled_scrolling; | ||
| 474 | } | ||
| 475 | oled_scrolling = true; | ||
| 476 | } | ||
| 477 | return oled_scrolling; | ||
| 478 | } | ||
| 479 | |||
| 480 | bool oled_scroll_off(void) { | ||
| 481 | if (oled_scrolling) { | ||
| 482 | static const uint8_t PROGMEM display_scroll_off[] = { I2C_CMD, DEACTIVATE_SCROLL }; | ||
| 483 | if (I2C_TRANSMIT_P(display_scroll_off) != I2C_STATUS_SUCCESS) { | ||
| 484 | print("oled_scroll_off cmd failed\n"); | ||
| 485 | return oled_scrolling; | ||
| 486 | } | ||
| 487 | oled_scrolling = false; | ||
| 488 | } | ||
| 489 | return !oled_scrolling; | ||
| 490 | } | ||
| 491 | |||
| 492 | uint8_t oled_max_chars(void) { | ||
| 493 | if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) { | ||
| 494 | return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH; | ||
| 495 | } | ||
| 496 | return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH; | ||
| 497 | } | ||
| 498 | |||
| 499 | uint8_t oled_max_lines(void) { | ||
| 500 | if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) { | ||
| 501 | return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT; | ||
| 502 | } | ||
| 503 | return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT; | ||
| 504 | } | ||
| 505 | |||
| 506 | void oled_task(void) { | ||
| 507 | if (!oled_initialized) { | ||
| 508 | return; | ||
| 509 | } | ||
| 510 | |||
| 511 | oled_set_cursor(0, 0); | ||
| 512 | |||
| 513 | oled_task_user(); | ||
| 514 | |||
| 515 | // Smart render system, no need to check for dirty | ||
| 516 | oled_render(); | ||
| 517 | |||
| 518 | // Display timeout check | ||
| 519 | #if !defined(OLED_DISABLE_TIMEOUT) | ||
| 520 | if (oled_active && timer_elapsed(oled_last_activity) > OLED_TIMEOUT) { | ||
| 521 | oled_off(); | ||
| 522 | } | ||
| 523 | #endif | ||
| 524 | } | ||
| 525 | |||
| 526 | __attribute__((weak)) | ||
| 527 | void oled_task_user(void) { | ||
| 528 | } | ||
