def interface(): def update_task(task): set_usb_voltage(power.getVbusVoltage()) set_usb_pwr(power.getVbusCurrent()) if power.isBatteryConnect(): set_batt_voltage(power.getBattVoltage()) if power.isChargeing(): set_batt_pwr("Charging", power.getBattChargeCurrent()) batt_percent.set_hidden(True) else: set_batt_pwr("Discharging", power.getBattDischargeCurrent()) set_batt_per(power.getBattPercentage()) batt_percent.set_hidden(False) def event_cb(obj, event): if event == lv.EVENT.VALUE_CHANGED: tft.set_backlight_level(obj.get_value()) def set_usb_pwr(pwr): usb_pwr.set_text("USB current: {} mA".format(pwr)) def set_usb_voltage(volts): usb_voltage.set_text("USB voltage: {} mV".format(volts)) def set_batt_voltage(volts): batt_voltage.set_text("Battery voltage: {} mV".format(volts)) def set_batt_pwr(mode, pwr): batt_pwr.set_text("{} battery \ncurrent: {} mA".format(mode, pwr)) def set_batt_per(per): batt_percent.set_text("Battery percent: {}%".format(per)) scr = lv.obj() tabview = lv.tabview(scr) vbus_tab = tabview.add_tab("USB") usb_voltage = lv.label(vbus_tab) usb_pwr = lv.label(vbus_tab) usb_pwr.set_y(usb_voltage.get_y() + 20) la = lv.switch(vbus_tab) batt_tab = tabview.add_tab("Battery") batt_voltage = lv.label(batt_tab) batt_pwr = lv.label(batt_tab) batt_pwr.set_y(batt_voltage.get_y() + 20) batt_percent = lv.label(batt_tab) batt_percent.set_y(batt_pwr.get_y() + 40) brightness_slider = lv.slider(batt_tab) brightness_slider.align(None, lv.ALIGN.IN_BOTTOM_MID, 0, 0) brightness_slider.set_range(0, 100) brightness_slider.set_value(100, 1) brightness_slider.set_event_cb(event_cb) update_task(None) lv.scr_load(scr) watch.tft.backlight_fade(100) lv.task_create(update_task, 1000, 5, None)
def init(parent): # global screen container = lv.obj(parent) container.set_size(parent.get_width(), parent.get_height()) container.add_style(lv.obj.PART.MAIN, background) clock = lv.cont(container) clock.set_fit(lv.FIT.PARENT) clock.set_layout(lv.LAYOUT.CENTER) clock.set_size(container.get_width(), container.get_height()) clock.add_style(lv.obj.PART.MAIN, background) batteryLabel = lv.label(container) hours = lv.label(clock) hours.set_text("00") hours.set_align(lv.label.ALIGN.CENTER) hours.add_style(lv.label.PART.MAIN, title) minutes = lv.label(clock) minutes.set_text("00") minutes.set_align(lv.label.ALIGN.CENTER) minutes.add_style(lv.label.PART.MAIN, title) date = lv.label(clock) date.set_text("1 jan. 2021") date.add_style(lv.label.PART.MAIN, subtitle) # minutes.align(hours, lv.ALIGN.OUT_BOTTOM_MID, 0, 0) batteryLabel.align(container, lv.ALIGN.IN_TOP_RIGHT, 0, 0) batteryLabel.set_align(lv.label.ALIGN.RIGHT) batteryLabel.add_style(lv.label.PART.MAIN, text) batteryLabel.set_style_local_text_color(lv.label.PART.MAIN, lv.STATE.DEFAULT, antracite_light) def update_clock(task): time = get_watch().rtc hours.set_text("{0:02n}".format(time.hours())) minutes.set_text("{0:02n}".format(time.minutes())) batteryLabel.set_text('{}%'.format( get_watch().pmu.getBattPercentage())) date.set_text("{} {} 20{}".format(time.date(), months[time.month()], time.year())) lv.task_create(update_clock, 500, lv.TASK_PRIO.HIGHEST, None) update_clock(None) return container
def __init__(self, page, screen): self.page = page self.screen = screen self.win = None # for fake run #self.fake_cmd = None #self.fake_task = lv.task_create(self.fake, 5000, lv.TASK_PRIO.MID, None); # create a task to check for commands from http_server self.http_task = lv.task_create(self.http_server_poll, 100, lv.TASK_PRIO.MID, None) # create a checkbox self.button = lv.checkbox(page) self.button.set_text("SD Card") self.button.align(page, lv.ALIGN.IN_TOP_LEFT, 16, 10) self.button.set_event_cb(self.event_handler) if not hasattr(machine, "SDCard"): self.button.set_disabled() # reload button self.reload_btn = lv.btn(page) self.reload_btn.set_size(32, 32) self.reload_btn.align(page, lv.ALIGN.IN_TOP_RIGHT, -12, 4) self.reload_btn.set_event_cb(self.on_reload_btn) lv.label(self.reload_btn).set_text(lv.SYMBOL.REFRESH) # List of Apps self.list = lv.list(page) self.list.set_size(216, 216) self.list.align(page, lv.ALIGN.IN_TOP_MID, 0, 38) self.reload()
def connect_with_key(self, ssid, password): def connection_done(ok): self.ssids.set_click(True) self.scan_btn.set_click(True) if ok: self.screen.set_status("wifi", True) self.write_key(ssid, password) self.ssids.set_show_selected(True) # find ssid in network list and select it for i in range(len(self.networks)): if self.networks[i]["ssid"] == ssid: self.ssids.set_selected(i) self.label.set_text("Connected\n\nSSID: " + ssid + "\nIP: " + self.wlan.ifconfig()[0] + "\n" + self.config["name"] + ".local") try: self.server = http_server() self.server.register_input_callback() except Exception as e: print("Webserver error:", e) import espidf if espidf.mdns_init() == 0: espidf.mdns_hostname_set(self.config["name"]) else: self.label.set_text("Connection failed") self.task.set_repeat_count(0) def connect_task(task): self.cnt = self.cnt + 1 if hasattr(self, "wlan"): if self.wlan.isconnected(): connection_done(True) else: # fake connection ok after 2.5 sec if self.cnt == 25: connection_done(True) # timeout after 100 * 100ms = 10 seconds if self.cnt == 100: if hasattr(self, "wlan"): self.wlan.active(False) # interrupt the connection attempt connection_done(False) self.wlan.active(True) self.wlan.connect(ssid, password) # start a task that checks if connection was successful self.cnt = 0 self.task = lv.task_create(connect_task, 100, lv.TASK_PRIO.MID, None)
def _register_tasks(self): # Task to update th self._task_update_th = lv.task_create(None, 1000, lv.TASK_PRIO.MID, None) lv.task_set_cb(self._task_update_th, self._update_th_cb) # Task to save th self._task_save_th = lv.task_create(None, 60000, lv.TASK_PRIO.MID, None) lv.task_set_cb(self._task_save_th, self._save_th_cb) # Task to update time self._task_update_time = lv.task_create(None, 1000, lv.TASK_PRIO.MID, None) lv.task_set_cb(self._task_update_time, self._update_time_cb) # Task to save time to flash self._task_save_time = lv.task_create(None, 60000, lv.TASK_PRIO.MID, None) lv.task_set_cb(self._task_save_time, self._save_time_cb) # Task to gc collect self._task_gc_collect = lv.task_create(None, 10000, lv.TASK_PRIO.MID, None) lv.task_set_cb(self._task_gc_collect, self._gc_collect_cb) # Task to update output self._task_update_laser_output = lv.task_create( None, 200, lv.TASK_PRIO.OFF, None) lv.task_set_cb(self._task_update_laser_output, self._update_laser_output_cb) # Task to wait for wait panel function self._task_wait_panel = lv.task_create(None, 1000, lv.TASK_PRIO.OFF, None) lv.task_set_cb(self._task_wait_panel, self._check_wait_panel_cb) return
def timer_start(rate, handler): if not "timer" in sys.modules['llvgl'].config: sys.modules['llvgl'].config["timer"] = [ ] timer = { "rate": rate, "handler": handler } timer["task"] = lv.task_create(lambda o: on_timer(timer), int(rate*1000), lv.TASK_PRIO.MID, None) sys.modules['llvgl'].config["timer"].append(timer) return timer
def run(self, code, done_cb=None): self.done_cb = done_cb # start wrapper to catch script output self.wrapper = self.Wrapper() uos.dupterm(self.wrapper) # run script in background self.running = True _thread.start_new_thread(self.execute, (code, )) # start task to read text from wrapper and display it in label self.task = lv.task_create(self.watcher, 100, lv.TASK_PRIO.MID, None)
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);
def start(self): lv.task_create(self.task_cb, self.rate, lv.TASK_PRIO.LOWEST, None)
def load(self): if 'wallpaper' in self.cfg and 'type' in self.cfg['wallpaper']: if self.cfg['wallpaper']['type'] == 'png': with open(self.cfg['wallpaper']['filename'], 'rb') as f: buff = f.read() self.bg_img_dsc.data = buff self.bg_img_dsc.data_size = len(buff) # print('buff size:', img_dsc.data_size) if self.bg_img_dsc.data_size > 0: self.bg_img.set_src(self.bg_img_dsc) # Build containers for watchface, default if none defined in config file if 'containers' in self.cfg and len(self.cfg['containers']) > 0: for _cont_i in self.cfg['containers']: _cont = LvWatchFaceContainer(container_cfg=_cont_i, parent_cont=self.screen) self.containers[_cont.name] = _cont else: self.containers['DEFAULT'] = LvWatchFaceContainer( container_cfg=None, parent_cont=self.screen) # self.update() # Build watchface objects for lvitem in self.cfg['lv_objects']: if 'type' not in lvitem or 'name' not in lvitem: print( 'Invalid lv_object config -> type or name missing, skipping:', lvitem) continue if 'container' in lvitem and 'inside' in lvitem['container']: _cont_name = lvitem['container']['inside'] else: _cont_name = 'DEFAULT' if _cont_name not in self.containers: print( 'Invalid lv_object config -> container doesnt exist, skipping:', lvitem) continue if lvitem['type'] in globals(): print("Build object:", lvitem['type'], lvitem) self.objs[lvitem['name']] = globals()[lvitem['type']]( watchface_cfg=lvitem, lv_cont=self.containers[_cont_name]) self.containers[_cont_name].num_objs += 1 else: print('Unknown watchface object type:', lvitem['type'], lvitem['name']) continue ''' if lvitem == 'digital_clock' and self.digital_clock is None: self.digital_clock = LvDigitalClock(watchface_cfg=self.cfg['lv_objects'][lvitem], lv_cont=self.screen) if lvitem == 'wifi' and self.wifi is None: self.wifi = LvWifi(watchface_cfg=self.cfg['lv_objects'][lvitem], lv_cont=self.screen) if lvitem == 'power' and self.power is None: self.power = LvPower(watchface_cfg=self.cfg['lv_objects'][lvitem], lv_cont=self.screen) ''' # Canvas approach # self.digital_clock = lv_digitlineclock.LvDigitLineClock(size_x=220, size_y=75, digit_margin=12) # self.digital_clock.set_pos(20, 90) # self.digital_clock.update() #self.canvas.draw_img(0, 0, self.bg_img, self.bg_draw_img) # for line_pts in self.digital_clock.digit_pts: # print('Line:', line_pts) # draw_line_dsc = lv.draw_line_dsc_t() # draw_line_dsc.init() # draw_line_dsc.color = styles.LV_COLOR_BLUE # draw_line_dsc.width = 8 # draw_line_dsc.round_end = True # draw_line_dsc.round_start = True # self.canvas.draw_line(line_pts, len(line_pts), draw_line_dsc) # img = lv.img(screen) # img.set_src(img_dsc) if 'refresh' in self.cfg and 'interval_msec' in self.cfg['refresh']: _refresh_msec = int(self.cfg['refresh']['interval_msec']) if _refresh_msec < 200: _refresh_msec = 200 else: _refresh_msec = 500 self.lv_task_updater = lv.task_create(self.update_task, _refresh_msec, lv.TASK_PRIO.MID, self.task_data)
def m5(): #Main home screen scr = lv.obj() lv.scr_load(scr) #Buttong Event - create this below is the menu def btn_event(obj, event): if obj == b1 and event == lv.EVENT.CLICKED: #Timer config m4() if obj == b2 and event == lv.EVENT.CLICKED: #Wifi Config m6() #temp if obj == b3 and event == lv.EVENT.CLICKED: #Dashborad m7() #temp if obj == b4 and event == lv.EVENT.CLICKED: m5() #temp if obj == b5 and event == lv.EVENT.CLICKED: m5() #temp if obj == b6 and event == lv.EVENT.CLICKED: m5() #temp #top tab cont = lv.cont(scr) cont.align(scr, lv.ALIGN.IN_TOP_MID,0,0) cont.set_auto_realign(True) cont.set_height(20) cont.set_fit2(lv.FIT.PARENT, lv.FIT.TIGHT) lbl_today = lv.label(cont) lbl_today.align(cont, lv.ALIGN.IN_LEFT_MID, 20, 0) def gui_refresh_date(): lbl_today.set_text("Sunday 14-01-2021 {:02}:{:02}:{:02}".format(int(clock.hour),int(clock.minute),int(clock.second))) lv.task_create(lambda task: gui_refresh_date(), 100, lv.TASK_PRIO.HIGH, None) #lbl_today.align(cont, lv.ALIGN.CENTER,0,10) #List of boxes using button #1st column b1 = lv.btn(scr) b1.align(cont, lv.ALIGN.OUT_BOTTOM_LEFT, 10, 10) b1.set_height(100) b1.set_style_local_radius(0,0,0) b1.set_event_cb(btn_event) #callback lbl_b1 = lv.label(b1) lbl_b1.set_text("Timer Config") b3 = lv.btn(scr) b3.align(b1, lv.ALIGN.OUT_BOTTOM_MID, 0, 10) b3.set_height(100) b3.set_style_local_radius(0,0,0) b3.set_event_cb(btn_event) lbl_b3 = lv.label(b3) lbl_b3.set_text("Dashboard") b5 = lv.btn(scr) b5.align(b3, lv.ALIGN.OUT_BOTTOM_MID, 0, 10) b5.set_height(100) b5.set_style_local_radius(0,0,0) b5.set_event_cb(btn_event) lbl_b5 = lv.label(b5) lbl_b5.set_text("Manual Control") #2nd column b2 = lv.btn(scr) b2.align(cont, lv.ALIGN.OUT_BOTTOM_RIGHT, -10, 10) b2.set_height(100) b2.set_style_local_radius(0,0,0) b2.set_event_cb(btn_event) lbl_b2 = lv.label(b2) lbl_b2.set_text("Wifi Config") b4 = lv.btn(scr) b4.align(b2, lv.ALIGN.OUT_BOTTOM_MID, 0, 10) b4.set_height(100) b4.set_style_local_radius(0,0,0) b4.set_event_cb(btn_event) lbl_b4 = lv.label(b4) lbl_b4.set_text("Clock Config") b6 = lv.btn(scr) b6.align(b4, lv.ALIGN.OUT_BOTTOM_MID, 0, 10) b6.set_height(100) b6.set_style_local_radius(0,0,0) b6.set_event_cb(btn_event) lbl_b6 = lv.label(b6) lbl_b6.set_text("Phone Config")