玩转RP2040之设计一个表盘
<div class='showpostmsg'><p><br /></p>
<p cid="n0" mdtype="paragraph">上篇文章,介绍了使用Python在LCD上显示各种类型的文字。</p>
<p> </p>
<p cid="n2" mdtype="paragraph">本篇,就来使用字体显示功能,来实现一个表盘。最终效果如下:</p>
<p cid="n3" mdtype="paragraph"></p>
<p cid="n4" mdtype="paragraph">表盘的内容包括:</p>
<ul cid="n5" data-mark="-" mdtype="list">
<li cid="n6" mdtype="list_item">
<p cid="n7" mdtype="paragraph">顶部的蓝牙连接图标和电量图标</p>
<ul cid="n8" data-mark="-" mdtype="list">
<li cid="n9" mdtype="list_item">
<p cid="n10" mdtype="paragraph">蓝牙图标通过画直线来完成</p>
<pre style="background:#555; padding:10px; color:#ddd !important;">
#蓝牙
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)</pre>
</li>
<li cid="n12" mdtype="list_item">
<p cid="n13" mdtype="paragraph">电量图标通过画直线和矩形来完成</p>
<pre style="background:#555; padding:10px; color:#ddd !important;">
#电量
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)</pre>
</li>
</ul>
</li>
<li cid="n15" mdtype="list_item">
<p cid="n16" mdtype="paragraph">中间的是当前的时间(时、分、秒)、日期和星期</p>
<p cid="n17" mdtype="paragraph">日期目前的是一个固定值,后面可优化为动态的。时间设计的是动态的,每过一秒会更新秒值,每过一分钟会更新分钟值。</p>
<pre style="background:#555; padding:10px; color:#ddd !important;">
#日期与星期
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)</pre>
<p cid="n19" mdtype="paragraph"> </p>
</li>
<li cid="n20" mdtype="list_item">
<p cid="n21" mdtype="paragraph">底部是运动步数的显示</p>
<pre style="background:#555; padding:10px; color:#ddd !important;">
#运动步数
tft.text(vga1_bold_16x16, "walk:4567", 50, 190, gc9a01.YELLOW)</pre>
</li>
</ul>
<p cid="n23" mdtype="paragraph">演示视频:</p>
<p cid="n23" mdtype="paragraph">b629c13f0071025d7c687764a813609d<br />
</p>
<p cid="n24" mdtype="paragraph">完整代码:</p>
<pre style="background:#555; padding:10px; color:#ddd !important;">
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()</pre>
<p cid="n26" mdtype="paragraph">总结,本篇在RP2040上实现一个表盘,实现了蓝牙、电量图标,时间与日志显示,运动步数显示等。</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> <p>用Python在LCD上显示看来可以做很多东西啦</p>
页:
[1]