ssd1306.c 6.2 KB

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