You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Draw app: make it faster
This commit is contained in:
@@ -6,34 +6,11 @@ indev_error_y = 120
|
||||
|
||||
DARKPINK = lv.color_hex(0xEC048C)
|
||||
|
||||
# doesnt work:
|
||||
def draw_line(x, y):
|
||||
global canvas
|
||||
# Line drawing like this doesn't work:
|
||||
layer = lv.layer_t()
|
||||
canvas.init_layer(layer)
|
||||
dsc = lv.draw_line_dsc_t()
|
||||
dsc.color = DARKPINK
|
||||
dsc.width = 4
|
||||
dsc.round_end = 1
|
||||
dsc.round_start = 1
|
||||
dsc.p1 = lv.point_precise_t()
|
||||
dsc.p1.x = x
|
||||
dsc.p1.y = y
|
||||
dsc.p2 = lv.point_precise_t()
|
||||
dsc.p2.x = 100
|
||||
dsc.p2.y = 200
|
||||
#layer.draw_line(dsc) doesnt exist!
|
||||
lv.draw_line(layer,dsc) # doesnt do anything!
|
||||
canvas.finish_layer(layer)
|
||||
|
||||
|
||||
|
||||
|
||||
class Draw(Activity):
|
||||
|
||||
hor_res = 0
|
||||
ver_res = 0
|
||||
layer = None
|
||||
|
||||
# Widgets:
|
||||
canvas = None
|
||||
@@ -51,44 +28,54 @@ class Draw(Activity):
|
||||
self.canvas.fill_bg(lv.color_white(), lv.OPA.COVER)
|
||||
self.canvas.add_flag(lv.obj.FLAG.CLICKABLE)
|
||||
self.canvas.add_event_cb(self.touch_cb, lv.EVENT.ALL, None)
|
||||
self.layer = lv.layer_t()
|
||||
self.canvas.init_layer(self.layer)
|
||||
self.setContentView(screen)
|
||||
|
||||
def touch_cb(self, event):
|
||||
event_code=event.get_code()
|
||||
# Ignore:
|
||||
# =======
|
||||
# 19: HIT_TEST
|
||||
# COVER_CHECK
|
||||
# DRAW_MAIN
|
||||
# DRAW_MAIN_BEGIN
|
||||
# DRAW_MAIN_END
|
||||
# DRAW_POST
|
||||
# DRAW_POST_BEGIN
|
||||
# DRAW_POST_END
|
||||
# GET_SELF_SIZE
|
||||
if event_code not in [19,23,25,26,27,28,29,30,49]:
|
||||
name = mpos.ui.get_event_name(event_code)
|
||||
#print(f"lv_event_t: code={event_code}, name={name}") # target={event.get_target()}, user_data={event.get_user_data()}, param={event.get_param()}
|
||||
if event_code == lv.EVENT.PRESSING: # this is probably enough
|
||||
#if event_code in [lv.EVENT.PRESSED, lv.EVENT.PRESSING, lv.EVENT.LONG_PRESSED, lv.EVENT.LONG_PRESSED_REPEAT]:
|
||||
x, y = mpos.ui.get_pointer_xy()
|
||||
# drawing a point works:
|
||||
#canvas.set_px(x,y,lv.color_black(),lv.OPA.COVER)
|
||||
#
|
||||
# drawing a square like this works:
|
||||
#for dx in range(-5,5):
|
||||
# for dy in range(-5,5):
|
||||
# canvas.set_px(x+dx,y+dy,DARKPINK,lv.OPA.COVER)
|
||||
#
|
||||
# drawing a circle works:
|
||||
radius = 7 # Set desired radius
|
||||
if x == indev_error_x and y == indev_error_y:
|
||||
radius = 25 # in case of indev error
|
||||
square = radius * radius
|
||||
for dx in range(-radius, radius):
|
||||
for dy in range(-radius, radius):
|
||||
if dx * dx + dy * dy <= square:
|
||||
newx, newy = x + dx, y + dy
|
||||
if 0 <= newx <= self.hor_res and 0 <= newy <= self.ver_res: # don't draw outside of canvas because that may crash
|
||||
self.canvas.set_px(x + dx, y + dy, DARKPINK, lv.OPA.COVER)
|
||||
|
||||
#canvas.set_px(x,y,lv.color_black(),lv.OPA.COVER) # draw a tiny point
|
||||
self.draw_rect(x,y)
|
||||
#self.draw_line(x,y)
|
||||
return
|
||||
|
||||
def draw_line(self, x, y):
|
||||
dsc = lv.draw_line_dsc_t()
|
||||
lv.draw_line_dsc_t.init(dsc)
|
||||
dsc.color = DARKPINK
|
||||
dsc.width = 4
|
||||
dsc.round_end = 1
|
||||
dsc.round_start = 1
|
||||
dsc.p1 = lv.point_precise_t()
|
||||
dsc.p1.x = x
|
||||
dsc.p1.y = y
|
||||
dsc.p2 = lv.point_precise_t()
|
||||
dsc.p2.x = 100
|
||||
dsc.p2.y = 200
|
||||
lv.draw_line(self.layer,dsc)
|
||||
self.canvas.finish_layer(self.layer)
|
||||
|
||||
@micropython.viper # make it with native compilation
|
||||
def draw_rect(self, x: int, y: int):
|
||||
draw_dsc = lv.draw_rect_dsc_t()
|
||||
lv.draw_rect_dsc_t.init(draw_dsc)
|
||||
draw_dsc.bg_color = lv.color_hex(0xffaaaa)
|
||||
draw_dsc.radius = lv.RADIUS_CIRCLE
|
||||
draw_dsc.border_color = lv.color_hex(0xff5555)
|
||||
draw_dsc.border_width = 2
|
||||
draw_dsc.outline_color = lv.color_hex(0xff0000)
|
||||
draw_dsc.outline_pad = 3
|
||||
draw_dsc.outline_width = 2
|
||||
a = lv.area_t()
|
||||
a.x1 = x-10
|
||||
a.y1 = y-10
|
||||
a.x2 = x+10
|
||||
a.y2 = y+10
|
||||
lv.draw_rect(self.layer, draw_dsc, a)
|
||||
self.canvas.finish_layer(self.layer)
|
||||
|
||||
Reference in New Issue
Block a user