def draw(self, co2, co2_history, timestamps, climate, message): w, h = display.size() co2_history_hash = hash(tuple(co2_history)) repaint = False if co2 != self.co2: draw_co2_label((0, 0, int(w * (2 / 5)), h // 4), co2) repaint = True if climate != self.climate: temperature, pressure, humidity = climate draw_climate_labels( (int(w * (3 / 5)), 0, int(w * (2 / 5)), h // 4), temperature, pressure, humidity) repaint = True if co2_history_hash != self.co2_history_hash or self.timestamps != self.timestamps or self.message != message: graph_rect = (0, h // 4, w, h // 4 * 3) draw_history_graph(graph_rect, co2_history, timestamps) draw_message_label((w // 2, graph_rect[1] + graph_rect[3] // 2), message) repaint = True if repaint: display.flush() # Cache rendered values so we can prevent repainting parts of the screen. self.co2 = co2 self.co2_history_hash = hash(tuple(co2_history)) self.timestamps = timestamps self.climate = climate self.message = message
def ledThread(): global spaces while 1: for i in range(len(spaces)): # Position xR, yR = spaces[i]["red"] xG, yG = spaces[i]["grn"] state = spaces[i]["state"] # Get LEDs R = display.getPixel(xR, yR) >> 16 G = display.getPixel(xG, yG) >> 16 red_brightness = 0x15 if state == 3: red_brightness = 0x30 tgtR = ((state == 1) or (state == 3)) * red_brightness tgtG = ((state == 2) or (state == 3)) * 0x10 # Set LEDs if R < tgtR: R = R + 1 elif R > tgtR: R = R - 1 if G < tgtG: G = G + 1 elif G > tgtG: G = G - 1 display.drawPixel(xR, yR, color(R)) display.drawPixel(xG, yG, color(G)) display.flush() time.sleep_ms(5)
def onA(pressed): global currentApp, apps if pressed: display.drawFill(0x000000) drawMessageBox("Moving app...") display.flush(display.FLAG_LUT_FASTEST) move(apps[currentApp]["path"])
def show_categories(pressed=True, fromAppInstall=False): if not pressed: return ugfx.input_attach(ugfx.JOY_UP, btn_unhandled) ugfx.input_attach(ugfx.JOY_DOWN, btn_unhandled) ugfx.input_attach(ugfx.JOY_LEFT, btn_unhandled) ugfx.input_attach(ugfx.JOY_RIGHT, btn_unhandled) ugfx.input_attach(ugfx.BTN_A, btn_unhandled) ugfx.input_attach(ugfx.BTN_B, btn_unhandled) ugfx.clear(ugfx.WHITE) myList.clear() for category in repo.categories: myList.add_item("%s (%d) >" % (category["name"], category["eggs"])) myList.enabled(True) myList.visible(True) #Input handling ugfx.input_attach(ugfx.BTN_B, btn_unhandled) ugfx.input_attach(ugfx.JOY_UP, btn_unhandled) ugfx.input_attach(ugfx.JOY_DOWN, btn_unhandled) ugfx.input_attach(ugfx.JOY_LEFT, btn_unhandled) ugfx.input_attach(ugfx.JOY_RIGHT, btn_unhandled) try: buttons.BTN_START ugfx.input_attach(ugfx.BTN_START, btn_exit) except: ugfx.input_attach(ugfx.BTN_B, btn_exit) try: buttons.BTN_SELECT ugfx.input_attach(ugfx.BTN_SELECT, btn_update) except: ugfx.input_attach(ugfx.JOY_UP, btn_update) ugfx.input_attach(ugfx.BTN_A, show_category) display.flush(display.FLAG_LUT_NORMAL)
def draw(): global counter for y in range(4): for x in range(4): color = color_off if pattern[x + y * 4] == 1: color = color_on elif pattern[x + y * 4] == 2: if counter % 2 == 0: color = color_off else: color = color_on elif pattern[x + y * 4] == 3: if counter % 2 == 0: color = color_on else: color = color_off display.drawPixel(x,y,color) display.flush() counter += 1 return blink_interval
def onB(pressed): if pressed: display.drawFill(0x000000) drawMessageBox("Loading homescreen...") display.flush(display.FLAG_LUT_FASTEST) time.sleep(0.1) system.home()
def messageCentered(message, firstLineTitle=True, png=None): try: color = 0x000000 font1 = "Roboto_Regular18" font2 = "Roboto_Regular12" font1Height = 22 #display.getTextHeight(" ", font1) font2Height = 16 #display.getTextHeight(" ", font2) message = message.split("\n") lines = [] for i in range(len(message)): line = message[i] if len(line) < 1: lines.append("") else: if firstLineTitle and i == 0: lines.extend(lineSplit(line, display.width(), font1)) else: lines.extend(lineSplit(line, display.width(), font2)) pngSize = (0, 0) if png != None: try: pngSize = badge.png_info(png) pngSize = [pngSize[0], pngSize[1] + 8] #Little bit of extra offset except BaseException as e: print("Error in PNG height", e) textHeight = len(lines) * font2Height if firstLineTitle: textHeight -= font2Height textHeight += font1Height offset_y = (display.height() - pngSize[1] - textHeight) // 2 display.drawFill() #display.drawLine(0,display.height()//2,display.width()-1,display.height()//2,0) if png != None: try: display.drawPng((display.width() - pngSize[0]) // 2, offset_y, png) offset_y += pngSize[1] except BaseException as e: print("Error in PNG draw", e) for i in range(len(lines)): if firstLineTitle and i == 0: lineCentered(offset_y, lines[i], font1, color) offset_y += font1Height else: lineCentered(offset_y, lines[i], font2, color) offset_y += font2Height display.flush(display.FLAG_LUT_FASTEST) except BaseException as e: print("Error in messageCentered!", e)
def show_category(pressed=True, fromAppInstall=False): global lastCategory if not pressed: return ugfx.input_attach(ugfx.JOY_UP, btn_unhandled) ugfx.input_attach(ugfx.JOY_DOWN, btn_unhandled) ugfx.input_attach(ugfx.JOY_LEFT, btn_unhandled) ugfx.input_attach(ugfx.JOY_RIGHT, btn_unhandled) ugfx.input_attach(ugfx.BTN_A, btn_unhandled) ugfx.input_attach(ugfx.BTN_B, btn_unhandled) ugfx.clear(ugfx.WHITE) global category if not fromAppInstall: lastCategory = myList.selected_index() slug = repo.categories[lastCategory]["slug"] showMessage("Loading " + slug + "...") display.drawFill() myList.clear() try: try: category = repo.getCategory(slug) except BaseException as e: showMessage("Failed to open category " + slug + "!", True) display.drawFill() time.sleep(2) show_categories() return print(category) gc.collect() for package in category: myList.add_item("%s rev. %s" % (package["name"], package["revision"])) print("Adding", "%s rev. %s" % (package["name"], package["revision"])) myList.visible(True) myList.enabled(True) try: ugfx.input_attach(ugfx.BTN_START, btn_exit) except: pass try: ugfx.input_attach(ugfx.BTN_SELECT, btn_unhandled) except: pass ugfx.input_attach(ugfx.BTN_A, install_app) ugfx.input_attach(ugfx.BTN_B, show_categories) ugfx.input_attach(ugfx.JOY_UP, btn_unhandled) ugfx.input_attach(ugfx.JOY_DOWN, btn_unhandled) ugfx.input_attach(ugfx.JOY_LEFT, btn_unhandled) ugfx.input_attach(ugfx.JOY_RIGHT, btn_unhandled) display.flush(display.FLAG_LUT_NORMAL) except BaseException as e: sys.print_exception(e) print("ERROR", e) showMessage("Internal error", True) display.drawFill() time.sleep(10) show_categories() print("Done!")
def drawNickname(): owner = machine.nvs_getstr("owner", "name") or "BADGE.TEAM" display.drawFill(0xFFFF00) x = (display.width()-display.getTextWidth(owner, "ocra16"))//2 y = (display.height()-display.getTextHeight(owner, "ocra16"))//2 display.drawText(x,y,owner,0x000000,"ocra16") display.flush() # Send the new buffer to the display display.backlight(0xFF) # Turn on backlight
def showLoadingScreen(app=""): try: display.drawFill(0x000000) display.drawText(0, 28, "LOADING APP...", 0xFFFFFF, "org18") display.drawText(0, 52, app, 0xFFFFFF, "org18") display.flush() except: pass
def onA(pressed): global currentApp, apps if pressed: display.drawFill(0x000000) drawMessageBox("Loading app...") display.flush(display.FLAG_LUT_FASTEST) time.sleep(0.1) system.start(apps[currentApp]["path"])
def demoThread(): global demoThreadRunning counter = 0 fpsTrig = False w, h, _, _ = display.pngInfo(mascot.snek) display.drawFill(0xFFFF00) prev = time.ticks_ms() av = [0] * 100 while demoThreadRunning: if counter > 100: fpsTrig = True current = time.ticks_ms() if (current - prev) > 0: fps = 1000 // (current - prev) else: fps = 0 _ = av.pop(0) av.append(fps) fps = 0 for i in range(len(av)): fps += av[i] fps = fps // len(av) display.drawRect(0, 0, display.width() - 1, 20, True, 0xFFFF00) if fpsTrig: display.drawText(0, 0, "{} fps".format(fps), 0x000000, "ocra16") else: display.drawText(0, 0, "Demo time!", 0x000000, "ocra16") my = (display.height() - h) // 2 - (round(math.sin(counter)) - 1) * 10 mx = (display.width() - w) // 2 - (round(math.cos(counter)) - 1) * 10 display.drawPng(mx, my, mascot.snek) #Draw squares display.drawRect(display.width() - 39 - (counter % display.width()), display.height() // 2 - 20, 40, 40, True, 0x00FF00) display.drawRect(counter % display.width(), display.height() // 2 - 10, 20, 20, True, 0xFF0000) #Flush to display display.flush() #Clear away the squares display.drawRect(display.width() - 39 - (counter % display.width()), display.height() // 2 - 20, 40, 40, True, 0xFFFF00) display.drawRect(counter % display.width(), display.height() // 2 - 10, 20, 20, True, 0xFFFF00) #Clear away the mascot display.drawRect(mx, my, w, h, True, 0xFFFF00) #Increment counter counter += 1 #time.sleep(0.01) prev = current drawNickname()
def drawTask(onSleep=False): global gui_redraw, cfg_nickname, gui_apps, gui_app_current, ota_available if gui_redraw or onSleep: gui_redraw = False display.drawFill(COLOR_BG) currHeight = 0 noLine = False if gui_app_current < 0: if cfg_logo: if cfg_nickname: # Logo and nickename enabled, first print nickname display.drawText((display.width()-display.getTextWidth(cfg_nick_text, "roboto_regular12"))//2, currHeight, cfg_nick_text, COLOR_FG, "roboto_regular12") currHeight += display.getTextHeight(cfg_nick_text, "roboto_regular12")#easydraw.nickname() currHeight += 4 display.drawLine(0, currHeight, display.width()-1, currHeight, COLOR_FG) currHeight += 4 #Print logo app_height = display.height()-16-currHeight logoHeight = drawLogo(currHeight, app_height, True) if logoHeight > 0: noLine = True if logoHeight < 1: #Logo enabled but failed to display title = "BADGE.TEAM" subtitle = "PLATFORM" logoHeight = display.getTextHeight(title, "permanentmarker22")+display.getTextHeight(subtitle, "fairlight12") display.drawText((display.width()-display.getTextWidth(title, "permanentmarker22"))//2, currHeight + (app_height - logoHeight)//2,title, COLOR_FG, "permanentmarker22") currHeight += display.getTextHeight(title, "permanentmarker22") display.drawText((display.width()-display.getTextWidth(subtitle, "fairlight12"))//2, currHeight + (app_height - logoHeight)//2,subtitle, COLOR_FG, "fairlight12") currHeight += display.getTextHeight(subtitle, "fairlight12") else: noLine = True display.drawPng(0,0,LOGO) else: display_app(currHeight) if onSleep: info = 'Sleeping...' #elif not rtc.isSet(): # info = "RTC not available" elif ota_available: info = "Update available!" #elif wifi_status_curr: # info = "WiFi connected" else: info = ""#'Press START' if not noLine: display.drawLine(0, display.height()-16, display.width(), display.height()-16, COLOR_FG) easydraw.disp_string_right_bottom(0, info) if len(gui_apps) > 0: drawPageIndicator(len(gui_apps), gui_app_current) if cfg_greyscale: display.flush(display.FLAG_LUT_GREYSCALE) else: display.flush(display.FLAG_LUT_NORMAL) return 1000
def cbSleep(idleTime=None): global stopThreads neopixel.send(bytes([0x00] * 3 * 12)) if idleTime == None: idleTime = virtualtimers.idle_time() gui_redraw = True stopThreads = True drawTask(True) display.flush() time.sleep(0.1) system.sleep(idleTime, True)