// u8g2.  exampel
//  핀 번호 10을 사용하면 키 전체가 먹지않는 현상이 발생함. 이유를 모름
// u8g2.begin(/*Select=*/ 7, /*Right/Next=*/ A2, /*Left/Prev=*/ A1, /*Up=*/ 9, /*Down=*/ 8, /*Home/Cancel=*/ 10 );
// - 팍샐루프를 사용하지 않아도 잘 출력되는 현상 시뮬레이터 버그 인가?
// u8g2.getMenuEvent() 값이 항상 0을 리턴함. 어떤 용도로 사용?
//  u8g2.userInterfaceSelectionList()함수는 u8g2가 메뉴를 그려주고 키입력을 받아 메뉴를 갱신해주는 블럭형 함수임.
#include <Arduino.h>
#include <U8g2lib.h>
int icounter;

// All Boards without Reset of the Display
U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);







void drawText(byte X, byte Y, const char *text) {
  u8g2.firstPage();
  do {
    u8g2.drawStr(X, Y, text);
  } while ( u8g2.nextPage() );
  delay(1000);
}

//-------------------------------------------------------------
// void select_menu(const char *menuTitle, const char *select_menu) {
//   current_selection = u8g2.userInterfaceSelectionList( menuTitle, current_selection,
//                       select_menu);
//   delay(15);         // debounce
// }


//==================================================================================== U8X8_PIN_NONE
void setup() {
  u8g2.begin(/*Select=*/ 7, /*Right/Next=*/ A2, /*Left/Prev=*/ A1, /*Up=*/ 9, /*Down=*/ 8, /*Home/Cancel=*/ 6 );
  u8g2.setFont(u8g2_font_6x12_tr);
  drawText(15, 40, "Welcome");
  Serial.begin(9600); Serial.println("start.");
}

//---------------------------------------------
void draw_RFID() {
  const char *subMenuList = "1.CCC\n2.DDD\n3.Exit";
  static int sub_menu_cur_sel = 1;
  bool exit = 0;
  Serial.println("draw_RFID");
  do {
    //const char *nameMenu = "RFID Menu";
    // select_menu(nameMenu, topMenuList);

    sub_menu_cur_sel = u8g2.userInterfaceSelectionList( "< RFID Menu >", sub_menu_cur_sel,
                       subMenuList);
    delay(15);         // debounce
    Serial.print("sub_menu_cur_sel="); Serial.print(sub_menu_cur_sel);
    Serial.print("  u8g2.getMenuEvent()="); Serial.println(u8g2.getMenuEvent());
    //if (u8g2.getMenuEvent() == U8X8_MSG_GPIO_MENU_SELECT) {
      switch ( sub_menu_cur_sel ) {
        case 0:            // home/cancel button
          exit = 1;  break;
        case 1:

          drawText(15, 15*4, "AAAA");
          break;
        case 3:
          exit = 1;
          break;
      };
    //}
  } while (exit != 1);

  }
//---------------------------------------------
void loop() {
  icounter +=1;
  Serial.println(icounter);
  static int top_menu_cur_sel = 1;
  // const char *nameMenu = "** Top Menu**";
  //select_menu(nameMenu, menu1List);

  const char *topMenuList =
  "NFC\n"
  "RFID\n"
  "Cirrocumulus\n"
  "Cirrostratus";

  top_menu_cur_sel = u8g2.userInterfaceSelectionList( "** Top Menu**", top_menu_cur_sel,
                     topMenuList);
  delay(15);         // debounce
  Serial.print("top_menu_cur_sel="); Serial.print(top_menu_cur_sel);
  Serial.print("  u8g2.getMenuEvent()="); Serial.println(u8g2.getMenuEvent());
  //if (u8g2.getMenuEvent() == U8X8_MSG_GPIO_MENU_SELECT) {
    switch (top_menu_cur_sel) {
      case   0 : break;
      case   1:
        drawText(15, 15, "Selected NFC.");
        break;
      case 2:
        draw_RFID();
        break;

    };
  //}
}
//--------------------------------------------