|
上篇文章,介绍了使用Python在LCD上显示各种类型的文字。
本篇,就来使用字体显示功能,来实现一个表盘。最终效果如下:
表盘的内容包括:
-
顶部的蓝牙连接图标和电量图标
-
蓝牙图标通过画直线来完成
#蓝牙
bl_x = 70
bl_y = 17
tft.vline(bl_x, bl_y, 20, gc9a01.BLUE)
tft.line(bl_x - 5, bl_y + 3, bl_x + 5, bl_y + 20 - 6, gc9a01.BLUE)
tft.line(bl_x - 5, bl_y + 20 - 3, bl_x + 5, bl_y + 6, gc9a01.BLUE)
tft.line(bl_x, bl_y, bl_x + 5, bl_y + 6, gc9a01.BLUE)
tft.line(bl_x, bl_y + 20, bl_x + 5, bl_y + 20 - 6, gc9a01.BLUE)
-
电量图标通过画直线和矩形来完成
#电量
tft.text(vga1_bold_16x16, "80%", 95, 20, gc9a01.CYAN)
bat_x = 145
bat_y = 20
bat_w = 30
bat_h = 15
tft.hline(bat_x, bat_y, bat_w, gc9a01.WHITE)
tft.hline(bat_x, bat_y + bat_h, bat_w, gc9a01.WHITE)
tft.vline(bat_x, bat_y, bat_h, gc9a01.WHITE)
tft.vline(bat_x + bat_w, bat_y, bat_h, gc9a01.WHITE)
tft.vline(bat_x + bat_w + 1, bat_y + 2, bat_h - 4, gc9a01.WHITE)
tft.vline(bat_x + bat_w + 2, bat_y + 2, bat_h - 4, gc9a01.WHITE)
tft.fill_rect(bat_x + 3, bat_y + 3, bat_w - 10, bat_h -5 , gc9a01.GREEN)
-
中间的是当前的时间(时、分、秒)、日期和星期
日期目前的是一个固定值,后面可优化为动态的。时间设计的是动态的,每过一秒会更新秒值,每过一分钟会更新分钟值。
#日期与星期
display_inconsolata_font(inconsolata_16, "12/08", 150, 60)
tft.text(vga1_bold_16x16, "Thur", 150, 90, gc9a01.YELLOW)
#时分秒
hour = 22
minute = 08
second = 55
last_hour = -1
last_minute = -1
last_second = -1
while True:
second = second + 1
if second >= 60:
minute = minute + 1
second = 0
hour_str = "%02d" % hour
minute_str = "%02d" % minute
second_str = "%02d" % second
if last_hour != hour:
tft.fill_rect(25, 50, 115, 60, gc9a01.BLACK)
tft.write(chango_64, hour_str, 25, 50, gc9a01.WHITE)
if last_minute != minute:
tft.fill_rect(60, 105, 115, 60, gc9a01.BLACK)
tft.write(chango_64, minute_str, 60, 105, gc9a01.YELLOW)
if last_second != second:
tft.write(NotoSans_32, second_str, 180, 130, gc9a01.GREEN)
last_hour = hour
last_minute = minute
last_second = second
time.sleep(1)
-
底部是运动步数的显示
#运动步数
tft.text(vga1_bold_16x16, "walk:4567", 50, 190, gc9a01.YELLOW)
演示视频:
VID_20221211_110813_x264
完整代码:
import time
from machine import Pin, SPI
import gc9a01
from prop_font import chango_16
from prop_font import chango_32
from prop_font import chango_64
from mono_font import inconsolata_16
from mono_font import inconsolata_32
from mono_font import inconsolata_64
import NotoSans_32
import NotoSerif_32
import NotoSansMono_32
import vga1_bold_16x16
DC = 8
CS = 9
SCK = 10
MOSI = 11
RST = 12
BL = 25
def main():
def display_inconsolata_font(font, data, column, row):
for char in data:
tft.bitmap(font, column, row, font.MAP.index(char))
column += font.WIDTH
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)
#蓝牙
bl_x = 70
bl_y = 17
tft.vline(bl_x, bl_y, 20, gc9a01.BLUE)
tft.line(bl_x - 5, bl_y + 3, bl_x + 5, bl_y + 20 - 6, gc9a01.BLUE)
tft.line(bl_x - 5, bl_y + 20 - 3, bl_x + 5, bl_y + 6, gc9a01.BLUE)
tft.line(bl_x, bl_y, bl_x + 5, bl_y + 6, gc9a01.BLUE)
tft.line(bl_x, bl_y + 20, bl_x + 5, bl_y + 20 - 6, gc9a01.BLUE)
#电量
tft.text(vga1_bold_16x16, "80%", 95, 20, gc9a01.CYAN)
bat_x = 145
bat_y = 20
bat_w = 30
bat_h = 15
tft.hline(bat_x, bat_y, bat_w, gc9a01.WHITE)
tft.hline(bat_x, bat_y + bat_h, bat_w, gc9a01.WHITE)
tft.vline(bat_x, bat_y, bat_h, gc9a01.WHITE)
tft.vline(bat_x + bat_w, bat_y, bat_h, gc9a01.WHITE)
tft.vline(bat_x + bat_w + 1, bat_y + 2, bat_h - 4, gc9a01.WHITE)
tft.vline(bat_x + bat_w + 2, bat_y + 2, bat_h - 4, gc9a01.WHITE)
tft.fill_rect(bat_x + 3, bat_y + 3, bat_w - 10, bat_h -5 , gc9a01.GREEN)
#日期与星期
display_inconsolata_font(inconsolata_16, "12/08", 150, 60)
tft.text(vga1_bold_16x16, "Thur", 150, 90, gc9a01.YELLOW)
#运动步数
tft.text(vga1_bold_16x16, "walk:4567", 50, 190, gc9a01.YELLOW)
#时分秒
hour = 22
minute = 08
second = 55
last_hour = -1
last_minute = -1
last_second = -1
while True:
second = second + 1
if second >= 60:
minute = minute + 1
second = 0
hour_str = "%02d" % hour
minute_str = "%02d" % minute
second_str = "%02d" % second
if last_hour != hour:
tft.fill_rect(25, 50, 115, 60, gc9a01.BLACK)
tft.write(chango_64, hour_str, 25, 50, gc9a01.WHITE)
if last_minute != minute:
tft.fill_rect(60, 105, 115, 60, gc9a01.BLACK)
tft.write(chango_64, minute_str, 60, 105, gc9a01.YELLOW)
if last_second != second:
tft.write(NotoSans_32, second_str, 180, 130, gc9a01.GREEN)
last_hour = hour
last_minute = minute
last_second = second
time.sleep(1)
main()
总结,本篇在RP2040上实现一个表盘,实现了蓝牙、电量图标,时间与日志显示,运动步数显示等。
|
|