     } // end switch
 
     last_state = state;
-  }
-  /*************************************/
-  /*   Processing depending of state   */
-  /*************************************/
+}
+/*************************************/
+/*   Processing depending of state   */
+/*************************************/
 #ifndef TVOUT_SCREENS
-  if (state == STATE_SCREEN_SAVER || state == STATE_SCREEN_SAVER_LITE) {
+if (state == STATE_SCREEN_SAVER || state == STATE_SCREEN_SAVER_LITE) {
 
 
 #ifdef USE_DIVERSITY


void loop()
{
  /*******************/
  /*   Mode Select   */
  /*******************/
  uint8_t in_menu;
  uint8_t in_menu_time_out;
  if (digitalRead(buttonMode) == LOW) // key pressed ?
  {
    //Serial.println("kei");

    time_screen_saver = 0;
    beep(50); // beep & debounce
    delay(KEY_DEBOUNCE / 2); // debounce
    beep(50); // beep & debounce
    delay(KEY_DEBOUNCE / 2); // debounce

    uint8_t press_time = 0;
    // on entry wait for release
    while (digitalRead(buttonMode) == LOW && press_time < 10)
    {
      delay(100);
      press_time++;
    }


    OSDParams[0] = 0; //this is main menu
    OSDParams[1] = 3; //By default goes over manual
    SendToOSD(); //UPDATE OSD

#define MAX_MENU 7
#define MENU_Y_SIZE 15

    char menu_id = 2; //state_last_used-1;
    // Show Mode Screen
    if (state == STATE_SEEK_FOUND)
    {
      state = STATE_SEEK;
    }
    in_menu = 1;
    in_menu_time_out = 50; // 20x 100ms = 5 seconds
    /*
      Enter Mode menu
      Show current mode
      Change mode by MODE key
      Any Mode will refresh screen
      If not MODE changes in 2 seconds, it uses last used mode
    */
    do
    {

      if (press_time >= 10) // if menu held for 1 second invoke quick save.
      {
        // user held the mode button and wants to quick save.
        in_menu = 0; // EXIT
        state = STATE_SAVE;
        OSDParams[0] = -2; //this is save
        SendToOSD(); //UPDATE OSD
        break;
      }
 
      switch (menu_id)
      {
        case 0: // AUTO MODE
          state = STATE_SEEK;
          force_seek = 1;
          seek_found = 0;
          break;
        case 1: // Band Scanner
          state = STATE_SCAN;
          scan_start = 1;
          break;
        case 2: // manual mode
          state = STATE_MANUAL;
          break;
#ifdef USE_DIVERSITY
        case 3: // Diversity
          if (isDiversity()) {
            state = STATE_DIVERSITY;
          }
          else {
            menu_id++;
            state = STATE_SETUP_MENU;
          }
          break;
#else
        case 3: // Skip
          menu_id++;
#endif
        case 4: // Favorites Menu       //gc9n
          state = STATE_FAVORITE;       //gc9n
          break;                        //gc9n
        case 5: // Setup Menu           //gc9n
          state = STATE_SETUP_MENU;     //gc9n
          break;                        //gc9n
        case 6:// Beeps enable/disable  //gc9n
          break;                        //gc9n
        case 7://Vres modelo            //gc9n
           state = STATE_SCREEN_SAVER_LITE;       //gc9n
           //drawScreen.updateScreenSaver(rssi);
          break;          
      } // end switch

      // draw mode select screen
      ////Serial.println (state);
      if (menu_id > 4)
      {
        drawScreen.mainMenuSecondPage(menu_id - 5, settings_OSD);
      }
      else
      {
        drawScreen.mainMenu(menu_id);

      }


      while (digitalRead(buttonMode) == LOW || digitalRead(buttonUp) == LOW || digitalRead(buttonDown) == LOW  || FSButtonDirection() == 1 || FSButtonDirection() == 2)
      {
        // wait for MODE release
        in_menu_time_out = 50;
      }
      while (--in_menu_time_out && ((digitalRead(buttonMode) == HIGH) && (digitalRead(buttonUp) == HIGH) && (digitalRead(buttonDown) == HIGH) &&  FSButtonDirection() == 0  )) // wait for next key press or time out
      {
        delay(100); // timeout delay
      }
      if (in_menu_time_out == 0 || digitalRead(buttonMode) == LOW)
      {
        if (menu_id == 6)
        {
          settings_OSD = !settings_OSD;
          EEPROM.write(EEPROM_ADR_OSD, settings_OSD);
          if (settings_OSD == false)
          { OSDParams[0] = -99; // CLEAR OSD
            SendToOSD(); //UPDATE OSD
          }
          else
          { OSDParams[0] = 0; // ENABLE OSD AND GO OVER MAIN MENU OSD SELECTION
            OSDParams[1] = 7; // OSD
            SendToOSD(); //UPDATE OSD
          }
        }
        else
        {

          // Serial.read();
          if (digitalRead(buttonMode) != LOW) {
            state = state_last_used; // exit to last state on timeout.
          }
          in_menu = 0; // EXIT
          beep(KEY_DEBOUNCE / 2); // beep & debounce
          delay(50); // debounce
          beep(KEY_DEBOUNCE / 2); // beep & debounce
          delay(50); // debounce
        }
      }
      else // no timeout, must be keypressed
      {
        /*********************/
        /*   Menu handler   */
        /*********************/

        if (digitalRead(buttonUp) == LOW  || FS_BUTTON_DIR == 1) {
          menu_id--;
#ifdef USE_DIVERSITY
          if (!isDiversity() && menu_id == 3) { // make sure we back up two menu slots.
            menu_id--;
          }
#endif
        }
        else if (digitalRead(buttonDown) == LOW   || FS_BUTTON_DIR == 2) {
          menu_id++;
        }

        if (menu_id > MAX_MENU)
        {
          menu_id = 0; // next state
        }
        if (menu_id < 0)
        {
          menu_id = MAX_MENU;
        }

        in_menu_time_out = 50;
        beep(50); // beep & debounce
        delay(KEY_DEBOUNCE); // debounce
        OSDParams[0] = 0; //this is main menu
        OSDParams[1] = menu_id + 1; //By defaut=lt goes over manual
        SendToOSD(); //UPDATE OSD
      }
    } while (in_menu);
    last_state = 255; // force redraw of current screen
    switch_count = 0;

  }
  else // key pressed
  { // reset debounce
    switch_count = 0;


  }
  /***********************/
  /*     Save buttom     */
  /***********************/
  // hardware save buttom support (if no display is used)
  if (digitalRead(buttonSave) == LOW)
  {
    state = STATE_SAVE;
  }
  /***************************************/
  /*   Draw screen if mode has changed   */
  /***************************************/

  if (force_menu_redraw || state != last_state)
  {
    force_menu_redraw = 0;
    /************************/
    /*   Main screen draw   */
    /************************/
    // changed state, clear an draw new screen

    // simple menu
    switch (state)
    {
      case STATE_SCAN: // Band Scanner
        state_last_used = state;
      case STATE_RSSI_SETUP: // RSSI setup
        // draw selected
        if (state == STATE_RSSI_SETUP)
        {
          // prepare new setup
          rssi_min_a = 50;
          rssi_max_a = 300; // set to max range
          rssi_setup_min_a = RSSI_MAX_VAL;
          rssi_setup_max_a = RSSI_MIN_VAL;
#ifdef USE_DIVERSITY
          rssi_min_b = 50;
          rssi_max_b = 300; // set to max range
          rssi_setup_min_b = RSSI_MAX_VAL;
          rssi_setup_max_b = RSSI_MIN_VAL;
#endif
          rssi_setup_run = RSSI_SETUP_RUN;
        }

        // trigger new scan from begin
        channel = CHANNEL_MIN;
        channelIndex = pgm_read_byte_near(channelList + channel);
        rssi_best = 0;
        scan_start = 1;
        drawScreen.bandScanMode(state);
        break;
      case STATE_SEEK: // seek mode
        rssi_seek_threshold = RSSI_SEEK_TRESHOLD;
        rssi_best = 0;
        force_seek = 1;
      case STATE_MANUAL: // manual mode
        if (state == STATE_MANUAL)
        {
          time_screen_saver = millis();
        }
        else if (state == STATE_SEEK)
        {
          time_screen_saver = 0; // dont show screen saver until we found a channel.
        }
        drawScreen.seekMode(state);

        // return user to their saved channel after bandscan
        if (state_last_used == STATE_SCAN || state_last_used == STATE_FAVORITE || last_state == STATE_RSSI_SETUP ) {
          channelIndex = EEPROM.read(EEPROM_ADR_TUNE);
        }
        state_last_used = state;
        break;
#ifdef USE_DIVERSITY
      case STATE_DIVERSITY:
        // diversity menu is below this is just a place holder.
        break;
#endif
      case STATE_SETUP_MENU:

        break;
      case STATE_SAVE:

        EEPROM.write(EEPROM_ADR_TUNE, channelIndex);
        EEPROM.write(EEPROM_ADR_STATE, state_last_used);
        EEPROM.write(EEPROM_ADR_BEEP, settings_beeps);
        EEPROM.write(EEPROM_ADR_ORDERBY, settings_orderby_channel);
        // save call sign
        for (uint8_t i = 0; i < sizeof(call_sign); i++) {
          EEPROM.write(EEPROM_ADR_CALLSIGN + i, call_sign[i]);
        }
#ifdef USE_DIVERSITY
        EEPROM.write(EEPROM_ADR_DIVERSITY, diversity_mode);
#endif

        ///////////////////////FAVORITIES SAVE Gc9n
        if (last_state != STATE_SETUP_MENU  && state_last_used != STATE_FAVORITE ) // if you didnt came from menu setup  save favorite
        {
          if ( EEPROM.read(EEPROM_ADR_TUNE_FAV[9]) != 255) //ALL FAVS full gc9n
          {
            int lfav;
            lfav = EEPROM.read(EEPROM_ADR_TUNE_FAV_LAST);
            if (lfav == 9)
            {
              lfav = 0;
            }
            else
            {
              lfav = lfav + 1;
            }
            EEPROM.write(EEPROM_ADR_TUNE_FAV[lfav], 255); // rotate the favs if full
            EEPROM.write(EEPROM_ADR_TUNE_FAV_LAST, lfav);
          }
          for (int i = 0; i < 10; i++)
          {
            if ( EEPROM.read(EEPROM_ADR_TUNE_FAV[i]) == 255) //not used  gc9n
            {
              EEPROM.write(EEPROM_ADR_TUNE_FAV[i], channelIndex);
              EEPROM.write(EEPROM_ADR_TUNE_FAV_LAST, i);
              i = 12; //exit loop
            }
          }
          OSDParams[0] = -2; //this is save
          OSDParams[1] = 0; //By default goes over manual
          SendToOSD(); //UPDATE OSD
          drawScreen.save(state_last_used, channelIndex, pgm_read_word_near(channelFreqTable + channelIndex), call_sign, EEPROM.read(EEPROM_ADR_TUNE_FAV_LAST) + 1);
        }
        if (last_state == STATE_SETUP_MENU)
        { OSDParams[0] = -2; //this is save
          OSDParams[1] = 0; //By default goes over manual
          SendToOSD(); //UPDATE OSD
          drawScreen.save(state_last_used, channelIndex, pgm_read_word_near(channelFreqTable + channelIndex), call_sign, -99);
        }

        ///LONG PRESS IN FAVORITES WILL DELETE THE CURRENT FAVORITE CHANNEL
        if  (state_last_used == STATE_FAVORITE && last_state == 255)
        {  
          OSDParams[0] = -3; //this is delete
          OSDParams[1] = 0; //By default goes over manual
          SendToOSD(); //UPDATE OSD
          EEPROM.write(EEPROM_ADR_TUNE_FAV[lfavs], 255);
          drawScreen.FavDelete(   pgm_read_word_near(channelFreqTable + channelIndex), lfavs + 1);
          
          for (int i = 0; i < 10; i++) {
            temp_EEPROM_ADR_TUNE_FAV[i] = 255;    //empty temp
          }

          //--REORGANIZE FAVS  GC9n
          int MaxFav = 0;
          for (int i = 0; i < 10; i++)
          {

            if ( EEPROM.read(EEPROM_ADR_TUNE_FAV[i]) != 255) //not used  gc9n
            {
              FillTemp_Tune_fav(EEPROM.read(EEPROM_ADR_TUNE_FAV[i]));
              MaxFav++;
            }
          }


          for (int i = 0; i < 10; i++)
          {
            EEPROM.write(EEPROM_ADR_TUNE_FAV[i], temp_EEPROM_ADR_TUNE_FAV[i] );

          }


          //DELETED SUCCESFULLY AND GO TO FIRST FAV (IF EXISTS)
          //drawScreen.FavReorg(MaxFav);
          RefreshFav = false;
           delay(1000);
          EEPROM.write(EEPROM_ADR_TUNE_FAV_LAST, 0);
          channelIndex = EEPROM.read(EEPROM_ADR_TUNE_FAV[0]) ;
          lfavs = 0;
          channelIndex = EEPROM.read(EEPROM_ADR_TUNE_FAV[0]) ;
          drawScreen.FavSel(1);
          channel = channel_from_index(channelIndex);
          EEPROM.write(EEPROM_ADR_TUNE, channelIndex);


          //drawScreen.screenSaver(diversity_mode, pgm_read_byte_near(channelNames + channelIndex), pgm_read_word_near(channelFreqTable + channelIndex), call_sign);
        }
        ///END LONG PRESS IN FAVORITES WILL DELETE THE CURRENT FAVORITE CHANNEL
        for (uint8_t loop = 0; loop < 5; loop++)
        {
          beep(100); // beep
          delay(100);
        }
        delay(800);
        state = state_last_used; // return to saved function
        force_menu_redraw = 1; // we change the state twice, must force redraw of menu

    drawScreen.updateBandScanMode((state == STATE_RSSI_SETUP), channel, rssi, bestChannelName, bestChannelFrequency, rssi_setup_min_a, rssi_setup_max_a);

    // next channel
    if (channel < CHANNEL_MAX)    {
        channel++;
    }
    else    {
        channel = CHANNEL_MIN;
        if (state == STATE_RSSI_SETUP)      {
            if (!rssi_setup_run--)        {
                // setup done
                rssi_min_a = rssi_setup_min_a;
                rssi_max_a = rssi_setup_max_a;
                if (rssi_max_a < 125) { // user probably did not turn on the VTX during calibration
                    rssi_max_a = RSSI_MAX_VAL;
                }
                // save 16 bit
                EEPROM.write(EEPROM_ADR_RSSI_MIN_A_L, (rssi_min_a & 0xff));
                EEPROM.write(EEPROM_ADR_RSSI_MIN_A_H, (rssi_min_a >> 8));
                // save 16 bit
                EEPROM.write(EEPROM_ADR_RSSI_MAX_A_L, (rssi_max_a & 0xff));
                EEPROM.write(EEPROM_ADR_RSSI_MAX_A_H, (rssi_max_a >> 8));

#ifdef USE_DIVERSITY

                if (isDiversity()) { // only calibrate RSSI B when diversity is detected.
                    rssi_min_b = rssi_setup_min_b;
                    rssi_max_b = rssi_setup_max_b;
                    if (rssi_max_b < 125) { // user probably did not turn on the VTX during calibration
                        rssi_max_b = RSSI_MAX_VAL;
                    }
                    // save 16 bit
                    EEPROM.write(EEPROM_ADR_RSSI_MIN_B_L, (rssi_min_b & 0xff));
                    EEPROM.write(EEPROM_ADR_RSSI_MIN_B_H, (rssi_min_b >> 8));
                    // save 16 bit
                    EEPROM.write(EEPROM_ADR_RSSI_MAX_B_L, (rssi_max_b & 0xff));
                    EEPROM.write(EEPROM_ADR_RSSI_MAX_B_H, (rssi_max_b >> 8));
                }
#endif
                state = EEPROM.read(EEPROM_ADR_STATE);
                beep(1000);
            }
        }
    }
    // new scan possible by press scan
    FS_BUTTON_DIR = FSButtonDirection();
    if (digitalRead(buttonUp) == LOW ||  FS_BUTTON_DIR == 1) // force new full new scan    {
        beep(50); // beep & debounce
    delay(KEY_DEBOUNCE); // debounce
    last_state = 255; // force redraw by fake state change ;-)
    channel = CHANNEL_MIN;
    scan_start = 1;
    rssi_best = 0;
    FS_BUTTON_DIR = 0;

}
// update index after channel change
channelIndex = pgm_read_byte_near(channelList + channel);
}

/****************************/
/*      SETUP_MENU   */
/****************************/
if (state == STATE_SETUP_MENU)  {
    // simple menu
    char menu_id = 0;
    in_menu = 1;
