写入了新固件,就可以体验LCD和触摸屏了,带触摸功能的大屏,是STM32F7DISC的一大特点,当年多少人为此着迷,这也是这次重新用它的主要原因。
先测试LCD,首先需要载入 lcdF7D库,然后才能使用lcd功能。
import lcdF7D as lcd
其次需要对lcd进行初始化
lcd.init()
然后就可以进行画线、画圆等操作了。下面是几个主要函数用法:
完整的测试程序
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>>16, (c1>>8)%256, c1%256
r2, g2, b2 = c2>>16, (c2>>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 > 20: _w, _h = 17, 24,
elif font > 16: _w, _h = 14, 20
elif font > 12: _w, _h = 11, 16
elif font > 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 > 20: _w, _h = 17, 24,
elif font > 16: _w, _h = 14, 20
elif font > 12: _w, _h = 11, 16
elif font > 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 >= mx:
px, py = 0, py + 1
if py >= 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 = [i%256 for i in range(MAX_ITER)]
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) > 4:
return i
return ITER-1
if RandPAL:
global pal
pal = [rand(0xFFFFFF) for i in range(MAX_ITER)]
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[c%ITER])
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) > 4:
return i
return ITER-1
if RandPAL:
global pal
pal = [rand(0xFFFFFF) for i in range(MAX_ITER)]
lcd.clear(0)
t0 = ticks_ms()
dx = (x1 - x0)/MAX_X
dy = (y1 - y0)/MAX_Y
c = [0]*16
for ix in range(MAX_X//4):
for iy in range(MAX_Y//4):
c = [0]*16
z = x0 + dx * ix * 4 + (y0 + dy * iy * 4) * 1j
c[0] = calc(z)
c[3] = calc(z + dx*3)
c[12] = calc(z + dy*3j)
c[15] = calc(z + dx*3 + dy*3j)
if c[0] == c[3] and c[0] == c[12] and c[0] == c[15]:
lcd.set_text_color(pal[c[0]%ITER])
lcd.fill_rect(ix*4, iy*4, 4, 4)
else:
for i in range(16):
if c[i] == 0:
c[i] = calc(z + dx*(i%4) + dy*(i//4)*1j)
lcd.draw_pixel(ix*4+(i%4), iy*4+(i//4), pal[c[i]%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()
注意事项
此内容由EEWORLD论坛网友dcexpert原创,如需转载或用于商业用途需征得作者同意并注明出处
随机画线测试,
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)
本帖最后由 dcexpert 于 2020-2-17 15:23 编辑
矩形显示测试
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)
本帖最后由 dcexpert 于 2020-2-17 15:27 编辑
背景渐变显示测试,计算两种颜色的渐变颜色,然后使用draw_Hline或draw_Vline画线填充屏幕
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>>16, (c1>>8)%256, c1%256
r2, g2, b2 = c2>>16, (c2>>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 > 20: _w, _h = 17, 24,
elif font > 16: _w, _h = 14, 20
elif font > 12: _w, _h = 11, 16
elif font > 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)
本帖最后由 dcexpert 于 2020-2-17 15:48 编辑
显示曼德不罗特集(mandelbrot set)
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) > 4:
return i
return ITER-1
if RandPAL:
global pal
pal = [rand(0xFFFFFF) for i in range(MAX_ITER)]
lcd.clear(0)
t0 = ticks_ms()
dx = (x1 - x0)/MAX_X
dy = (y1 - y0)/MAX_Y
c = [0]*16
for ix in range(MAX_X//4):
for iy in range(MAX_Y//4):
c = [0]*16
z = x0 + dx * ix * 4 + (y0 + dy * iy * 4) * 1j
c[0] = calc(z)
c[3] = calc(z + dx*3)
c[12] = calc(z + dy*3j)
c[15] = calc(z + dx*3 + dy*3j)
if c[0] == c[3] and c[0] == c[12] and c[0] == c[15]:
lcd.set_text_color(pal[c[0]%ITER])
lcd.fill_rect(ix*4, iy*4, 4, 4)
else:
for i in range(16):
if c[i] == 0:
c[i] = calc(z + dx*(i%4) + dy*(i//4)*1j)
lcd.draw_pixel(ix*4+(i%4), iy*4+(i//4), pal[c[i]%ITER])
return ticks_diff(ticks_ms(), t0)