
#include <Arduino.h>
#include <U8g2lib.h>

#include <SPI.h>


// JDN for 1.3 SPI 128x64 display based on SH1106 display chip

// http://jensd.dk/edu/doc/esp32/mono-oleds.html
// https://github.com/olikraus/u8g2/wiki/u8g2reference


U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI   u8g2  (U8G2_R0, 5, 22, 17);

void setup()
{
  u8g2.begin();
}


/* IDEA IS YOU CAN SAVE TIME AND MANY CALLS AND JUSTR DEFINE IMAGE DRAWING
 *  
 *
 *
 * First element - value 10 - below is number of elements to be drawn 
 * in the array
 * The rest is description of each element
 * Look down in switch-case to see
 * format 
 *   nr-1 :  element type: 1 drawLine, 2: drawCicle,.. See case-switch below
 *   nr-2:   color ;  1: color, 2 black
 *   nr-3 .. : the parameters to call as specified in  https://github.com/olikraus/u8g2/wiki/u8g2reference
 *   
 */

/* n = 0 rad, neg angle */
int fig1[] = {10,    
              1, 1, 10, 5, 10, 15,                // drawLine(10,5  ,  10,15)
              1, 1, 10, 5, 15, 5,                 // drawLine(10,5  ,  15,15)
              1, 1, 10, 5, 5, 5,                  // drawLine(10,5  ,  5, 5)
              1, 1, 10, 15, 5, 25,                // drawLine(10,15  ,  5,25)
              1, 1, 10, 15, 15, 25,               // drawLine(10,15  ,  11,15)
              2, 1, 64, 32, 20, U8G2_DRAW_ALL,    // drawCirle (64,32,20,U8G_DRAW_ALL)
              3, 1, 64, 32, 30, 10, U8G2_DRAW_ALL,// drawEllipse(64,32,30,20,U8G_DRAW_ALL)
              4, 1, 64, 32, 15, 10,               // drawFrame(64,32,15,10)
              5, 1, 0, 0, 8, 8,                   // drawBox(0,0,8,8)
              6, 1, 10, 10,   20, 20,  10, 40,    // drawTriangle(10, 10,   20, 20,  10, 40)
              7, 1, 127, 63                       // drawPixel(127,63)
             };
/*
 * first parameter is pointer to elemn array
 * x and y is relative positioning so you can locate
 * your drawing relative on the display
 */
void drawElements(int *a, int x, int y)
{
  int nr = *a;
  a++;
  u8g2.setDrawColor(*(a + 1));
  for (int i = 0; i < nr; i++) {
    switch ( *a) {
      case 1: u8g2.drawLine(*(a + 2) + x, *(a + 3) + y, *(a + 4) + x, *(a + 5) + y);
        a += 6; // advance to next element drawLine fills 6
        break;
      case 2: u8g2.drawCircle( *(a + 2) + x , *(a + 3) + y, *(a + 4), *(a + 5)); // U8G2_DRAW_ALL);
        a += 6;
        break;
      case 3: u8g2.drawEllipse(*(a + 2) + x, *(a + 3) + y, *(a + 4), *(a + 5) , *(a + 6) );
        a += 7;
      case 4: u8g2.drawFrame(*(a + 2) + x, *(a + 3) + y, *(a + 4), *(a + 5) );
        a += 6;
        break;
      case 5: u8g2.drawBox(*(a + 2) + x, *(a + 3) + y, *(a + 4), *(a + 5) );
        a += 6;
        break;
      case 6: u8g2.drawTriangle(*(a + 2) + x, *(a + 3) + y,  *(a + 4) + x, *(a + 5) + y, *(a + 6) + x, *(a + 7) + y);
        a += 8;
        break;
      case 7: u8g2.drawPixel(*(a + 2) + x, *(a + 3));
        a += 4;
        break;
      default:
        return;
    }
  }
}

void loop()
{
  u8g2.clearBuffer();
 
  drawElements(fig1, 0, 4);

  u8g2.sendBuffer();

  delay(1000);

}
