def event_cb(e): dsc = lv.obj_draw_part_dsc_t.cast(e.get_param()) if dsc.part != lv.PART.INDICATOR: return obj = lv.bar.__cast__(e.get_target()) label_dsc = lv.draw_label_dsc_t() label_dsc.init() # label_dsc.font = LV_FONT_DEFAULT; value_txt = str(obj.get_value()) txt_size = lv.point_t() lv.txt_get_size(txt_size, value_txt, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, lv.COORD.MAX, label_dsc.flag) txt_area = lv.area_t() # If the indicator is long enough put the text inside on the right if dsc.draw_area.get_width() > txt_size.x + 20: txt_area.x2 = dsc.draw_area.x2 - 5 txt_area.x1 = txt_area.x2 - txt_size.x + 1 label_dsc.color = lv.color_white() # If the indicator is still short put the text out of it on the right*/ else: txt_area.x1 = dsc.draw_area.x2 + 5 txt_area.x2 = txt_area.x1 + txt_size.x - 1 label_dsc.color = lv.color_black() txt_area.y1 = dsc.draw_area.y1 + (dsc.draw_area.get_height() - txt_size.y) // 2 txt_area.y2 = txt_area.y1 + txt_size.y - 1 lv.draw_label(txt_area, dsc.clip_area, label_dsc, value_txt, None)
def slider_event_cb(e): code = e.get_code() obj = e.get_target() # Provide some extra space for the value if code == lv.EVENT.REFR_EXT_DRAW_SIZE: e.set_ext_draw_size(50) elif code == lv.EVENT.DRAW_PART_END: # print("DRAW_PART_END") dsc = lv.obj_draw_part_dsc_t.__cast__(e.get_param()) # print(dsc) if dsc.part == lv.PART.INDICATOR: label_text = "{:d} - {:d}".format(obj.get_left_value(),slider.get_value()) label_size = lv.point_t() lv.txt_get_size(label_size, label_text, lv.font_default(), 0, 0, lv.COORD.MAX, 0) # print(label_size.x,label_size.y) label_area = lv.area_t() label_area.x1 = dsc.draw_area.x1 + dsc.draw_area.get_width() // 2 - label_size.x // 2 label_area.x2 = label_area.x1 + label_size.x label_area.y2 = dsc.draw_area.y1 - 10 label_area.y1 = label_area.y2 - label_size.y label_draw_dsc = lv.draw_label_dsc_t() label_draw_dsc.init() lv.draw_label(label_area, dsc.clip_area, label_draw_dsc, label_text, None)
def text_test(canvas): print("Test text printing") label_dsc = lv.draw_label_dsc_t() label_dsc.init() v = 0 label_dsc.color = lv_colors.RED canvas.draw_text(0, v, CANVAS_WIDTH - 1, label_dsc, " Hello World!", lv.label.ALIGN.LEFT) v += 20 label_dsc.color = lv_colors.GREEN canvas.draw_text(0, v, CANVAS_WIDTH - 1, label_dsc, str(math.pi), lv.label.ALIGN.LEFT) v += 20 canvas.draw_text(0, v, CANVAS_WIDTH - 1, label_dsc, " Want pi?", lv.label.ALIGN.LEFT) v += 20 canvas.draw_text(0, v, CANVAS_WIDTH - 1, label_dsc, hex(8675309), lv.label.ALIGN.LEFT) v += 20 canvas.draw_text(0, v, CANVAS_WIDTH - 1, label_dsc, " Print HEX!", lv.label.ALIGN.LEFT) v += 20 label_dsc.color = lv_colors.WHITE canvas.draw_text(0, v, CANVAS_WIDTH - 1, label_dsc, " Sketch has been", lv.label.ALIGN.LEFT) v += 20 canvas.draw_text(0, v, CANVAS_WIDTH - 1, label_dsc, " running for: ", lv.label.ALIGN.LEFT) v += 20 canvas.draw_text(0, v, CANVAS_WIDTH - 1, label_dsc, str((time.ticks_ms() - start_time) / 1000), lv.label.ALIGN.LEFT) v += 20 canvas.draw_text(0, v, CANVAS_WIDTH - 1, label_dsc, " seconds.", lv.label.ALIGN.LEFT)
def event_cb(self, e): code = e.get_code() chart = lv.chart.__cast__(e.get_target()) if code == lv.EVENT.VALUE_CHANGED: # print("last_id: ",self.last_id) self.last_id = chart.get_pressed_point() if self.last_id != lv.CHART_POINT.NONE: p = lv.point_t() chart.get_point_pos_by_id(self.ser, self.last_id, p) chart.set_cursor_point(self.cursor, None, self.last_id) elif code == lv.EVENT.DRAW_PART_END: # print("EVENT.DRAW_PART_END") dsc = lv.obj_draw_part_dsc_t.cast(e.get_param()) # if dsc.p1 and dsc.p2: # print("p1, p2", dsc.p1,dsc.p2) # print("p1.y, p2.y", dsc.p1.y, dsc.p2.y) # print("last_id: ",self.last_id) if dsc.part == lv.PART.CURSOR and dsc.p1 and dsc.p2 and dsc.p1.y == dsc.p2.y and self.last_id >= 0: v = self.ser_p[self.last_id] # print("value: ",v) value_txt = str(v) size = lv.point_t() lv.txt_get_size(size, value_txt, lv.font_default(), 0, 0, lv.COORD.MAX, lv.TEXT_FLAG.NONE) a = lv.area_t() a.y2 = dsc.p1.y - 5 a.y1 = a.y2 - size.y - 10 a.x1 = dsc.p1.x + 10 a.x2 = a.x1 + size.x + 10 draw_rect_dsc = lv.draw_rect_dsc_t() draw_rect_dsc.init() draw_rect_dsc.bg_color = lv.palette_main(lv.PALETTE.BLUE) draw_rect_dsc.radius = 3 lv.draw_rect(a, dsc.clip_area, draw_rect_dsc) draw_label_dsc = lv.draw_label_dsc_t() draw_label_dsc.init() draw_label_dsc.color = lv.color_white() a.x1 += 5 a.x2 -= 5 a.y1 += 5 a.y2 -= 5 lv.draw_label(a, dsc.clip_area, draw_label_dsc, value_txt, None)
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 = 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
def updateClock(self, task): center = 120 + 100j if self.mainbar.pcf8563: # read time from pcf8563 localTime = self.mainbar.pcf8563.datetime() year = localTime[0] + 2000 else: now = time.time() localTime = time.localtime(now) year = localTime[0] seconds = localTime[5] minutes = localTime[4] hours = localTime[3] month = localTime[1] day = localTime[2] weekday = localTime[6] # print('{}:{}:{}'.format(hours,minutes,seconds)) if hours > 12: hours -= 12 hours *= 5 # the angle corresponding to the hour hours += 5 / 60 * minutes # add the angle corresponding to the minutes theta = cmath.pi / 2 - 2 * hours * cmath.pi / 60 hrs_endpoint = cmath.rect(self.hrs_radius, theta) polar(self.canvas, center, hrs_endpoint, 3, lv_colors.RED) theta = cmath.pi / 2 - 2 * minutes * cmath.pi / 60 min_endpoint = cmath.rect(self.min_radius, theta) polar(self.canvas, center, min_endpoint, 3, lv_colors.RED) # clear the old hands if self.oldSec != seconds: theta = cmath.pi / 2 - 2 * self.oldSec * cmath.pi / 60 sec_endpoint = cmath.rect(self.sec_radius + 2, theta) polar(self.canvas, center, sec_endpoint, 5, lv_colors.BLACK) if self.oldMin != minutes: theta = cmath.pi / 2 - 2 * self.oldMin * cmath.pi / 60 min_endpoint = cmath.rect(self.min_radius + 2, theta) polar(self.canvas, center, min_endpoint, 7, lv_colors.BLACK) theta = cmath.pi / 2 - 2 * self.oldHour * cmath.pi / 60 hrs_endpoint = cmath.rect(self.hrs_radius + 2, theta) polar(self.canvas, center, hrs_endpoint, 7, lv_colors.BLACK) # set the new hands according to the current time theta = cmath.pi / 2 - 2 * hours * cmath.pi / 60 hrs_endpoint = cmath.rect(self.hrs_radius, theta) polar(self.canvas, center, hrs_endpoint, 3, lv_colors.RED) theta = cmath.pi / 2 - 2 * minutes * cmath.pi / 60 min_endpoint = cmath.rect(self.min_radius, theta) polar(self.canvas, center, min_endpoint, 3, lv_colors.RED) theta = cmath.pi / 2 - 2 * seconds * cmath.pi / 60 sec_endpoint = cmath.rect(self.sec_radius, theta) polar(self.canvas, center, sec_endpoint, 1, lv_colors.WHITE) self.oldSec = seconds self.oldMin = minutes self.oldHour = hours date = [year, month, day] if self.oldDate != date: # clear old date overwriting it with a black rectangle rect_dsc = lv.draw_rect_dsc_t() rect_dsc.init() rect_dsc.bg_color = lv_colors.BLACK rect_dsc.bg_opa = lv.OPA.COVER self.canvas.draw_rect(0, self.CANVAS_HEIGHT - 30, self.CANVAS_WIDTH, 20, rect_dsc) # write new date dateText = dayOfWeekString(weekday) + " " + str( day) + '.' + monthString(month) + ' ' + str(year) label_dsc = lv.draw_label_dsc_t() label_dsc.init() label_dsc.color = lv_colors.WHITE self.canvas.draw_text(0, self.CANVAS_HEIGHT - 30, self.CANVAS_WIDTH, label_dsc, dateText, lv.label.ALIGN.CENTER) self.oldDate = date