/*How to display images on arduino with 0.96" OLED
  subscribe for more arduino Tuorials and Projects
  Mission Critical
  https://www.youtube.com/channel/UCM6rbuieQBBLFsxszWA85AQ?sub_confirmation=1
  www.minov.in
 */


#include <Arduino.h>
#include <U8g2lib.h> // u8g2 library is used to draw graphics on the OLED display
#include <Wire.h> // library required for IIC communication

#define BTN 9 //User+Power Button


//#define OLED_RESET 4

//#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
//#define SCREEN_WIDTH 128 // OLED display width, in pixels
//#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//#define OLED_RESET -1   //   QT-PY / XIAO
//Adafruit_SH1107 display = Adafruit_SH1107(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


//Adafruit_SH110X display(OLED_RESET);
//Adafruit_SH1106G display(OLED_RESET);
//Adafruit_SSD1306 display(OLED_RESET);

//U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);   // OLED SSD1306 chino 
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the 128x64px OLED display

//U8G2_SH1107_64X128_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the


// 'bola8', 32x32px
const unsigned char bola8[] PROGMEM = {
	0x00, 0xf0, 0x0f, 0x00, 0x00, 0x0e, 0x70, 0x00, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0x00, 0x03, 
	0x20, 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x10, 
	0x04, 0x00, 0x00, 0x20, 0x02, 0xc0, 0x03, 0x40, 0x02, 0xf0, 0x0f, 0x40, 0x02, 0xf8, 0x1f, 0x40, 
	0x02, 0x3c, 0x3c, 0x40, 0x01, 0xdc, 0x3b, 0x80, 0x01, 0xde, 0x7b, 0x80, 0x01, 0x3e, 0x7c, 0x80, 
	0x01, 0xde, 0x7b, 0x80, 0x01, 0xde, 0x7b, 0x80, 0x01, 0xdc, 0x3b, 0x80, 0x02, 0x3c, 0x3c, 0x40, 
	0x02, 0xf8, 0x1f, 0x40, 0x02, 0xf0, 0x0f, 0x40, 0x02, 0xc0, 0x03, 0x40, 0x04, 0x00, 0x00, 0x20, 
	0x08, 0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x08, 0x20, 0x00, 0x00, 0x04, 
	0xc0, 0x00, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0x00, 0x0e, 0x70, 0x00, 0x00, 0xf0, 0x0f, 0x00
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 144)
const int bola8allArray_LEN = 1;
const unsigned char* bola8allArray[1] = {
	bola8
};


// 'mano_16x16', 16x16px
const unsigned char mano[] PROGMEM = {
	0x00, 0x00, 0x80, 0x01, 0x40, 0x02, 0x30, 0x02, 0x08, 0x01, 0x84, 0x7f, 0x03, 0x80, 0x01, 0x80, 
	0x79, 0x0a, 0x01, 0x04, 0x79, 0x04, 0x01, 0x02, 0x79, 0xf8, 0x07, 0x12, 0x18, 0x10, 0xe0, 0x1b
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 48)
const int mano_16x16allArray_LEN = 1;
const unsigned char* manoallArray[1] = {
	mano
};

unsigned char LineBuff[128];

/*
String Messages[]={"Es cierto",
                   "Esta decidido",
                   "Sin duda",
                   "Si, definitivamen",
                   "Puede confiar el",
                   "A mi modo de ver, sí",
                   "Lo mas probable",
                   "Perspectiva Buena",
                   "Si",
                   "Todo apunta que si",
                   
                   "El futuro es gris",
                   "Pregunta mas tarde",
                   "Mejor no decidir",
                   "No decidir ahora",
                   "Pregunte de nuevo",
                   
                   "No cuentes  ello",
                   "Mi respuesta es no",
                   "Mis fuentes dicen no",
                   "no  tan buenas",
                   "Muy dudoso",
                   };

*/

void setup() 
{
  
  pinMode(BTN,INPUT_PULLUP);
  
  begin();
  clear();
  splashscreen();
  delay(3000);
  clear();
  //u8g2.drawStr(2,0,"Magic 8 Ball");
	u8g2.clearDisplay();
  delay(1000);
}

void splashscreen(void){
  u8g2.clear();
  u8g2.setFont(u8g2_font_ncenR08_tr); //  u8g2.setFont(u8g2_font_ncenB14_tr); 
  u8g2.drawStr(30,20,"Magic 8 Ball");
  u8g2.drawStr(30,30,"v1.0");
  u8g2.drawStr(30,40,"sjm4306");
  u8g2.drawStr(30,55,"2020");
  u8g2.sendBuffer(); // transfer internal memory to the display
}

void tile_animate(int row,int x,bool j){
  unsigned char c=0;
  //u8g2.clearLine(row);
    for(int i=0;i<24;i++){
      if(i>=x%8&&c<16){
       // if(j==1){
       //   LineBuff[i]=Hand_1[c];
       // }else{
        //  u8g2.writeBufferXBM[i]=mano[c];
        u8g2.setDrawColor(1);
        u8g2.drawXBMP(50+(i*2), 40, 16, 16, mano);
        u8g2.sendBuffer(); // transfer internal memory to the display
        //u8g2.clearBuffer();
        //u8g2.setDrawColor(0);
        //u8g2.drawXBMP(50+(i*2), 40, 16, 16, mano);
        //u8g2.sendBuffer(); // transfer internal memory to the display
        }
        c++;
      //}
    }
   //u8g2.drawTile(x/8,row,3,LineBuff);
   // for(int i=0;i<24;i++){LineBuff[i]=0;}
}

void printchar(int x, char ASCII){
  for(int i=0;i<5;i++){
  //  LineBuff[x+i]=pgm_read_byte_near(FONT5x7+5*(ASCII-32)+i);
  }
}

void writeString(int x, int y, String str){
  bool chr=0;
  int t=0;
  unsigned char len = str.length();
  unsigned char p;
  while(len-20*t>0){
    for(int i=0;i<20;i++){ //must make sure str is less than 20 characters long
      p=str.charAt(i+20*t);
      if(p==0){i=20;}else{printchar(x+i*(chr+5), p);}
      chr=1;
    }
    u8g2.drawTile(0,y+t,16,LineBuff);
    u8g2.sendBuffer(); // transfer internal memory to the display
    for(int i=0;i<128;i++){LineBuff[i]=0;}
    t++;
    //Serial.println(t);
  }
  //Serial.println("A");
}

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


void clear(void){
  u8g2.clear();
}

void drawTile(int a,int b,int c,int d){
  u8g2.drawTile(a,b,c,d);
}

void drawString(int a,int b,int c){
  u8g2.drawStr(a,b,c);
}



/*
void loop() 
{
  u8g2.drawXBMP(0, 0, 32, 32, bola8);
	u8g2.drawXBMP(40, 40, 56, 56, mano);
	
  u8g2.sendBuffer(); // transfer internal memory to the display
	}
*/

void loop() { // put your main code here, to run repeatedly:

  bool flash=0;
  bool dir=0;
  unsigned int cnt=20;
  while(digitalRead(BTN)){   //Spin Lock, waiting for button press
    if(cnt<10){
      cnt++;
    }else{
      cnt=0;
      flash=1;
      if(flash==1){
        u8g2.drawStr(45,15,"Pulsa el Boton");
        u8g2.drawStr(45,30,"  Para Agitar ");
        u8g2.sendBuffer(); // transfer internal memory to the display
      }else{
        u8g2.clearBuffer();
        u8g2.drawStr(45,10,"               ");
        u8g2.drawStr(45,25,"               ");
        u8g2.sendBuffer(); // transfer internal memory to the display
      }
      u8g2.drawXBMP(0, 0+(cnt*2), 32, 32, bola8);
      u8g2.sendBuffer(); // transfer internal memory to the display
      
    }

    unsigned char t=0;
    if(flash==1){t=50+cnt;}else{t=60-cnt;}
    tile_animate(6,t,0);
    tile_animate(7,t,1);
    
    delay(50);

    /*
    if(sleep_flg==1){
      attachInterrupt(0,pinInterrupt,LOW);
      set_sleep_mode(SLEEP_MODE_PWR_DOWN);
      setPowerSave(1);
      digitalWrite(OP,LOW);
      for(int i=0;i<18;i++){
        if(i!=2){digitalWrite(i,LOW);pinMode(i,INPUT);}
      }
      //sleep_bod_disable();
      wdt_disable();  //disable watchdog
      ADCSRA &= ~(1<<7);   //ADEN=0, turn off adc
      PRR=0b01101111; //power down individual peripherals to lower current consumption
      MCUCR |= (3<<5); //set both BODS and BODSE at the same time
      MCUCR = (MCUCR & ~(1<<5))|(1<<6); //then set BODS bit and clear BODSE bit at the same time
      sleep_enable();
      sleep_mode();
      sleep_disable();
      PRR=0b00000000;
      setPowerSave(0);
      sleep_flg=0;
      //Serial.println("Awake!");
      pinMode(SPKR,OUTPUT);
      begin();
      drawString(2,0,"Magic 8 Ball");
      delay(500);
    }
    */
  }

  

  u8g2.clearDisplay();
  

  unsigned long c=0;
  unsigned char randNumber=0;
  while(!digitalRead(BTN)){   //Count while button is held
    c++;
    //writeString(40,10,"Consultando");
    //writeString(40,25,"a los Espiritus");
    //u8g2.sendBuffer(); // transfer internal memory to the display
    u8g2.drawStr(45,15,"  Consultando");
    u8g2.drawStr(45,30,"a los Espiritus");
    u8g2.drawXBMP(0,c*5,32,32,bola8);
    
    u8g2.sendBuffer(); // transfer internal memory to the display

    u8g2.clearBuffer();
    
    if(c>3){c=0;}
    //beep(3000,25);
    delay(100);
  }
  //for(int i=1;i<8;i++){
    //u8x8.clearLine(i);
  //}
  u8g2.clearBuffer();
  randomSeed(c+random());
  delay(1000);
  randNumber=random(20);  //20 messages total
  
  //u8x8.clearLine(6);
  //u8x8.clearLine(7);
  //writeString(0,7,Messages[randNumber]);

  //writeString(35,3,"Magic 8 Ball");
  //writeString(35,4,"Says:");
  u8g2.drawStr(45,30,"Magic 8 Ball");
  u8g2.drawStr(45,40,"Says:");

  drawTile(0,2,4,bola8);
  //drawTile(0,2,4,EightBall_0);
  //drawTile(0,3,4,EightBall_1);
  //drawTile(0,4,4,EightBall_2);
  //drawTile(0,5,4,EightBall_3);

  delay(5000);
  //u8g2.clearBuffer();
  //u8x8.clearLine(0);
}