The Adafruit MacroPad RP2040 features a 3x4 key pad with NeoPixel LEDs, and a display. This example reads key presses, lights up NeoPixel associated the key pressed, and prints to the display which key is pressed.

The keys are numbered 0 through 11 (remember Python begins counting at 0), beginning at the top left of the keypad, and ending at the lower right, numbered along each row reading left to right. The NeoPixels are numbered the same way, left to right, top to bottom. The image below shows the standard key and pixel numbering.

The MacroPad lends itself to many different projects, most of them using the MacroPad as shown above. What if you'd rather use it in a different orientation? Perhaps a 90 degree rotation to have a 4x3 keypad with the rotary encoder and display on the left. What about a 180 degree rotation to put the display and the rotary encoder on the bottom. Or a 270 degree rotation to have the rotary encoder on the bottom right below the display. This type of rotation involves remapping the keys and the NeoPixels and rotating the display to match. Sound like a lot of work? It is. And the MacroPad library does all the work for you!

The MacroPad library makes it simple to rotate your MacroPad. When you instantiate the library after import, instead of simply doing macropad = MacroPad(), you include a rotation. The supported rotation options are:

  • 0 - This is the default. This is the MacroPad in a standard orientation with the USB connector pointing upward.
  • 90 - This is the MacroPad with the USB connector pointing to the left.
  • 180 - This is the MacroPad with the USB connector pointing towards the bottom.
  • 270 - This is the MacroPad with the USB connector pointing to the right.

Any other rotation value will cause an error.

For example, to rotate the MacroPad 90 degrees, you would import and instantiate the library as follows.

from adafruit_macropad import MacroPad

macropad = MacroPad(rotation=90)

This rotates the display 90 degrees and remaps the keys and NeoPixels to match. The rest of your code will now read the keys and NeoPixels, left to right, top to bottom beginning at the key closest to the rotary encoder. What does that look like? Check out the following example.

Rotation Example

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
"""
MacroPad rotation demo. Rotates the display 90 degrees and remaps the NeoPixels and keys to match.
Lights up the associated pixel when the key is pressed. Displays the key number pressed and the
rotary encoder relative position on the display.
"""
from rainbowio import colorwheel
from adafruit_macropad import MacroPad

macropad = MacroPad(rotation=90)

text_lines = macropad.display_text(title="MacroPad \nInfo")

while True:
    key_event = macropad.keys.events.get()
    if key_event:
        if key_event.pressed:
            text_lines[1].text = "Key {}!".format(key_event.key_number)
            macropad.pixels[key_event.key_number] = colorwheel(
                int(255 / 12) * key_event.key_number
            )
        else:
            macropad.pixels.fill((0, 0, 0))
    text_lines[2].text = "Encoder {}".format(macropad.encoder)
    text_lines.show()

Now, rotate your MacroPad 90 degrees, and try pressing the keys and rotate the rotary encoder! The 90 degree orientation is when the rotary encoder is in the upper left corner. The key and NeoPixels are numbered starting on the key closest to the rotary encoder.

This example rotates the MacroPad 90 degrees. Remember, when the MacroPad is in a sideways orientation, the display is 64x128 pixels (instead of the standard 128x64 pixels). Any text displayed needs to be shortened to fit on the display. So, this example puts the title on two lines to shorten it up and uses shorter strings than the Display Text example.

In your code, you first import colorwheel from rainbowio.

Next, you import and instantiate the MacroPad library, specifying the 90 degree rotation with rotation=90.

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\nInfo". Note the \n which puts Info on a separate line.

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). If it is a key being pressed (key_event.pressed), you print to the display the key number pressed and light up the key using the key_number to generate a colorwheel() value. Otherwise when the key is released, turn off the LEDs.

Then, you print the relative encoder value to the display.

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

Other Rotations

Simply updating the rotation= in the MacroPad library instantiation allows you to view the other orientation options.

For example, to rotate this example to 180 degrees, you would change the line of code from macropad = MacroPad(rotation=90) to the following:

macropad = MacroPad(rotation=180)

Rotate the MacroPad so the USB connector is pointed downwards and try pressing the keys to see the key number printed to the display.

To rotate this example to 270 degrees, you would change the line of code from macropad = MacroPad(rotation=180) to the following:

macropad = MacroPad(rotation=180)

Rotate the MacroPad so the USB connector is pointed to the left and try pressing the keys to see the key number printed to the display.

Once rotated, you can include any other features of the MacroPad library using the matching key and NeoPixel map with the MacroPad oriented in whatever way you like!

That's all there is to rotating the 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 Rotation) was last updated on Sep 22, 2021.

Text editor powered by tinymce.