/*
    CONNECTION from ESP32 to a composite video TV

    A) voltageDivider = false; B) voltageDivider = true

       55 shades                  179 shades

    ESP32        TV            ESP32                       TV     
    -----+                     -----+    ____ 100 ohm
        G|-                        G|---|____|+          
    pin25|--------- Comp       pin25|---|____|+--------- Comp    
    pin26|-                    pin26|-        220 ohm
         |                          |
         |                          |
    -----+                     -----+                              

    Connect pin 25 or 26
  
    C) R–2R resistor ladder; D) unequal rungs ladder

       55 shades                  up to 254 shades?

    ESP32        TV           ESP32                       TV
    -----+                    -----+    ____ 
        G|-+_____                 G|---|____|
    pinA0|-| R2R |- Comp      pinA0|---|____|+--------- Comp
    pinA1|-|     |            pinA1|---|____|
    pinA2|-|     |              ...|
      ...|-|_____|                 |
    -----+                    -----+

    Connect pins of your choice (A0...A8=any pins).
    Custom ladders can be used by tweaking colorMinValue and colorMaxValue
*/

#define COMPOSITE_VIDEO_PIN 25 // hardcoded, this pin has a HW DAC

#include <ESP32Lib.h>
#include <Ressources/Font6x8.h>
#include <Ressources/CodePage437_8x8.h>
//#include <Ressources/CodePage437_8x14.h>
//#include <Ressources/CodePage437_8x16.h>
//#include <Ressources/CodePage437_8x19.h>
//#include <Ressources/CodePage437_9x16.h>
#define FONT CodePage437_8x8

//pin configuration for DAC
const int outputPin = COMPOSITE_VIDEO_PIN;
// A) B)
CompositeGrayDAC videodisplay;
// C) D)
//CompositeGrayLadder videodisplay;

class Display {

public:
  // view(able) size
  int vw = 350;
  int vh = 235;
  
  // offsets (to fix overscan, depends on your display)
  int ow = 10;
  int oh = 26;
  
  // display size
  int w = 365; // vw + 2*ow
  int h = 275; // vh + 2*oh

  int font_w = FONT.charWidth;
  int font_h = FONT.charHeight + 1;
  
  int cols = vw / font_w;
  int rows = vh / font_h;
  
  void setup()
  {
    // Composite video init, see options in the header ^^^
    // A)
    //videodisplay.init(CompMode::MODEPAL288P, 25, false);
    // B)
    videodisplay.init(CompMode::MODEPAL288P, 25, true);
    videodisplay.clear();

    videodisplay.xres = w;
    videodisplay.yres = h;
    
    videodisplay.setFont(FONT);

    /*/ view area test
    videodisplay.rect(ow, oh, vw, vh, 50);
    /**/
  }

  void scroll(int d) {
    videodisplay.scroll(d, 0);
  }

  void fill_rect(int x, int y, int w, int h, int color) {
    videodisplay.fillRect(x, y, w, h, color);
  }

  void pixel(int x, int y, int color) {
    videodisplay.dotFast(x, y, color);
  }

  void print_character(int col, int row, int fg_color, int bg_color, char character) {
    int x = ow + col*font_w;
    int y = oh + row*font_h;
    videodisplay.setCursor(x, y);
    videodisplay.setTextColor(fg_color, bg_color);
    videodisplay.print(character);
  }

  int get_display_width() { return w; }
  int get_display_height() { return h; }
  int get_view_width() { return vw; }
  int get_view_height() { return vh; }
  int get_view_width_offset() { return ow; }
  int get_view_height_offset() { return oh; }

};
