// *****************************************************************************************************
//     HELLO WORLD:  ESP32 on ePaper 2.90" 128x296                                    created: Dec.2023
//     ============  
//                   code should be valid on LILYGO® TTGO T5 2.13" 122x250
//
//     Hardware:  ESP32-S3 'normal', OLED display 250x122 SPI
//     Libraries: GxEPD2.h   library for ePaper displays Jean Marc Zingg 
//     Processor: Selection in Arduino IDE:  ESP32 Dev Module
//     Possible  Touch Pins for ESP32 38pin:      GPIO #:	0  2  4  12 13 14 15 32 33
//     Possible  Input only Pins for ESP32 38pin: GPIO #:	34 35 36 39
//     Possible  GPIO Pins for ESP32 38pin:       GPIO #:	32 33 25 26 27 14 12 13 23 22 01 03 21 19 18 05 17 16 04 00 02 15 
//     Special pins for ESP32 38pin:              I2C: SDA 21, SCL 22    
//                                                SPI: DIN/MOSI 23, CLK 18, BUSY 4, RST 16, DC/MISO 17, CS 5
//                                                Buttons on T5: RST 0, free 39  /  LED: 19 internal RGB LED / Batt Volt. 35
//     to be added: 
//     -  
//     -  
//
// ESP32 ---> 2.13 inch e-paper SPI
// ePaper========>=====ESP32=====ESP32S3=====ESP32 Wokwi=========here:==========Zingg=Lolin32==D32 Pro==TTGO T8 ESP32 WROVER==
// BUSY     ----->     4                     4         4         4  arbitrary         4        15       4 
// RST      ----->     16                    "RX2"!    21        16 arbitrary         16       0        0 
// DC MISO? ----->     17                    "TX2"!    19        17 arbitrary         17       2        2 
// CS       ----->     5                     5         5         5  SS                SS(5)    5        SS(5) 
// CLK/SCK  ----->     18        HW 12 !!!   18        18        18                   18       18       18 
// DIN MOSI ----->     23        HW 11 !!!   23        23        23                   23       23       23 
// GND      ----->     GND
// 3V3      ----->     VCC
// *****************************************************************************************************

//
// ===== Versuch mit alter GxEPD.h library ===================================================================
#include <GxEPD.h>
#include <GxGDEM029T94/GxGDEM029T94.h>   
#include <GxIO/GxIO_SPI/GxIO_SPI.h>
#include <GxIO/GxIO.h>  
#include <SPI.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeMonoBold12pt7b.h>

#define CS   5                           // 15 CS         5  pin# std.S3 10        (ESP32 5)  
#define DC   22                          // 21 DC         22 pin# std.S3 14        (ESP32 2) 
#define RSET 21                          // 22 RST        21 pin# std.S3 13        (ESP32 0)
#define BUSY 4                           // 23 BUSY       4  pin# std.S3 9 or 46   (ESP32 4)
#define MOSI 23                          // 5  MOSI Data  23 HW  pin# std.S3 11    (ESP32 23)
#define SCLK 18                          // 4  CLK  Clock 18 HW  pin# std.S3 12    (ESP32 18)
#define MISO -1                          // -1 MISO       -  pin# std.S3 -         (ESP32 -)

GxIO_Class io(SPI, CS, DC, RSET);       // GxIO_Class io(SPI,  EPD_CS, EPD_DC,  EPD_RSET); 
GxEPD_Class display(io, RSET, BUSY);    // GxEPD_Class display(io, EPD_RSET, EPD_BUSY); 
// ====================================================================================================
//

/*
// ===== Versuch mit GxEPD2.h library ===================================================================
#include <GxEPD2_BW.h>
#include <GxEPD2_3C.h>
#include <GxEPD2_7C.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeMonoBold12pt7b.h>
//#include <Fonts/FreeSerif9pt7b.h>

#include <Adafruit_GFX.h>                 // Core graphics library
#include <SPI.h>
                                          // Data 23 HW (S3: 11), CLK 18 HW (S3: 12)
#define CS   5                            // CS   5  pin# std.S3 10  (ESP32 5)  
#define DC   22                           // DC   22 pin# std.S3 14  (ESP32 2) 
#define RST  21                           // RST  21 pin# std.S3 13  (ESP32 0)
#define BUSY 4                            // BUSY 4  pin# std.S3 9 or 46   (ESP32 4)
*/
// alternately you can copy the constructor from GxEPD2_display_selection.h or GxEPD2_display_selection_added.h to here
 //    GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(CS, DC, RST, BUSY));            // GDEH029A1 128x296, SSD1608 (IL3820)
//GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=5*/ 5, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));                 // GDEH029A1 128x296, SSD1608 (IL3820)
// e.g. for 2.9" and  #if defined(ESP32) && defined(ARDUINO_ESP32_DEV):
//GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));                   // GDEH029A1 128x296, SSD1608 (IL3820)
//GxEPD2_BW<GxEPD2_290_T5, GxEPD2_290_T5::HEIGHT> display(GxEPD2_290_T5(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));          // GDEW029T5 128x296, UC8151 (IL0373)
//GxEPD2_BW<GxEPD2_290_T5D, GxEPD2_290_T5D::HEIGHT> display(GxEPD2_290_T5D(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));       // GDEW029T5D 128x296, UC8151D
//GxEPD2_BW<GxEPD2_290_I6FD, GxEPD2_290_I6FD::HEIGHT> display(GxEPD2_290_I6FD(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));    // GDEW029I6FD 128x296, UC8151D
//GxEPD2_BW<GxEPD2_290_T94, GxEPD2_290_T94::HEIGHT> display(GxEPD2_290_T94(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));       // GDEM029T94 128x296, SSD1680
//GxEPD2_BW<GxEPD2_290_T94_V2, GxEPD2_290_T94_V2::HEIGHT> display(GxEPD2_290_T94_V2(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4)); // GDEM029T94 128x296, SSD1680, Waveshare 2.9" V2 variant
//GxEPD2_BW<GxEPD2_290_BS, GxEPD2_290_BS::HEIGHT> display(GxEPD2_290_BS(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));          // DEPG0290BS 128x296, SSD1680
//GxEPD2_BW<GxEPD2_290_M06, GxEPD2_290_M06::HEIGHT> display(GxEPD2_290_M06(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));       // GDEW029M06  128x296, UC8151D
//GxEPD2_BW<GxEPD2_290_GDEY029T94, GxEPD2_290_GDEY029T94::HEIGHT> display(GxEPD2_290_GDEY029T94(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4)); // GDEY029T94  128x296, SSD1680, (FPC-A005 20.06.15)
 // GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));             // GDEW029Z10 128x296, UC8151 (IL0373)
//GxEPD2_3C<GxEPD2_290_Z13c, GxEPD2_290_Z13c::HEIGHT> display(GxEPD2_290_Z13c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH029Z13 128x296, UC8151D
//GxEPD2_3C<GxEPD2_290_C90c, GxEPD2_290_C90c::HEIGHT> display(GxEPD2_290_C90c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEM029C90 128x296, SSD1680

// e.g. for 2.13" and  #if defined(ESP32) && defined(ARDUINO_LOLIN_D32_PRO):
//GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));                   // GDE0213B1 122x250, IL3895, phased out
//GxEPD2_BW<GxEPD2_213_B72, GxEPD2_213_B72::HEIGHT> display(GxEPD2_213_B72(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));       // GDEH0213B72 122x250, SSD1675A (IL3897)
//GxEPD2_BW<GxEPD2_213_B73, GxEPD2_213_B73::HEIGHT> display(GxEPD2_213_B73(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));       // GDEH0213B73 122x250, SSD1675B
//GxEPD2_BW<GxEPD2_213_B74, GxEPD2_213_B74::HEIGHT> display(GxEPD2_213_B74(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));       // GDEM0213B74 122x250, SSD1680
//GxEPD2_BW<GxEPD2_213_flex, GxEPD2_213_flex::HEIGHT> display(GxEPD2_213_flex(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));    // GDEW0213I5F 104x212, UC8151 (IL0373)
//GxEPD2_BW<GxEPD2_213_M21, GxEPD2_213_M21::HEIGHT> display(GxEPD2_213_M21(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));       // GDEW0213M21 104x212, UC8151 (IL0373)
//GxEPD2_BW<GxEPD2_213_T5D, GxEPD2_213_T5D::HEIGHT> display(GxEPD2_213_T5D(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));       // GDEW0213T5D 104x212, UC8151D
//GxEPD2_BW<GxEPD2_213_BN, GxEPD2_213_BN::HEIGHT> display(GxEPD2_213_BN(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));          // DEPG0213BN 122x250, SSD1680, TTGO T5 V2.4.1, V2.3.1
//GxEPD2_BW<GxEPD2_213_GDEY0213B74, GxEPD2_213_GDEY0213B74::HEIGHT> display(GxEPD2_213_GDEY0213B74(/*CS=5*/ 5, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4)); // GDEY0213B74 122x250, SSD1680, (FPC-A002 20.04.08)
// ====================================================================================================


const char TextBsp[] = "Irgendein Text";

int cnt;            // counter zum Testen des Beispiels mit alter GxEPD library

void setup()
{ Serial.begin(9600);
  
  SPI.begin(SCLK, MISO, MOSI);                     // GxEPD library alt, Definition     
  display.init();                                  // alte und neue library, Initialisierung 
  //display.init(115200);                          // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  //display.init(115200, true, 2, false);          // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  
  /* 
  display.setRotation(1);
  display.fillScreen(GxEPD_WHITE);
  display.setFont(&FreeMonoBold9pt7b);
  display.setTextColor(GxEPD_BLACK);
  
  int16_t tbx, tby; uint16_t tbw, tbh;
  display.getTextBounds(TextBsp, 0, 0, &tbx, &tby, &tbw, &tbh);
  // center the bounding box by transposition of the origin:
  uint16_t x = ((display.width() - tbw) / 2) - tbx;
  uint16_t y = ((display.height() - tbh) / 2) - tby;
  // display.setFullWindow();                        // wozu?
  
  display.firstPage();                               // variant with do... while   
  do
  { display.setCursor(x, y);
    //display.print(TextBsp);
    display.print("ein Blabla ");
    //display.setFont(u8g2_font_helvB18_tf);         // Set the font to Helvetica Bold 18
    //display.setCursor(10, 10);                     // Set the cursor to the top left corner
    //display.print("Hello, world!");                // Print 
    //display.setFont(u8g2_font_helvB12_tf);         // Set the font to Helvetica Bold 12
    //display.setCursor(20, 20);                     // Set the cursor to the second line
    //display.print("Text in Setup()");              // Print 
  }
  while (display.nextPage());
  
  display.hibernate();                               // original Zingg, what for?

  Serial.println(TextBsp);                        
  */
}

void loop() 
{ //
  display.setRotation(1);
  display.fillScreen(GxEPD_WHITE);
  display.setTextColor(GxEPD_BLACK);
  display.setFont(&FreeMonoBold9pt7b);
  display.setCursor(10, 30);
  display.println("Hello World");
  display.setFont(&FreeMonoBold12pt7b);
  display.printf("Loop Anzahl %d",cnt);
  cnt++;
  display.update();
  delay(10000);
  //
  
  /*
  display.setRotation(1);
  display.fillScreen(GxEPD_WHITE);
  display.setTextColor(GxEPD_BLACK);
  display.setFont(&FreeMonoBold9pt7b);
  //display.setFont(&FreeMonoBold12pt7b);
  display.setCursor(10, 20);
  display.println("Text in Loop()");
  display.display();                                 // Update the display  
  //display.update();                                // nur GxEPD alt
  Serial.print("im Loop()...");
  delay(10000);                                       // Wait before clearing the display and drawing again
  */
};
Loading
epaper-2in9