深圳网站专业建设公司无线网络优化是做什么的
《K230 从熟悉到...》屏幕手绘板
- 添加功能
- 带保存功能的手画板
《庐山派 K230 从熟悉到...》屏幕手绘板
https://wiki.lckfb.com/zh-hans/lushan-pi-k230/media/touch.html
添加功能
参考下面的格式去添加自己想要的功能
带保存功能的手画板
import time, os, gc, sys, urandom
from media.display import *
from media.media import *
from machine import TOUCHtry:# LCDDISPLAY_MODE = "LCD"if DISPLAY_MODE == "LCD":# 3.1寸屏幕模式DISPLAY_WIDTH = 800DISPLAY_HEIGHT = 480# 根据模式初始化显示器if DISPLAY_MODE == "LCD":Display.init(Display.ST7701, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, to_ide=False)width = DISPLAY_WIDTHheight = DISPLAY_HEIGHT# 初始化媒体管理器MediaManager.init()# 创建绘制的图像img = image.Image(width, height, image.RGB565)img.clear()# 设置默认画笔颜色和大小current_color = (0, 255, 0) # 默认绿色brush_size = 10 # 默认画笔大小# 定义画布中的按钮区域clear_button_area = (width - 130, 0, 130, 50) # 清除按钮区域(右上角)color_button_area = (0, 0, 130, 50) # 颜色选择按钮区域(左上角)save_button_area = (0, 60, 130, 50) # 保存按钮区域(左上角下方)# 实例化 TOUCH 设备 0tp = TOUCH(0)last_point = None # 记录上一个触摸点def draw_button(x, y, w, h, text, bg_color, text_color):"""绘制圆角矩形按钮"""# 绘制圆角矩形img.draw_rectangle(x, y, w, h, color=bg_color, fill=True)# 绘制文本img.draw_string_advanced(x + 40, y + 8, 30, text, color=text_color, scale=2)def draw_clear_button():"""绘制清除按钮"""draw_button(clear_button_area[0], clear_button_area[1], clear_button_area[2], clear_button_area[3],"清除", (255, 0, 0), (255, 255, 255))def draw_color_buttons():"""绘制颜色选择按钮"""draw_button(color_button_area[0], color_button_area[1], color_button_area[2], color_button_area[3],"随机", (255, 255, 0), (0, 0, 0))# 显示当前颜色img.draw_circle(color_button_area[0] + 170, 25, 20, color=current_color, thickness=3, fill=True)def draw_save_button():"""绘制保存按钮"""draw_button(save_button_area[0], save_button_area[1], save_button_area[2], save_button_area[3],"保存", (0, 128, 255), (255, 255, 255))def select_color(x, y):"""选择随机颜色"""global current_colorif (color_button_area[0] <= x <= color_button_area[0] + color_button_area[2] andcolor_button_area[1] <= y <= color_button_area[1] + color_button_area[3]):current_color = (urandom.getrandbits(8), urandom.getrandbits(8), urandom.getrandbits(8))print(f"select_color to {current_color}")def check_clear_button(x, y):"""检查是否点击了清除按钮"""if (clear_button_area[0] <= x <= clear_button_area[0] + clear_button_area[2] andclear_button_area[1] <= y <= clear_button_area[1] + clear_button_area[3]):img.clear() # 清除画布def check_save_button(x, y):"""检查是否点击了保存按钮"""if (save_button_area[0] <= x <= save_button_area[0] + save_button_area[2] andsave_button_area[1] <= y <= save_button_area[1] + save_button_area[3]):save_image() # 保存图像def save_image():"""保存当前图像到文件"""filename = f'/data/xianyujiang/1.jpg' # 保存路径img.save(filename)print(f"Image saved to {filename}")def draw_line_between_points(last_point, current_point):"""在两个触摸点之间绘制连线,插入中间点以平滑移动"""if last_point is not None:dx = current_point.x - last_point.xdy = current_point.y - last_point.ydistance = (dx ** 2 + dy ** 2) ** 0.5if distance > 30:returnmin_distance = 10 # 插值的最小距离if distance > min_distance:steps = int(distance // min_distance)for i in range(1, steps + 1):new_x = last_point.x + i * dx / (steps + 1)new_y = last_point.y + i * dy / (steps + 1)img.draw_circle(int(new_x), int(new_y), brush_size, color=current_color, thickness=3, fill=True)img.draw_circle(current_point.x, current_point.y, brush_size, color=current_color, thickness=3, fill=True)while True:os.exitpoint()# 只读取 1 个触摸点数据p = tp.read(1)if p != ():for idx, point in enumerate(p, start=1):select_color(point.x, point.y) # 选择颜色check_clear_button(point.x, point.y) # 检查清除按钮check_save_button(point.x, point.y) # 检查保存按钮draw_line_between_points(last_point, point) # 绘制线条last_point = point# 绘制按钮和其他元素draw_clear_button()draw_color_buttons()draw_save_button()# 显示绘制结果Display.show_image(img)time.sleep_ms(1)
except KeyboardInterrupt as e:print(f"user stop")
except BaseException as e:print(f"Exception '{e}'")
finally:# 销毁 displayDisplay.deinit()os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)time.sleep_ms(100)# 释放媒体缓冲区MediaManager.deinit()