K230D BOX利用其具有的触摸功能,提供了一个单色的徒手绘图画板。
鉴于其表现力受到一定的制约,这里就为其配置了色彩设置功能,以提高表现力。
当需要实时变更画笔色彩时,只需点触一下“当前色彩”的位置,即可切换到下一种色彩,这样用起来就方便许多。
图1 是其操作界面,在使用时只需在屏幕上滑动,就可绘制出指定色彩的内容。

图1 绘制效果
实现彩色画板功能的程序内容为:
import time, os, urandom, sys
from media.display import *
from media.media import *
from machine import TOUCH
# 实例化TOUCH设备0
tp = TOUCH(0)
c=0
def load_draw_dialog():
img.clear()
# 清屏
img.draw_string_advanced(500, 0, 30, "【 清除 】", color = (255, 255, 255))
img.draw_string_advanced(240, 0, 30, "【 彩色画板 】", color = (0,255, 0))
img.draw_string_advanced(30, 0, 30, "【当前色彩】", color = ( 255,0,0))
t=0
for t in range(0,639): # 画线输出
img.draw_circle(t, 50, 1, color=(255, 255, 255)) # 画点
img.draw_circle(t, 470, 1, color=(255, 255, 255)) # 画点
def lcd_draw_bline(x1,y1,x2,y2,size,color):
t = 0
xerr = 0
yerr = 0
delta_x = 0
delta_y = 0
distance = 0
incx = 0
incy = 0
row = 0
col = 0
delta_x = x2 - x1 # 计算坐标增量
delta_y = y2 - y1
row = x1
col = y1
if delta_x > 0:
incx = 1 # 置单步方向
elif delta_x == 0:
incx = 0 # 垂直线
else:
incx = -1
delta_x = -delta_x
if delta_y > 0:
incy = 1
elif delta_y == 0:
incy = 0 # 水平线
else:
incy = -1
delta_y = -delta_y
if delta_x > delta_y:
distance = delta_x; # 选取基本增量坐标轴
else:
distance = delta_y
for t in range(0,distance + 1): # 画线输出
img.draw_circle(row, col, size, color) # 画点
xerr += delta_x
yerr += delta_y
if xerr > distance:
xerr -= distance
row += incx
if yerr > distance:
yerr -= distance
col += incy
if __name__ == "__main__":
os.exitpoint(os.EXITPOINT_ENABLE)
img = image.Image(640, 480, image.RGB888)
Display.init(Display.ST7701, width=640, height=480, to_ide=True)
MediaManager.init()
lastpos = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]] #最后一次的数据
# 清空屏幕并在右上角显示"RST"
load_draw_dialog()
Display.show_image(img, 640, 480)
try:
while True:
# 获取TOUCH数据
p = tp.read(5)
if p == (): # 发生触摸事件
lastpos = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]] # 重新清空所有点
for i in range(len(p)): # 打印每个点坐标信息,最大5点。
if (p[i].x < 640 and p[i].y < 480):
#print(p[i].x)
#print(p[i].y)
if lastpos[i][0] == 0x0 and lastpos[i][1] == 0x0:
lastpos[i][0] = p[i].x
lastpos[i][1] = p[i].y
if p[i].y > 50:
if c==0:
lcd_draw_bline(lastpos[i][0], lastpos[i][1], p[i].x, p[i].y, 2, color=(255,0,0))
if c==1:
lcd_draw_bline(lastpos[i][0], lastpos[i][1], p[i].x, p[i].y, 2, color=(0,255,0))
if c==2:
lcd_draw_bline(lastpos[i][0], lastpos[i][1], p[i].x, p[i].y, 2, color=(0,0,255))
lastpos[i][0] = p[i].x
lastpos[i][1] = p[i].y
if (p[i].x > 500 and p[i].y < 50):
load_draw_dialog()
lastpos[i][0] = 0
lastpos[i][1] = 0
if (p[i].x > 30 and p[i].x <500 and p[i].y < 50):
c=(c+1)%3
print(c)
if c==0:
img.draw_string_advanced(30, 0, 30, "【当前色彩】", color = (255,0,0))
if c==1:
img.draw_string_advanced(30, 0, 30, "【当前色彩】", color = (0,255,0))
if c==2:
img.draw_string_advanced(30, 0, 30, "【当前色彩】", color = (0,0,255))
else:
lastpos[i][0] = 0x0
lastpos[i][1] = 0x0
# 刷新到显示器上
Display.show_image(img)
time.sleep_ms(5)
os.exitpoint()
except KeyboardInterrupt as e:
print("user stop: ", e)
except BaseException as e:
print(f"Exception {e}")
# deinit display
Display.deinit()
os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
time.sleep_ms(100)
# release media buffer
MediaManager.deinit()经运行程序,其设置色彩的编码值输出见图2所示,使用彩色画板绘制的效果见图3所示。

图2 色彩编码值输出

图3 绘制效果
我要赚赏金
