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_SSD1331.cpp (4254B)

      1 /*
      2  * start rewrite from:
      3  * https://github.com/adafruit/Adafruit-GFX-Library.git
      4  */
      5 #include "Arduino_SSD1331.h"
      6 #include "SPI.h"
      7 
      8 Arduino_SSD1331::Arduino_SSD1331(
      9     Arduino_DataBus *bus, int8_t rst, uint8_t r, int16_t w, int16_t h,
     10     uint8_t col_offset1, uint8_t row_offset1, uint8_t col_offset2, uint8_t row_offset2)
     11     : Arduino_TFT(bus, rst, r, false, w, h, col_offset1, row_offset1, col_offset2, row_offset2)
     12 {
     13 }
     14 
     15 bool Arduino_SSD1331::begin(int32_t speed)
     16 {
     17   return Arduino_TFT::begin(speed);
     18 }
     19 
     20 // Companion code to the above tables.  Reads and issues
     21 // a series of LCD commands stored in PROGMEM byte array.
     22 void Arduino_SSD1331::tftInit()
     23 {
     24   if (_rst != GFX_NOT_DEFINED)
     25   {
     26     pinMode(_rst, OUTPUT);
     27     digitalWrite(_rst, HIGH);
     28     delay(100);
     29     digitalWrite(_rst, LOW);
     30     delay(SSD1331_RST_DELAY);
     31     digitalWrite(_rst, HIGH);
     32     delay(SSD1331_RST_DELAY);
     33   }
     34   else
     35   {
     36     // Software Rest
     37   }
     38 
     39   // Initialization Sequence
     40   _bus->sendCommand(SSD1331_DISPLAYOFF); // 0xAE
     41   _bus->sendCommand(SSD1331_STARTLINE);  // 0xA1
     42   _bus->sendCommand(0x0);
     43   _bus->sendCommand(SSD1331_DISPLAYOFFSET); // 0xA2
     44   _bus->sendCommand(0x0);
     45   _bus->sendCommand(SSD1331_NORMALDISPLAY); // 0xA4
     46   _bus->sendCommand(SSD1331_SETMULTIPLEX);  // 0xA8
     47   _bus->sendCommand(0x3F);                  // 0x3F 1/64 duty
     48   _bus->sendCommand(SSD1331_SETMASTER);     // 0xAD
     49   _bus->sendCommand(0x8E);
     50   _bus->sendCommand(SSD1331_POWERMODE); // 0xB0
     51   _bus->sendCommand(0x0B);
     52   _bus->sendCommand(SSD1331_PRECHARGE); // 0xB1
     53   _bus->sendCommand(0x31);
     54   _bus->sendCommand(SSD1331_CLOCKDIV);   // 0xB3
     55   _bus->sendCommand(0xF0);               // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
     56   _bus->sendCommand(SSD1331_PRECHARGEA); // 0x8A
     57   _bus->sendCommand(0x64);
     58   _bus->sendCommand(SSD1331_PRECHARGEB); // 0x8B
     59   _bus->sendCommand(0x78);
     60   _bus->sendCommand(SSD1331_PRECHARGEA); // 0x8C
     61   _bus->sendCommand(0x64);
     62   _bus->sendCommand(SSD1331_PRECHARGELEVEL); // 0xBB
     63   _bus->sendCommand(0x3A);
     64   _bus->sendCommand(SSD1331_VCOMH); // 0xBE
     65   _bus->sendCommand(0x3E);
     66   _bus->sendCommand(SSD1331_MASTERCURRENT); // 0x87
     67   _bus->sendCommand(0x06);
     68   _bus->sendCommand(SSD1331_CONTRASTA); // 0x81
     69   _bus->sendCommand(0x91);
     70   _bus->sendCommand(SSD1331_CONTRASTB); // 0x82
     71   _bus->sendCommand(0x50);
     72   _bus->sendCommand(SSD1331_CONTRASTC); // 0x83
     73   _bus->sendCommand(0x7D);
     74   _bus->sendCommand(SSD1331_DISPLAYON); //--turn on oled panel
     75 }
     76 
     77 void Arduino_SSD1331::writeAddrWindow(int16_t x, int16_t y, uint16_t w, uint16_t h)
     78 {
     79   if ((x != _currentX) || (w != _currentW))
     80   {
     81     uint8_t cmd = (_rotation & 0x01) ? SSD1331_SETROW : SSD1331_SETCOLUMN;
     82     uint8_t x_start = x + _xStart, x_end = x + w - 1 + _xStart;
     83 
     84     _bus->writeCommand(cmd);     // Column addr set
     85     _bus->writeCommand(x_start); // XSTART
     86     _bus->writeCommand(x_end);   // XEND
     87 
     88     _currentX = x;
     89     _currentW = w;
     90   }
     91   if ((y != _currentY) || (h != _currentH))
     92   {
     93     uint8_t cmd = (_rotation & 0x01) ? SSD1331_SETCOLUMN : SSD1331_SETROW;
     94     uint8_t y_start = y + _yStart, y_end = y + h - 1 + _yStart;
     95 
     96     _bus->writeCommand(cmd);     // Row addr set
     97     _bus->writeCommand(y_start); // YSTART
     98     _bus->writeCommand(y_end);   // YEND
     99 
    100     _currentY = y;
    101     _currentH = h;
    102   }
    103 }
    104 
    105 /**************************************************************************/
    106 /*!
    107     @brief   Set origin of (0,0) and orientation of TFT display
    108     @param   m  The index for rotation, from 0-3 inclusive
    109 */
    110 /**************************************************************************/
    111 void Arduino_SSD1331::setRotation(uint8_t r)
    112 {
    113   Arduino_TFT::setRotation(r);
    114   switch (_rotation)
    115   {
    116   case 1:
    117     r = 0b01110001;
    118     break;
    119   case 2:
    120     r = 0b01100000;
    121     break;
    122   case 3:
    123     r = 0b01100011;
    124     break;
    125   default: // case 0:
    126     r = 0b01110010;
    127     break;
    128   }
    129   _bus->beginWrite();
    130   _bus->writeCommand(SSD1331_SETREMAP);
    131   _bus->writeCommand(r);
    132   _bus->endWrite();
    133 }
    134 
    135 void Arduino_SSD1331::invertDisplay(bool i)
    136 {
    137   _bus->sendCommand(i ? SSD1331_INVERTDISPLAY : SSD1331_NORMALDISPLAY);
    138 }
    139 
    140 void Arduino_SSD1331::displayOn(void)
    141 {
    142   _bus->sendCommand(SSD1331_DISPLAYALLON);
    143 }
    144 
    145 void Arduino_SSD1331::displayOff(void)
    146 {
    147   _bus->sendCommand(SSD1331_DISPLAYALLOFF);
    148 }