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_ST7789.cpp (2992B)

      1 /*
      2  * start rewrite from:
      3  * https://github.com/adafruit/Adafruit-GFX-Library.git
      4  * https://github.com/ananevilya/Arduino-ST7789-Library.git
      5  */
      6 #include "Arduino_ST7789.h"
      7 #include "SPI.h"
      8 
      9 Arduino_ST7789::Arduino_ST7789(
     10     Arduino_DataBus *bus, int8_t rst, uint8_t r,
     11     bool ips, int16_t w, int16_t h,
     12     uint8_t col_offset1, uint8_t row_offset1, uint8_t col_offset2, uint8_t row_offset2)
     13     : Arduino_TFT(bus, rst, r, ips, w, h, col_offset1, row_offset1, col_offset2, row_offset2)
     14 {
     15 }
     16 
     17 bool Arduino_ST7789::begin(int32_t speed)
     18 {
     19 #if defined(ESP32) || defined(ARDUINO_ARCH_NRF52840)
     20   _override_datamode = SPI_MODE3;
     21 #elif defined(ESP8266)
     22   _override_datamode = SPI_MODE2;
     23 #endif
     24 
     25   return Arduino_TFT::begin(speed);
     26 }
     27 
     28 /**************************************************************************/
     29 /*!
     30     @brief   Set origin of (0,0) and orientation of TFT display
     31     @param   m  The index for rotation, from 0-3 inclusive
     32 */
     33 /**************************************************************************/
     34 void Arduino_ST7789::setRotation(uint8_t r)
     35 {
     36   Arduino_TFT::setRotation(r);
     37   switch (_rotation)
     38   {
     39   case 1:
     40     r = ST7789_MADCTL_MX | ST7789_MADCTL_MV | ST7789_MADCTL_RGB;
     41     break;
     42   case 2:
     43     r = ST7789_MADCTL_MX | ST7789_MADCTL_MY | ST7789_MADCTL_RGB;
     44     break;
     45   case 3:
     46     r = ST7789_MADCTL_MY | ST7789_MADCTL_MV | ST7789_MADCTL_RGB;
     47     break;
     48   default: // case 0:
     49     r = ST7789_MADCTL_RGB;
     50     break;
     51   }
     52   _bus->beginWrite();
     53   _bus->writeCommand(ST7789_MADCTL);
     54   _bus->write(r);
     55   _bus->endWrite();
     56 }
     57 
     58 void Arduino_ST7789::writeAddrWindow(int16_t x, int16_t y, uint16_t w, uint16_t h)
     59 {
     60   if ((x != _currentX) || (w != _currentW))
     61   {
     62     _currentX = x;
     63     _currentW = w;
     64     x += _xStart;
     65     _bus->writeC8D16D16(ST7789_CASET, x, x + w - 1);
     66   }
     67 
     68   if ((y != _currentY) || (h != _currentH))
     69   {
     70     _currentY = y;
     71     _currentH = h;
     72     y += _yStart;
     73     _bus->writeC8D16D16(ST7789_RASET, y, y + h - 1);
     74   }
     75 
     76   _bus->writeCommand(ST7789_RAMWR); // write to RAM
     77 }
     78 
     79 void Arduino_ST7789::invertDisplay(bool i)
     80 {
     81   _bus->sendCommand(_ips ? (i ? ST7789_INVOFF : ST7789_INVON) : (i ? ST7789_INVON : ST7789_INVOFF));
     82 }
     83 
     84 void Arduino_ST7789::displayOn(void)
     85 {
     86   _bus->sendCommand(ST7789_SLPOUT);
     87   delay(ST7789_SLPOUT_DELAY);
     88 }
     89 
     90 void Arduino_ST7789::displayOff(void)
     91 {
     92   _bus->sendCommand(ST7789_SLPIN);
     93   delay(ST7789_SLPIN_DELAY);
     94 }
     95 
     96 // Companion code to the above tables.  Reads and issues
     97 // a series of LCD commands stored in PROGMEM byte array.
     98 void Arduino_ST7789::tftInit()
     99 {
    100   if (_rst != GFX_NOT_DEFINED)
    101   {
    102     pinMode(_rst, OUTPUT);
    103     digitalWrite(_rst, HIGH);
    104     delay(100);
    105     digitalWrite(_rst, LOW);
    106     delay(ST7789_RST_DELAY);
    107     digitalWrite(_rst, HIGH);
    108     delay(ST7789_RST_DELAY);
    109   }
    110   // else
    111   // {
    112   // Software Rest
    113   _bus->sendCommand(ST7789_SWRESET);
    114   delay(ST7789_RST_DELAY);
    115   // }
    116 
    117   _bus->batchOperation(st7789_init_operations, sizeof(st7789_init_operations));
    118 
    119   if (_ips)
    120   {
    121     _bus->sendCommand(ST7789_INVON);
    122   }
    123 }