ssd1306.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #define ScreenOffInterval 300000 /* milliseconds */
  28. #if DEBUG_TO_SCREEN
  29. static uint8_t displaying;
  30. #endif
  31. static uint16_t last_flush;
  32. // Write command sequence.
  33. // Returns true on success.
  34. static inline bool _send_cmd1(uint8_t cmd) {
  35. bool res = false;
  36. if (i2c_start_write(SSD1306_ADDRESS)) {
  37. xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
  38. goto done;
  39. }
  40. if (i2c_master_write(0x0 /* command byte follows */)) {
  41. print("failed to write control byte\n");
  42. goto done;
  43. }
  44. if (i2c_master_write(cmd)) {
  45. xprintf("failed to write command %d\n", cmd);
  46. goto done;
  47. }
  48. res = true;
  49. done:
  50. i2c_master_stop();
  51. return res;
  52. }
  53. // Write 2-byte command sequence.
  54. // Returns true on success
  55. static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
  56. if (!_send_cmd1(cmd)) {
  57. return false;
  58. }
  59. return _send_cmd1(opr);
  60. }
  61. // Write 3-byte command sequence.
  62. // Returns true on success
  63. static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
  64. if (!_send_cmd1(cmd)) {
  65. return false;
  66. }
  67. if (!_send_cmd1(opr1)) {
  68. return false;
  69. }
  70. return _send_cmd1(opr2);
  71. }
  72. #define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
  73. #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
  74. #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
  75. static void clear_display(void) {
  76. matrix_clear(&display);
  77. // Clear all of the display bits (there can be random noise
  78. // in the RAM on startup)
  79. send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
  80. send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
  81. if (i2c_start_write(SSD1306_ADDRESS)) {
  82. goto done;
  83. }
  84. if (i2c_master_write(0x40)) {
  85. // Data mode
  86. goto done;
  87. }
  88. for (uint8_t row = 0; row < MatrixRows; ++row) {
  89. for (uint8_t col = 0; col < DisplayWidth; ++col) {
  90. i2c_master_write(0);
  91. }
  92. }
  93. display.dirty = false;
  94. done:
  95. i2c_master_stop();
  96. }
  97. #if DEBUG_TO_SCREEN
  98. #undef sendchar
  99. static int8_t capture_sendchar(uint8_t c) {
  100. sendchar(c);
  101. iota_gfx_write_char(c);
  102. if (!displaying) {
  103. iota_gfx_flush();
  104. }
  105. return 0;
  106. }
  107. #endif
  108. bool iota_gfx_init(bool rotate) {
  109. bool success = false;
  110. i2c_master_init();
  111. send_cmd1(DisplayOff);
  112. send_cmd2(SetDisplayClockDiv, 0x80);
  113. send_cmd2(SetMultiPlex, DisplayHeight - 1);
  114. send_cmd2(SetDisplayOffset, 0);
  115. send_cmd1(SetStartLine | 0x0);
  116. send_cmd2(SetChargePump, 0x14 /* Enable */);
  117. send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
  118. if(rotate){
  119. // the following Flip the display orientation 180 degrees
  120. send_cmd1(SegRemap);
  121. send_cmd1(ComScanInc);
  122. }else{
  123. // Flips the display orientation 0 degrees
  124. send_cmd1(SegRemap | 0x1);
  125. send_cmd1(ComScanDec);
  126. }
  127. send_cmd2(SetComPins, 0x2);
  128. send_cmd2(SetContrast, 0x8f);
  129. send_cmd2(SetPreCharge, 0xf1);
  130. send_cmd2(SetVComDetect, 0x40);
  131. send_cmd1(DisplayAllOnResume);
  132. send_cmd1(NormalDisplay);
  133. send_cmd1(DeActivateScroll);
  134. send_cmd1(DisplayOn);
  135. send_cmd2(SetContrast, 0); // Dim
  136. clear_display();
  137. success = true;
  138. iota_gfx_flush();
  139. #if DEBUG_TO_SCREEN
  140. print_set_sendchar(capture_sendchar);
  141. #endif
  142. done:
  143. return success;
  144. }
  145. bool iota_gfx_off(void) {
  146. bool success = false;
  147. send_cmd1(DisplayOff);
  148. success = true;
  149. done:
  150. return success;
  151. }
  152. bool iota_gfx_on(void) {
  153. bool success = false;
  154. send_cmd1(DisplayOn);
  155. success = true;
  156. done:
  157. return success;
  158. }
  159. void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
  160. *matrix->cursor = c;
  161. ++matrix->cursor;
  162. if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
  163. // We went off the end; scroll the display upwards by one line
  164. memmove(&matrix->display[0], &matrix->display[1],
  165. MatrixCols * (MatrixRows - 1));
  166. matrix->cursor = &matrix->display[MatrixRows - 1][0];
  167. memset(matrix->cursor, ' ', MatrixCols);
  168. }
  169. }
  170. void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
  171. matrix->dirty = true;
  172. if (c == '\n') {
  173. // Clear to end of line from the cursor and then move to the
  174. // start of the next line
  175. uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
  176. while (cursor_col++ < MatrixCols) {
  177. matrix_write_char_inner(matrix, ' ');
  178. }
  179. return;
  180. }
  181. matrix_write_char_inner(matrix, c);
  182. }
  183. void iota_gfx_write_char(uint8_t c) {
  184. matrix_write_char(&display, c);
  185. }
  186. void matrix_write(struct CharacterMatrix *matrix, const char *data) {
  187. const char *end = data + strlen(data);
  188. while (data < end) {
  189. matrix_write_char(matrix, *data);
  190. ++data;
  191. }
  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