Advertisement
Guest User

Untitled

a guest
Apr 16th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. // supported chips: ST7735, ST7735S
  2.  
  3. #include "../main/main.h"
  4. #include "../main/color.h"
  5. #include "../main/drivers/screen.h"
  6. #include "driver/spi_master.h"
  7.  
  8. #ifndef SCREEN_RESX
  9. #define SCREEN_RESX 160
  10. #endif
  11.  
  12. #ifndef SCREEN_RESY
  13. #define SCREEN_RESY 80
  14. #endif
  15.  
  16. #ifndef SCREEN_OFFSET_X
  17. #define SCREEN_OFFSET_X 0
  18. #endif
  19.  
  20. #ifndef SCREEN_OFFSET_Y
  21. #define SCREEN_OFFSET_Y 24
  22. #endif
  23.  
  24. //SCREEN_LONG_INIT_DELAYS allows for more stable screen operation in some cases (compliance with the screen specification)
  25. #ifndef SCREEN_INIT_DELAYS
  26. #ifdef SCREEN_LONG_INIT_DELAYS
  27. #define SCREEN_INIT_DELAYS 200
  28. #else
  29. #define SCREEN_INIT_DELAYS 50
  30. #endif
  31. #endif
  32.  
  33. static uint8_t buffer[SCREEN_RESX * SCREEN_RESY * 2];
  34.  
  35. // -------------------------------- SPI
  36.  
  37. static void spi_pre_transfer_callback(spi_transaction_t *t) {
  38. gpio_set_level(SCREEN_DC, (int)t->user);
  39. }
  40.  
  41. spi_device_handle_t spi;
  42.  
  43. static void sendCmd(uint8_t cmd) {
  44. spi_transaction_t t;
  45. memset(&t, 0, sizeof(t));
  46. t.length=8;
  47. t.tx_buffer=&cmd;
  48. t.user=(void*)0;
  49. spi_device_polling_transmit(spi, &t);
  50. }
  51.  
  52. static void sendData(const void* data, int len) {
  53. spi_transaction_t t;
  54. memset(&t, 0, sizeof(t));
  55. t.length=len*8;
  56. t.tx_buffer=data;
  57. t.user=(void*)1;
  58. spi_device_polling_transmit(spi, &t);
  59. }
  60.  
  61. static void sendDataByte(uint8_t data) {
  62. spi_transaction_t t;
  63. memset(&t, 0, sizeof(t));
  64. t.length=8;
  65. t.tx_buffer=&data;
  66. t.user=(void*)1;
  67. spi_device_polling_transmit(spi, &t);
  68. }
  69.  
  70. static void sendCmdArg(uint8_t cmd, uint8_t arg) {
  71. sendCmd(cmd);
  72. sendDataByte(arg);
  73. }
  74.  
  75. static void sendCmdArgs(uint8_t cmd, const void* data, int len) {
  76. sendCmd(cmd);
  77. sendData(data, len);
  78. }
  79.  
  80. // --------------------------------
  81.  
  82. screen_colormode screen_getColormode() {
  83. return screen_colored;
  84. }
  85.  
  86. tcolor screen_get(int x, int y) {
  87. int index = (x + (y * SCREEN_RESX)) * 2;
  88. uint16_t color565 = (buffer[index] << 8) + buffer[index+1];
  89. #ifdef SCREEN_INVERT_COLORS
  90. color565 = 0xffff - color565;
  91. #endif
  92. tcolor color = color_from565(color565);
  93. #ifdef SCREEN_ALT_COLOR_ORDER
  94. color = color_swap(color);
  95. #endif
  96. return color;
  97. }
  98.  
  99. void screen_set(int x, int y, tcolor color) {
  100. #ifdef SCREEN_ALT_COLOR_ORDER
  101. color = color_swap(color);
  102. #endif
  103. uint16_t color565 = color_to565(color);
  104. #ifdef SCREEN_INVERT_COLORS
  105. color565 = 0xffff - color565;
  106. #endif
  107. int index = (x + (y * SCREEN_RESX)) * 2;
  108. buffer[index] = color565 >> 8;
  109. buffer[index+1] = color565 % 256;
  110. }
  111.  
  112. void screen_update() {
  113. sendData(buffer, sizeof(buffer));
  114. }
  115.  
  116. int screen_x() {
  117. return SCREEN_RESX;
  118. }
  119.  
  120. int screen_y() {
  121. return SCREEN_RESY;
  122. }
  123.  
  124.  
  125. void static _init() {
  126. sendCmdArg(0x36, 0b01101000); //Memory data access control
  127. sendCmdArg(0x3A, 5); //Color mode
  128. sendCmd(0x11); //Sleep out
  129. sendCmd(0x29); //Display on
  130. }
  131.  
  132. void static _select(uint8_t x, uint8_t y, uint8_t w, uint8_t h) {
  133. uint8_t COL_START = SCREEN_OFFSET_X;
  134. uint8_t ROW_START = SCREEN_OFFSET_Y;
  135. uint8_t args[4];
  136.  
  137. args[0] = 0;
  138. args[1] = COL_START + x;
  139. args[2] = 0;
  140. args[3] = COL_START + x + w - 1;
  141. sendCmdArgs(0x2A, args, sizeof(args)); // CASET
  142.  
  143. args[1] = ROW_START + y;
  144. args[3] = ROW_START + y + h - 1;
  145. sendCmdArgs(0x2B, args, sizeof(args)); // RASET
  146. }
  147.  
  148.  
  149. void static _setup() {
  150. _select(0, 0, SCREEN_RESX, SCREEN_RESY);
  151. sendCmd(0x2C);
  152. }
  153.  
  154. esp_err_t screen_init() {
  155. esp_err_t ret = ESP_OK;
  156.  
  157. // SPI init
  158. pin(SCREEN_DC, GPIO_MODE_DEF_OUTPUT);
  159. spi_bus_config_t buscfg={
  160. .mosi_io_num=SCREEN_DIN,
  161. .sclk_io_num=SCREEN_CLK,
  162. .quadwp_io_num=-1,
  163. .quadhd_io_num=-1,
  164. .max_transfer_sz=sizeof(buffer)
  165. };
  166. ret = spi_bus_initialize(SCREEN_SPI, &buscfg, SPI_DMA_CH_AUTO);
  167. if (ret != ESP_OK) return ret;
  168.  
  169. // device init
  170. spi_device_interface_config_t devcfg={
  171. .clock_speed_hz=SCREEN_SPI_SPEED,
  172. .mode=0,
  173. .queue_size=7,
  174. .pre_cb=spi_pre_transfer_callback,
  175. };
  176. #ifdef SCREEN_CS
  177. pin(SCREEN_CS, GPIO_MODE_DEF_OUTPUT);
  178. devcfg.spics_io_num=SCREEN_CS;
  179. #endif
  180. ret = spi_bus_add_device(SCREEN_SPI, &devcfg, &spi);
  181. if (ret != ESP_OK) return ret;
  182.  
  183. // screen init
  184. #ifdef SCREEN_RST
  185. pin(SCREEN_RST, GPIO_MODE_DEF_OUTPUT);
  186. gpio_set_level(SCREEN_RST, 0);
  187. wait(SCREEN_INIT_DELAYS);
  188. gpio_set_level(SCREEN_RST, 1);
  189. #else
  190. sendCmd(0x01);
  191. #endif
  192. wait(SCREEN_INIT_DELAYS);
  193. _init();
  194. #ifdef SCREEN_LONG_INIT_DELAYS
  195. wait(50);
  196. #endif
  197. _setup();
  198. sendData(buffer, sizeof(buffer));
  199.  
  200. return ret;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement