1735 views|5 replies

1w

Posts

25

Resources
The OP
 

"Playing with the Board" + Replaying MicroPython on the STM32F7DISC (2) [Copy link]

 
This post was last edited by dcexpert on 2020-2-17 15:48

After writing the new firmware, you can experience the LCD and touch screen. The large screen with touch function is a major feature of STM32F7DISC. Many people were fascinated by it back then, and this is also the main reason for using it again this time.

To test the LCD first, you need to load the lcdF7D library before you can use the LCD function.

import lcdF7D as lcd

Secondly, you need to initialize the LCD

lcd.init()

Then you can draw lines, circles, etc. Here are some main functions:

  • clear(color), clear the entire screen
    color with color color is defined as
    • If empty, use the color defined in set_text_color
    • If it is a number, it is in the form of 0xRRGGBB
    • If there are two numbers, the first one represents Alpha, and the second one is the same as above.
    • If it is 3 numbers, they represent R, G, and B components respectively, and Alpha is fixed to 255
    • If it is 4 numbers, they represent A, R, G, and B respectively.
  • set_text_color(color), set the foreground color
  • set_back_color(color), set the background color
  • draw_line(x1, y1, x2, y2), draw a straight line
  • draw_Hline(x, y, len), draw a horizontal line
  • draw_Vline(x, y, len), draw a vertical line
  • draw_rect(x1, y1, x2, y2), draw a hollow rectangle
  • fill_rect(x1, y1, x2, y2), draw a solid rectangle
  • draw_circle(x, y, r), draw a circle
  • set_font(n), set the font size, valid sizes are 8/12/16/20/24
  • display_char(x, y, asc), displays the character at x, y
  • display_string_at(x, y, string, mode), display string

Complete testing program

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()

Precautions

  • Be careful not to exceed the screen range when drawing, otherwise it may cause buffer overflow and even system reset.
  • Alpha channels cannot be directly superimposed onto previous images.


This content is originally created by dcexpert , a user of EEWORLD forum. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source

 
 

1w

Posts

25

Resources
2
 
This post was last edited by dcexpert on 2020-2-17 15:23

Random line drawing test ,

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)

 
 
 

1w

Posts

25

Resources
3
 
This post was last edited by dcexpert on 2020-2-17 15:27

Rectangular display test

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)


 
 
 

1w

Posts

25

Resources
4
 

Background gradient display test, calculate the gradient color of two colors, and then use draw_Hline or draw_Vline to draw lines to fill the screen

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)

Pictures to be added

 
 
 

1w

Posts

25

Resources
5
 
This post was last edited by dcexpert on 2020-2-17 15:48

Display characters randomly, you can choose different font sizes

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)

 
 
 

1w

Posts

25

Resources
6
 

Display the 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)

 
 
 

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