Skip to content

Commit af91013

Browse files
Draw app: make it faster
1 parent 83e71a7 commit af91013

File tree

1 file changed

+42
-55
lines changed
  • internal_filesystem/apps/com.micropythonos.draw/assets

1 file changed

+42
-55
lines changed

internal_filesystem/apps/com.micropythonos.draw/assets/draw.py

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,11 @@
66

77
DARKPINK = lv.color_hex(0xEC048C)
88

9-
# doesnt work:
10-
def draw_line(x, y):
11-
global canvas
12-
# Line drawing like this doesn't work:
13-
layer = lv.layer_t()
14-
canvas.init_layer(layer)
15-
dsc = lv.draw_line_dsc_t()
16-
dsc.color = DARKPINK
17-
dsc.width = 4
18-
dsc.round_end = 1
19-
dsc.round_start = 1
20-
dsc.p1 = lv.point_precise_t()
21-
dsc.p1.x = x
22-
dsc.p1.y = y
23-
dsc.p2 = lv.point_precise_t()
24-
dsc.p2.x = 100
25-
dsc.p2.y = 200
26-
#layer.draw_line(dsc) doesnt exist!
27-
lv.draw_line(layer,dsc) # doesnt do anything!
28-
canvas.finish_layer(layer)
29-
30-
31-
32-
339
class Draw(Activity):
3410

3511
hor_res = 0
3612
ver_res = 0
13+
layer = None
3714

3815
# Widgets:
3916
canvas = None
@@ -51,44 +28,54 @@ def onCreate(self):
5128
self.canvas.fill_bg(lv.color_white(), lv.OPA.COVER)
5229
self.canvas.add_flag(lv.obj.FLAG.CLICKABLE)
5330
self.canvas.add_event_cb(self.touch_cb, lv.EVENT.ALL, None)
31+
self.layer = lv.layer_t()
32+
self.canvas.init_layer(self.layer)
5433
self.setContentView(screen)
5534

5635
def touch_cb(self, event):
5736
event_code=event.get_code()
58-
# Ignore:
59-
# =======
60-
# 19: HIT_TEST
61-
# COVER_CHECK
62-
# DRAW_MAIN
63-
# DRAW_MAIN_BEGIN
64-
# DRAW_MAIN_END
65-
# DRAW_POST
66-
# DRAW_POST_BEGIN
67-
# DRAW_POST_END
68-
# GET_SELF_SIZE
6937
if event_code not in [19,23,25,26,27,28,29,30,49]:
7038
name = mpos.ui.get_event_name(event_code)
7139
#print(f"lv_event_t: code={event_code}, name={name}") # target={event.get_target()}, user_data={event.get_user_data()}, param={event.get_param()}
7240
if event_code == lv.EVENT.PRESSING: # this is probably enough
7341
#if event_code in [lv.EVENT.PRESSED, lv.EVENT.PRESSING, lv.EVENT.LONG_PRESSED, lv.EVENT.LONG_PRESSED_REPEAT]:
7442
x, y = mpos.ui.get_pointer_xy()
75-
# drawing a point works:
76-
#canvas.set_px(x,y,lv.color_black(),lv.OPA.COVER)
77-
#
78-
# drawing a square like this works:
79-
#for dx in range(-5,5):
80-
# for dy in range(-5,5):
81-
# canvas.set_px(x+dx,y+dy,DARKPINK,lv.OPA.COVER)
82-
#
83-
# drawing a circle works:
84-
radius = 7 # Set desired radius
85-
if x == indev_error_x and y == indev_error_y:
86-
radius = 25 # in case of indev error
87-
square = radius * radius
88-
for dx in range(-radius, radius):
89-
for dy in range(-radius, radius):
90-
if dx * dx + dy * dy <= square:
91-
newx, newy = x + dx, y + dy
92-
if 0 <= newx <= self.hor_res and 0 <= newy <= self.ver_res: # don't draw outside of canvas because that may crash
93-
self.canvas.set_px(x + dx, y + dy, DARKPINK, lv.OPA.COVER)
94-
43+
#canvas.set_px(x,y,lv.color_black(),lv.OPA.COVER) # draw a tiny point
44+
self.draw_rect(x,y)
45+
#self.draw_line(x,y)
46+
return
47+
48+
def draw_line(self, x, y):
49+
dsc = lv.draw_line_dsc_t()
50+
lv.draw_line_dsc_t.init(dsc)
51+
dsc.color = DARKPINK
52+
dsc.width = 4
53+
dsc.round_end = 1
54+
dsc.round_start = 1
55+
dsc.p1 = lv.point_precise_t()
56+
dsc.p1.x = x
57+
dsc.p1.y = y
58+
dsc.p2 = lv.point_precise_t()
59+
dsc.p2.x = 100
60+
dsc.p2.y = 200
61+
lv.draw_line(self.layer,dsc)
62+
self.canvas.finish_layer(self.layer)
63+
64+
@micropython.viper # make it with native compilation
65+
def draw_rect(self, x: int, y: int):
66+
draw_dsc = lv.draw_rect_dsc_t()
67+
lv.draw_rect_dsc_t.init(draw_dsc)
68+
draw_dsc.bg_color = lv.color_hex(0xffaaaa)
69+
draw_dsc.radius = lv.RADIUS_CIRCLE
70+
draw_dsc.border_color = lv.color_hex(0xff5555)
71+
draw_dsc.border_width = 2
72+
draw_dsc.outline_color = lv.color_hex(0xff0000)
73+
draw_dsc.outline_pad = 3
74+
draw_dsc.outline_width = 2
75+
a = lv.area_t()
76+
a.x1 = x-10
77+
a.y1 = y-10
78+
a.x2 = x+10
79+
a.y2 = y+10
80+
lv.draw_rect(self.layer, draw_dsc, a)
81+
self.canvas.finish_layer(self.layer)

0 commit comments

Comments
 (0)