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.

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