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