/*********************************************************************************
*  _   _                _______           _                                _     *
* | \ | |              | |  _  \         (_)                              | |    *
* |  \| | _____   _____| | | | |_____   ___  ___ ___  ___   ___ ___  _   _| | __ *
* | . ` |/ _ \ \ / / _ \ | | | / _ \ \ / / |/ __/ _ \/ __| / __/ _ \| | | | |/ / *
* | |\  | (_) \ V /  __/ | |/ /  __/\ V /| | (_|  __/\__ \| (_| (_) | |_| |   <  *
* \_| \_/\___/ \_/ \___|_|___/ \___| \_/ |_|\___\___||___(_)___\___(_)__,_|_|\_\ *
*                                                                                *
**********************************************************************************
* InvertSevenSegment()  JST Lawrence           October 2020                      *
*                                                                                *
* 05-Nov-2020 V1.0 JSTL Created.                                                 *
*                  Swaps glyph bits to display a 7-segment glyph upside-down.    *
*                                                                                *
*********************************************************************************/

// Declarations
unsigned char InvertSevenSegment(unsigned char Char);
unsigned char SwitchTwoBits(unsigned char Byte, int SwapBit1, int SwapBit2);

/*********************************************************************************
*                                                                                *
* InvertSevenSegment()                                                           *
*                                                                                *
*********************************************************************************/
unsigned char InvertSevenSegment(unsigned char Char)
{
// Diagram of LED segment codes
//         0
//       -----
//   5  |     | 1
//      |  6  |
//       ----- 
//   4  |     | 2
//      |     |
//       -----  o7 
//         3
// Cannot invert decimal point so just look at the seven bits 0-6
// Bit 6 does not switch with anything

// New value to be returned
  unsigned char NewChar;

// Start off with the current value
  NewChar=Char;

// Call the function that swaps two bits in an 8-bit byte
// If two bits are the same, we don't check but swap them anyway
  NewChar=SwitchTwoBits(NewChar,0,3);
  NewChar=SwitchTwoBits(NewChar,1,4);
  NewChar=SwitchTwoBits(NewChar,2,5);
  return NewChar;
}

/*********************************************************************************
*                                                                                *
* SwitchTwoBits()                                                                *
*                                                                                *
*********************************************************************************/
unsigned char SwitchTwoBits(unsigned char Byte, int SwapBit1, int SwapBit2)
{
//     
    unsigned char Bit1;
    unsigned char Bit2;
    unsigned char XOR;

    Bit1=(Byte>>SwapBit1) & 1; // Extract first bit
    Bit2=(Byte>>SwapBit2) & 1; // Extract second bit

// Find the XOR - this is 1 if the two bits are different
// NB you could check for a 0 here and just return Byte unchanged as there's nothing to do
    XOR=Bit1^Bit2;
    
    return (Byte^(XOR<<SwapBit1 | XOR<<SwapBit2));
}
