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.

330 lines
7.1 KiB

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