ST7789LCDDisplay.cpp 4.0 KB

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