PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.5KB

  1. /***************************************************
  2. This is a library for the Adafruit 1.8" SPI display.
  3. This library works with the Adafruit 1.8" TFT Breakout w/SD card
  4. ----> http://www.adafruit.com/products/358
  5. as well as Adafruit raw 1.8" TFT display
  6. ----> http://www.adafruit.com/products/618
  7. Check out the links above for our tutorials and wiring diagrams
  8. These displays use SPI to communicate, 4 or 5 pins are required to
  9. interface (RST is optional)
  10. Adafruit invests time and resources providing this open source code,
  11. please support Adafruit and open-source hardware by purchasing
  12. products from Adafruit!
  13. Written by Limor Fried/Ladyada for Adafruit Industries.
  14. MIT license, all text above must be included in any redistribution
  15. ****************************************************/
  16. #include "ST7789_t3.h"
  17. #include <limits.h>
  18. #include "pins_arduino.h"
  19. #include "wiring_private.h"
  20. #include <SPI.h>
  21. #define ST77XX_MADCTL_MY 0x80
  22. #define ST77XX_MADCTL_MX 0x40
  23. #define ST77XX_MADCTL_MV 0x20
  24. #define ST77XX_MADCTL_ML 0x10
  25. #define ST77XX_MADCTL_RGB 0x00
  26. ST7789_t3::ST7789_t3(uint8_t CS, uint8_t RS, uint8_t SID, uint8_t SCLK, uint8_t RST) : ST7735_t3(CS, RS, SID, SCLK, RST)
  27. {
  28. // Assume the majority of ones.
  29. tabcolor = INIT_ST7789_TABCOLOR;
  30. _screenHeight = 240;
  31. _screenWidth = 240;
  32. cursor_y = cursor_x = 0;
  33. textsize_x = 1;
  34. textsize_y = 1;
  35. textcolor = textbgcolor = 0xFFFF;
  36. wrap = true;
  37. font = NULL;
  38. setClipRect();
  39. setOrigin();
  40. }
  41. ST7789_t3::ST7789_t3(uint8_t CS, uint8_t RS, uint8_t RST) :
  42. ST7735_t3(CS, RS, RST)
  43. {
  44. tabcolor = INIT_ST7789_TABCOLOR;
  45. _screenHeight = 240;
  46. _screenWidth = 240;
  47. cursor_y = cursor_x = 0;
  48. textsize_x = 1;
  49. textsize_y = 1;
  50. textcolor = textbgcolor = 0xFFFF;
  51. wrap = true;
  52. font = NULL;
  53. setClipRect();
  54. setOrigin();
  55. }
  56. void ST7789_t3::setRotation(uint8_t m)
  57. {
  58. beginSPITransaction();
  59. writecommand(ST7735_MADCTL);
  60. rotation = m % 4; // can't be higher than 3
  61. switch (rotation) {
  62. case 0:
  63. writedata_last(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB);
  64. _xstart = _colstart;
  65. _ystart = _rowstart;
  66. _width = _screenWidth;
  67. _height = _screenHeight;
  68. break;
  69. case 1:
  70. writedata_last(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB);
  71. _xstart = _rowstart;
  72. _ystart = _colstart;
  73. _height = _screenWidth;
  74. _width = _screenHeight;
  75. break;
  76. case 2:
  77. writedata_last(ST77XX_MADCTL_RGB);
  78. if ((_screenWidth == 135) && (_screenHeight == 240)) {
  79. _xstart = _colstart - 1;
  80. _ystart = _rowstart;
  81. } else {
  82. _xstart = 0;
  83. _ystart = 0;
  84. }
  85. _width = _screenWidth;
  86. _height = _screenHeight;
  87. break;
  88. case 3:
  89. writedata_last(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB);
  90. if ((_screenWidth == 135) && (_screenHeight == 240)) {
  91. _xstart = _rowstart;
  92. _ystart = _colstart;
  93. } else {
  94. _xstart = 0;
  95. _ystart = 0;
  96. }
  97. _height = _screenWidth;
  98. _width = _screenHeight;
  99. break;
  100. }
  101. _rot = m;
  102. endSPITransaction();
  103. // Serial.printf("Set rotation %d start(%d %d) row: %d, col: %d\n", m, _xstart, _ystart, _rowstart, _colstart);
  104. setClipRect();
  105. setOrigin();
  106. cursor_x = 0;
  107. cursor_y = 0;
  108. }
  109. #define ST7789_240x240_XSTART 0
  110. #define ST7789_240x240_YSTART 80
  111. // Probably should use generic names like Adafruit..
  112. #define DELAY 0x80
  113. static const uint8_t PROGMEM
  114. cmd_st7789[] = { // Initialization commands for 7735B screens
  115. 9, // 9 commands in list:
  116. ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
  117. 150, // 150 ms delay
  118. ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
  119. 255, // 255 = 500 ms delay
  120. ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
  121. 0x55, // 16-bit color
  122. 10, // 10 ms delay
  123. ST7735_MADCTL , 1 , // 4: Memory access ctrl (directions), 1 arg:
  124. 0x08, // Row addr/col addr, bottom to top refresh
  125. ST7735_CASET , 4 , // 5: Column addr set, 4 args, no delay:
  126. 0x00,
  127. 0x00, // XSTART = 0
  128. 0x00,
  129. 240, // XEND = 240
  130. ST7735_RASET , 4 , // 6: Row addr set, 4 args, no delay:
  131. 0x00,
  132. 0x00, // YSTART = 0
  133. 320>>8,
  134. 320 & 0xFF, // YEND = 320
  135. ST7735_INVON , DELAY, // 7: hack
  136. 10,
  137. ST7735_NORON , DELAY, // 8: Normal display on, no args, w/delay
  138. 10, // 10 ms delay
  139. ST7735_DISPON , DELAY, // 9: Main screen turn on, no args, w/delay
  140. 255 }; // 255 = 500 ms delay
  141. void ST7789_t3::init(uint16_t width, uint16_t height, uint8_t mode)
  142. {
  143. Serial.printf("ST7789_t3::init mode: %x\n", mode);
  144. commonInit(NULL, mode);
  145. if ((width == 240) && (height == 240)) {
  146. _colstart = 0;
  147. _rowstart = 80;
  148. } else if ((width == 135) && (height == 240)) { // 1.13" display Their smaller display
  149. _colstart = 53;
  150. _rowstart = 40;
  151. } else {
  152. _colstart = 0;
  153. _rowstart = 0;
  154. }
  155. _height = height;
  156. _width = width;
  157. _screenHeight = height;
  158. _screenWidth = width;
  159. commandList(cmd_st7789);
  160. setRotation(0);
  161. cursor_y = cursor_x = 0;
  162. textsize_x = textsize_y = 1;
  163. textcolor = textbgcolor = 0xFFFF;
  164. wrap = true;
  165. font = NULL;
  166. setClipRect();
  167. setOrigin();
  168. }