PenguinTutor YouTube Channel

Programming the Raspberry Pi Pico with Display Pack

The Raspberry Pi Pico is a small microcontroller board. It's not a full desktop style computer in the same way that the traditional Raspberry Pi is and it doesn't work with a normal screen. This is a separate add-on screen designed specifically for the Raspberry Pi Pico.

Raspberry Pi Pico with Pimoroni Display Pack - game programming

Unlike a Raspberry Pi HAT which fits on top of the Raspberry Pi computer, this is called a pack because it goes on the back of the Pico like a back pack. The display pack is one of the less expensive add-ons which is a bit surprising considering it’s capability. Although it’s still many times more than the cost of a Pico.

The display pack a miniature screen a little over an inch in size. with a resolution of 240 x 130 pixels. It also has 4 buttons and a single RGB LED.

Introduction to the game and programming

This video explains about the game and some of the challenges I faced when porting a game from Pygame Zero to the Raspberry Pi Pico.

Programming differences from Pygame Zero to the Raspberry Pi Pico

Here I'll highlight some of the differences between writing code in Pygame Zero and for the Raspberry Pi Pico. This is mainly from the point of view of a vector based game. I'm currently working on an alternative for a bitmap based program (such as using a Pygame Zero Actor class).

You can take a look at the source code to help understand how programs can be written for the Pico Display.

The following are some hints that may help when converting code from Pygame Zero to the Pico.

Install the Pimoroni version of MicroPython

To access the libraries for the display pack you will need to install the Pimoroni verison of MicroPython. This can be downloaded from Pimoroni guide to getting started with the Pico.

Thonny Editor instead of Mu

One of the first things is the choice of editor. Whilst you can develop for either platform using any text editor the Mu editor is best for Pygame Zero (due to Pygame Zero integration) and the Thonny editor for the Raspberry Pi Pico (as it can save and run files directly on the Pico).

Restart Pico between edits

When making changes (particularly to OOP classes) then you may need to restart the Pico by disconnecting and reconnecting the USB connection.

Passing the display object to other classes

When intialising the display then you normal create an object called display, picodisplay or similar. As this is initialised at the start of the code that object can be passed to the constructure of your classes so that they can access the information about the display.

Don't forget to update the display

The code to interface with the display typically just updates the display buffer. The update() method needs to be called to update the display otherwise you won't see anything.

Lack of support for polygons

The MicroPython libraries do not currently support polygons. To create images then you can create them as individual pixels:
display.pixel(x, y)
a horizontal line of pixels
display.pixel_span(x, y, l)
or a rectangle:
display.rectangle(x, y, w, h)

You can also create circles using:
display.circle(x, y, r)

Set the pen color

Instead of specifying the color each time the Pico works by setting the display color and then future updates will be in that color. This is more efficient if you have several updates to make in the same color, but may be less efficient if drawing with lots of different colors.

Access the buttons on the Pico display

The buttons can be accessed using the is_pressed method. For example to test if button A is pressed then you can use:
if (display.is_pressed(display.BUTTON_A) == True):

Reading from the screen

The micropython method has no methods that allow you to read data from the screen. You can however access the bytearray directly, but you will need to convert the image into the correct format. In particular the lowest 2 or 3 bits are dropped from the colour values. This is best explained in the video, but the following code snippets show how you can convert colour from the RGB format to the byte array format and how you can access the pixels from the display byte array.

Convert a color (RGB value) to a two byte list of values



def color_to_bytes (color):

    r, g, b = color

    bytes = [0,0]

    bytes[0] = r & 0xF8

    bytes[0] += (g & 0xE0) >> 5

    bytes[1] = (g & 0x1C) << 3

    bytes[1] += (b & 0xF8) >> 3    

    return bytes

Get values for an x,y position in the byte array - return as two byte list



def get_display_bytes (x, y):

    buffer_pos = (x*2) + (y*width*2)

    byte_list = [display_buffer[buffer_pos], display_buffer[buffer_pos+1]]

    return (byte_list)

Performance issues

Whilst the Pico is quite respectable for a microcontroller it is not the same as having a full computer. As a result care needs to be taken to avoid performance issues. In my case I made the artillery shells move faster by increasing the distance they moved in each frame, and I also did the same for the adjustment of the power and angle for the user entries.

There are other performance factors that I could have looked at, perhaps storing the tanks calculations so they can be blitted direct into the byte array but I haven't yet investigated them further.

Bitmap Sprites

I'm currently working on how bitmap based sprites can be used on the Pico Display. Due to the size of the Python png libraries and due to performance reasons I am looking at how an image can be stored as a binary memory image and be displayed on the screen.

More information

Blog links and videos on programming

Previous Raspberry Pi Pico LCD display
Raspberry Pi Pico LCD display
Next Images and animations on Pico
Images and animations on Pico