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.

347 lines
7.4 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 30000 /* 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. #ifdef SSD1306_128X64
  125. send_cmd2(SetComPins, 0x12);
  126. #else
  127. send_cmd2(SetComPins, 0x2);
  128. #endif
  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 matrix_write_ln(struct CharacterMatrix *matrix, const char *data) {
  195. char data_ln[strlen(data)+2];
  196. snprintf(data_ln, sizeof(data_ln), "%s\n", data);
  197. matrix_write(matrix, data_ln);
  198. }
  199. void iota_gfx_write(const char *data) {
  200. matrix_write(&display, data);
  201. }
  202. void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
  203. while (true) {
  204. uint8_t c = pgm_read_byte(data);
  205. if (c == 0) {
  206. return;
  207. }
  208. matrix_write_char(matrix, c);
  209. ++data;
  210. }
  211. }
  212. void iota_gfx_write_P(const char *data) {
  213. matrix_write_P(&display, data);
  214. }
  215. void matrix_clear(struct CharacterMatrix *matrix) {
  216. memset(matrix->display, ' ', sizeof(matrix->display));
  217. matrix->cursor = &matrix->display[0][0];
  218. matrix->dirty = true;
  219. }
  220. void iota_gfx_clear_screen(void) {
  221. matrix_clear(&display);
  222. }
  223. void matrix_render(struct CharacterMatrix *matrix) {
  224. last_flush = timer_read();
  225. iota_gfx_on();
  226. #if DEBUG_TO_SCREEN
  227. ++displaying;
  228. #endif
  229. // Move to the home position
  230. send_cmd3(PageAddr, 0, MatrixRows - 1);
  231. send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
  232. if (i2c_start_write(SSD1306_ADDRESS)) {
  233. goto done;
  234. }
  235. if (i2c_master_write(0x40)) {
  236. // Data mode
  237. goto done;
  238. }
  239. for (uint8_t row = 0; row < MatrixRows; ++row) {
  240. for (uint8_t col = 0; col < MatrixCols; ++col) {
  241. const uint8_t *glyph = font + (matrix->display[row][col] * FontWidth);
  242. for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
  243. uint8_t colBits = pgm_read_byte(glyph + glyphCol);
  244. i2c_master_write(colBits);
  245. }
  246. // 1 column of space between chars (it's not included in the glyph)
  247. //i2c_master_write(0);
  248. }
  249. }
  250. matrix->dirty = false;
  251. done:
  252. i2c_master_stop();
  253. #if DEBUG_TO_SCREEN
  254. --displaying;
  255. #endif
  256. }
  257. void iota_gfx_flush(void) {
  258. matrix_render(&display);
  259. }
  260. __attribute__ ((weak))
  261. void iota_gfx_task_user(void) {
  262. }
  263. void iota_gfx_task(void) {
  264. iota_gfx_task_user();
  265. if (display.dirty|| force_dirty) {
  266. iota_gfx_flush();
  267. force_dirty = false;
  268. }
  269. if (timer_elapsed(last_flush) > ScreenOffInterval) {
  270. iota_gfx_off();
  271. }
  272. }
  273. bool process_record_gfx(uint16_t keycode, keyrecord_t *record) {
  274. force_dirty = true;
  275. return true;
  276. }
  277. #endif