You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
7.3 KiB

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