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.

342 lines
7.2 KiB

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