DDZZ669 发表于 2022-12-4 14:32

玩转RP2040之使用Python在LCD上显示多类型字体

<div class='showpostmsg'> 本帖最后由 DDZZ669 于 2022-12-4 14:32 编辑

<p cid="n0" mdtype="paragraph">上篇文章:<a href="https://bbs.eeworld.com.cn/thread-1227388-1-1.html" target="_blank">玩转RP2040之使用Python在LCD上显示图片</a>,介绍了使用一个开源的Python固件后,可以方便的在微雪RP2040上显示图片。此开源项目中,使用这个固件,还可以方便的显示多种不同的字体,并可以定义多种不同的字体大小,本篇就来测试下不同字体的显示情况。</p>

<h2 cid="n2" mdtype="heading">1 随机位置随机颜色显示字体-hello.py</h2>

<p cid="n3" mdtype="paragraph">实测效果如下图,由于字体位置在快速变动,拍照效果有些糊,可看下后面的视频效果。</p>

<p>&nbsp;</p>

<p cid="n4" mdtype="paragraph"></p>

<p cid="n5" mdtype="paragraph">对应的程序如下:</p>

<pre>
<code class="language-python">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()</code></pre>

<p cid="n7" mdtype="paragraph">分析主函数,在屏幕的随机位置,使用随机的函数,在一个LCD屏幕上,显示128个Hello字体</p>

<p cid="n7" mdtype="paragraph">动态效果:</p>

<p cid="n7" mdtype="paragraph">e97dcba7602205ffd2c196d8bc15942d<br />
&nbsp;</p>

<h2 cid="n10" mdtype="heading">2 指定位置指定颜色显示字符串-hershey.py</h2>

<p>&nbsp;</p>

<p cid="n11" mdtype="paragraph">实测效果如下图</p>

<p>&nbsp;</p>

<p cid="n12" mdtype="paragraph"></p>

<p>&nbsp;</p>

<p cid="n13" mdtype="paragraph">对应的程序如下:</p>

<pre>
<code class="language-python">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()

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()</code></pre>

<p cid="n15" mdtype="paragraph">分析主程序,在LCD上的7行位置,显示指定颜色的指定字符串</p>

<p cid="n16" mdtype="paragraph">动态效果:</p>

<p>99c8d10cba6a4d492720b12834d7dda2<br />
&nbsp;</p>

<h2 cid="n17" mdtype="heading">3 scroll.py滚动字体</h2>

<p cid="n18" mdtype="paragraph">实测效果如下图,由于字体位置在快速变动,拍照效果有些糊,可看下后面的视频效果。</p>

<p>&nbsp;</p>

<p cid="n19" mdtype="paragraph"></p>

<p>&nbsp;</p>

<p cid="n20" mdtype="paragraph">对应的程序如下:</p>

<pre>
<code class="language-python">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()
    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 &gt; 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 &gt; font.LAST:
                character = font.FIRST

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

      utime.sleep(0.01)


main()</code></pre>

<p cid="n22" mdtype="paragraph">分析主程序,在LCD上滚动显示不同颜色的字符</p>

<p>&nbsp;</p>

<p cid="n23" mdtype="paragraph">动态效果:</p>

<p>f6ef7734554cff1d3289876252a5681d<br />
&nbsp;</p>

<h2 cid="n24" mdtype="heading">4 noto_fonts.py</h2>

<p cid="n25" mdtype="paragraph">实测效果如下图</p>

<p cid="n26" mdtype="paragraph"></p>

<p>&nbsp;</p>

<p cid="n27" mdtype="paragraph">对应的程序如下:</p>

<pre>
<code class="language-python">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 &lt; screen:             # if the string &lt; 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()</code></pre>

<h2 cid="n29" mdtype="heading">5 mono_fonts.py</h2>

<p cid="n30" mdtype="paragraph">实测效果如下图</p>

<p>&nbsp;</p>

<p cid="n31" mdtype="paragraph"></p>

<p>&nbsp;</p>

<p cid="n32" mdtype="paragraph">对应的程序如下:</p>

<pre>
<code class="language-python">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 &gt;= tft.width() - font.WIDTH:
                row += font.HEIGHT
                column = 0

                if row &gt; 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 :
            display_font(font)

      fast = not fast


main()</code></pre>

<p cid="n34" mdtype="paragraph">分析主程序,在LCD上依次显示3中不同大小的字体</p>

<p cid="n35" mdtype="paragraph">动态效果:</p>

<p cid="n35" mdtype="paragraph">4be0a16fc47605cbd7faac3371f3e454<br />
&nbsp;</p>

<h2 cid="n36" mdtype="heading">6 changp.py</h2>

<p cid="n37" mdtype="paragraph">实测效果如下图</p>

<p cid="n38" mdtype="paragraph"></p>

<p>&nbsp;</p>

<p cid="n39" mdtype="paragraph">对应的程序如下:</p>

<pre>
<code class="language-python">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()</code></pre>

<p cid="n41" mdtype="paragraph">分析主程序,在LCD上显示4中不同大小的字体</p>

<h2 cid="n42" mdtype="heading">总结</h2>

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

<p>&nbsp;</p>

<p>&nbsp;</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

Jacktang 发表于 2022-12-5 07:30

<h2 cid="n2" mdtype="heading">&nbsp;</h2>

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

lugl4313820 发表于 2022-12-5 09:14

可以做个手表玩的。
页: [1]
查看完整版本: 玩转RP2040之使用Python在LCD上显示多类型字体