The Adafruit MacroPad RP2040 features a 3x4 key pad with NeoPixel LEDs, a rotary encoder with push switch, and a display. This example reads the key presses, the relative position of the rotary encoder and the state of the rotary encoder switch, and displays the information on the display.

Update your code.py to the following.

Click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the folder that matches your CircuitPython version, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.

# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
"""
Simpletest demo for MacroPad. Displays the key pressed, the relative position of the rotary
encoder, and the state of the rotary encoder switch to the built-in display. Note that the key
pressed line does not appear until a key is pressed.
"""
from adafruit_macropad import MacroPad

macropad = MacroPad()

text_lines = macropad.display_text(title="MacroPad Info")

while True:
    key_event = macropad.keys.events.get()
    if key_event and key_event.pressed:
        text_lines[0].text = "Key {} pressed!".format(key_event.key_number)
    text_lines[1].text = "Rotary encoder {}".format(macropad.encoder)
    text_lines[2].text = "Encoder switch: {}".format(macropad.encoder_switch)
    text_lines.show()

Now, check out the display!

If you rotate the rotary encoder, the number will change. It doesn't matter where the rotary encoder is, it will begin at 0. The number provided is a relative position.

If you press the rotary encoder switch down, it will display True.

Note that the key press line does not show up initially. Try pressing a key!

To use the display_text feature of the MacroPad library, you need to instantiate it by assigning it to a variable, e.g. text_lines = macropad.display_text(title="MacroPad Info"). Once created, the title cannot be updated. Note that if you want to be able to dynamically update the title, simply instantiate it without a tittle (text_lines = macropad.display_text()), and treat the first line of text as the title, which can be dynamically updated.

Once you've instantiated it, you can create lines of text below the title with dynamic information in them, such as the key number being pressed or the relative position of the rotary encoder. To do this, you use the text_lines object, and provide it a line number and a string to display. Remember, Python begins counting at 0. For example, to display a line of text with the rotary encoder relative position below the title, you would include text_lines[0].text = "Rotary encoder {}".format(macropad.encoder) in your code. To include a second line of code, you would use text_lines[1].text = and provide a string to display.

This feature uses the Simple Text Display library; for advanced usage check out the Simple Text Display documentation.

In your code, first, you import the MacroPad library, and then instantiate it.

Then, you create a text_lines variable, initialise the display_text feature by assigning text_lines = macropad.display_text(), and, inside the parentheses, provide it the title as a string, e.g. title="MacroPad Info".

Inside the loop, the first thing you do is setup to look for the key press by creating the key_event variable and assigning it to macropad.keys.events.get(). Then, you check to see if there is a key_event (i.e. a key being pressed) and if it is a key being pressed (key_event.pressed). Then, if so, if so, update the first line of text to appear on the display showing which key number (key_event.key_number) was pressed.

Next, you display two more lines of text - one for the rotary encoder relative position and one for the rotary encoder switch state. Each of these updates when you rotate the rotary encoder or press on the rotary encoder switch.

Finally, you call text_lines.show() to make the lines of text show up on the display.

That's all there is to displaying lines of text on the built-in display of the Adafruit MacroPad using the CircuitPython MacroPad library!

This guide was first published on Jun 30, 2021. It was last updated on 2021-07-29 15:09:16 -0400.

This page (MacroPad Display Text) was last updated on Sep 16, 2021.

Text editor powered by tinymce.