ssd1306.c 7.6 KB

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