ssd1306.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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) if (!_send_cmd1(c)) {goto done;}
  69. #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
  70. #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
  71. static void clear_display(void) {
  72. matrix_clear(&display);
  73. // Clear all of the display bits (there can be random noise
  74. // in the RAM on startup)
  75. send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
  76. send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
  77. if (i2c_start_write(SSD1306_ADDRESS)) {
  78. goto done;
  79. }
  80. if (i2c_master_write(0x40)) {
  81. // Data mode
  82. goto done;
  83. }
  84. for (uint8_t row = 0; row < MatrixRows; ++row) {
  85. for (uint8_t col = 0; col < DisplayWidth; ++col) {
  86. i2c_master_write(0);
  87. }
  88. }
  89. display.dirty = false;
  90. done:
  91. i2c_master_stop();
  92. }
  93. #if DEBUG_TO_SCREEN
  94. #undef sendchar
  95. static int8_t capture_sendchar(uint8_t c) {
  96. sendchar(c);
  97. iota_gfx_write_char(c);
  98. if (!displaying) {
  99. iota_gfx_flush();
  100. }
  101. return 0;
  102. }
  103. #endif
  104. bool iota_gfx_init(void) {
  105. bool success = false;
  106. send_cmd1(DisplayOff);
  107. send_cmd2(SetDisplayClockDiv, 0x80);
  108. send_cmd2(SetMultiPlex, DisplayHeight - 1);
  109. send_cmd2(SetDisplayOffset, 0);
  110. send_cmd1(SetStartLine | 0x0);
  111. send_cmd2(SetChargePump, 0x14 /* Enable */);
  112. send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
  113. #ifdef OLED_ROTATE180
  114. // the following Flip the display orientation 180 degrees
  115. send_cmd1(SegRemap);
  116. send_cmd1(ComScanInc);
  117. #endif
  118. #ifndef OLED_ROTATE180
  119. // Flips the display orientation 0 degrees
  120. send_cmd1(SegRemap | 0x1);
  121. send_cmd1(ComScanDec);
  122. #endif
  123. send_cmd2(SetComPins, 0x2);
  124. send_cmd2(SetContrast, 0x8f);
  125. send_cmd2(SetPreCharge, 0xf1);
  126. send_cmd2(SetVComDetect, 0x40);
  127. send_cmd1(DisplayAllOnResume);
  128. send_cmd1(NormalDisplay);
  129. send_cmd1(DeActivateScroll);
  130. send_cmd1(DisplayOn);
  131. send_cmd2(SetContrast, 0); // Dim
  132. clear_display();
  133. success = true;
  134. iota_gfx_flush();
  135. #if DEBUG_TO_SCREEN
  136. print_set_sendchar(capture_sendchar);
  137. #endif
  138. done:
  139. return success;
  140. }
  141. bool iota_gfx_off(void) {
  142. bool success = false;
  143. send_cmd1(DisplayOff);
  144. success = true;
  145. done:
  146. return success;
  147. }
  148. bool iota_gfx_on(void) {
  149. bool success = false;
  150. send_cmd1(DisplayOn);
  151. success = true;
  152. done:
  153. return success;
  154. }
  155. void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
  156. *matrix->cursor = c;
  157. ++matrix->cursor;
  158. if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
  159. // We went off the end; scroll the display upwards by one line
  160. memmove(&matrix->display[0], &matrix->display[1],
  161. MatrixCols * (MatrixRows - 1));
  162. matrix->cursor = &matrix->display[MatrixRows - 1][0];
  163. memset(matrix->cursor, ' ', MatrixCols);
  164. }
  165. }
  166. void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
  167. matrix->dirty = true;
  168. if (c == '\n') {
  169. // Clear to end of line from the cursor and then move to the
  170. // start of the next line
  171. uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
  172. while (cursor_col++ < MatrixCols) {
  173. matrix_write_char_inner(matrix, ' ');
  174. }
  175. return;
  176. }
  177. matrix_write_char_inner(matrix, c);
  178. }
  179. void iota_gfx_write_char(uint8_t c) {
  180. matrix_write_char(&display, c);
  181. }
  182. void matrix_write(struct CharacterMatrix *matrix, const char *data) {
  183. const char *end = data + strlen(data);
  184. while (data < end) {
  185. matrix_write_char(matrix, *data);
  186. ++data;
  187. }
  188. }
  189. void iota_gfx_write(const char *data) {
  190. matrix_write(&display, data);
  191. }
  192. void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
  193. while (true) {
  194. uint8_t c = pgm_read_byte(data);
  195. if (c == 0) {
  196. return;
  197. }
  198. matrix_write_char(matrix, c);
  199. ++data;
  200. }
  201. }
  202. void iota_gfx_write_P(const char *data) {
  203. matrix_write_P(&display, data);
  204. }
  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) {
  211. matrix_clear(&display);
  212. }
  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) {
  248. matrix_render(&display);
  249. }
  250. __attribute__ ((weak))
  251. void iota_gfx_task_user(void) {
  252. }
  253. void iota_gfx_task(void) {
  254. iota_gfx_task_user();
  255. if (display.dirty) {
  256. iota_gfx_flush();
  257. }
  258. if (timer_elapsed(last_flush) > ScreenOffInterval) {
  259. iota_gfx_off();
  260. }
  261. }
  262. #endif