#include "my_oled.h"
#include <SPI.h>
#include <Adafruit_GFX.h>

static const char* tag = "oled";
Adafruit_SSD1306 *oled;

void Init_OLED(uint8_t width, uint8_t height, uint8_t mosiPin, uint8_t sckPin, uint8_t dcPin, uint8_t rstPin, uint8_t csPin)
{
  oled = new Adafruit_SSD1306(width, height, mosiPin, sckPin, dcPin, rstPin, csPin);

  if(!oled->begin(SSD1306_SWITCHCAPVCC)) {
    ESP_LOGE(tag, "SSD1306 allocation failed");
    for(;;); // Don't proceed, loop forever
  }

  oled_ShowInfo();
}

void oled_ShowInfo(void){
 //if(sysProps.oledEnabled) {
    oled->display();
    vTaskDelay(2000); // Pause for 2 seconds

    // Clear the buffer
    oled->clearDisplay();

    // Draw a single pixel in white
    oled->drawPixel(10, 10, SSD1306_WHITE);

    // Show the display buffer on the screen. You MUST call display() after
    // drawing commands to make them visible on screen!
    oled->display();
    vTaskDelay(2000);
    // display.display() is NOT necessary after every single drawing command,
    // unless that's what you want...rather, you can batch up a bunch of
    // drawing operations and then update the screen all at once by calling
    // display.display(). These examples demonstrate both approaches...

    //testdrawline(*oled);      // Draw many lines

    //testdrawrect(*oled);      // Draw rectangles (outlines)

    testdrawline(oled);      // Draw many lines

    testdrawrect(oled);      // Draw rectangles (outlines)
  //}
}

void testdrawline(Adafruit_SSD1306* display) {
  //if(sysProps.oledEnabled) {
    int16_t i;

    display->clearDisplay(); // Clear display buffer

    for(i=0; i<display->width(); i+=4) {
      display->drawLine(0, 0, i, display->height()-1, SSD1306_WHITE);
      display->display(); // Update screen with each newly-drawn line
      vTaskDelay(1);
    }
    for(i=0; i<display->height(); i+=4) {
      display->drawLine(0, 0, display->width()-1, i, SSD1306_WHITE);
      display->display();
      vTaskDelay(1);
    }
    vTaskDelay(250);

    display->clearDisplay();

    for(i=0; i<display->width(); i+=4) {
      display->drawLine(0, display->height()-1, i, 0, SSD1306_WHITE);
      display->display();
      vTaskDelay(1);
    }
    for(i=display->height()-1; i>=0; i-=4) {
      display->drawLine(0, display->height()-1, display->width()-1, i, SSD1306_WHITE);
      display->display();
      vTaskDelay(1);
    }
    vTaskDelay(250);

    display->clearDisplay();

    for(i=display->width()-1; i>=0; i-=4) {
      display->drawLine(display->width()-1, display->height()-1, i, 0, SSD1306_WHITE);
      display->display();
      vTaskDelay(1);
    }
    for(i=display->height()-1; i>=0; i-=4) {
      display->drawLine(display->width()-1, display->height()-1, 0, i, SSD1306_WHITE);
      display->display();
      vTaskDelay(1);
    }
    vTaskDelay(250);

    display->clearDisplay();

    for(i=0; i<display->height(); i+=4) {
      display->drawLine(display->width()-1, 0, 0, i, SSD1306_WHITE);
      display->display();
      vTaskDelay(1);
    }
    for(i=0; i<display->width(); i+=4) {
      display->drawLine(display->width()-1, 0, i, display->height()-1, SSD1306_WHITE);
      display->display();
      vTaskDelay(1);
    }

    vTaskDelay(2000); // Pause for 2 seconds
  //}
}

void testdrawrect(Adafruit_SSD1306* display) {
  //if(sysProps.oledEnabled) {
    display->clearDisplay();

    for(int16_t i=0; i<display->height()/2; i+=2) {
      display->drawRect(i, i, display->width()-2*i, display->height()-2*i, SSD1306_WHITE);
      display->display(); // Update screen with each newly-drawn rectangle
      vTaskDelay(1);
    }

    vTaskDelay(2000);
  //}
}