ssd1306.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. 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 iota_gfx_write(const char *data) {
  193. matrix_write(&display, data);
  194. }
  195. void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
  196. while (true) {
  197. uint8_t c = pgm_read_byte(data);
  198. if (c == 0) {
  199. return;
  200. }
  201. matrix_write_char(matrix, c);
  202. ++data;
  203. }
  204. }
  205. void iota_gfx_write_P(const char *data) {
  206. matrix_write_P(&display, data);
  207. }
  208. void matrix_clear(struct CharacterMatrix *matrix) {
  209. memset(matrix->display, ' ', sizeof(matrix->display));
  210. matrix->cursor = &matrix->display[0][0];
  211. matrix->dirty = true;
  212. }
  213. void iota_gfx_clear_screen(void) {
  214. matrix_clear(&display);
  215. }
  216. void matrix_render(struct CharacterMatrix *matrix) {
  217. last_flush = timer_read();
  218. iota_gfx_on();
  219. #if DEBUG_TO_SCREEN
  220. ++displaying;
  221. #endif
  222. // Move to the home position
  223. send_cmd3(PageAddr, 0, MatrixRows - 1);
  224. send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
  225. if (i2c_start_write(SSD1306_ADDRESS)) {
  226. goto done;
  227. }
  228. if (i2c_master_write(0x40)) {
  229. // Data mode
  230. goto done;
  231. }
  232. for (uint8_t row = 0; row < MatrixRows; ++row) {
  233. for (uint8_t col = 0; col < MatrixCols; ++col) {
  234. const uint8_t *glyph = font + (matrix->display[row][col] * FontWidth);
  235. for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
  236. uint8_t colBits = pgm_read_byte(glyph + glyphCol);
  237. i2c_master_write(colBits);
  238. }
  239. // 1 column of space between chars (it's not included in the glyph)
  240. //i2c_master_write(0);
  241. }
  242. }
  243. matrix->dirty = false;
  244. done:
  245. i2c_master_stop();
  246. #if DEBUG_TO_SCREEN
  247. --displaying;
  248. #endif
  249. }
  250. void iota_gfx_flush(void) {
  251. matrix_render(&display);
  252. }
  253. __attribute__ ((weak))
  254. void iota_gfx_task_user(void) {
  255. }
  256. void iota_gfx_task(void) {
  257. iota_gfx_task_user();
  258. if (display.dirty) {
  259. iota_gfx_flush();
  260. }
  261. if (timer_elapsed(last_flush) > ScreenOffInterval) {
  262. iota_gfx_off();
  263. }
  264. }
  265. #endif