def lv_canvas(screen): cbuff = bytearray(240 * 240 * 4) canvas = lv.canvas(screen) canvas.set_buffer(cbuff, 240, 240, lv.img.CF.TRUE_COLOR) canvas.fill_bg(styles.LV_COLOR_GREY, lv.OPA.COVER) rect_dsc = lv.draw_rect_dsc_t() rect_dsc.init() rect_dsc.radius = 10 rect_dsc.bg_opa = lv.OPA.COVER rect_dsc.bg_grad_dir = lv.GRAD_DIR.HOR rect_dsc.bg_color = styles.LV_COLOR_RED rect_dsc.bg_grad_color = styles.LV_COLOR_BLUE rect_dsc.border_width = 2 rect_dsc.border_opa = lv.OPA._90 rect_dsc.border_color = styles.LV_COLOR_WHITE rect_dsc.shadow_width = 5 rect_dsc.shadow_ofs_x = 5 rect_dsc.shadow_ofs_y = 5 canvas.draw_rect(70, 60, 100, 70, rect_dsc) label1 = lv.label(canvas) label1.add_style(label1.PART.MAIN, styles.gstyle_font1) label1.set_text("test font")
def __init__(self): self.value = 0 canvas_style = lv.style_t() canvas_style.init() canvas_style.set_bg_color(lv.STATE.DEFAULT, lv_colors.WHITE) cbuf = bytearray(SCREEN_WIDTH * 30 * 4) canvas = lv.canvas(lv.scr_act(), None) canvas.set_buffer(cbuf, SCREEN_WIDTH, 30, lv.img.CF.TRUE_COLOR) # canvas.add_style(lv.canvas.PART.MAIN,canvas_style) canvas.align(None, lv.ALIGN.IN_TOP_LEFT, 0, 0) p1 = lv.point_t() p2 = lv.point_t() point_array = [p1, p2] p1.x = 0 p1.y = 0 p2.x = 15 p2.y = p1.y # style for horizontal lines line_dsc = lv.draw_line_dsc_t() line_dsc.init() line_dsc.color = lv_colors.BLUE line_dsc.opa = lv.OPA.COVER line_dsc.width = 5 # draw the horizontal lines canvas.draw_line(point_array, 2, line_dsc) p1.x = 130 p2.x = SCREEN_WIDTH canvas.draw_line(point_array, 2, line_dsc) # draw the vertical lines p1.x = 0 p2.x = p1.x p2.y = 30 canvas.draw_line(point_array, 2, line_dsc) p1.x = SCREEN_WIDTH - 3 p2.x = p1.x canvas.draw_line(point_array, 2, line_dsc) text_style = lv.style_t() text_style.init() text_style.set_text_font(lv.STATE.DEFAULT, lv.font_montserrat_18) text_style.set_bg_color(lv.STATE.DEFAULT, lv_colors.BLACK) text_style.set_bg_opa(lv.STATE.DEFAULT, lv.OPA.COVER) self.label = lv.label(canvas, None) self.label.add_style(lv.label.PART.MAIN, text_style) self.label.set_recolor(True) self.reset() self.label.align(None, lv.ALIGN.OUT_TOP_MID, -60, 25)
def main_page(self, tile_num): self.aclock_tile = self.mainbar.get_tile_obj(tile_num) self.aclock_style = lv.style_t() self.aclock_style.copy(self.mainbar.get_style()) self.CANVAS_HEIGHT = lv.scr_act().get_disp().driver.ver_res self.CANVAS_WIDTH = self.CANVAS_HEIGHT cbuf = bytearray(self.CANVAS_HEIGHT * self.CANVAS_HEIGHT * 4) self.canvas = lv.canvas(self.aclock_tile, None) self.canvas.set_buffer(cbuf, self.CANVAS_HEIGHT, self.CANVAS_HEIGHT, lv.img.CF.TRUE_COLOR) self.canvas.align(self.aclock_tile, lv.ALIGN.CENTER, 0, 0) circle_dsc = lv.draw_line_dsc_t() circle_dsc.init() circle_dsc.color = lv_colors.GREEN self.radius = 90 xo = self.CANVAS_WIDTH // 2 yo = self.CANVAS_HEIGHT // 2 - 20 self.canvas.draw_arc(xo, yo, self.radius, 0, 360, circle_dsc) vor = xo + 1j * yo vtstart = 0.9 * self.radius + 0j # start of tick vtick = 0.1 * self.radius + 0j # tick vrot = cmath.exp(2j * cmath.pi / 12) # unit rotation for _ in range(12): polar(self.canvas, vor + conj(vtstart), vtick, 1, lv_colors.GREEN) vtick *= vrot vtstart *= vrot vtick = 0.05 * self.radius + 0j # tick vrot = cmath.exp(2j * cmath.pi / 60) # unit rotation for _ in range(60): polar(self.canvas, vor + conj(vtstart), vtick, 1, lv_colors.GREEN) vtick *= vrot vtstart *= vrot self.hrs_radius = self.radius - 32 self.min_radius = self.radius - 12 self.sec_radius = self.radius - 12 self.task = lv.task_create_basic() self.task.set_cb(lambda task: self.updateClock(self.task)) self.task.set_period(100) self.task.set_prio(lv.TASK_PRIO.LOWEST) exit_btn = lv.imgbtn(self.aclock_tile, None) exit_btn.set_src(lv.btn.STATE.RELEASED, self.mainbar.get_exit_btn_dsc()) exit_btn.add_style(lv.imgbtn.PART.MAIN, self.aclock_style) exit_btn.align(self.aclock_tile, lv.ALIGN.IN_BOTTOM_LEFT, 10, -10) self.log.debug("setting up exit callback") exit_btn.set_event_cb(self.exit_aclock_app_event_cb) self.aclock_style.set_text_opa(lv.obj.PART.MAIN, lv.OPA.COVER)
def test(): """Test code.""" cbuf = bytearray(CANVAS_WIDTH * CANVAS_HEIGHT * 4) # create a canvas canvas = lv.canvas(lv.scr_act(), None) canvas.set_buffer(cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, lv.img.CF.TRUE_COLOR) canvas.align(None, lv.ALIGN.CENTER, 0, 0) offset = (CANVAS_WIDTH - CANVAS_HEIGHT) // 2 c = 0 rect_dsc = lv.draw_rect_dsc_t() rect_dsc.init() rect_dsc.bg_opa = lv.OPA.COVER for x in range(0, CANVAS_HEIGHT, 48): for y in range(0, CANVAS_HEIGHT, 48): box = coloredBox(lv.scr_act(), x + offset, y, 48, 48, colors[c]) c += 1
def __init__(self,parent): self.CANVAS_HEIGHT = lv.scr_act().get_disp().driver.ver_res self.CANVAS_WIDTH = self.CANVAS_HEIGHT cbuf=bytearray(self.CANVAS_HEIGHT * self.CANVAS_HEIGHT * 4) self.canvas = lv.canvas(parent,None) self.canvas.set_buffer(cbuf,self.CANVAS_HEIGHT,self.CANVAS_HEIGHT,lv.img.CF.TRUE_COLOR) self.canvas.align(None,lv.ALIGN.CENTER,0,0) circle_dsc = lv.draw_line_dsc_t() circle_dsc.init() circle_dsc.color = lv_colors.GREEN self.radius = 90 xo=self.CANVAS_WIDTH//2 yo=self.CANVAS_HEIGHT//2-20 self.canvas.draw_arc(xo,yo,self.radius,0,360,circle_dsc) vor = xo + 1j * yo vtstart = 0.9 * self.radius + 0j # start of tick vtick = 0.1 * self.radius + 0j # tick vrot = cmath.exp(2j * cmath.pi/12) # unit rotation for _ in range(12): polar(self.canvas, vor + conj(vtstart), vtick, 1, lv_colors.GREEN) vtick *= vrot vtstart *= vrot vtick = 0.05 * self.radius + 0j # tick vrot = cmath.exp(2j * cmath.pi/60) # unit rotation for _ in range(60): polar(self.canvas, vor + conj(vtstart), vtick, 1, lv_colors.GREEN) vtick *= vrot vtstart *= vrot self.hrs_radius = self.radius-32 self.min_radius = self.radius -12 self.sec_radius = self.radius -12 self.task = lv.task_create_basic() self.task.set_cb(lambda task: self.updateClock(self.task)) self.task.set_period(100) self.task.set_prio(lv.TASK_PRIO.LOWEST)
def test(): """Test code.""" cbuf = bytearray(CANVAS_WIDTH * CANVAS_HEIGHT * 4) # create a canvas canvas = lv.canvas(lv.scr_act(), None) canvas.set_buffer(cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, lv.img.CF.TRUE_COLOR) canvas.align(None, lv.ALIGN.CENTER, 0, 0) p1 = lv.point_t() p2 = lv.point_t() point_array = [p1, p2] p2.x = CENTER_X p2.y = CENTER_Y line_dsc = lv.draw_line_dsc_t() line_dsc.init() line_dsc.opa = lv.OPA.COVER x, y = 0, 0 angle = 0.0 # Loop all angles from 0 to 2 * PI radians while angle < PI2: # Calculate x, y from a vector with known length and angle x = int(CENTER_Y * sin(angle) + HALF_WIDTH) y = int(CENTER_Y * cos(angle) + HALF_HEIGHT) color = LV_COLOR_MAKE(*hsv_to_rgb(angle / PI2, 1, 1)) line_dsc.color = color p1.x = x p1.y = y canvas.draw_line(point_array, 2, line_dsc) angle += ANGLE_STEP_SIZE sleep(5) clear(canvas) line_dsc.width = 3 for r in range(CENTER_Y, 0, -1): line_dsc.color = LV_COLOR_MAKE(*hsv_to_rgb(r / HALF_HEIGHT, 1, 1)) canvas.draw_arc(CENTER_X, CENTER_Y, r, 0, 360, line_dsc) sleep(5)
def __init__(self, page): self.cbuf = bytearray(CANVAS_WIDTH * CANVAS_HEIGHT * 4) # create a canvas self.canvas = lv.canvas(page, None) self.canvas.set_buffer(self.cbuf,CANVAS_WIDTH,CANVAS_HEIGHT,lv.img.CF.TRUE_COLOR) self.canvas.align(page,lv.ALIGN.CENTER,0,0) self.clear(); # prepare line self.line_dsc = lv.draw_line_dsc_t() self.line_dsc.init() self.line_dsc.color = lv.color_hex(0xff0000); self.line_dsc.opa = lv.OPA.COVER self.color_state = (0,0) self.x = 0 self.y = CANVAS_HEIGHT-1 self.state = 0 # the line drawing is done by a task so it doesn't interfere # with LVGL itself self.task = lv.task_create(self.line_task, 10, lv.TASK_PRIO.MID, None);
import lvgl as lv import display_driver import time import math from lv_colors import lv_colors disp = lv.scr_act().get_disp() CANVAS_WIDTH = disp.driver.hor_res CANVAS_HEIGHT = disp.driver.ver_res def clear(): canvas.fill_bg(lv_colors.BLACK, lv.OPA.COVER) cbuf=bytearray(CANVAS_WIDTH * CANVAS_HEIGHT * 4) # create a canvas canvas = lv.canvas(lv.scr_act(),None) canvas.set_buffer(cbuf,CANVAS_WIDTH,CANVAS_HEIGHT,lv.img.CF.TRUE_COLOR) canvas.align(None,lv.ALIGN.CENTER,0,0) print("Triangle") p1=lv.point_t() p2=lv.point_t() p3=lv.point_t() p4=lv.point_t() p5=lv.point_t() p6=lv.point_t() p7=lv.point_t() p8=lv.point_t() point_array=[p1,p2,p3]
rect_dsc.bg_color = lv.palette_main(lv.PALETTE.RED) rect_dsc.bg_grad_color = lv.palette_main(lv.PALETTE.BLUE) rect_dsc.border_width = 2 rect_dsc.border_opa = lv.OPA._90 rect_dsc.border_color = lv.color_white() rect_dsc.shadow_width = 5 rect_dsc.shadow_ofs_x = 5 rect_dsc.shadow_ofs_y = 5 label_dsc = lv.draw_label_dsc_t() label_dsc.init() label_dsc.color = lv.palette_main(lv.PALETTE.YELLOW) cbuf = bytearray(_CANVAS_WIDTH * _CANVAS_HEIGHT * 4) canvas = lv.canvas(lv.scr_act()) canvas.set_buffer(cbuf, _CANVAS_WIDTH, _CANVAS_HEIGHT, lv.img.CF.TRUE_COLOR) canvas.center() canvas.fill_bg(lv.palette_lighten(lv.PALETTE.GREY, 3), lv.OPA.COVER) canvas.draw_rect(70, 60, 100, 70, rect_dsc) canvas.draw_text(40, 20, 100, label_dsc, "Some text on text canvas") # Test the rotation. It requires an other buffer where the orignal image is stored. # So copy the current image to buffer and rotate it to the canvas img = lv.img_dsc_t() img.data = cbuf[:] img.header.cf = lv.img.CF.TRUE_COLOR img.header.w = _CANVAS_WIDTH img.header.h = _CANVAS_HEIGHT
def __init__(self, x=None, y=None, min_value=0, max_value=100, value=50, divisions=5, legend=None, label_text=None, value_text_format=None, bar_color=lv_colors.BLUE, legend_color=lv_colors.YELLOW, value_color=lv_colors.WHITE, bg_color=lv_colors.BLACK): self.x = x self.y = y self.min_value = min_value self.max_value = max_value self.value = value self.width = 40 self.height = 104 self.label_text = label_text self.value_text_format = value_text_format self.bg_color = bg_color self.bar_color = bar_color self.value_color = value_color self.legend_color = legend_color self.base = 102 if legend: self.divisions = len(legend) - 1 else: self.divisions = divisions scr_style = lv.style_t() scr_style.set_bg_color(lv.STATE.DEFAULT, self.bg_color) lv.scr_act().add_style(lv.obj.PART.MAIN, scr_style) # create a container self.container = lv.cont(lv.scr_act(), None) self.container.set_fit(lv.FIT.TIGHT) self.container.align(None, lv.ALIGN.CENTER, 0, 0) if self.x: self.container.set_x(self.x) if self.y: self.container.set_y(self.y) self.container.add_style(lv.obj.PART.MAIN, scr_style) cbuf = bytearray(self.width * self.height * 4) # create a canvas self.canvas = lv.canvas(self.container, None) self.canvas.set_buffer(cbuf, self.width, self.height, lv.img.CF.TRUE_COLOR) self.canvas.align(self.container, lv.ALIGN.CENTER, 0, -50) text_style = lv.style_t() text_style.init() text_style.set_text_color(lv.STATE.DEFAULT, self.legend_color) if self.value_text_format: value_text_style = lv.style_t() value_text_style.init() value_text_style.set_text_color(lv.STATE.DEFAULT, self.value_color) self.value_label = lv.label(self.container) self.value_label.add_style(lv.label.PART.MAIN, value_text_style) value_text = self.value_text_format.format(self.value) self.value_label.set_text(value_text) if self.label_text: self.label = lv.label(self.container) self.label.set_text(label_text) self.label.align(self.canvas, lv.ALIGN.OUT_BOTTOM_MID, 0, 10) self.label.add_style(lv.label.PART.MAIN, text_style) if self.value_text_format: self.value_label.align(self.label, lv.ALIGN.OUT_BOTTOM_MID, 0, 0) else: self.value_label.align(self.canvas, lv.ALIGN.OUT_BOTTOM_MID, 0, 10) #bar = lv_bar(container) p1 = lv.point_t() p2 = lv.point_t() point_array = [p1, p2] # style for horizontal lines self.hline_dsc = lv.draw_line_dsc_t() self.hline_dsc.init() self.hline_dsc.color = self.legend_color self.hline_dsc.opa = lv.OPA.COVER rect_dsc = lv.draw_rect_dsc_t() rect_dsc.init() rect_dsc.bg_opa = lv.OPA.TRANSP rect_dsc.border_color = self.legend_color rect_dsc.bg_color = lv_colors.BLACK rect_dsc.border_width = 1 # draw the outline, tock marks, legend and value text self.canvas.draw_rect(0, 0, self.width, self.height, rect_dsc) if self.divisions > 0: dy = (self.height - 4) / self.divisions # Tick marks # print("dy: ",dy) for tick in range(self.divisions + 1): ypos = int(dy * tick) p1.x = self.width // 5 p1.y = ypos + 2 p2.x = p1.x + self.width - 2 * self.width // 5 p2.y = p1.y self.canvas.draw_line(point_array, 2, self.hline_dsc) # print("tick: %d, pos: %d"%(tick,p1.y)) if legend: label = lv.label(self.container) label.align(self.canvas, lv.ALIGN.OUT_RIGHT_TOP, 5, ypos - 5) label.set_text(legend[self.divisions - tick]) label.add_style(lv.label.PART.MAIN, text_style) self.base = p1.y # draw the value rectangle # print("max: ",self.max_value) value_y = round( (self.height - 4) / (self.max_value - self.min_value) * self.value) rect_dsc.bg_color = self.bar_color rect_dsc.bg_opa = lv.OPA.COVER rect_dsc.border_width = 0 # print("Pos of last tick: ",p1.y) # print("bar height: ",value_y) self.canvas.draw_rect(self.width // 5 + 5, self.base - value_y, 2 * self.width // 5, value_y, rect_dsc)