ST7789LCDDisplay.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "ST7789LCDDisplay.h"
  2. #ifndef DISPLAY_ROTATION
  3. #define DISPLAY_ROTATION 3
  4. #endif
  5. #ifndef DISPLAY_SCALE_X
  6. #define DISPLAY_SCALE_X 2.5f // 320 / 128
  7. #endif
  8. #ifndef DISPLAY_SCALE_Y
  9. #define DISPLAY_SCALE_Y 3.75f // 240 / 64
  10. #endif
  11. #define DISPLAY_WIDTH 240
  12. #define DISPLAY_HEIGHT 320
  13. bool ST7789LCDDisplay::i2c_probe(TwoWire& wire, uint8_t addr) {
  14. return true;
  15. }
  16. bool ST7789LCDDisplay::begin() {
  17. if (!_isOn) {
  18. if (_peripher_power) _peripher_power->claim();
  19. if (PIN_TFT_LEDA_CTL != -1) {
  20. pinMode(PIN_TFT_LEDA_CTL, OUTPUT);
  21. digitalWrite(PIN_TFT_LEDA_CTL, HIGH);
  22. }
  23. if (PIN_TFT_RST != -1) {
  24. digitalWrite(PIN_TFT_RST, HIGH);
  25. }
  26. // Im not sure if this is just a t-deck problem or not, if your display is slow try this.
  27. #ifdef LILYGO_TDECK
  28. displaySPI.begin(PIN_TFT_SCL, -1, PIN_TFT_SDA, PIN_TFT_CS);
  29. #endif
  30. display.init(DISPLAY_WIDTH, DISPLAY_HEIGHT);
  31. display.setRotation(DISPLAY_ROTATION);
  32. display.setSPISpeed(40e6);
  33. display.fillScreen(ST77XX_BLACK);
  34. display.setTextColor(ST77XX_WHITE);
  35. display.setTextSize(2 * DISPLAY_SCALE_X);
  36. display.cp437(true); // Use full 256 char 'Code Page 437' font
  37. _isOn = true;
  38. }
  39. return true;
  40. }
  41. void ST7789LCDDisplay::turnOn() {
  42. ST7789LCDDisplay::begin();
  43. }
  44. void ST7789LCDDisplay::turnOff() {
  45. if (_isOn) {
  46. if (PIN_TFT_LEDA_CTL != -1) {
  47. digitalWrite(PIN_TFT_LEDA_CTL, HIGH);
  48. }
  49. if (PIN_TFT_RST != -1) {
  50. digitalWrite(PIN_TFT_RST, LOW);
  51. }
  52. if (PIN_TFT_LEDA_CTL != -1) {
  53. digitalWrite(PIN_TFT_LEDA_CTL, LOW);
  54. }
  55. _isOn = false;
  56. if (_peripher_power) _peripher_power->release();
  57. }
  58. }
  59. void ST7789LCDDisplay::clear() {
  60. display.fillScreen(ST77XX_BLACK);
  61. }
  62. void ST7789LCDDisplay::startFrame(Color bkg) {
  63. display.fillScreen(ST77XX_BLACK);
  64. display.setTextColor(ST77XX_WHITE);
  65. display.setTextSize(1 * DISPLAY_SCALE_X); // This one affects size of Please wait... message
  66. display.cp437(true); // Use full 256 char 'Code Page 437' font
  67. }
  68. void ST7789LCDDisplay::setTextSize(int sz) {
  69. display.setTextSize(sz * DISPLAY_SCALE_X);
  70. }
  71. void ST7789LCDDisplay::setColor(Color c) {
  72. switch (c) {
  73. case DisplayDriver::DARK :
  74. _color = ST77XX_BLACK;
  75. break;
  76. case DisplayDriver::LIGHT :
  77. _color = ST77XX_WHITE;
  78. break;
  79. case DisplayDriver::RED :
  80. _color = ST77XX_RED;
  81. break;
  82. case DisplayDriver::GREEN :
  83. _color = ST77XX_GREEN;
  84. break;
  85. case DisplayDriver::BLUE :
  86. _color = ST77XX_BLUE;
  87. break;
  88. case DisplayDriver::YELLOW :
  89. _color = ST77XX_YELLOW;
  90. break;
  91. case DisplayDriver::ORANGE :
  92. _color = ST77XX_ORANGE;
  93. break;
  94. default:
  95. _color = ST77XX_WHITE;
  96. break;
  97. }
  98. display.setTextColor(_color);
  99. }
  100. void ST7789LCDDisplay::setCursor(int x, int y) {
  101. display.setCursor(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y);
  102. }
  103. void ST7789LCDDisplay::print(const char* str) {
  104. display.print(str);
  105. }
  106. void ST7789LCDDisplay::fillRect(int x, int y, int w, int h) {
  107. display.fillRect(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, w * DISPLAY_SCALE_X, h * DISPLAY_SCALE_Y, _color);
  108. }
  109. void ST7789LCDDisplay::drawRect(int x, int y, int w, int h) {
  110. display.drawRect(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, w * DISPLAY_SCALE_X, h * DISPLAY_SCALE_Y, _color);
  111. }
  112. void ST7789LCDDisplay::drawXbm(int x, int y, const uint8_t* bits, int w, int h) {
  113. uint8_t byteWidth = (w + 7) / 8;
  114. for (int j = 0; j < h; j++) {
  115. for (int i = 0; i < w; i++) {
  116. uint8_t byte = bits[j * byteWidth + i / 8];
  117. bool pixelOn = byte & (0x80 >> (i & 7));
  118. if (pixelOn) {
  119. for (int dy = 0; dy < DISPLAY_SCALE_X; dy++) {
  120. for (int dx = 0; dx < DISPLAY_SCALE_X; dx++) {
  121. display.drawPixel(x * DISPLAY_SCALE_X + i * DISPLAY_SCALE_X + dx, y * DISPLAY_SCALE_Y + j * DISPLAY_SCALE_X + dy, _color);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. uint16_t ST7789LCDDisplay::getTextWidth(const char* str) {
  129. int16_t x1, y1;
  130. uint16_t w, h;
  131. display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
  132. return w / DISPLAY_SCALE_X;
  133. }
  134. void ST7789LCDDisplay::endFrame() {
  135. // display.display();
  136. }