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.

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