/*
 * in C:\Dir-ProgPortable\Arduino\Arduino-1.8.19\portable\sketchbook\sketches\00_Test_Box\Sketches\
 * 02_OLED-Test\OLED_1-3_Test\D1mini_OLED_u8g2
 * getestet für 128x64 OLED 1,3" + 0,96"
 * 
	https://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/
  Beschreibung:
  https://github.com/olikraus/u8g2/wiki/u8g2reference#drawrframe
  Schriften Fonts
  https://github.com/olikraus/u8g2/wiki/fntlistall

  void initOLED()
  void showLogo()
  void showStartText()
  void showSensor(char* text, float val1, float val2)
  void showTextOLED(char* lines[], size_t nr_of_lines)

  Beispiel für Aufruf:
    snprintf(line1, sizeof(line1), "Hallo");  // Überschreibt line1 mit "Hallo", Zeichenlänge wird geprüft
    showTextOLED(lines, 3);       // line1, line2, line3 werden angezeigt. line4, line5 werden ignoriert
  
*/

// verhindert doppelte Verwendung
#ifndef MY_OLED_U8G2_H      // if not define then define .... endif
#define MY_OLED_U8G2_H

//#include <SPI.h>
#include <Wire.h>

#include <Arduino.h>
#include <U8g2lib.h>
//#include <Fonts/FreeSerif9pt7b.h>

#include "myLogo_u8g2.h"

#define NUMFLAKES     10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16

// OLED-Display setting
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)

//U8G2_SH1106_128X64_NONAME_F_HW_I2C      myOLED(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);		// für 1,3" OLED
U8G2_SSD1306_128X64_NONAME_F_HW_I2C   myOLED(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);		// für 0,96" OLED

#define DISPLAY_ADR1  0x3C		// oder 0x3D

// define Example of lines
char line1[30] = "Hello Line 1";  // maximale Zeilen erstellen, 30 Zeichen
char line2[30] = "Hello Line 2";
char line3[30] = "Hello Line 3";
char line4[30] = "Hello Line 4";
char line5[30] = "Hello Line 5";

// Ein Array mit den Zeilen erstellen.
char* lines[] = {line1, line2, line3, line4, line5};

int displayDelay = 6000;		// 6 sec

//********* OLED initialisieren *********************
void initOLED() {
  if (!myOLED.begin()) {
    Serial.println(F("OLED allocation failed"));
    //for(;;); // Don't proceed, loop forever
  }
  myOLED.enableUTF8Print();    // enable UTF8 support for the Arduino print() function
  myOLED.setPowerSave(0);      // display ein
  //myOLED.setDrawColor(2);   // 0 = schwarz, 1 = weiß, 2 = invert
  //myOLED.setBitmapMode(1); // 1 Hintergrund transparent, 0 = nicht transparent
}

//********* Start Text mit Rahmen auf OLED anzeigen *********************
void showStartText() {
  myOLED.clearBuffer();
  myOLED.drawRFrame(0, 0, 128, 64, 4);
  myOLED.enableUTF8Print();    // enable UTF8 support for the Arduino print() function

  myOLED.setFont(u8g2_font_timB12_tf);   // Breite x Hoehe
  //myOLED.setFont(u8g2_font_unifont_tf);
  //myOLED.setFont(u8g2_font_6x13_me);   // Breite x Hoehe
  //myOLED.setFont(u8g2_font_ncenB14_tr);
  //myOLED.setFont(u8g2_font_ncenB08_tr);  // choose a suitable font
  
  //myOLED.setFont(u8g2_font_unifont_tf); // Schriftart mit Umlauten
  myOLED.drawStr(4, 14, "NTP_Date_Time_Oled"); // write text to the internal memory
  myOLED.drawStr(4, 29, "Datum + Uhrzeit"); // write text to the internal memory
  //myOLED.drawStr(10, 44, "  G\xFCnther"); // write text to the internal memory ü - funktioniert
  //myOLED.drawUTF8(10, 44, "  Günther"); // write text to the internal memory ü - funktioniert
  myOLED.drawUTF8(20, 44, "mit OLED"); // write text to the internal memory ü - funktioniert
  myOLED.drawStr(25, 59,  "Juni 2025"); // write text to the internal memory

  myOLED.sendBuffer();
}


void showLogo() {
  myOLED.clearBuffer();

  // Draw bitmap on the screen
  //myOLED.drawXBM(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, image_data_htllogo_2022_black_v4);
  //myOLED.drawXBM(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, image_data_SmartHome);
  
  myOLED.drawXBM(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, image_invdata_SmartHome);
  myOLED.sendBuffer();
}

void showSensor(char* text, float val1, float val2) {
  myOLED.clearBuffer();
  myOLED.drawRFrame(0, 0, 128, 64, 4);
  //myOLED.setFont(u8g2_font_6x13_me);   // Breite x Hoehe
  myOLED.setFont(u8g2_font_ncenB14_tr);

  String strLine = "";

  strLine = String( text );
  myOLED.drawStr(6, 18, strLine.c_str()); // write something to the internal memory

  strLine = String( "T:   " + String(val1, 1) + "   C" ); // \xB0 ist ASCII-Code für °
  myOLED.drawStr(10, 38, strLine.c_str()); // write something to the internal memory

  strLine = String( "H:  " + String(val2, 0) + " %rel" );
  myOLED.drawStr(10, 58, strLine.c_str()); // write something to the internal memory

  myOLED.setFont(u8g2_font_helvB14_tf);  // Alternative Fonts testen
  myOLED.drawUTF8(88, 38, "°");         //  Sonderzeichen nicht bei jeder Schrift verfügbar

  myOLED.sendBuffer();
}


void showTextOLED(char* lines[], size_t nr_of_lines) {
  myOLED.clearBuffer();
  myOLED.drawRFrame(0, 0, 128, 64, 4);
  int xPos = 3;
  int yPos = 1;
  int line_height = 0;

  switch (nr_of_lines) {
    case 1:
      myOLED.setFont(u8g2_font_ncenB14_tr);
      xPos = 3; yPos = 10;
      line_height = 20;
      break;
    case 2:
      myOLED.setFont(u8g2_font_ncenB14_tr);
      xPos = 3; yPos = 5;
      line_height = 20;
      break;
    case 3:
      myOLED.setFont(u8g2_font_ncenB14_tr);
      xPos = 3; yPos = -1;
      line_height = 20;
      break;
    case 4:
      myOLED.setFont(u8g2_font_timB12_tf);
      xPos = 5; yPos = 3;
      line_height = 14;
      break;
    case 5:
      myOLED.setFont(u8g2_font_ncenB08_tr);
       xPos = 5; yPos = 3;
     line_height = 11;
      break;
    default:
      break;
  }

  for (int i = 0; i < nr_of_lines; i++) {
    myOLED.drawStr(xPos, ((i + 1)*line_height + yPos), lines[i] );  // write text to the internal memory
  }
  myOLED.sendBuffer();
}  
//********** showTextOLED
#endif  // MY_OLED_U8G2_H
