#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "big_globe_coords.h"

#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)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// ********************************************************************

//globe variables
float tx, nx, p;
float ty, ny, py;
float rot, rotx, roty, rotz, rotxx, rotyy, rotzz, rotxxx, rotyyy, rotzzz;
int i; //0 to 360
int fl, scale; //focal length
int wireframe[250][2];
float vector;

int yangle;
int zangle;
float yrot;
float zrot;
int w;

float scalefactor = 0;

int originx = 64;
int originy = 32; //32

int fd = 0; //0=orthographic

void setup(void)
{
  Serial.begin(115200);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
 // Clear the buffer
  display.clearDisplay();

 
}

void draw_vertices()
{
display.drawPixel (rotxxx, rotyyy, WHITE);
}

void draw_wireframe(void)
{
 
display.drawLine(wireframe[0][0], wireframe[0][1], wireframe[1][0], wireframe[1][1], WHITE);
display.drawLine(wireframe[1][0], wireframe[1][1], wireframe[2][0], wireframe[2][1], WHITE);
display.drawLine(wireframe[2][0], wireframe[2][1], wireframe[3][0], wireframe[3][1], WHITE);
display.drawLine(wireframe[3][0], wireframe[3][1], wireframe[0][0], wireframe[0][1], WHITE);//greenland
display.drawLine(wireframe[4][0], wireframe[4][1], wireframe[5][0], wireframe[5][1], WHITE); 
display.drawLine(wireframe[5][0], wireframe[5][1], wireframe[6][0], wireframe[6][1], WHITE); 
display.drawLine(wireframe[6][0], wireframe[6][1], wireframe[4][0], wireframe[4][1], WHITE);//iceland
display.drawLine(wireframe[7][0], wireframe[7][1], wireframe[8][0], wireframe[8][1], WHITE);
display.drawLine(wireframe[8][0], wireframe[8][1], wireframe[9][0], wireframe[9][1], WHITE);
display.drawLine(wireframe[9][0], wireframe[9][1], wireframe[10][0], wireframe[10][1], WHITE);
display.drawLine(wireframe[10][0], wireframe[10][1], wireframe[11][0], wireframe[11][1], WHITE);
display.drawLine(wireframe[11][0], wireframe[11][1], wireframe[12][0], wireframe[12][1], WHITE);
display.drawLine(wireframe[12][0], wireframe[12][1], wireframe[13][0], wireframe[13][1], WHITE);
display.drawLine(wireframe[13][0], wireframe[13][1], wireframe[14][0], wireframe[14][1], WHITE);
display.drawLine(wireframe[14][0], wireframe[14][1], wireframe[15][0], wireframe[15][1], WHITE);
display.drawLine(wireframe[15][0], wireframe[15][1], wireframe[16][0], wireframe[16][1], WHITE);
display.drawLine(wireframe[16][0], wireframe[16][1], wireframe[17][0], wireframe[17][1], WHITE);
display.drawLine(wireframe[17][0], wireframe[17][1], wireframe[18][0], wireframe[18][1], WHITE);
display.drawLine(wireframe[18][0], wireframe[18][1], wireframe[19][0], wireframe[19][1], WHITE);
display.drawLine(wireframe[19][0], wireframe[19][1], wireframe[20][0], wireframe[20][1], WHITE);
display.drawLine(wireframe[20][0], wireframe[20][1], wireframe[21][0], wireframe[21][1], WHITE);
display.drawLine(wireframe[21][0], wireframe[21][1], wireframe[7][0], wireframe[7][1], WHITE);//Britain
display.drawLine(wireframe[22][0], wireframe[22][1], wireframe[23][0], wireframe[23][1], WHITE);
display.drawLine(wireframe[23][0], wireframe[23][1], wireframe[24][0], wireframe[24][1], WHITE);
display.drawLine(wireframe[24][0], wireframe[24][1], wireframe[25][0], wireframe[25][1], WHITE);
display.drawLine(wireframe[25][0], wireframe[25][1], wireframe[26][0], wireframe[26][1], WHITE);
display.drawLine(wireframe[26][0], wireframe[26][1], wireframe[22][0], wireframe[22][1], WHITE);//Ireland

 for (w = 27; w < 84; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[84][0], wireframe[84][1], wireframe[37][0], wireframe[27][1], WHITE);//Europe, Asia, Africa

 for (w = 85; w < 117; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[117][0], wireframe[117][1], wireframe[85][0], wireframe[85][1], WHITE);//Americas

 for (w = 118; w < 125; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[125][0], wireframe[125][1], wireframe[118][0], wireframe[118][1], WHITE);//Australia

 for (w = 126; w < 128; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[128][0], wireframe[128][1], wireframe[126][0], wireframe[126][1], WHITE);//Madagascar

 for (w = 129; w < 133; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[133][0], wireframe[133][1], wireframe[129][0], wireframe[129][1], WHITE);//Japan

 for (w = 134; w < 136; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[136][0], wireframe[136][1], wireframe[134][0], wireframe[134][1], WHITE);//Borneo

 for (w = 137; w < 140; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[140][0], wireframe[140][1], wireframe[137][0], wireframe[137][1], WHITE);//New Zealand

 for (w = 141; w < 152; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[152][0], wireframe[152][1], wireframe[141][0], wireframe[141][1], WHITE);//Antarctica

 for (w = 154; w < 156; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[156][0], wireframe[156][1], wireframe[154][0], wireframe[154][1], WHITE);//Cuba

 for (w = 157; w < 159; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[159][0], wireframe[159][1], wireframe[157][0], wireframe[157][1], WHITE);//Dominica 

 for (w = 161; w < 164; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE);
 }
display.drawLine(wireframe[164][0], wireframe[164][1], wireframe[161][0], wireframe[161][1], WHITE);//Dominica 

 for (w = 170; w < 189; w++){
 display.drawLine(wireframe[w][0], wireframe[w][1], wireframe[w+1][0], wireframe[w+1][1], WHITE); //ISS Orbital Path
 }

 display.drawCircle(originx,originy,65*scalefactor, WHITE);
// display.setFont(u8g2_font_u8glib_4_tr);
// display.drawFrame(0, 0, 128,64);  //setup fixed screen info and borders

 display.drawCircle(wireframe[169][0],wireframe[169][1],4, WHITE);
// display.drawDisc(wireframe[169][0],wireframe[169][1],2, WHITE);
/*
  if (wireframe[169][1] >32){
 display.setCursor(wireframe[169][0],wireframe[169][1]-6);
  }
  else {
   display.setCursor(wireframe[169][0],wireframe[169][1]+10);
  }
 display.print("ISS");
*/    
}

// ,,,,,,,,,,,,,,,,,,,,,,  END SETUP  ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,


void loop()
{

/*
//picture loop

 yangle = 270;//270 Can't quite get my head round why these numbers work but they do!
 zangle = 90;//90
 
 for (int angle = 0; angle <= 360; angle = angle + 3) {
  u8g2.firstPage();
  do {
   for (int i = 0; i < globe_cnt; i++) {

     
     rot = angle * 0.0174532; //0.0174532 = one degree
     yrot = yangle * 0.0174532; //0.0174532 = one degree
     zrot = zangle * 0.0174532; //0.0174532 = one degree
     
//rotateY
    rotz = globe_vertices[i][2] * cos(yrot) - globe_vertices[i][0] * sin(yrot);
    rotx = globe_vertices[i][2] * sin(yrot) + globe_vertices[i][0] * cos(yrot);
    roty = globe_vertices[i][1];
//rotateX
    rotyy = roty * cos(rot) - rotz * sin(rot);
    //rotzz = roty * sin(rot) + rotz * cos(rot);
    rotxx = rotx;
//rotateZ
    rotxxx = rotxx * cos(zrot) - rotyy * sin(zrot);
    rotyyy = rotxx * sin(zrot) + rotyy * cos(zrot);
    //rotzzz = rotzz;

//orthographic projection
    rotxxx = rotxxx * scalefactor + originx;
    rotyyy = rotyyy * scalefactor + originy;

//store new vertices values for wireframe drawing
    wireframe[i][0] = rotxxx;
    wireframe[i][1] = rotyyy;
    //wireframe[i][2] = rotzzz;
*/
     draw_vertices();
     
 //  }

   
   
    draw_wireframe();
  if (scalefactor < 0.4) scalefactor = scalefactor + 0.005;
 


 
//   }
   display.display();
   delay(10);
 }