dcexpert 发表于 2020-2-17 14:06

“玩板”+在STM32F7DISC上重玩micropython(2)

<div class='showpostmsg'> 本帖最后由 dcexpert 于 2020-2-17 15:48 编辑

<p>写入了新固件,就可以体验LCD和触摸屏了,带触摸功能的大屏,是STM32F7DISC的一大特点,当年多少人为此着迷,这也是这次重新用它的主要原因。</p>

<p>&nbsp;</p>

<p>先测试LCD,首先需要载入 lcdF7D库,然后才能使用lcd功能。</p>

<p>&nbsp;</p>

<p><strong><span style="font-family:Courier;">import lcdF7D as lcd</span></strong></p>

<p>&nbsp;</p>

<p>其次需要对lcd进行初始化</p>

<p>&nbsp;</p>

<p><strong><span style="font-family:Courier;">lcd.init()</span></strong></p>

<p>&nbsp;</p>

<p>然后就可以进行画线、画圆等操作了。下面是几个主要函数用法:</p>

<p>&nbsp;</p>

<ul>
        <li>clear(color),用颜色color清除整个屏幕<br />
        颜色的定义为
        <ul>
                <li>如果是空,使用set_text_color中定义的颜色</li>
                <li>如果是一个数字,以0xRRGGBB方式</li>
                <li>如果是两个数字,第一个代表Alpha,第二个数字同上</li>
                <li>如果是3个数字,分别代表R、G、B分量,Alpha固定为255</li>
                <li>如果是4个数字,分别代表A、R、G、B</li>
        </ul>
        </li>
        <li>set_text_color(color),设置前景颜色</li>
        <li>set_back_color(color),设置背景颜色</li>
        <li>draw_line(x1, y1, x2, y2),画直线</li>
        <li>draw_Hline(x, y, len),画水平线</li>
        <li>draw_Vline(x, y, len),画垂直线</li>
        <li>draw_rect(x1, y1, x2, y2),画空心矩形</li>
        <li>fill_rect(x1, y1, x2, y2),画实心矩形</li>
        <li>draw_circle(x, y, r),画圆</li>
        <li>set_font(n),设置字体大小,有效大小是8/12/16/20/24</li>
        <li>display_char(x, y, asc),在x,y处显示字符</li>
        <li>display_string_at(x, y, string, mode),显示字符串</li>
</ul>

<p>&nbsp;</p>

<p><strong>完整的测试程序</strong></p>

<p>&nbsp;</p>

<pre>
<code class="language-python">from time import sleep_ms, ticks_ms, ticks_diff
import pyb, machine
from random import randrange as rand

import lcdF7D as lcd
import tchF7D as ts

MAX_X = 480
MAX_Y = 272

lcd.init()

ts.init(MAX_X, MAX_Y)


def test_rand_line(n = 5000, delay = 1):
    lcd.clear()
    lcd.set_font(24)
    lcd.set_text_color(255,255,128,0)
    lcd.display_string_at(0, 0, 'line test' ,0)
    sleep_ms(1000)
    t0 = ticks_ms()
    for i in range(n):
      lcd.set_text_color(rand(0xFF), rand(0xFFFFFF))
      lcd.draw_line(rand(MAX_X), rand(MAX_Y), rand(MAX_X), rand(MAX_Y))
      sleep_ms(delay)
    return ticks_diff(ticks_ms(), t0)

def test_rand_rect(n = 2000, delay = 1):
    lcd.clear()
    lcd.set_font(24)
    lcd.set_text_color(255,255,128,0)
    lcd.display_string_at(0, 0, 'rect test' ,0)
    sleep_ms(1000)
    t0 = ticks_ms()
    for i in range(n):
      lcd.set_text_color(rand(0xFF), rand(0xFFFFFF))
      x, y = rand(MAX_X), rand(MAX_Y)
      lcd.draw_rect(x, y, rand(MAX_X - x), rand(MAX_Y - y))
      sleep_ms(delay)
    return ticks_diff(ticks_ms(), t0)

def test_rand_fillrect(n = 2000, delay = 1):
    lcd.clear()
    lcd.set_font(24)
    lcd.set_text_color(255,255,128,0)
    lcd.display_string_at(0, 0, 'fill rect test' ,0)
    sleep_ms(1000)
    t0 = ticks_ms()
    for i in range(n):
      lcd.set_text_color(rand(0xFF), rand(0xFFFFFF))
      x, y = rand(MAX_X), rand(MAX_Y)
      lcd.fill_rect(x, y, rand(MAX_X - x), rand(MAX_Y - y))
      sleep_ms(delay)
    return ticks_diff(ticks_ms(), t0)

def test_gradient(c1=0, c2=0xff, dir=0):
    def cr(x1, x2, i, n):
      return int(x1 + i*(x2-x1)/n)

    r1, g1, b1 = c1&gt;&gt;16, (c1&gt;&gt;8)%256, c1%256
    r2, g2, b2 = c2&gt;&gt;16, (c2&gt;&gt;8)%256, c2%256
    if dir == 0:
      for i in range(MAX_X):
            r = cr(r1, r2, i, MAX_X)
            g = cr(g1, g2, i, MAX_X)
            b = cr(b1, b2, i, MAX_X)
            lcd.set_text_color(r, g, b)
            lcd.draw_Vline(i, 0, MAX_Y)
    elif dir == 1:
      for i in range(MAX_Y):
            r = cr(r1, r2, i, MAX_Y)
            g = cr(g1, g2, i, MAX_Y)
            b = cr(b1, b2, i, MAX_Y)
            lcd.set_text_color(r, g, b)
            lcd.draw_Hline(0, i, MAX_X)
    elif dir == 2:
      for i in range(MAX_X):
            r = cr(r2, r1, i, MAX_X)
            g = cr(g2, g1, i, MAX_X)
            b = cr(b2, b1, i, MAX_X)
            lcd.set_text_color(r, g, b)
            lcd.draw_Vline(i, 0, MAX_Y)   
    else:
      for i in range(MAX_Y):
            r = cr(r2, r1, i, MAX_Y)
            g = cr(g2, g1, i, MAX_Y)
            b = cr(b2, b1, i, MAX_Y)
            lcd.set_text_color(r, g, b)
            lcd.draw_Hline(0, i, MAX_X)

def test_rand_char(n = 2000, font = 12, delay = 1):
    if font &gt; 20: _w, _h = 17, 24,
    elif font &gt; 16: _w, _h = 14, 20
    elif font &gt; 12: _w, _h = 11, 16
    elif font &gt; 8: _w, _h = 7, 12
    else: _w, _h = 5, 8

    lcd.clear()
    lcd.set_font(_h)
    mx, my = MAX_X//_w, MAX_Y//_h

    for i in range(n):
      lcd.set_text_color(rand(0xFFFFFF))
      lcd.display_char(rand(mx) * _w, rand(my) * _h, rand(95)+32)
      sleep_ms(delay)

def test_rand_pchar(n = 2000, font = 12, delay = 5):
    if font &gt; 20: _w, _h = 17, 24,
    elif font &gt; 16: _w, _h = 14, 20
    elif font &gt; 12: _w, _h = 11, 16
    elif font &gt; 8: _w, _h = 7, 12
    else: _w, _h = 5, 8

    lcd.clear()
    lcd.set_font(_h)
    mx, my = MAX_X//_w, MAX_Y//_h
    px, py = 0, 0

    for i in range(n):
      lcd.set_text_color(rand(0xFFFFFF))
      lcd.display_char(px * _w, py * _h, rand(95)+32)
      px += 1
      if px &gt;= mx:
            px, py = 0, py + 1
            if py &gt;= my:
                py = my - 1
                lcd.scroll(0, -_h)
                lcd.set_text_color(0)
                lcd.fill_rect(0, MAX_Y - _h, MAX_X-1, MAX_Y-1)
      sleep_ms(delay)

def test_rand_gradient(n = 20, delay = 500):
    for i in range(n):
      test_gradient(rand(0xFFFFFF), rand(0xFFFFFF), rand(4))
      sleep_ms(delay)

MAX_ITER = 60
pal =

def test_mandelbrot(x0 = -2.5, y0 = -2, x1 = 1.5, y1 = 2, ITER = MAX_ITER, RandPAL = 1):
    def calc(c):
      z = 0
      for i in range(ITER):
            z = z * z + c
            if abs(z) &gt; 4:
                return i
      return ITER-1

    if RandPAL:
      global pal
      pal =
   
    lcd.clear(0)

    t0 = ticks_ms()
    dx = (x1 - x0)/MAX_X
    dy = (y1 - y0)/MAX_Y

    for ix in range(MAX_X):
      for iy in range(MAX_Y):
            z = x0 + dx * ix + (y0 + dy * iy) * 1j
            c = calc(z)
            lcd.draw_pixel(ix, iy, pal)
    return ticks_diff(ticks_ms(), t0)

def test_fast_mandelbrot(x0 = -2.5, y0 = -2, x1 = 1.5, y1 = 2, ITER = MAX_ITER, RandPAL = 1):
    def calc(c):
      z = 0
      for i in range(ITER):
            z = z * z + c
            if abs(z) &gt; 4:
                return i
      return ITER-1

    if RandPAL:
      global pal
      pal =
   
    lcd.clear(0)

    t0 = ticks_ms()
    dx = (x1 - x0)/MAX_X
    dy = (y1 - y0)/MAX_Y
    c = *16

    for ix in range(MAX_X//4):
      for iy in range(MAX_Y//4):
            c = *16
            z = x0 + dx * ix * 4 + (y0 + dy * iy * 4) * 1j
            c = calc(z)
            c = calc(z + dx*3)
            c = calc(z + dy*3j)
            c = calc(z + dx*3 + dy*3j)
            if c == c and c == c and c == c:
                lcd.set_text_color(pal%ITER])
                lcd.fill_rect(ix*4, iy*4, 4, 4)
            else:
                for i in range(16):
                  if c == 0:
                        c = calc(z + dx*(i%4) + dy*(i//4)*1j)
                  lcd.draw_pixel(ix*4+(i%4), iy*4+(i//4), pal%ITER])
    return ticks_diff(ticks_ms(), t0)
            
def lcd_test():
    test_rand_line()
    test_rand_rect()
    test_rand_fillrect()
    test_rand_gradient()
    test_rand_char(font = 8)
    test_rand_char(font = 12)
    test_rand_char(font = 16)
    test_rand_char(font = 20)
    test_rand_char(font = 24)
    test_rand_pchar(font = rand(32))

lcd_test()
test_fast_mandelbrot()</code></pre>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><strong>注意事项</strong></p>

<ul>
        <li>注意画图时不要超出屏幕范围,否则可能引起缓冲区溢出造成问题,甚至系统复位</li>
        <li>Alpha通道不能直接叠加到以前的图像上。</li>
</ul>

<p><br />
<b><font color="#5E7384">此内容由EEWORLD论坛网友<font size="3">dcexpert</font>原创,如需转载或用于商业用途需征得作者同意并注明出处</font></b><br />
&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>

dcexpert 发表于 2020-2-17 14:01

本帖最后由 dcexpert 于 2020-2-17 15:23 编辑

<p><strong>随机画线测试</strong>,</p>

<p>&nbsp;</p>

<pre>
<code class="language-python">def test_rand_line(n = 5000, delay = 1):
    lcd.clear()
    lcd.set_font(24)
    lcd.set_text_color(255,255,128,0)
    lcd.display_string_at(0, 0, 'line test' ,0)
    sleep_ms(1000)
    t0 = ticks_ms()
    for i in range(n):
      lcd.set_text_color(rand(0xFF), rand(0xFFFFFF))
      lcd.draw_line(rand(MAX_X), rand(MAX_Y), rand(MAX_X), rand(MAX_Y))
      sleep_ms(delay)
    return ticks_diff(ticks_ms(), t0)</code></pre>

<p>&nbsp;</p>

<p></p>

dcexpert 发表于 2020-2-17 14:06

本帖最后由 dcexpert 于 2020-2-17 15:27 编辑

<p><strong>矩形显示测试</strong></p>

<p>&nbsp;</p>

<pre>
<code class="language-python">def test_rand_rect(n = 2000, delay = 1):
    lcd.clear()
    lcd.set_font(24)
    lcd.set_text_color(255,255,128,0)
    lcd.display_string_at(0, 0, 'rect test' ,0)
    sleep_ms(1000)
    t0 = ticks_ms()
    for i in range(n):
      lcd.set_text_color(rand(0xFF), rand(0xFFFFFF))
      x, y = rand(MAX_X), rand(MAX_Y)
      lcd.draw_rect(x, y, rand(MAX_X - x), rand(MAX_Y - y))
      sleep_ms(delay)
    return ticks_diff(ticks_ms(), t0)

def test_rand_fillrect(n = 2000, delay = 1):
    lcd.clear()
    lcd.set_font(24)
    lcd.set_text_color(255,255,128,0)
    lcd.display_string_at(0, 0, 'fill rect test' ,0)
    sleep_ms(1000)
    t0 = ticks_ms()
    for i in range(n):
      lcd.set_text_color(rand(0xFF), rand(0xFFFFFF))
      x, y = rand(MAX_X), rand(MAX_Y)
      lcd.fill_rect(x, y, rand(MAX_X - x), rand(MAX_Y - y))
      sleep_ms(delay)
    return ticks_diff(ticks_ms(), t0)</code></pre>

<p>&nbsp;</p>

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

<p>&nbsp;</p>

dcexpert 发表于 2020-2-17 14:08

<p>背景渐变显示测试,计算两种颜色的渐变颜色,然后使用draw_Hline或draw_Vline画线填充屏幕</p>

<p>&nbsp;</p>

<pre>
<code class="language-python">def test_gradient(c1=0, c2=0xff, dir=0):
    def cr(x1, x2, i, n):
      return int(x1 + i*(x2-x1)/n)

    r1, g1, b1 = c1&gt;&gt;16, (c1&gt;&gt;8)%256, c1%256
    r2, g2, b2 = c2&gt;&gt;16, (c2&gt;&gt;8)%256, c2%256
    if dir == 0:
      for i in range(MAX_X):
            r = cr(r1, r2, i, MAX_X)
            g = cr(g1, g2, i, MAX_X)
            b = cr(b1, b2, i, MAX_X)
            lcd.set_text_color(r, g, b)
            lcd.draw_Vline(i, 0, MAX_Y)
    elif dir == 1:
      for i in range(MAX_Y):
            r = cr(r1, r2, i, MAX_Y)
            g = cr(g1, g2, i, MAX_Y)
            b = cr(b1, b2, i, MAX_Y)
            lcd.set_text_color(r, g, b)
            lcd.draw_Hline(0, i, MAX_X)
    elif dir == 2:
      for i in range(MAX_X):
            r = cr(r2, r1, i, MAX_X)
            g = cr(g2, g1, i, MAX_X)
            b = cr(b2, b1, i, MAX_X)
            lcd.set_text_color(r, g, b)
            lcd.draw_Vline(i, 0, MAX_Y)   
    else:
      for i in range(MAX_Y):
            r = cr(r2, r1, i, MAX_Y)
            g = cr(g2, g1, i, MAX_Y)
            b = cr(b2, b1, i, MAX_Y)
            lcd.set_text_color(r, g, b)
            lcd.draw_Hline(0, i, MAX_X)</code></pre>

<p>&nbsp;</p>

<p>图片待补充</p>

dcexpert 发表于 2020-2-17 15:31

本帖最后由 dcexpert 于 2020-2-17 15:48 编辑

<p>随机显示字符,可以选择不同大小字体</p>

<p>&nbsp;</p>

<pre>
<code class="language-python">def test_rand_char(n = 2000, font = 12, delay = 1):
    if font &gt; 20: _w, _h = 17, 24,
    elif font &gt; 16: _w, _h = 14, 20
    elif font &gt; 12: _w, _h = 11, 16
    elif font &gt; 8: _w, _h = 7, 12
    else: _w, _h = 5, 8

    lcd.clear()
    lcd.set_font(_h)
    mx, my = MAX_X//_w, MAX_Y//_h

    for i in range(n):
      lcd.set_text_color(rand(0xFFFFFF))
      lcd.display_char(rand(mx) * _w, rand(my) * _h, rand(95)+32)
      sleep_ms(delay)</code></pre>

<p>&nbsp;</p>

<p></p>

<p></p>

dcexpert 发表于 2020-2-17 15:46

<p>显示曼德不罗特集(mandelbrot set)</p>

<p>&nbsp;</p>

<pre>
<code class="language-python">def test_fast_mandelbrot(x0 = -2.5, y0 = -2, x1 = 1.5, y1 = 2, ITER = MAX_ITER, RandPAL = 1):
    def calc(c):
      z = 0
      for i in range(ITER):
            z = z * z + c
            if abs(z) &gt; 4:
                return i
      return ITER-1

    if RandPAL:
      global pal
      pal =
   
    lcd.clear(0)

    t0 = ticks_ms()
    dx = (x1 - x0)/MAX_X
    dy = (y1 - y0)/MAX_Y
    c = *16

    for ix in range(MAX_X//4):
      for iy in range(MAX_Y//4):
            c = *16
            z = x0 + dx * ix * 4 + (y0 + dy * iy * 4) * 1j
            c = calc(z)
            c = calc(z + dx*3)
            c = calc(z + dy*3j)
            c = calc(z + dx*3 + dy*3j)
            if c == c and c == c and c == c:
                lcd.set_text_color(pal%ITER])
                lcd.fill_rect(ix*4, iy*4, 4, 4)
            else:
                for i in range(16):
                  if c == 0:
                        c = calc(z + dx*(i%4) + dy*(i//4)*1j)
                  lcd.draw_pixel(ix*4+(i%4), iy*4+(i//4), pal%ITER])
    return ticks_diff(ticks_ms(), t0)</code></pre>

<p>&nbsp;</p>

<p></p>

<p>&nbsp;</p>

<p></p>
页: [1]
查看完整版本: “玩板”+在STM32F7DISC上重玩micropython(2)