ssd1306.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #ifdef SSD1306OLED
  2. # include "ssd1306.h"
  3. # include "i2c.h"
  4. # include <string.h>
  5. # include "print.h"
  6. # include "glcdfont.c"
  7. # ifdef PROTOCOL_LUFA
  8. # include "lufa.h"
  9. # endif
  10. # include "sendchar.h"
  11. # include "timer.h"
  12. // Set this to 1 to help diagnose early startup problems
  13. // when testing power-on with ble. Turn it off otherwise,
  14. // as the latency of printing most of the debug info messes
  15. // with the matrix scan, causing keys to drop.
  16. # define DEBUG_TO_SCREEN 0
  17. // static uint16_t last_battery_update;
  18. // static uint32_t vbat;
  19. //#define BatteryUpdateInterval 10000 /* milliseconds */
  20. # define ScreenOffInterval 300000 /* milliseconds */
  21. # if DEBUG_TO_SCREEN
  22. static uint8_t displaying;
  23. # endif
  24. static uint16_t last_flush;
  25. // Write command sequence.
  26. // Returns true on success.
  27. static inline bool _send_cmd1(uint8_t cmd) {
  28. bool res = false;
  29. if (i2c_start_write(SSD1306_ADDRESS)) {
  30. xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
  31. goto done;
  32. }
  33. if (i2c_master_write(0x0 /* command byte follows */)) {
  34. print("failed to write control byte\n");
  35. goto done;
  36. }
  37. if (i2c_master_write(cmd)) {
  38. xprintf("failed to write command %d\n", cmd);
  39. goto done;
  40. }
  41. res = true;
  42. done:
  43. i2c_master_stop();
  44. return res;
  45. }
  46. // Write 2-byte command sequence.
  47. // Returns true on success
  48. static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
  49. if (!_send_cmd1(cmd)) {
  50. return false;
  51. }
  52. return _send_cmd1(opr);
  53. }
  54. // Write 3-byte command sequence.
  55. // Returns true on success
  56. static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
  57. if (!_send_cmd1(cmd)) {
  58. return false;
  59. }
  60. if (!_send_cmd1(opr1)) {
  61. return false;
  62. }
  63. return _send_cmd1(opr2);
  64. }
  65. # define send_cmd1(c) \
  66. if (!_send_cmd1(c)) { \
  67. goto done; \
  68. }
  69. # define send_cmd2(c, o) \
  70. if (!_send_cmd2(c, o)) { \
  71. goto done; \
  72. }
  73. # define send_cmd3(c, o1, o2) \
  74. if (!_send_cmd3(c, o1, o2)) { \
  75. goto done; \
  76. }
  77. static void clear_display(void) {
  78. matrix_clear(&display);
  79. // Clear all of the display bits (there can be random noise
  80. // in the RAM on startup)
  81. send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
  82. send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
  83. if (i2c_start_write(SSD1306_ADDRESS)) {
  84. goto done;
  85. }
  86. if (i2c_master_write(0x40)) {
  87. // Data mode
  88. goto done;
  89. }
  90. for (uint8_t row = 0; row < MatrixRows; ++row) {
  91. for (uint8_t col = 0; col < DisplayWidth; ++col) {
  92. i2c_master_write(0);
  93. }
  94. }
  95. display.dirty = false;
  96. done:
  97. i2c_master_stop();
  98. }
  99. # if DEBUG_TO_SCREEN
  100. # undef sendchar
  101. static int8_t capture_sendchar(uint8_t c) {
  102. sendchar(c);
  103. iota_gfx_write_char(c);
  104. if (!displaying) {
  105. iota_gfx_flush();
  106. }
  107. return 0;
  108. }
  109. # endif
  110. bool iota_gfx_init(void) {
  111. bool success = false;
  112. send_cmd1(DisplayOff);
  113. send_cmd2(SetDisplayClockDiv, 0x80);
  114. send_cmd2(SetMultiPlex, DisplayHeight - 1);
  115. send_cmd2(SetDisplayOffset, 0);
  116. send_cmd1(SetStartLine | 0x0);
  117. send_cmd2(SetChargePump, 0x14 /* Enable */);
  118. send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
  119. # ifdef OLED_ROTATE180
  120. // the following Flip the display orientation 180 degrees
  121. send_cmd1(SegRemap);
  122. send_cmd1(ComScanInc);
  123. # endif
  124. # ifndef OLED_ROTATE180
  125. // Flips the display orientation 0 degrees
  126. send_cmd1(SegRemap | 0x1);
  127. send_cmd1(ComScanDec);
  128. # endif
  129. send_cmd2(SetComPins, 0x2);
  130. send_cmd2(SetContrast, 0x8f);
  131. send_cmd2(SetPreCharge, 0xf1);
  132. send_cmd2(SetVComDetect, 0x40);
  133. send_cmd1(DisplayAllOnResume);
  134. send_cmd1(NormalDisplay);
  135. send_cmd1(DeActivateScroll);
  136. send_cmd1(DisplayOn);
  137. send_cmd2(SetContrast, 0); // Dim
  138. clear_display();
  139. success = true;
  140. iota_gfx_flush();
  141. # if DEBUG_TO_SCREEN
  142. print_set_sendchar(capture_sendchar);
  143. # endif
  144. done:
  145. return success;
  146. }
  147. bool iota_gfx_off(void) {
  148. bool success = false;
  149. send_cmd1(DisplayOff);
  150. success = true;
  151. done:
  152. return success;
  153. }
  154. bool iota_gfx_on(void) {
  155. bool success = false;
  156. send_cmd1(DisplayOn);
  157. success = true;
  158. done:
  159. return success;
  160. }
  161. void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
  162. *matrix->cursor = c;
  163. ++matrix->cursor;
  164. if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
  165. // We went off the end; scroll the display upwards by one line
  166. memmove(&matrix->display[0], &matrix->display[1], MatrixCols * (MatrixRows - 1));
  167. matrix->cursor = &matrix->display[MatrixRows - 1][0];
  168. memset(matrix->cursor, ' ', MatrixCols);
  169. }
  170. }
  171. void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
  172. matrix->dirty = true;
  173. if (c == '\n') {
  174. // Clear to end of line from the cursor and then move to the
  175. // start of the next line
  176. uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
  177. while (cursor_col++ < MatrixCols) {
  178. matrix_write_char_inner(matrix, ' ');
  179. }
  180. return;
  181. }
  182. matrix_write_char_inner(matrix, c);
  183. }
  184. void iota_gfx_write_char(uint8_t c) { matrix_write_char(&display, c); }
  185. void matrix_write(struct CharacterMatrix *matrix, const char *data) {
  186. const char *end = data + strlen(data);
  187. while (data < end) {
  188. matrix_write_char(matrix, *data);
  189. ++data;
  190. }
  191. }
  192. void iota_gfx_write(const char *data) { matrix_write(&display, data); }
  193. void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
  194. while (true) {
  195. uint8_t c = pgm_read_byte(data);
  196. if (c == 0) {
  197. return;
  198. }
  199. matrix_write_char(matrix, c);
  200. ++data;
  201. }
  202. }
  203. void iota_gfx_write_P(const char *data) { matrix_write_P(&display, data); }
  204. void matrix_clear(struct CharacterMatrix *matrix) {
  205. memset(matrix->display, ' ', sizeof(matrix->display));
  206. matrix->cursor = &matrix->display[0][0];
  207. matrix->dirty = true;
  208. }
  209. void iota_gfx_clear_screen(void) { matrix_clear(&display); }
  210. void matrix_render(struct CharacterMatrix *matrix) {
  211. last_flush = timer_read();
  212. iota_gfx_on();
  213. # if DEBUG_TO_SCREEN
  214. ++displaying;
  215. # endif
  216. // Move to the home position
  217. send_cmd3(PageAddr, 0, MatrixRows - 1);
  218. send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
  219. if (i2c_start_write(SSD1306_ADDRESS)) {
  220. goto done;
  221. }
  222. if (i2c_master_write(0x40)) {
  223. // Data mode
  224. goto done;
  225. }
  226. for (uint8_t row = 0; row < MatrixRows; ++row) {
  227. for (uint8_t col = 0; col < MatrixCols; ++col) {
  228. const uint8_t *glyph = font + (matrix->display[row][col] * (FontWidth - 1));
  229. for (uint8_t glyphCol = 0; glyphCol < FontWidth - 1; ++glyphCol) {
  230. uint8_t colBits = pgm_read_byte(glyph + glyphCol);
  231. i2c_master_write(colBits);
  232. }
  233. // 1 column of space between chars (it's not included in the glyph)
  234. i2c_master_write(0);
  235. }
  236. }
  237. matrix->dirty = false;
  238. done:
  239. i2c_master_stop();
  240. # if DEBUG_TO_SCREEN
  241. --displaying;
  242. # endif
  243. }
  244. void iota_gfx_flush(void) { matrix_render(&display); }
  245. __attribute__((weak)) void iota_gfx_task_user(void) {}
  246. void iota_gfx_task(void) {
  247. iota_gfx_task_user();
  248. if (display.dirty) {
  249. iota_gfx_flush();
  250. }
  251. if (timer_elapsed(last_flush) > ScreenOffInterval) {
  252. iota_gfx_off();
  253. }
  254. }
  255. #endif