ST7789LCDDisplay.cpp 4.0 KB

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