4607 views|9 replies

291

Posts

5

Resources
The OP
 

Playing with RP2040: Using Python to display images on LCD [Copy link]

The previous article: Playing with RP2040 LCD Drawing Basic Shape Test , introduced the use of MicroPython to perform basic graphics drawing on the RP2040 LCD, including pixel drawing, straight line drawing, rectangle drawing, etc. However, there seems to be no good support for drawing pictures.

I found an example of using MicroPython to display pictures on the LCD of gc9a01 on the Internet. The LCD driver firmware has been compiled and can be used directly or modified and compiled by yourself. It is quite good. The project link address is:

1. Change Python firmware

To use MicroPython and the function interface in this project to display images on the LCD, you need to first replace the RP2040 with the firmware here. The replacement method is the same as described before, that is, press BOOT and RESET to restart. After installing BOOT, a folder will pop up. Drag the following firmware into it. The specific process of firmware burning can be referred to in the previous article:

2 Modify pin definition

Then you can use the test program in the project to perform a picture display test. Before testing, you need to change the pin definition, because the LCD pin definition of the board used in the downloaded project is different from that of RP2040. You can refer to the pin definition writing method in the Weixue LCD basic routine and make corresponding modifications. The modified code is as follows, jpg.py program:

import gc
import time 
from machine import Pin, SPI
import gc9a01

DC = 8
CS = 9
SCK = 10
MOSI = 11
RST = 12

BL = 25

gc.enable()
gc.collect()


def main():
  '''
   Decode and draw jpg on display
   '''
  spi = SPI(1, baudrate=60000000, sck=Pin(SCK), mosi=Pin(MOSI))
  tft = gc9a01.GC9A01(
    spi,
    240,
    240,
    reset=Pin(RST, Pin.OUT),
    cs=Pin(CS, Pin.OUT),
    dc=Pin(DC, Pin.OUT),
    backlight=Pin(BL, Pin.OUT),
    rotation=0)

  # enable display and clear screen
  tft.init()

  # cycle thru jpg's
  while True:
    for image in ["bigbuckbunny.jpg", "bluemarble.jpg"]:
      tft.jpg(image, 0, 0, gc9a01.SLOW)
      time.sleep(5)


main()

It can be seen that with the support of the LCD compilation library, the application code becomes very concise. After specifying the LCD pin and initializing it, you only need to specify the jpg image to be displayed to display the image, and the image does not require additional encoding conversion.

3. Put the image file into the board

Before testing, you need to put the jpg image and program to be displayed into the RP2040 board, corresponding to the py file. The previous article ( Playing with RP2040 and Setting up the Python Development Environment ) introduced a way: open the py file through Thonny on the computer, then connect the serial port to the board, click Save As, and choose to save it to the board.

How to save jpg images to the board? Here is another method of copying files, using the ampy tool.

3.1 Install ampy

You need to have a Python development environment on your Windows computer, and then use the pip command to install the adafruit-ampy tool

pip install adafruit-ampy

After the installation is complete, you can enter the ampy command to confirm whether the installation is successful:

The main command used here is put, which is used to send files from the computer to the RP2040 board.

3.2 Use apmy command to transfer files

In the directory of the file to be uploaded, hold down the shift key and right-click the directory, and select "Open PowerShell window this time".

For example, if the jpg image to be uploaded is in the examples/RP2/jpg directory, hold down the shift key and right-click on the jpg folder.

Use the ls command to view the files in the jpg directory, and then use the following command to push the files

ampy --port COM31 put alien.jpg
  • COM31 is the serial port number of your board

  • alien.jpg is the file name to be sent. This is a picture file. In addition, folder sending is also supported.

Please note that: when executing this command, the serial port of the board must be connected to the computer, but the serial port cannot be connected to other software. If it is connected to Thonny, disconnect Thonny from the board.

The result of a successful file sending is as follows:

After sending successfully, you can signal Thonny to connect to the board again, and confirm whether the image is already in the board by opening the file and opening the board file.

4 Run the test

After putting the program and the picture into the board, you can test it. Since my board put a main.py program into it when testing the basic elements of LCD painting in the previous article, the program will run by default when it is turned on. Without deleting the program, use Thonny to open the jpg.py picture test program in the board and click Run in Thonny to run it. The test effect is as follows:

5 Conclusion

This article introduces how to use MicroPython to test the display of full-screen images on the LCD on the RP2040. By replacing the compiled Python firmware of the LCD driver, placing the jpg file to be displayed into the board through ampy, and modifying the pin definition of the test program, the image display on the LCD can be realized.

This post is from Mobile and portable

Latest reply

Is there any example for this? I ran it here but got no response.   Details Published on 2023-3-24 23:58
 
 

291

Posts

5

Resources
2
 

Remove the delay in the program and switch the image display at the fastest speed. The actual effect is:

VID_20221204_123556

This post is from Mobile and portable
 
 
 

291

Posts

5

Resources
3
 

The animation effect of small icons is still good, such as the toasters.py program

import time
import random
from machine import Pin, SPI
import gc9a01
import t1, t2, t3, t4, t5

TOASTERS = [t1, t2, t3, t4]
TOAST = [t5]

DC = 8
CS = 9
SCK = 10
MOSI = 11
RST = 12

BL = 25

class toast():
    '''
    toast class to keep track of a sprites locaton and step
    '''
    def __init__(self, sprites, x, y):
        self.sprites = sprites
        self.steps = len(sprites)
        self.x = x
        self.y = y
        self.step = random.randint(0, self.steps-1)
        self.speed = random.randint(2, 5)

    def move(self):
        if self.x <= 0:
            self.speed = random.randint(2, 5)
            self.x = 240-64

        self.step += 1
        self.step %= self.steps
        self.x -= self.speed


def main():
    '''
    Draw and move sprite
    '''
    spi = SPI(1, baudrate=60000000, sck=Pin(SCK), mosi=Pin(MOSI))
    tft = gc9a01.GC9A01(
        spi,
        240,
        240,
        reset=Pin(RST, Pin.OUT),
        cs=Pin(CS, Pin.OUT),
        dc=Pin(DC, Pin.OUT),
        backlight=Pin(BL, Pin.OUT),
        rotation=0)

    # enable display and clear screen
    tft.init()
    tft.fill(gc9a01.BLACK)

    # create toast spites in random positions
    sprites = [
        toast(TOASTERS, tft.width()-64, 0),
        toast(TOAST, tft.width()-64*2, 80),
        toast(TOASTERS, tft.width()-64*4, 160)
    ]

    # move and draw sprites
    while True:
        for man in sprites:
            bitmap = man.sprites[man.step]
            tft.fill_rect(
                man.x+bitmap.WIDTH-man.speed,
                man.y,
                man.speed,
                bitmap.HEIGHT,
                gc9a01.BLACK)

            man.move()

            if man.x > 0:
                tft.bitmap(bitmap, man.x, man.y)
            else:
                tft.fill_rect(
                    0,
                    man.y,
                    bitmap.WIDTH,
                    bitmap.HEIGHT,
                    gc9a01.BLACK)

        time.sleep(0.05)


main()

Test image:

Animation effects:

VID_20221204_125921_x264

This post is from Mobile and portable
 
 
 

1w

Posts

25

Resources
4
 

Using thonny to transfer files is more convenient than ampy

This post is from Mobile and portable

Comments

Can thonny also transfer image files to the board? I didn't find this function.  Details Published on 2022-12-4 14:03
 
 
 

291

Posts

5

Resources
5
 
dcexpert posted on 2022-12-4 13:56 Using thonny to transfer files is more convenient than ampy

Can thonny also transfer image files to the board? I didn't find this function.

This post is from Mobile and portable
 
 
 

1w

Posts

25

Resources
6
 
DDZZ669 posted on 2022-12-4 14:03 Can thonny also upload image files to the board? I didn't find this function

Any file will do

First right click on the file

Then select "Upload to" from the menu

If a directory is created, you can also enter the directory and upload to the specified directory.

This post is from Mobile and portable

Comments

This method is indeed more convenient, and I learned another trick. Thanks for sharing  Details Published on 2022-12-4 20:42
 
 
 

291

Posts

5

Resources
7
 

This method is indeed more convenient, and I learned another trick. Thanks for sharing

This post is from Mobile and portable
 
 
 

151

Posts

0

Resources
8
 

It's really well written. It's so detailed and accurate.

This post is from Mobile and portable
 
 
 

286

Posts

1

Resources
9
 

Is there any example for this? I ran it here but got no response.

This post is from Mobile and portable

Comments

Source code? As mentioned at the beginning of the article, the source code comes from an open source project on GitHub.  Details Published on 2023-3-27 22:51
Personal signature

失恋中。。。

 
 
 

291

Posts

5

Resources
10
 
liaoyuanhong posted on 2023-3-24 23:58 Is there any example for this? I ran it here but there was no response.

Source code? As mentioned at the beginning of the article, the source code comes from an open source project on GitHub.

This post is from Mobile and portable
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list