RGB LCD Character Codes – Display Sketch

While working on projects using the RGB LCD shield, I’m finding it handy to see different character / color combinations on the screen.  I’ve written a sketch that allows me to page through all the available characters using the left and right buttons and all available backlight colors using the up and down buttons (this also works with the negative version of the RGB LCD shield).

LCD Character Code Sketch Screen Capture

All the characters on the bottom row share the same four high bits; the character representing those four bits is indicated in the top row by underlining (in the above picture, the number seven is underlined indicating that all characters in the bottom row have a character code starting with seven).  The characters are displayed below the digit representing the low four bits for the character code.  In the above picture, the left arrow is under the F character, so the code for left arrow is 0x7F.  Following the same logic, the code for q is 0x71.

There are a few areas in the code worth calling out.  The first is the function that treats a button press and release as a single click.  The shield library provides readButtons(), which is a handy function for reading the current state of the buttons.  You can detect clicks by calling readButtons() at regular intervals and performing some bitwise operations on the results.  The first step is performing a bitwise XOR (exclusive OR) on two consecutive reads to get a bitmask value showing which buttons changed state:

1100 XOR 1001 results in 0101

This value needs to be further filtered to find out which buttons were released on the second read.  Performing a bitwise negation on the second value produces a new value where the off buttons are ones and the on buttons are zeros.  This value can be bitwise ANDed with the changed button value to get a new value where clicked buttons are ones and all other buttons are zeros.

Which can be represented in code as follows:

uint8_t read_clicked_buttons() {
  // Static variables are intialized once and hold value between calls to the function.
  static uint8_t last_buttons = 0;

  // Read which buttons are currently on.
  uint8_t current_buttons = lcd.readButtons();

  // Calculate which buttons changed state since the last read.
  uint8_t changed_buttons = last_buttons ^ current_buttons;

  // Calculate which buttons are currently off.
  uint8_t off_buttons = ~current_buttons;

  // Calculate which buttons transitioned from on to off between reads.
  uint8_t clicked_buttons = changed_buttons & off_buttons;

  // Store current buttons for next call.
  last_buttons = current_buttons;

  return clicked_buttons;

The second interesting section is the underlining of the selected column.  This is implemented by manipulating the cursor settings as the last step when drawing the screen:


  // draw screen

  lcd.setCursor(char_offset, 0);

Full source is available on github.

Make a robot friend with Adafruit’s CRICKIT – A Creative Robotics & Interactive Construction Kit. It’s an add-on to our popular Circuit Playground Express, FEATHER and other platforms to make and program robots with CircuitPython, MakeCode, and Arduino. Start controlling motors, servos, solenoids. You also get signal pins, capacitive touch sensors, a NeoPixel driver and amplified speaker output. It complements & extends your boards so you can still use all the goodies on the microcontroller, now you have a robotics playground as well.

Join 7,500+ makers on Adafruit’s Discord channels and be part of the community! http://adafru.it/discord

CircuitPython in 2018 – Python on Microcontrollers is here!

Have an amazing project to share? Join the SHOW-AND-TELL every Wednesday night at 7:30pm ET on Google+ Hangouts.

Join us every Wednesday night at 8pm ET for Ask an Engineer!

Follow Adafruit on Instagram for top secret new products, behinds the scenes and more https://www.instagram.com/adafruit/

Maker Business — Fewer startups, and other collateral damage from the 2018 tariffs

Wearables — Light as a Worbla feather

Electronics — How to make your own magnetic field probe!

Biohacking — The State of DNA Analysis in Three Mindmaps

Python for Microcontrollers — One year of CircuitPython weeklies!

Get the only spam-free daily newsletter about wearables, running a "maker business", electronic tips and more! Subscribe at AdafruitDaily.com !

No Comments

No comments yet.

Sorry, the comment form is closed at this time.