I recently had a problem where I needed to invert the glyphs (characters) on a four-digit 7-segment LED display. The display was on a hand-held digital thermometer that could be used in any orientation, including upside down. To make it easier to read when inverted I fitted an accelerometer so it could detect its orientation. The data from the accelerometer, together with some bit twiddling, provided an automatic means to invert the display.


There is a limitation of course to inverting the display because decimal points can no longer be used. However, since I was displaying a three-digit temperature and either C or F, this was not important.


Using the writeDigitRaw() function from the Adafruit LED backpack library you can specify, using an 8-bit number, any pattern of LED segments you want to illuminate. I wrote the following function to swap the relevant bits so that a glyph would be inverted. The function converts one byte at a time and it is the programmer's responsibility to re-order the characters on the display because the order will also be reversed.

You can download the function here. The source code includes a supplementary function SwitchTwoBits() that does the actual bit swapping. Ensure that you call the function with a source data variable of the correct type which is unsigned char. The function also returns the new value as unsigned char. The syntax is:

unsigned char NewGlyph = InvertSevenSegment(OldGlyph);

Where OldGlyph is the current glyph value. It's safe to put the function call as an argument in a call to writeDigitRaw(), for example:





The bit swapping process is shown in the diagram below and works for all glyph values between zero and 255 inclusive. There are no checks to ensure that you are not trying to swap bits outside the boundary of the unsigned char, but these could be added easily. As you're returning an unsigned char, you can't check for a return value of -1, so will need to add another argument in the call list.

Bit swapping process