display.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Motenpoche
  2. *
  3. * (c) 2023 Daniele Lacamera <root@danielinux.net>
  4. *
  5. *
  6. * Motenpoche is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Motenpoche is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  19. *
  20. */
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "display.h"
  24. #include "hardware/i2c.h"
  25. #include "bsp/board.h"
  26. #define DISPLAY_I2C_ADDR 0x3C
  27. static uint8_t contrast = 0xcf;
  28. static i2c_inst_t *i2c_inst = NULL;
  29. static void display_send_data(const uint8_t *buf, int len)
  30. {
  31. const uint8_t start_data = 0x40;
  32. int i;
  33. uint8_t data_buf[200];
  34. data_buf[0] = 0x40;
  35. if (len > 199)
  36. len = 199;
  37. memcpy(data_buf + 1, buf, len);
  38. if (!i2c_inst)
  39. return;
  40. i2c_write_blocking(i2c_inst, DISPLAY_I2C_ADDR, data_buf, len+1, 0);
  41. }
  42. static void display_send_cmd(uint8_t cmd)
  43. {
  44. uint8_t buf[2] = {0x00, cmd};
  45. if (!i2c_inst)
  46. return;
  47. i2c_write_blocking(i2c_inst, DISPLAY_I2C_ADDR, buf, 2, 0);
  48. }
  49. static void display_send_cmd1(uint8_t cmd, uint8_t arg1)
  50. {
  51. uint8_t buf[3] = {0x00, cmd, arg1};
  52. if (!i2c_inst)
  53. return;
  54. i2c_write_blocking(i2c_inst, DISPLAY_I2C_ADDR, buf, 3, 0);
  55. }
  56. static void display_send_cmd2(uint8_t cmd, uint8_t arg1, uint8_t arg2)
  57. {
  58. uint8_t buf[4] = {0x00, cmd, arg1, arg2};
  59. if (!i2c_inst)
  60. return;
  61. i2c_write_blocking(i2c_inst, DISPLAY_I2C_ADDR, buf, 4, 0);
  62. }
  63. void display_scroll(uint8_t line)
  64. {
  65. display_send_cmd1(SSD1306_SETSTARTLINE, (line % 0x40) + 0x40);
  66. }
  67. void display_setcontrast(uint8_t c)
  68. {
  69. contrast = c;
  70. display_send_cmd1(SSD1306_SETCONTRAST, contrast);
  71. }
  72. uint8_t display_getcontrast(void)
  73. {
  74. return contrast;
  75. }
  76. void display_clear(void)
  77. {
  78. int i,j;
  79. uint8_t zeros[8] = {0, 0, 0, 0, 0, 0, 0, 0 };
  80. display_send_cmd(0x00);
  81. display_send_cmd(0x10);
  82. for (i = 0; i < 8; i++)
  83. {
  84. display_send_cmd2(SSD1306_PAGEADDR, i, 0xFF);
  85. for (j = 0; j < WIDTH; j++)
  86. display_send_data(zeros, 8);
  87. }
  88. }
  89. void display_text(int row, const char *text)
  90. {
  91. int k;
  92. display_send_cmd(0x00);
  93. display_send_cmd(0x10);
  94. display_send_cmd2(SSD1306_PAGEADDR, 7 - row, 0xFF);
  95. for(k = 0; k < strlen(text); k++)
  96. display_send_data(fb_font[text[k]], 8);
  97. }
  98. void display_text_inverse(int row, int col, const char *text)
  99. {
  100. int k, j;
  101. uint8_t inv_buf[8];
  102. uint8_t ones[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  103. int len = strlen(text);
  104. if (len > 15)
  105. len = 15;
  106. display_send_cmd(0x00);
  107. display_send_cmd(0x10);
  108. sleep_us(1000);
  109. display_send_cmd2(SSD1306_PAGEADDR, 7 - row, 0x00);
  110. display_send_cmd2(SSD1306_COLUMNADDR, col << 3, WIDTH - 1);
  111. for(k = 0; k < len; k++) {
  112. for (j = 0; j < 8; j++)
  113. inv_buf[j] = ~fb_font[text[k]][j];
  114. display_send_data(inv_buf, 8);
  115. }
  116. display_send_cmd2(SSD1306_COLUMNADDR, 0, WIDTH - 1);
  117. }
  118. int display_init(void *priv)
  119. {
  120. int i;
  121. int k = 0;
  122. int row = 0;
  123. volatile int j;
  124. uint8_t dbuf2[64] = {};
  125. int page = 0;
  126. int seg = 0;
  127. volatile uint32_t now;
  128. i2c_inst = (i2c_inst_t *)priv;
  129. for (i = 1; i < 64; i++) {
  130. dbuf2[i] = 0;
  131. }
  132. display_send_cmd(SSD1306_DISPLAYOFF);
  133. display_send_cmd1(0xD6, 0x01);
  134. display_send_cmd(0xA1);
  135. display_setcontrast(contrast);
  136. display_send_cmd1(SSD1306_CHARGEPUMP, 0x14);
  137. display_send_cmd1(SSD1306_MEMORYMODE, 0x00);
  138. display_send_cmd1(SSD1306_SETCOMPINS, 0x12);
  139. display_send_cmd1(SSD1306_SETDISPLAYOFFSET, 0x00);
  140. display_send_cmd1(SSD1306_SETVCOMDETECT, 0x00);
  141. display_send_cmd1(SSD1306_SETMULTIPLEX, 63);
  142. display_send_cmd(SSD1306_COMSCANINC);
  143. display_send_cmd(SSD1306_DISPLAYALLON_RESUME);
  144. display_send_cmd(SSD1306_DISPLAYON);
  145. display_send_cmd(0x2E);
  146. display_send_cmd2(SSD1306_PAGEADDR, 0, 0xFF);
  147. display_send_cmd2(SSD1306_COLUMNADDR, 0, WIDTH - 1);
  148. display_send_cmd1(SSD1306_SETSTARTLINE, 0);
  149. display_send_cmd(0x00);
  150. display_send_cmd(0x10);
  151. for (page = 0; page < 8; page++) {
  152. display_send_cmd2(SSD1306_PAGEADDR, page, 0xFF);
  153. for (seg= 0; seg < 32; seg++) {
  154. display_send_data(dbuf2, 8);
  155. }
  156. display_send_cmd1(SSD1306_SETSTARTLINE, row);
  157. }
  158. return 0;
  159. }