ssd1306.c 7.2 KB

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