oled_driver.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. Copyright 2019 Ryan Caltabiano <https://github.com/XScorpion2>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "i2c_master.h"
  15. #include "oled_driver.h"
  16. #include OLED_FONT_H
  17. #include "timer.h"
  18. #include "print.h"
  19. #include <string.h>
  20. #if defined(__AVR__)
  21. #include <avr/io.h>
  22. #include <avr/pgmspace.h>
  23. #elif defined(ESP8266)
  24. #include <pgmspace.h>
  25. #else // defined(ESP8266)
  26. #define PROGMEM
  27. #define memcpy_P(des, src, len) memcpy(des, src, len)
  28. #endif // defined(__AVR__)
  29. // Used commands from spec sheet: https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf
  30. // Fundamental Commands
  31. #define CONTRAST 0x81
  32. #define DISPLAY_ALL_ON 0xA5
  33. #define DISPLAY_ALL_ON_RESUME 0xA4
  34. #define NORMAL_DISPLAY 0xA6
  35. #define DISPLAY_ON 0xAF
  36. #define DISPLAY_OFF 0xAE
  37. // Scrolling Commands
  38. #define ACTIVATE_SCROLL 0x2F
  39. #define DEACTIVATE_SCROLL 0x2E
  40. #define SCROLL_RIGHT 0x26
  41. #define SCROLL_LEFT 0x27
  42. #define SCROLL_RIGHT_UP 0x29
  43. #define SCROLL_LEFT_UP 0x2A
  44. // Addressing Setting Commands
  45. #define MEMORY_MODE 0x20
  46. #define COLUMN_ADDR 0x21
  47. #define PAGE_ADDR 0x22
  48. // Hardware Configuration Commands
  49. #define DISPLAY_START_LINE 0x40
  50. #define SEGMENT_REMAP 0xA0
  51. #define SEGMENT_REMAP_INV 0xA1
  52. #define MULTIPLEX_RATIO 0xA8
  53. #define COM_SCAN_INC 0xC0
  54. #define COM_SCAN_DEC 0xC8
  55. #define DISPLAY_OFFSET 0xD3
  56. #define COM_PINS 0xDA
  57. // Timing & Driving Commands
  58. #define DISPLAY_CLOCK 0xD5
  59. #define PRE_CHARGE_PERIOD 0xD9
  60. #define VCOM_DETECT 0xDB
  61. // Charge Pump Commands
  62. #define CHARGE_PUMP 0x8D
  63. // Misc defines
  64. #define OLED_TIMEOUT 60000
  65. #define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8)
  66. #define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT)
  67. // i2c defines
  68. #define I2C_CMD 0x00
  69. #define I2C_DATA 0x40
  70. #if defined(__AVR__)
  71. // already defined on ARM
  72. #define I2C_TIMEOUT 100
  73. #define I2C_TRANSMIT_P(data) i2c_transmit_P((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), I2C_TIMEOUT)
  74. #else // defined(__AVR__)
  75. #define I2C_TRANSMIT_P(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), I2C_TIMEOUT)
  76. #endif // defined(__AVR__)
  77. #define I2C_TRANSMIT(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), I2C_TIMEOUT)
  78. #define I2C_WRITE_REG(mode, data, size) i2c_writeReg((OLED_DISPLAY_ADDRESS << 1), mode, data, size, I2C_TIMEOUT)
  79. #define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
  80. // Display buffer's is the same as the OLED memory layout
  81. // this is so we don't end up with rounding errors with
  82. // parts of the display unusable or don't get cleared correctly
  83. // and also allows for drawing & inverting
  84. uint8_t oled_buffer[OLED_MATRIX_SIZE];
  85. uint8_t* oled_cursor;
  86. OLED_BLOCK_TYPE oled_dirty = 0;
  87. bool oled_initialized = false;
  88. bool oled_active = false;
  89. bool oled_scrolling = false;
  90. uint8_t oled_rotation = 0;
  91. uint8_t oled_rotation_width = 0;
  92. #if !defined(OLED_DISABLE_TIMEOUT)
  93. uint16_t oled_last_activity;
  94. #endif
  95. // Internal variables to reduce math instructions
  96. #if defined(__AVR__)
  97. // identical to i2c_transmit, but for PROGMEM since all initialization is in PROGMEM arrays currently
  98. // probably should move this into i2c_master...
  99. static i2c_status_t i2c_transmit_P(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
  100. i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
  101. for (uint16_t i = 0; i < length && status >= 0; i++) {
  102. status = i2c_write(pgm_read_byte((const char*)data++), timeout);
  103. if (status) break;
  104. }
  105. i2c_stop();
  106. return status;
  107. }
  108. #endif
  109. // Flips the rendering bits for a character at the current cursor position
  110. static void InvertCharacter(uint8_t *cursor)
  111. {
  112. const uint8_t *end = cursor + OLED_FONT_WIDTH;
  113. while (cursor < end) {
  114. *cursor = ~(*cursor);
  115. cursor++;
  116. }
  117. }
  118. bool oled_init(uint8_t rotation) {
  119. oled_rotation = oled_init_user(rotation);
  120. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  121. oled_rotation_width = OLED_DISPLAY_WIDTH;
  122. } else {
  123. oled_rotation_width = OLED_DISPLAY_HEIGHT;
  124. }
  125. i2c_init();
  126. static const uint8_t PROGMEM display_setup1[] = {
  127. I2C_CMD,
  128. DISPLAY_OFF,
  129. DISPLAY_CLOCK, 0x80,
  130. MULTIPLEX_RATIO, OLED_DISPLAY_HEIGHT - 1,
  131. DISPLAY_OFFSET, 0x00,
  132. DISPLAY_START_LINE | 0x00,
  133. CHARGE_PUMP, 0x14,
  134. MEMORY_MODE, 0x00, }; // Horizontal addressing mode
  135. if (I2C_TRANSMIT_P(display_setup1) != I2C_STATUS_SUCCESS) {
  136. print("oled_init cmd set 1 failed\n");
  137. return false;
  138. }
  139. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) {
  140. static const uint8_t PROGMEM display_normal[] = {
  141. I2C_CMD,
  142. SEGMENT_REMAP_INV,
  143. COM_SCAN_DEC };
  144. if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) {
  145. print("oled_init cmd normal rotation failed\n");
  146. return false;
  147. }
  148. } else {
  149. static const uint8_t PROGMEM display_flipped[] = {
  150. I2C_CMD,
  151. SEGMENT_REMAP,
  152. COM_SCAN_INC };
  153. if (I2C_TRANSMIT_P(display_flipped) != I2C_STATUS_SUCCESS) {
  154. print("display_flipped failed\n");
  155. return false;
  156. }
  157. }
  158. static const uint8_t PROGMEM display_setup2[] = {
  159. I2C_CMD,
  160. COM_PINS, 0x02,
  161. CONTRAST, 0x8F,
  162. PRE_CHARGE_PERIOD, 0xF1,
  163. VCOM_DETECT, 0x40,
  164. DISPLAY_ALL_ON_RESUME,
  165. NORMAL_DISPLAY,
  166. DEACTIVATE_SCROLL,
  167. DISPLAY_ON };
  168. if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) {
  169. print("display_setup2 failed\n");
  170. return false;
  171. }
  172. oled_clear();
  173. oled_initialized = true;
  174. oled_active = true;
  175. oled_scrolling = false;
  176. return true;
  177. }
  178. __attribute__((weak))
  179. oled_rotation_t oled_init_user(oled_rotation_t rotation) {
  180. return rotation;
  181. }
  182. void oled_clear(void) {
  183. memset(oled_buffer, 0, sizeof(oled_buffer));
  184. oled_cursor = &oled_buffer[0];
  185. oled_dirty = -1; // -1 will be max value as long as display_dirty is unsigned type
  186. }
  187. static void calc_bounds(uint8_t update_start, uint8_t* cmd_array)
  188. {
  189. cmd_array[1] = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_WIDTH;
  190. cmd_array[4] = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_WIDTH;
  191. cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) % OLED_DISPLAY_WIDTH + cmd_array[1];
  192. cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) / OLED_DISPLAY_WIDTH - 1;
  193. }
  194. static void calc_bounds_90(uint8_t update_start, uint8_t* cmd_array)
  195. {
  196. cmd_array[1] = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8;
  197. cmd_array[4] = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT;
  198. cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8 - 1 + cmd_array[1];;
  199. cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) % OLED_DISPLAY_HEIGHT / 8;
  200. }
  201. uint8_t crot(uint8_t a, int8_t n)
  202. {
  203. const uint8_t mask = 0x7;
  204. n &= mask;
  205. return a << n | a >> (-n & mask);
  206. }
  207. static void rotate_90(const uint8_t* src, uint8_t* dest)
  208. {
  209. for (uint8_t i = 0, shift = 7; i < 8; ++i, --shift) {
  210. uint8_t selector = (1 << i);
  211. for (uint8_t j = 0; j < 8; ++j) {
  212. dest[i] |= crot(src[j] & selector, shift - (int8_t)j);
  213. }
  214. }
  215. }
  216. void oled_render(void) {
  217. // Do we have work to do?
  218. if (!oled_dirty || oled_scrolling) {
  219. return;
  220. }
  221. // Find first dirty block
  222. uint8_t update_start = 0;
  223. while (!(oled_dirty & (1 << update_start))) { ++update_start; }
  224. // Set column & page position
  225. static uint8_t display_start[] = {
  226. I2C_CMD,
  227. COLUMN_ADDR, 0, OLED_DISPLAY_WIDTH - 1,
  228. PAGE_ADDR, 0, OLED_DISPLAY_HEIGHT / 8 - 1 };
  229. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  230. calc_bounds(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  231. } else {
  232. calc_bounds_90(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  233. }
  234. // Send column & page position
  235. if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) {
  236. print("oled_render offset command failed\n");
  237. return;
  238. }
  239. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  240. // Send render data chunk as is
  241. if (I2C_WRITE_REG(I2C_DATA, &oled_buffer[OLED_BLOCK_SIZE * update_start], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
  242. print("oled_render data failed\n");
  243. return;
  244. }
  245. } else {
  246. // Rotate the render chunks
  247. const static uint8_t source_map[] = OLED_SOURCE_MAP;
  248. const static uint8_t target_map[] = OLED_TARGET_MAP;
  249. static uint8_t temp_buffer[OLED_BLOCK_SIZE];
  250. memset(temp_buffer, 0, sizeof(temp_buffer));
  251. for(uint8_t i = 0; i < sizeof(source_map); ++i) {
  252. rotate_90(&oled_buffer[OLED_BLOCK_SIZE * update_start + source_map[i]], &temp_buffer[target_map[i]]);
  253. }
  254. // Send render data chunk after rotating
  255. if (I2C_WRITE_REG(I2C_DATA, &temp_buffer[0], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
  256. print("oled_render data failed\n");
  257. return;
  258. }
  259. }
  260. // Turn on display if it is off
  261. oled_on();
  262. // Clear dirty flag
  263. oled_dirty &= ~(1 << update_start);
  264. }
  265. void oled_set_cursor(uint8_t col, uint8_t line) {
  266. uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH;
  267. // Out of bounds?
  268. if (index >= OLED_MATRIX_SIZE) {
  269. index = 0;
  270. }
  271. oled_cursor = &oled_buffer[index];
  272. }
  273. void oled_advance_page(bool clearPageRemainder) {
  274. uint16_t index = oled_cursor - &oled_buffer[0];
  275. uint8_t remaining = oled_rotation_width - (index % oled_rotation_width);
  276. if (clearPageRemainder) {
  277. // Remaining Char count
  278. remaining = remaining / OLED_FONT_WIDTH;
  279. // Write empty character until next line
  280. while (remaining--)
  281. oled_write_char(' ', false);
  282. } else {
  283. // Next page index out of bounds?
  284. if (index + remaining >= OLED_MATRIX_SIZE) {
  285. index = 0;
  286. remaining = 0;
  287. }
  288. oled_cursor = &oled_buffer[index + remaining];
  289. }
  290. }
  291. void oled_advance_char(void) {
  292. uint16_t nextIndex = oled_cursor - &oled_buffer[0] + OLED_FONT_WIDTH;
  293. uint8_t remainingSpace = oled_rotation_width - (nextIndex % oled_rotation_width);
  294. // Do we have enough space on the current line for the next character
  295. if (remainingSpace < OLED_FONT_WIDTH) {
  296. nextIndex += remainingSpace;
  297. }
  298. // Did we go out of bounds
  299. if (nextIndex >= OLED_MATRIX_SIZE) {
  300. nextIndex = 0;
  301. }
  302. // Update cursor position
  303. oled_cursor = &oled_buffer[nextIndex];
  304. }
  305. // Main handler that writes character data to the display buffer
  306. void oled_write_char(const char data, bool invert) {
  307. // Advance to the next line if newline
  308. if (data == '\n') {
  309. // Old source wrote ' ' until end of line...
  310. oled_advance_page(true);
  311. return;
  312. }
  313. // copy the current render buffer to check for dirty after
  314. static uint8_t oled_temp_buffer[OLED_FONT_WIDTH];
  315. memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH);
  316. // set the reder buffer data
  317. uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
  318. if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) {
  319. memset(oled_cursor, 0x00, OLED_FONT_WIDTH);
  320. } else {
  321. const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH];
  322. memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH);
  323. }
  324. // Invert if needed
  325. if (invert) {
  326. InvertCharacter(oled_cursor);
  327. }
  328. // Dirty check
  329. if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) {
  330. uint16_t index = oled_cursor - &oled_buffer[0];
  331. oled_dirty |= (1 << (index / OLED_BLOCK_SIZE));
  332. // Edgecase check if the written data spans the 2 chunks
  333. oled_dirty |= (1 << ((index + OLED_FONT_WIDTH) / OLED_BLOCK_SIZE));
  334. }
  335. // Finally move to the next char
  336. oled_advance_char();
  337. }
  338. void oled_write(const char *data, bool invert) {
  339. const char *end = data + strlen(data);
  340. while (data < end) {
  341. oled_write_char(*data, invert);
  342. data++;
  343. }
  344. }
  345. void oled_write_ln(const char *data, bool invert) {
  346. oled_write(data, invert);
  347. oled_advance_page(true);
  348. }
  349. #if defined(__AVR__)
  350. void oled_write_P(const char *data, bool invert) {
  351. uint8_t c = pgm_read_byte(data);
  352. while (c != 0) {
  353. oled_write_char(c, invert);
  354. c = pgm_read_byte(++data);
  355. }
  356. }
  357. void oled_write_ln_P(const char *data, bool invert) {
  358. oled_write_P(data, invert);
  359. oled_advance_page(true);
  360. }
  361. #endif // defined(__AVR__)
  362. bool oled_on(void) {
  363. #if !defined(OLED_DISABLE_TIMEOUT)
  364. oled_last_activity = timer_read();
  365. #endif
  366. static const uint8_t PROGMEM display_on[] = { I2C_CMD, DISPLAY_ON };
  367. if (!oled_active) {
  368. if (I2C_TRANSMIT_P(display_on) != I2C_STATUS_SUCCESS) {
  369. print("oled_on cmd failed\n");
  370. return oled_active;
  371. }
  372. oled_active = true;
  373. }
  374. return oled_active;
  375. }
  376. bool oled_off(void) {
  377. static const uint8_t PROGMEM display_off[] = { I2C_CMD, DISPLAY_OFF };
  378. if (oled_active) {
  379. if (I2C_TRANSMIT_P(display_off) != I2C_STATUS_SUCCESS) {
  380. print("oled_off cmd failed\n");
  381. return oled_active;
  382. }
  383. oled_active = false;
  384. }
  385. return !oled_active;
  386. }
  387. bool oled_scroll_right(void) {
  388. // Dont enable scrolling if we need to update the display
  389. // This prevents scrolling of bad data from starting the scroll too early after init
  390. if (!oled_dirty && !oled_scrolling) {
  391. static const uint8_t PROGMEM display_scroll_right[] = {
  392. I2C_CMD, SCROLL_RIGHT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL };
  393. if (I2C_TRANSMIT_P(display_scroll_right) != I2C_STATUS_SUCCESS) {
  394. print("oled_scroll_right cmd failed\n");
  395. return oled_scrolling;
  396. }
  397. oled_scrolling = true;
  398. }
  399. return oled_scrolling;
  400. }
  401. bool oled_scroll_left(void) {
  402. // Dont enable scrolling if we need to update the display
  403. // This prevents scrolling of bad data from starting the scroll too early after init
  404. if (!oled_dirty && !oled_scrolling) {
  405. static const uint8_t PROGMEM display_scroll_left[] = {
  406. I2C_CMD, SCROLL_LEFT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL };
  407. if (I2C_TRANSMIT_P(display_scroll_left) != I2C_STATUS_SUCCESS) {
  408. print("oled_scroll_left cmd failed\n");
  409. return oled_scrolling;
  410. }
  411. oled_scrolling = true;
  412. }
  413. return oled_scrolling;
  414. }
  415. bool oled_scroll_off(void) {
  416. if (oled_scrolling) {
  417. static const uint8_t PROGMEM display_scroll_off[] = { I2C_CMD, DEACTIVATE_SCROLL };
  418. if (I2C_TRANSMIT_P(display_scroll_off) != I2C_STATUS_SUCCESS) {
  419. print("oled_scroll_off cmd failed\n");
  420. return oled_scrolling;
  421. }
  422. oled_scrolling = false;
  423. }
  424. return !oled_scrolling;
  425. }
  426. uint8_t oled_max_chars(void) {
  427. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  428. return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH;
  429. }
  430. return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH;
  431. }
  432. uint8_t oled_max_lines(void) {
  433. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  434. return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT;
  435. }
  436. return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT;
  437. }
  438. void oled_task(void) {
  439. if (!oled_initialized) {
  440. return;
  441. }
  442. oled_set_cursor(0, 0);
  443. oled_task_user();
  444. // Smart render system, no need to check for dirty
  445. oled_render();
  446. // Display timeout check
  447. #if !defined(OLED_DISABLE_TIMEOUT)
  448. if (oled_active && timer_elapsed(oled_last_activity) > OLED_TIMEOUT) {
  449. oled_off();
  450. }
  451. #endif
  452. }
  453. __attribute__((weak))
  454. void oled_task_user(void) {
  455. }