RP2040前面的几篇测试报告:
1 玩转RP2040之开箱测评与上电运行
2 玩转RP2040之Python开发环境搭建
3 玩转RP2040之LCD绘制基本形状测试
4 玩转RP2040之使用Python在LCD上显示图片
5 玩转RP2040之使用Python在LCD上显示多类型字体
6 玩转RP2040之设计一个表盘
本篇实现一个综合的功能:
程序编写
一些定义
import time
from machine import Pin, SPI
import gc9a01
import myimu
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
表盘显示功能封装为一个类
class WatchPanel:
def init(self):
self.spi = SPI(1, baudrate=60000000, sck=Pin(SCK), mosi=Pin(MOSI))
self.tft = gc9a01.GC9A01(
self.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)
#时分秒
self.hour = 22
self.minute = 08
self.second = 55
self.last_hour = -1
self.last_minute = -1
self.last_second = -1
# enable display and clear screen
self.tft.init()
def display_inconsolata_font(self, font, data, column, row):
for char in data:
self.tft.bitmap(font, column, row, font.MAP.index(char))
column += font.WIDTH
def show_static_info(self):
#蓝牙
bl_x = 70
bl_y = 17
self.tft.vline(bl_x, bl_y, 20, gc9a01.BLUE)
self.tft.line(bl_x - 5, bl_y + 3, bl_x + 5, bl_y + 20 - 6, gc9a01.BLUE)
self.tft.line(bl_x - 5, bl_y + 20 - 3, bl_x + 5, bl_y + 6, gc9a01.BLUE)
self.tft.line(bl_x, bl_y, bl_x + 5, bl_y + 6, gc9a01.BLUE)
self.tft.line(bl_x, bl_y + 20, bl_x + 5, bl_y + 20 - 6, gc9a01.BLUE)
#电量
self.tft.text(vga1_bold_16x16, "80%", 95, 20, gc9a01.CYAN)
bat_x = 145
bat_y = 20
bat_w = 30
bat_h = 15
self.tft.hline(bat_x, bat_y, bat_w, gc9a01.WHITE)
self.tft.hline(bat_x, bat_y + bat_h, bat_w, gc9a01.WHITE)
self.tft.vline(bat_x, bat_y, bat_h, gc9a01.WHITE)
self.tft.vline(bat_x + bat_w, bat_y, bat_h, gc9a01.WHITE)
self.tft.vline(bat_x + bat_w + 1, bat_y + 2, bat_h - 4, gc9a01.WHITE)
self.tft.vline(bat_x + bat_w + 2, bat_y + 2, bat_h - 4, gc9a01.WHITE)
self.tft.fill_rect(bat_x + 3, bat_y + 3, bat_w - 10, bat_h -5 , gc9a01.GREEN)
#日期与星期
self.display_inconsolata_font(inconsolata_16, "12/08", 150, 60)
self.tft.text(vga1_bold_16x16, "Thur", 150, 90, gc9a01.YELLOW)
#运动步数
self.tft.text(vga1_bold_16x16, "walk:4567", 50, 190, gc9a01.YELLOW)
def show_time_info(self):
self.second = self.second + 1
if self.second >= 60:
self.minute = self.minute + 1
self.second = 0
hour_str = "%02d" % self.hour
minute_str = "%02d" % self.minute
second_str = "%02d" % self.second
self.tft.fill_rect(25, 50, 115, 60, gc9a01.BLACK)
self.tft.write(chango_64, hour_str, 25, 50, gc9a01.WHITE)
self.tft.fill_rect(60, 105, 115, 60, gc9a01.BLACK)
self.tft.write(chango_64, minute_str, 60, 105, gc9a01.YELLOW)
self.tft.write(NotoSans_32, second_str, 180, 130, gc9a01.GREEN)
self.last_hour = self.hour
self.last_minute = self.minute
self.last_second = self.second
def show_img(self, img):
self.tft.jpg(img, 0, 0, gc9a01.SLOW)
def show_watch(self):
self.tft.fill(gc9a01.BLACK)
self.show_static_info()
self.show_time_info()
主函数
def main():
#IMU
qmi8658=myimu.IMU_QMI8658C()
watchPanel=WatchPanel()
watchPanel.init()
idx = 1
while True:
#read QMI8658
xyz=qmi8658.Read_XYZ()
print("accz:%f" % xyz[2])
if xyz[2] > -1:
idx = idx+1
if (idx == 4):
idx = 1
if (idx == 1):
watchPanel.show_watch()
elif (idx == 2):
watchPanel.show_img("weather.jpg")
elif (idx == 3):
watchPanel.show_img("pay.jpg")
time.sleep(1)
main()
测试
显示时间界面
显示天气界面
显示支付宝界面
演示视频
VID_20230102_215443_x264
总结
本篇利用RP2040实现了一个综合功能:运动表盘的显示与功能的切换,切换功能只利用IMU检测Z轴加速度的变化,后续可考虑结合6轴的数据解算出姿态角,从而可以使姿态检测更加准确。