本程序实现了通过Socket通信收发消息。程序开始时顶部会显示connecting...,连接路由器后显示connected并展开键盘。接收的消息会显示在屏幕顶部,发送消息通过键盘输入并按回车。实际测试时,可能由于实时性问题,接收消息失败率较高,发送消息基本发不出,因此采用try...except进行处理,在收发失败时跳过以确保程序持续运行。
代码如下
import lvgl as lv
import time
from espidf import VSPI_HOST
from ili9XXX import ili9341
from xpt2046 import xpt2046
import fs_driver
from machine import Pin
import network
import usocketi=0
# ------------------------------ 屏幕初始化操作 --start------------------------
# 屏幕宽高
WIDTH = 240
HEIGHT = 320# 创建显示屏对象
disp = ili9341(miso=19, mosi=23, clk=18, cs=5, dc=26, rst=27, power=14, backlight=-1, backlight_on=0, power_on=0, rot=0x80,spihost=VSPI_HOST, mhz=60, factor=16, hybrid=True, width=WIDTH, height=HEIGHT,invert=False, double_buffer=True, half_duplex=False, initialize=True)# 创建触摸屏对象
touch = xpt2046(cs=25, spihost=VSPI_HOST, mosi=-1, miso=-1, clk=-1, cal_y0 = 423, cal_y1=3948)
# ------------------------------ 屏幕初始化操作 --stop------------------------# 1. 创建显示screen对象。将需要显示的组件添加到这个screen才能显示
scr = lv.obj() # scr====> screen 屏幕
fs_drv = lv.fs_drv_t()
fs_driver.fs_register(fs_drv, 'S')
scr = lv.scr_act()
scr.clean()#路由器WIFI账号和密码
ssid=" "
password=" "#服务器地址和端口
dest_ip="192.168.1.100"
dest_port=10000#WIFI连接
def wifi_connect():wlan=network.WLAN(network.STA_IF) #STA模式wlan.active(True) #激活start_time=time.time() #记录时间做超时判断if not wlan.isconnected():print("conneting to network...")wlan.connect(ssid,password) #输入WIFI账号和密码while not wlan.isconnected():#超时判断,15 秒没连接成功判定为超时if time.time()-start_time>15:print("WIFI Connect Timeout!")breakreturn Falseelse:print("network information:", wlan.ifconfig())return True# 2. 封装要显示的组件
class MyWidget():def __init__(self, scr):# 创建文本框ta = lv.textarea(scr)ta.set_one_line(False) # 关闭1行模式ta.set_size(240, 60) # 设置宽高ta.align(lv.ALIGN.TOP_MID, 0, 30) # 设置位置ta.add_event_cb(lambda e: self.textarea_event_handler(e, ta), lv.EVENT.READY, None) # 添加回调函数ta.add_state(lv.STATE.FOCUSED) # 设置光标# 定义数字键盘要显示的内容btnm_map = ["a", "b", "c","d", "e", "f", "\n","g", "h", "i","j", "k", "l", "\n","m", "n", "o","p", "q", "r", "\n","s", "t", "u","v", "w", "x", "\n","y", "z", "1","2", "3", "4", "\n","5", "6", "7","8", "9", "0", "\n",",", ".", "?","!",lv.SYMBOL.BACKSPACE, lv.SYMBOL.NEW_LINE, ""]# 按钮矩阵btnm = lv.btnmatrix(scr)btnm.set_size(240, 230) # 设置宽高btnm.align(lv.ALIGN.BOTTOM_MID, 0, 0) # 设置位置btnm.add_event_cb(lambda e: self.btnm_event_handler(e, ta), lv.EVENT.VALUE_CHANGED, None)btnm.clear_flag(lv.obj.FLAG.CLICK_FOCUSABLE) # 设置为非活跃,即文本框中的光标一直聚焦btnm.set_map(btnm_map) # 设置要显示的内容(数字键盘)# 设置有源蜂鸣器引脚self.p15 = Pin(15, Pin.OUT)self.p15.value(1) # 不响def textarea_event_handler(self, e, ta):try:socket.send(ta.get_text()) except:passdef btnm_event_handler(self, e, ta):obj = e.get_target()txt = obj.get_btn_text(obj.get_selected_btn()) # 获取被点击的按钮的内容,例如数字3if txt == lv.SYMBOL.BACKSPACE: # 如果是退格键,那么就删除1个字符ta.del_char()elif txt == lv.SYMBOL.NEW_LINE: # 如果是回车键,那么就触发事件lv.event_send(ta, lv.EVENT.READY, None)elif txt:ta.add_text(txt) # 如果不是回车键,那么就将当前数字显示到文本框btn = lv.btn(scr) # 将当前按钮与screen对象进行关联
btn.set_size(240, 30) # 设置按钮尺寸
btn.align(lv.ALIGN.TOP_MID,0,0) # 置于屏幕顶部
label = lv.label(btn) # 在按钮上创建一个标签Label,用来显示文字用
label.set_text("connecting...") # 设置文字内容
label.center() # 相对于父对象居中while i<10 :i+=1if wifi_connect():socket=usocket.socket() #创建socket连接addr=(dest_ip,dest_port) #服务器IP地址和端口socket.connect(addr)print("Connected")i=10 # 3. 创建要显示的组件
MyWidget(scr)
# 4. 显示screen对象中的内容
lv.scr_load(scr)
label.set_text('connected')
while True:try:text=socket.recv(128) #单次最多接收128字节if text==None:passelse: text=text.decode("utf-8")label.set_text(text) time.sleep_ms(300)except:pass