2865|2

291

帖子

5

TA的资源

纯净的硅(中级)

楼主
 

玩转RP2040之使用Python在LCD上显示多类型字体 [复制链接]

本帖最后由 DDZZ669 于 2022-12-4 14:32 编辑

上篇文章:玩转RP2040之使用Python在LCD上显示图片,介绍了使用一个开源的Python固件后,可以方便的在微雪RP2040上显示图片。此开源项目中,使用这个固件,还可以方便的显示多种不同的字体,并可以定义多种不同的字体大小,本篇就来测试下不同字体的显示情况。

1 随机位置随机颜色显示字体-hello.py

实测效果如下图,由于字体位置在快速变动,拍照效果有些糊,可看下后面的视频效果。

 

对应的程序如下:

import random
from machine import Pin, SPI
import gc9a01

import vga1_bold_16x32 as font

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

BL = 25

def main():
    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)

    tft.init()

    while True:
        for rotation in range(4):
            tft.rotation(rotation)
            tft.fill(0)
            col_max = tft.width() - font.WIDTH*6
            row_max = tft.height() - font.HEIGHT

            for _ in range(128):
                tft.text(
                    font,
                    "Hello!",
                    random.randint(0, col_max),
                    random.randint(0, row_max),
                    gc9a01.color565(
                        random.getrandbits(8),
                        random.getrandbits(8),
                        random.getrandbits(8)),
                    gc9a01.color565(
                        random.getrandbits(8),
                        random.getrandbits(8),
                        random.getrandbits(8))
                )


main()

分析主函数,在屏幕的随机位置,使用随机的函数,在一个LCD屏幕上,显示128个Hello字体

动态效果:

1

 

2 指定位置指定颜色显示字符串-hershey.py

 

实测效果如下图

 

 

对应的程序如下:

import utime
from machine import Pin, SPI
import gc9a01

# Load several frozen fonts from flash

import greeks
import italicc
import italiccs
import meteo
import romanc
import romancs
import romand
import romanp
import romans
import scriptc
import scripts

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

BL = 25

def cycle(p):
    '''
    return the next item in a list
    '''
    try:
        len(p)
    except TypeError:
        cache = []
        for i in p:
            yield i
            cache.append(i)
        p = cache
    while p:
        yield from p


COLORS = cycle([0xe000, 0xece0, 0xe7e0, 0x5e0, 0x00d3, 0x7030])

FONTS = cycle([
    greeks, italicc, italiccs, meteo, romanc, romancs,
    romand, romanp, romans, scriptc, scripts])

GREETINGS = cycle([
    "bonjour", "buenas noches", "buenos dias",
    "good day", "good morning", "hey",
    "hi-ya", "hi", "how are you", "how goes it",
    "howdy-do", "howdy", "shalom", "welcome",
    "what's happening", "what's up"])


def main():
    '''
    Draw greetings on display cycling thru hershey fonts and colors
    '''
    # configure 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)

    tft.init()
    tft.fill(gc9a01.BLACK)
    width = tft.width()

    while True:
        for line in range(1, 7):
            row = line * 32
            color = next(COLORS)
            tft.fill_rect(0, row-16, width, 38, gc9a01.BLACK)
            tft.draw(next(FONTS), next(GREETINGS), 0, row, color)
            utime.sleep(0.25)


main()

分析主程序,在LCD上的7行位置,显示指定颜色的指定字符串

动态效果:

2

 

3 scroll.py滚动字体

实测效果如下图,由于字体位置在快速变动,拍照效果有些糊,可看下后面的视频效果。

 

 

对应的程序如下:

import utime
from machine import Pin, SPI
import gc9a01

import vga1_bold_16x16 as font

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

BL = 25

def cycle(p):
    try:
        len(p)
    except TypeError:
        cache = []
        for i in p:
            yield i
            cache.append(i)
        p = cache
    while p:
        yield from p


def main():
    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)

    colors = cycle([0xe000, 0xece0, 0xe7e0, 0x5e0, 0x00d3, 0x7030])
    foreground = next(colors)
    background = gc9a01.BLACK

    tft.init()
    tft.fill(background)
    utime.sleep(1)

    height = tft.height()
    width = tft.width()
    last_line = height - font.HEIGHT

    tfa = 0        # top free area
    tfb = 0        # bottom free area
    tft.vscrdef(tfa, height, tfb)

    scroll = 0
    character = font.FIRST

    while True:
        # clear top line before scrolling off display
        tft.fill_rect(0, scroll, width, 1, background)

        # Write new line when we have scrolled the height of a character
        if scroll % font.HEIGHT == 0:
            line = (scroll + last_line) % height

            # write character hex value as a string
            tft.text(
                font,
                'x{:02x}'.format(character),
                16,
                line,
                foreground,
                background)

            # write character using a integer (could be > 0x7f)
            tft.text(
                font,
                character,
                90,
                line,
                foreground,
                background)

            # change color for next line
            foreground = next(colors)

            # next character with rollover at 256
            character += 1
            if character > font.LAST:
                character = font.FIRST

        # scroll the screen up 1 row
        tft.vscsad(scroll+tfa)
        scroll += 1
        scroll %= height

        utime.sleep(0.01)


main()

分析主程序,在LCD上滚动显示不同颜色的字符

 

动态效果:

3

 

4 noto_fonts.py

实测效果如下图

 

对应的程序如下:

from machine import SPI, Pin
import gc9a01

import NotoSans_32 as noto_sans
import NotoSerif_32 as noto_serif
import NotoSansMono_32 as noto_mono

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

BL = 25

def main():

    def center(font, s, row, color=gc9a01.WHITE):
        screen = tft.width()                     # get screen width
        width = tft.write_len(font, s)           # get the width of the string
        if width and width < screen:             # if the string < display
            col = tft.width() // 2 - width // 2  # find the column to center
        else:                                    # otherwise
            col = 0                              # left justify

        tft.write(font, s, col, row, color)      # and write the string

    try:
        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)

        tft.init()

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

        # center the name of the first font, using the font
        row = 16
        center(noto_sans, "NotoSans", row, gc9a01.RED)
        row += noto_sans.HEIGHT

        # center the name of the second font, using the font
        center(noto_serif, "NotoSerif", row, gc9a01.GREEN)
        row += noto_serif.HEIGHT

        # center the name of the third font, using the font
        center(noto_mono, "NotoSansMono", row, gc9a01.BLUE)
        row += noto_mono.HEIGHT

    finally:
        # shutdown spi
        if 'spi' in locals():
            spi.deinit()


main()

5 mono_fonts.py

实测效果如下图

 

 

对应的程序如下:

import time
from machine import Pin, SPI
import gc9a01

from mono_font import inconsolata_16 as font_16
from mono_font import inconsolata_32 as font_32
from mono_font import inconsolata_64 as font_64

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

BL = 25

def main():
    fast = False

    def display_font(font):
        tft.fill(gc9a01.BLUE)
        column = 0
        row = 0
        for char in font.MAP:
            tft.bitmap(font, column, row, font.MAP.index(char))
            column += font.WIDTH
            if column >= tft.width() - font.WIDTH:
                row += font.HEIGHT
                column = 0

                if row > tft.height() - font.HEIGHT:
                    row = 0

            if not fast:
                time.sleep(0.05)

    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)

    tft.init()

    while True:
        for font in [font_16, font_32, font_64]:
            display_font(font)

        fast = not fast


main()

分析主程序,在LCD上依次显示3中不同大小的字体

动态效果:

5

 

6 changp.py

实测效果如下图

 

对应的程序如下:

from machine import Pin, SPI
import gc9a01

from prop_font import chango_16 as font_16
from prop_font import chango_32 as font_32
from prop_font import chango_64 as font_64

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

BL = 25

def main():

    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)

    row = 0

    tft.write(font_16, "abcdefghijklmnopqrstuvwxyz", 0, row)
    row += font_16.HEIGHT

    tft.write(font_32, "abcdefghijklm", 0, row)
    row += font_32.HEIGHT

    tft.write(font_32, "nopqrstuvwxy", 0, row)
    row += font_32.HEIGHT

    tft.write(font_64, "abcdef", 0, row)
    row += font_64.HEIGHT

    tft.write(font_64, "ghijkl", 0, row)
    row += font_64.HEIGHT


main()

分析主程序,在LCD上显示4中不同大小的字体

总结

本篇介绍了使用MicroPython在RP2040的LCD屏幕上进行多种字体的显示测试,使用一个开源的Python固件后,可以方便地在LCD上显示多种类型、多种颜色、多种大小的字体。

 

 

此帖出自移动便携论坛

最新回复

可以做个手表玩的。  详情 回复 发表于 2022-12-5 09:14
点赞 关注
 
 

回复
举报

6802

帖子

0

TA的资源

五彩晶圆(高级)

沙发
 

 

随机位置随机颜色显示字体-hello.py测试的不错

此帖出自移动便携论坛
 
 
 

回复

6960

帖子

11

TA的资源

版主

板凳
 
可以做个手表玩的。
此帖出自移动便携论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表