acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Arduino_ST7735.cpp (3693B)
1 /* 2 * start rewrite from: 3 * https://github.com/adafruit/Adafruit-GFX-Library.git 4 */ 5 #include "Arduino_ST7735.h" 6 #include "SPI.h" 7 8 Arduino_ST7735::Arduino_ST7735( 9 Arduino_DataBus *bus, int8_t rst, uint8_t r, 10 bool ips, int16_t w, int16_t h, 11 uint8_t col_offset1, uint8_t row_offset1, uint8_t col_offset2, uint8_t row_offset2, 12 bool bgr) 13 : Arduino_TFT(bus, rst, r, ips, w, h, col_offset1, row_offset1, col_offset2, row_offset2), _bgr(bgr) 14 { 15 } 16 17 bool Arduino_ST7735::begin(int32_t speed) 18 { 19 #if defined(ESP8266) || defined(ESP32) 20 if (speed == GFX_NOT_DEFINED) 21 { 22 speed = 27000000UL; // ST7735 Maximum supported speed 23 } 24 // Teensy 4.x 25 #elif defined(__IMXRT1052__) || defined(__IMXRT1062__) 26 if (speed == GFX_NOT_DEFINED) 27 { 28 speed = 27000000UL; // ST7735 Maximum supported speed 29 } 30 #endif 31 _override_datamode = SPI_MODE0; // always use SPI_MODE0 32 33 return Arduino_TFT::begin(speed); 34 } 35 36 // Companion code to the above tables. Reads and issues 37 // a series of LCD commands stored in PROGMEM byte array. 38 void Arduino_ST7735::tftInit() 39 { 40 if (_rst != GFX_NOT_DEFINED) 41 { 42 pinMode(_rst, OUTPUT); 43 digitalWrite(_rst, HIGH); 44 delay(100); 45 digitalWrite(_rst, LOW); 46 delay(ST7735_RST_DELAY); 47 digitalWrite(_rst, HIGH); 48 delay(ST7735_RST_DELAY); 49 } 50 else 51 { 52 // Software Rest 53 _bus->sendCommand(ST7735_SWRESET); // 1: Software reset 54 delay(ST7735_RST_DELAY); 55 } 56 57 _bus->batchOperation(st7735_init_operations, sizeof(st7735_init_operations)); 58 59 if (_ips) 60 { 61 _bus->sendCommand(ST7735_INVON); 62 } 63 } 64 65 void Arduino_ST7735::writeAddrWindow(int16_t x, int16_t y, uint16_t w, uint16_t h) 66 { 67 if ((x != _currentX) || (w != _currentW)) 68 { 69 int16_t x_start = x + _xStart, x_end = x + w - 1 + _xStart; 70 71 _bus->writeCommand(ST7735_CASET); // Column addr set 72 _bus->write(x_start >> 8); 73 _bus->write(x_start & 0xFF); // XSTART 74 _bus->write(x_end >> 8); 75 _bus->write(x_end & 0xFF); // XEND 76 77 _currentX = x; 78 _currentW = w; 79 } 80 if ((y != _currentY) || (h != _currentH)) 81 { 82 int16_t y_start = y + _yStart, y_end = y + h - 1 + _yStart; 83 84 _bus->writeCommand(ST7735_RASET); // Row addr set 85 _bus->write(y_start >> 8); 86 _bus->write(y_start & 0xFF); // YSTART 87 _bus->write(y_end >> 8); 88 _bus->write(y_end & 0xFF); // YEND 89 90 _currentY = y; 91 _currentH = h; 92 } 93 94 _bus->writeCommand(ST7735_RAMWR); // write to RAM 95 } 96 97 /**************************************************************************/ 98 /*! 99 @brief Set origin of (0,0) and orientation of TFT display 100 @param m The index for rotation, from 0-3 inclusive 101 */ 102 /**************************************************************************/ 103 void Arduino_ST7735::setRotation(uint8_t r) 104 { 105 Arduino_TFT::setRotation(r); 106 switch (_rotation) 107 { 108 case 1: 109 r = ST7735_MADCTL_MY | ST7735_MADCTL_MV | (_bgr ? ST7735_MADCTL_BGR : ST7735_MADCTL_RGB); 110 break; 111 case 2: 112 r = (_bgr ? ST7735_MADCTL_BGR : ST7735_MADCTL_RGB); 113 break; 114 case 3: 115 r = ST7735_MADCTL_MX | ST7735_MADCTL_MV | (_bgr ? ST7735_MADCTL_BGR : ST7735_MADCTL_RGB); 116 break; 117 default: // case 0: 118 r = ST7735_MADCTL_MX | ST7735_MADCTL_MY | (_bgr ? ST7735_MADCTL_BGR : ST7735_MADCTL_RGB); 119 break; 120 } 121 _bus->beginWrite(); 122 _bus->writeCommand(ST7735_MADCTL); 123 _bus->write(r); 124 _bus->endWrite(); 125 } 126 127 void Arduino_ST7735::invertDisplay(bool i) 128 { 129 _bus->sendCommand(_ips ? (i ? ST7735_INVOFF : ST7735_INVON) : (i ? ST7735_INVON : ST7735_INVOFF)); 130 } 131 132 void Arduino_ST7735::displayOn(void) 133 { 134 _bus->sendCommand(ST7735_SLPOUT); 135 delay(ST7735_SLPOUT_DELAY); 136 } 137 138 void Arduino_ST7735::displayOff(void) 139 { 140 _bus->sendCommand(ST7735_SLPIN); 141 delay(ST7735_SLPIN_DELAY); 142 }