ssd1306.c 7.6 KB

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