 
 // Channels to sent to the SPI registers
 const uint16_t channelTable[] PROGMEM = {
-    // Channel 1 - 8
-    0x2A05,    0x299B,    0x2991,    0x2987,    0x291D,    0x2913,    0x2909,    0x289F,    // Band A
-    0x2903,    0x290C,    0x2916,    0x291F,    0x2989,    0x2992,    0x299C,    0x2A05,    // Band B
-    0x2895,    0x288B,    0x2881,    0x2817,    0x2A0F,    0x2A19,    0x2A83,    0x2A8D,    // Band E
-    0x2906,    0x2910,    0x291A,    0x2984,    0x298E,    0x2998,    0x2A02,    0x2A0C,    // Band F / Airwave
+  // Channel 1 - 8
+  0x2A05,    0x299B,    0x2991,    0x2987,    0x291D,    0x2913,    0x2909,    0x289F,    // Band A
+  0x2903,    0x290C,    0x2916,    0x291F,    0x2989,    0x2992,    0x299C,    0x2A05,    // Band B
+  0x2895,    0x288B,    0x2881,    0x2817,    0x2A0F,    0x2A19,    0x2A83,    0x2A8D,    // Band E
+  0x2906,    0x2910,    0x291A,    0x2984,    0x298E,    0x2998,    0x2A02,    0x2A0C,    // Band F / Airwave
 #ifdef USE_LBAND
-    0x281d ,   0x2890 ,   0x2902 ,   0x2915 ,   0x2987 ,   0x299a ,  0x2a0c ,    0x2a1f,    // Band C / Immersion Raceband
-    0x2609,    0x261C,    0x268E,    0x2701,    0x2713,    0x2786,    0x2798,    0x280B     // Band D / 5.3
+  0x281d ,   0x2890 ,   0x2902 ,   0x2915 ,   0x2987 ,   0x299a ,  0x2a0c ,    0x2a1f,    // Band C / Immersion Raceband
+  0x2609,    0x261C,    0x268E,    0x2701,    0x2713,    0x2786,    0x2798,    0x280B     // Band D / 5.3
 #else
-    0x281d ,   0x2890 ,   0x2902 ,   0x2915 ,   0x2987 ,   0x299a ,  0x2a0c ,    0x2a1f,    // Band C / Immersion Raceband
+  0x281d ,   0x2890 ,   0x2902 ,   0x2915 ,   0x2987 ,   0x299a ,  0x2a0c ,    0x2a1f,    // Band C / Immersion Raceband
 #endif
 };
 
 // Channels with their Mhz Values
 const uint16_t channelFreqTable[] PROGMEM = {
-    // Channel 1 - 8
-    5865, 5845, 5825, 5805, 5785, 5765, 5745, 5725, // Band A
-    5733, 5752, 5771, 5790, 5809, 5828, 5847, 5866, // Band B
-    5705, 5685, 5665, 5645, 5885, 5905, 5925, 5945, // Band E
-    5740, 5760, 5780, 5800, 5820, 5840, 5860, 5880, // Band F / Airwave
+  // Channel 1 - 8
+  5865, 5845, 5825, 5805, 5785, 5765, 5745, 5725, // Band A
+  5733, 5752, 5771, 5790, 5809, 5828, 5847, 5866, // Band B
+  5705, 5685, 5665, 5645, 5885, 5905, 5925, 5945, // Band E
+  5740, 5760, 5780, 5800, 5820, 5840, 5860, 5880, // Band F / Airwave
 #ifdef USE_LBAND
-    5658, 5695, 5732, 5769, 5806, 5843, 5880, 5917, // Band C / Immersion Raceband
-    5362, 5399, 5436, 5473, 5510, 5547, 5584, 5621  // Band D / 5.3
+  5658, 5695, 5732, 5769, 5806, 5843, 5880, 5917, // Band C / Immersion Raceband
+  5362, 5399, 5436, 5473, 5510, 5547, 5584, 5621  // Band D / 5.3
 #else
-    5658, 5695, 5732, 5769, 5806, 5843, 5880, 5917  // Band C / Immersion Raceband
+  5658, 5695, 5732, 5769, 5806, 5843, 5880, 5917  // Band C / Immersion Raceband
 #endif
 };
 
 // do coding as simple hex value to save memory.
 const uint8_t channelNames[] PROGMEM = {
-    0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, // Band A
-    0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, // Band B
-    0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, // Band E
-    0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, // Band F / Airwave
+  0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, // Band A
+  0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, // Band B
+  0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, // Band E
+  0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, // Band F / Airwave
 #ifdef USE_LBAND
-    0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, // Band C / Immersion Raceband
-    0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8  // BAND D / 5.3
+  0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, // Band C / Immersion Raceband
+  0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8  // BAND D / 5.3
 #else
-    0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8  // Band C / Immersion Raceband
+  0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8  // Band C / Immersion Raceband
 #endif
 };
 
 // All Channels of the above List ordered by Mhz
 const uint8_t channelList[] PROGMEM = {
 #ifdef USE_LBAND
-    40, 41, 42, 43, 44, 45, 46, 47, 19, 18, 32, 17, 33, 16, 7, 34, 8, 24, 6, 9, 25, 5, 35, 10, 26, 4, 11, 27, 3, 36, 12, 28, 2, 13, 29, 37, 1, 14, 30, 0, 15, 31, 38, 20, 21, 39, 22, 23
+  40, 41, 42, 43, 44, 45, 46, 47, 19, 18, 32, 17, 33, 16, 7, 34, 8, 24, 6, 9, 25, 5, 35, 10, 26, 4, 11, 27, 3, 36, 12, 28, 2, 13, 29, 37, 1, 14, 30, 0, 15, 31, 38, 20, 21, 39, 22, 23
 #else
-    19, 18, 32, 17, 33, 16, 7, 34, 8, 24, 6, 9, 25, 5, 35, 10, 26, 4, 11, 27, 3, 36, 12, 28, 2, 13, 29, 37, 1, 14, 30, 0, 15, 31, 38, 20, 21, 39, 22, 23
+  19, 18, 32, 17, 33, 16, 7, 34, 8, 24, 6, 9, 25, 5, 35, 10, 26, 4, 11, 27, 3, 36, 12, 28, 2, 13, 29, 37, 1, 14, 30, 0, 15, 31, 38, 20, 21, 39, 22, 23
 #endif
 };
 

uint16_t readRSSI()
{
#ifdef USE_DIVERSITY
    return readRSSI(-1);
}
uint16_t readRSSI(char receiver)
{
#endif
    int rssi = 0;
    int rssiA = 0;

#ifdef USE_DIVERSITY
    int rssiB = 0;
#endif
    for (uint8_t i = 0; i < RSSI_READS; i++)  {
        analogRead(rssiPinA);
        rssiA += analogRead(rssiPinA);//random(RSSI_MAX_VAL-200, RSSI_MAX_VAL);//

#ifdef USE_DIVERSITY
        analogRead(rssiPinB);
        rssiB += analogRead(rssiPinB);//random(RSSI_MAX_VAL-200, RSSI_MAX_VAL);//
#endif



#ifdef Debug
        rssiB += 1;// random(RSSI_MAX_VAL - 200, RSSI_MAX_VAL); //
#endif


    }
    rssiA = rssiA / RSSI_READS; // average of RSSI_READS readings

#ifdef USE_DIVERSITY
    rssiB = rssiB / RSSI_READS; // average of RSSI_READS readings
#endif
    // special case for RSSI setup
    if (state == STATE_RSSI_SETUP)  { // RSSI setup
        if (rssiA < rssi_setup_min_a)    {
            rssi_setup_min_a = rssiA;
        }
        if (rssiA > rssi_setup_max_a)    {
            rssi_setup_max_a = rssiA;
        }

#ifdef USE_DIVERSITY
        if (rssiB < rssi_setup_min_b)    {
            rssi_setup_min_b = rssiB;
        }
        if (rssiB > rssi_setup_max_b)    {
            rssi_setup_max_b = rssiB;
        }
#endif
    }

    rssiA = map(rssiA, rssi_min_a, rssi_max_a , 1, 100);   // scale from 1..100%
#ifdef USE_DIVERSITY
    rssiB = map(rssiB, rssi_min_b, rssi_max_b , 1, 100);   // scale from 1..100%
    if (receiver == -1) // no receiver was chosen using diversity  {
        switch (diversity_mode)    {
        case useReceiverAuto:
            // select receiver
            if ((int)abs((float)(((float)rssiA - (float)rssiB) / (float)rssiB) * 100.0) >= DIVERSITY_CUTOVER)        {
                if (rssiA > rssiB && diversity_check_count > 0)          {
                    diversity_check_count--;
                }
                if (rssiA < rssiB && diversity_check_count < DIVERSITY_MAX_CHECKS)          {
                    diversity_check_count++;
                }
                // have we reached the maximum number of checks to switch receivers?
                if (diversity_check_count == 0 || diversity_check_count >= DIVERSITY_MAX_CHECKS) {
                    receiver = (diversity_check_count == 0) ? useReceiverA : useReceiverB;
                }
                else {
                    receiver = active_receiver;
                }
            }
            else {
                receiver = active_receiver;
            }
            break;
        case useReceiverB:
            receiver = useReceiverB;
            break;
        case useReceiverA:
        default:
            receiver = useReceiverA;
        }
    // set the antenna LED and switch the video
    setReceiver(receiver);
}
#endif

#ifdef USE_DIVERSITY
if (receiver == useReceiverA || state == STATE_RSSI_SETUP)  {
#endif
    rssi = rssiA;
#ifdef USE_DIVERSITY
}  else {
    rssi = rssiB;
}
#endif


//SEND RSSI FEEDBACK
if ( (( time_screen_saver2 != 0 &&  time_screen_saver2 + 8000 < millis()))   && (  state == STATE_SCREEN_SAVER) )  {
    if ((p_active_receiver != active_receiver )  || ((rssi - 60 > p_rssi) ||  (rssi + 60 < p_rssi)))     {
        p_active_receiver = active_receiver;
        p_rssi = rssi ;
        OSDParams[0] = rssiA; //rssiA; //this is  A
        OSDParams[1] = rssiB; //rssiB; //this is  B
        OSDParams[2] = receiver; //active_receiver; //CUrrent reciever
        OSDParams[3] = -1; //THIS is for rssi method
        SendToOSD();
        time_screen_saver2 = 1;
    }
}

return constrain(rssi, 1, 100); // clip values to only be within this range.
}

void setReceiver(uint8_t receiver) {
#ifdef USE_DIVERSITY
    if (receiver == useReceiverA)  {
#ifdef USE_FAST_SWITCHING
        PORTC = (PORTC & B11111101) | B00000001; //This turns off receiverB_led and turns on receiverA_led simultaniously, this does however breaks the adaptability of receiverA_led/receiverB_led in settings.h
#else
        digitalWrite(receiverB_led, LOW);
        digitalWrite(receiverA_led, HIGH);
#endif
    }
    else
    {
#ifdef USE_FAST_SWITCHING
        PORTC = (PORTC & B11111110) | B00000010; //This turns off receiverA_led and turns on receiverB_led simultaniously
#else
        digitalWrite(receiverA_led, LOW);
        digitalWrite(receiverB_led, HIGH);
#endif
    }
#else
    digitalWrite(receiverA_led, HIGH);
#endif

    active_receiver = receiver;
}



void setChannelModule(uint8_t channel) {
    uint8_t i;
    uint16_t channelData;

    channelData = pgm_read_word_near(channelTable + channel);

    // bit bash out 25 bits of data
    // Order: A0-3, !R/W, D0-D19
    // A0=0, A1=0, A2=0, A3=1, RW=0, D0-19=0
    SERIAL_ENABLE_HIGH();
    delayMicroseconds(1);
    //delay(2);
    SERIAL_ENABLE_LOW();

    SERIAL_SENDBIT0();
    SERIAL_SENDBIT0();
    SERIAL_SENDBIT0();
    SERIAL_SENDBIT1();

    SERIAL_SENDBIT0();

    // remaining zeros
    for (i = 20; i > 0; i--)
        SERIAL_SENDBIT0();

    // Clock the data in
    SERIAL_ENABLE_HIGH();
    //delay(2);
    delayMicroseconds(1);
    SERIAL_ENABLE_LOW();

    // Second is the channel data from the lookup table
    // 20 bytes of register data are sent, but the MSB 4 bits are zeros
    // register address = 0x1, write, data0-15=channelData data15-19=0x0
    SERIAL_ENABLE_HIGH();
    SERIAL_ENABLE_LOW();

    // Register 0x1
    SERIAL_SENDBIT1();
    SERIAL_SENDBIT0();
    SERIAL_SENDBIT0();
    SERIAL_SENDBIT0();

    // Write to register
    SERIAL_SENDBIT1();

    // D0-D15
    //   note: loop runs backwards as more efficent on AVR
    for (i = 16; i > 0; i--)  {
        // Is bit high or low?
        if (channelData & 0x1)    {
            SERIAL_SENDBIT1();
        }
        else    {
            SERIAL_SENDBIT0();
        }

        // Shift bits along to check the next one
        channelData >>= 1;
    }

    // Remaining D16-D19
    for (i = 4; i > 0; i--)
        SERIAL_SENDBIT0();

    // Finished clocking data in
    SERIAL_ENABLE_HIGH();
    delayMicroseconds(1);
    //delay(2);

    digitalWrite(slaveSelectPin, LOW);
    digitalWrite(spiClockPin, LOW);
    digitalWrite(spiDataPin, LOW);
}


void SERIAL_SENDBIT1() {
    digitalWrite(spiClockPin, LOW);
    delayMicroseconds(1);

    digitalWrite(spiDataPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(spiClockPin, HIGH);
    delayMicroseconds(1);

    digitalWrite(spiClockPin, LOW);
    delayMicroseconds(1);
}

void SERIAL_SENDBIT0() {
    digitalWrite(spiClockPin, LOW);
    delayMicroseconds(1);

    digitalWrite(spiDataPin, LOW);
    delayMicroseconds(1);
    digitalWrite(spiClockPin, HIGH);
    delayMicroseconds(1);

    digitalWrite(spiClockPin, LOW);
    delayMicroseconds(1);
}

void SERIAL_ENABLE_LOW() {
    delayMicroseconds(1);
    digitalWrite(slaveSelectPin, LOW);
    delayMicroseconds(1);
}

void SERIAL_ENABLE_HIGH() {
    delayMicroseconds(1);
    digitalWrite(slaveSelectPin, HIGH);
    delayMicroseconds(1);
}


void FillTemp_Tune_fav(int FavChannelx) {
    for (int i = 0; i < 10; i++)  {
        if ( temp_EEPROM_ADR_TUNE_FAV[i] == 255 && FavChannelx != 255) //not used  gc9n    {
            temp_EEPROM_ADR_TUNE_FAV[i] = FavChannelx;
        i = 12;
    }
}
}

void SendToOSD() //GC9N
{
    if (settings_OSD == true || OSDParams[0] == -99 )  {
        char one[5]  ;
        char two[5] ;
        char three[5] ;
        char four[5] ;
        char blank[5] = " " ;
        char combined[20] = {0};


        itoa(OSDParams[0], one, 10);
        itoa(OSDParams[1], two, 10);
        itoa(OSDParams[2], three, 10);
        itoa(OSDParams[3], four, 10);

        char ClearScreen[20] = " CLEAR ";
        char RssiScreen[20] = " DRAWRSSI";

        strcat(combined, OSDCommand);

        if (OSDParams[0] == -99) // CLEARSCREEN    {
            for (uint8_t i = 0; i < 20; i++)      {
                combined[i] = ClearScreen[i];
            }
    }

    if (OSDParams[3] == -1) // DRAW_RSSI    {
        for (uint8_t i = 0; i < 20; i++)      {
            combined[i] = RssiScreen[i];
        }
}

strcat(combined, blank);
strcat(combined, one);
strcat(combined, blank);
strcat(combined, two);
strcat(combined, blank);
strcat(combined, three);


Serial.println(combined);
OSDParams[3] = 0;

}
}

int8_t FSButtonDirection () //gc9n
{
    char dir; //1 UP 2 DOWN
    while ( Serial.available() > 0) {

        dir = Serial.read();
        if (dir == ']')    {
            FS_BUTTON_DIR = 2;
            return 2;
        }

        else if (dir == '[')    {
            FS_BUTTON_DIR = 1;
            return 1;
        }

        else {
            FS_BUTTON_DIR = 0;
            return 0;
        }
    }

}

        // selection by inverted box
        break;

      case STATE_FAVORITE: // FAV mode
        if (state == STATE_FAVORITE)
        {
          time_screen_saver = millis();
        }
        else if (state == STATE_SEEK)
        {
          time_screen_saver = 0; // dont show screen saver until we found a channel.
        }

        //drawScreen.FavMode(state,FirstFav);

        // 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;
