def delayLoop(secs): global oldorientation for i in range(0, secs): print("miniloop") ival = imu.get_acceleration() if ival['y'] < 0: neworientation = 0 else: neworientation = 180 if neworientation != oldorientation: ugfx.orientation(neworientation) cls() oldorientation = neworientation time.sleep(1)
style = ugfx.Style() style.set_enabled([ ugfx.WHITE, ugfx.html_color(0x800080), ugfx.html_color(0x800080), ugfx.html_color(0x800080) ]) style.set_background(ugfx.html_color(0x800080)) ugfx.set_default_style(style) # Logo stuff ugfx.display_image(int((ugfx.width() - logo_width) / 2), int((ugfx.height() - logo_height) / 2), logo_path) # Draw for people to see ugfx.orientation(90) # Draw introduction ugfx.set_default_font(ugfx.FONT_TITLE) ugfx.Label(0, ugfx.height() - name_height - intro_height, ugfx.width(), intro_height, intro_text, justification=ugfx.Label.CENTER) # Process name name_setting = name("Set your name in the settings app") if len(name_setting) <= max_name: ugfx.set_default_font(ugfx.FONT_NAME) else: ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) # Draw name
# This file is executed on every boot (including wake-boot from deepsleep) import badge, machine, esp, ugfx, sys, time # Initialise UGFX ugfx.init() ugfx.orientation(0) ugfx.input_init() esp.rtcmem_write(0,0) esp.rtcmem_write(1,0) # setup timezone rtc = machine.RTC() if (rtc.timezone()==""): rtc.timezone(badge.nvs_get_str('system', 'timezone', 'CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00')) if badge.safe_mode(): splash = 'splash' else: splash = badge.nvs_get_str('boot','splash','splash') if machine.wake_reason() == (7, 0): print('[BOOT] Cold boot') import post_ota else: if machine.wake_reason() == (3, 4): print("[BOOT] Wake from sleep (timer)") elif machine.wake_reason() == (3, 1): print("[BOOT] Wake from sleep (button)") else:
ugfx.text(35, 221, "EMFFAX: The World at Your Fingertips", ugfx.BLUE) from imu import IMU imu = IMU() pages = Pages([Page1(),Page2()]) counter = 10000 orient = 0 while True: pyb.wfi() if buttons.is_pressed("BTN_MENU"): break counter += 1 if counter >= 10000: pages.print_random() counter = 0 ival = imu.get_acceleration() if ival['y'] < 0: if orient != 0: ugfx.orientation(0) orient = 0 pages.print_current() elif orient != 180: ugfx.orientation(180) orient = 180 pages.print_current()
def write_loading(): ugfx.clear(ugfx.html_color(0x000000)) ugfx.orientation(90) ugfx.text(5, 5, "Loading...", ugfx.WHITE) ugfx.orientation(270) ugfx.text(5, 5, "Loading...", ugfx.WHITE)
def write_hot_instructions(): ugfx.orientation(270) ugfx.text(3, 85, "Press A to refresh or press B", ugfx.WHITE) ugfx.text(3, 105, "to change the url or check", ugfx.WHITE) ugfx.text(3, 125, "your wifi settings...", ugfx.WHITE)
def write_instructions(): ugfx.clear(ugfx.html_color(0x000000)) ugfx.orientation(270) ugfx.text(5, 5, "Press A to refresh", ugfx.WHITE) ugfx.text(5, 25, "Press B to change the url", ugfx.WHITE) ugfx.text(5, 45, "Press Menu to exit", ugfx.WHITE)
def get_external_hook_paths(): return ["%s/external" % app.folder_path for app in get_local_apps() if is_file("%s/external.py" % app.folder_path)] def low_power(): ugfx.backlight(0) ugfx.power_mode(ugfx.POWER_OFF) ugfx.init() imu=IMU() neo = pyb.Neopix(pyb.Pin("PB13")) neo.display(0x04040404) ledg = pyb.LED(2) ival = imu.get_acceleration() if ival['y'] < 0: ugfx.orientation(0) else: ugfx.orientation(180) buttons.init() if not onboard.is_splash_hidden(): splashes = ["splash1.bmp"] for s in splashes: ugfx.display_image(0,0,s) delay = 2000 while delay: delay -= 1 if buttons.is_triggered("BTN_MENU"): break; if buttons.is_triggered("BTN_A"):
def drawCustomImage(): ugfx.clear() ugfx.orientation(90) ugfx.display_image(0, 0, 'customImage.png')
"shared/nyan/3.png", "shared/nyan/4.png", "shared/nyan/5.png"] ___categories___ = ["Homescreens", "Other"] import ugfx_helper, os, wifi, ugfx, http, time, sleep, app from tilda import Buttons # initialize screen ugfx_helper.init() ugfx.clear(ugfx.BLACK) ugfx.backlight(100) n = 0 r = 270 while True: ugfx.display_image( 0, 90, "shared/nyan/{}.png".format(n) ) n = (n+1) % 6 sleep.sleep_ms(10) if Buttons.is_pressed(Buttons.BTN_B): break elif Buttons.is_pressed(Buttons.BTN_A): r = (r + 180) % 360 ugfx.clear(ugfx.BLACK) ugfx.orientation(r) ugfx.clear() app.restart_to_default()
c.show() # Unused, broadcast and multicasts are blocked on the wifi #s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #s.bind(('', 14612)) #s.setblocking(True) #while True: #data, addr = s.recvfrom(1024) #curOut.text(data) imu = IMU() connectto = usocket.getaddrinfo('monitor1.emf.camp', 80) oldorientation = ugfx.orientation() looped = 0 def updateStats(): # Download the JSON file s = usocket.socket() try: s.connect(connectto[0][4]) s.send("GET /api/\r\n") output = s.recv(4096) s.close() except Exception as e: sys.print_exception(e)
c.show() # Unused, broadcast and multicasts are blocked on the wifi #s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #s.bind(('', 14612)) #s.setblocking(True) #while True: #data, addr = s.recvfrom(1024) #curOut.text(data) imu = IMU() connectto = usocket.getaddrinfo('monitor1.emf.camp',80) oldorientation = ugfx.orientation() looped = 0 def updateStats(): # Download the JSON file s = usocket.socket() try: s.connect(connectto[0][4]) s.send("GET /api/\r\n") output = s.recv(4096) s.close() except Exception as e: sys.print_exception(e)
def home_main(): global orientation, next_tick, tick ugfx.area(0,0,320,240,sty_tb.background()) ugfx.set_default_font(ugfx.FONT_MEDIUM) win_bv = ugfx.Container(0,0,80,25, style=sty_tb) win_wifi = ugfx.Container(82,0,60,25, style=sty_tb) win_name = ugfx.Container(0,25,320,240-25-60, style=dialogs.default_style_badge) win_text = ugfx.Container(0,240-60,320,60, style=sty_tb) win_clock = ugfx.Container(250, 0, 70, 25, style=sty_tb) windows = [win_bv, win_wifi, win_clock, win_text] obj_name = apps.home.draw_name.draw(0,25,win_name) buttons.init() gc.collect() ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) hook_feeback = ugfx.List(0, 0, win_text.width(), win_text.height(), parent=win_text) win_bv.show() win_text.show() win_wifi.show() win_clock.show() # Create external hooks so other apps can run code in the context of # the home screen. # To do so an app needs to have an external.py with a tick() function. # The tick period will default to 60 sec, unless you define something # else via a "period" variable in the module context (use milliseconds) # If you set a variable "needs_wifi" in the module context tick() will # only be called if wifi is available # If you set a variable "needs_icon" in the module context tick() will # be called with a reference to a 25x25 pixel ugfx container that you # can modify external_hooks = [] icon_x = 150 for path in get_external_hook_paths(): try: module = __import__(path) if not hasattr(module, "tick"): raise Exception("%s must have a tick function" % path) hook = { "name": path[5:-9], "tick": module.tick, "needs_wifi": hasattr(module, "needs_wifi"), "period": module.period if hasattr(module, "period") else 60 * 1000, "next_tick_at": 0 } if hasattr(module, "needs_icon"): hook["icon"] = ugfx.Container(icon_x, 0, 25, 25) icon_x += 27 external_hooks.append(hook) except Exception as e: # Since we dont know what exception we're looking for, we cant do much print ("%s while parsing background task %s. This task will not run! " % (type(e).__name__, path[5:-9])) sys.print_exception(e) continue # If the module fails to load or dies during the setup, skip it backlight_adjust() inactivity = 0 last_rssi = 0 ## start connecting to wifi in the background wifi_timeout = 30 #seconds wifi_reconnect_timeout = 0 wifi_did_connect = 0 try: wifi.connect(wait = False, prompt_on_fail = False) except OSError: print("Connect failed") while True: pyb.wfi() ugfx.poll() if (next_tick <= pyb.millis()): tick = True next_tick = pyb.millis() + 1000 #if wifi still needs poking if (wifi_timeout > 0): if wifi.nic().is_connected(): wifi_timeout = -1 #wifi is connected, but if becomes disconnected, reconnect after 5 sec wifi_reconnect_timeout = 5 else: wifi.nic().update() if tick: tick = False ledg.on() if (wifi_timeout > 0): wifi_timeout -= 1; # change screen orientation ival = imu.get_acceleration() if ival['y'] < -0.5: if orientation != 0: ugfx.orientation(0) elif ival['y'] > 0.5: if orientation != 180: ugfx.orientation(180) if orientation != ugfx.orientation(): inactivity = 0 ugfx.area(0,0,320,240,sty_tb.background()) orientation = ugfx.orientation() for w in windows: w.hide(); w.show() apps.home.draw_name.draw(0,25,win_name) #if wifi timeout has occured and wifi isnt connected in time if (wifi_timeout == 0) and not (wifi.nic().is_connected()): print("Giving up on Wifi connect") wifi_timeout = -1 wifi.nic().disconnect() #give up wifi_reconnect_timeout = 30 #try again in 30sec wifi_is_connected = wifi.nic().is_connected() #if not connected, see if we should try again if not wifi_is_connected: if wifi_did_connect > 0: wifi_did_connect = 0 if wifi_reconnect_timeout>0: wifi_reconnect_timeout -= 1 if wifi_reconnect_timeout == 0: wifi_timeout = 60 #seconds wifi.connect(wait = False) else: # If we've just connected, set NTP time if wifi_did_connect == 0: wifi_did_connect = 1 ntp.set_NTP_time() ledg.on() # display the wifi logo rssi = wifi.nic().get_rssi() if rssi == 0: rssi = last_rssi else: last_rssi = rssi time = pyb.RTC().datetime() if time[0] >= 2016: draw_time(sty_tb.background(), pyb.RTC().datetime(), win_clock) draw_wifi(sty_tb.background(),rssi, wifi_is_connected,wifi_timeout>0,win_wifi) battery_percent = onboard.get_battery_percentage() draw_battery(sty_tb.background(),battery_percent,win_bv) inactivity += 1 # turn off after some period # takes longer to turn off in the 'used' position if ugfx.orientation() == 180: inactivity_limit = 120 else: inactivity_limit = 30 if battery_percent > 120: #if charger plugged in if ugfx.backlight() == 0: ugfx.power_mode(ugfx.POWER_ON) ugfx.backlight(100) elif inactivity > inactivity_limit: low_power() else: backlight_adjust() ledg.off() for hook in external_hooks: try: if hook["needs_wifi"] and not wifi.nic().is_connected(): continue; if hook["next_tick_at"] < pyb.millis(): text = None if "icon" in hook: text = hook["tick"](hook["icon"]) else: text = hook["tick"]() hook["next_tick_at"] = pyb.millis() + hook["period"] if text: if hook_feeback.count() > 10: hook_feeback.remove_item(0) hook_feeback.add_item(text) if hook_feeback.selected_index() >= (hook_feeback.count()-2): hook_feeback.selected_index(hook_feeback.count()-1) except Exception as e: # if anything in the hook fails to work, we need to skip it print ("%s in %s background task. Not running again until next reboot! " % (type(e).__name__, hook['name'])) sys.print_exception(e) external_hooks.remove(hook) continue if buttons.is_pressed("BTN_MENU"): pyb.delay(20) break if buttons.is_pressed("BTN_A"): inactivity = 0 tick = True if buttons.is_pressed("BTN_B"): inactivity = 0 tick = True for hook in external_hooks: if "icon" in hook: hook["icon"].destroy() for w in windows: w.destroy() apps.home.draw_name.draw_destroy(obj_name) win_name.destroy() hook_feeback.destroy() if ugfx.backlight() == 0: ugfx.power_mode(ugfx.POWER_ON) ugfx.backlight(100) ugfx.orientation(180) #if we havnt connected yet then give up since the periodic function wont be poked if wifi_timeout >= 0: # not (wifi.nic().is_connected()): wifi.nic().disconnect()
def drawEMFscreen(): # Padding for name intro_height = 30 intro_text = "Hi! I'm" name_height = 60 status_height = 20 info_height = 30 logo_path = "shared/logo.png" logo_height = 150 logo_width = 56 # Maximum length of name before downscaling max_name = 8 # Background stuff init() ugfx.clear(ugfx.html_color(0x800080)) # Colour stuff style = ugfx.Style() style.set_enabled([ ugfx.WHITE, ugfx.html_color(0x800080), ugfx.html_color(0x800080), ugfx.html_color(0x800080) ]) style.set_background(ugfx.html_color(0x800080)) ugfx.set_default_style(style) # Logo stuff ugfx.display_image(int((ugfx.width() - logo_width) / 2), int((ugfx.height() - logo_height) / 2), logo_path) # Draw for people to see ugfx.orientation(90) # Draw introduction ugfx.set_default_font(ugfx.FONT_TITLE) ugfx.Label(0, ugfx.height() - name_height - intro_height, ugfx.width(), intro_height, intro_text, justification=ugfx.Label.CENTER) # Process name name_setting = name("Set your name in the settings app") if len(name_setting) <= max_name: ugfx.set_default_font(ugfx.FONT_NAME) else: ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) # Draw name ugfx.Label(0, ugfx.height() - name_height, ugfx.width(), name_height, name_setting, justification=ugfx.Label.CENTER) # Draw for wearer to see ugfx.orientation(270) # Title ugfx.set_default_font(ugfx.FONT_TITLE) ugfx.Label(0, ugfx.height() - info_height * 2, ugfx.width(), info_height, "TiLDA Mk4", justification=ugfx.Label.CENTER) # info ugfx.Label(0, ugfx.height() - info_height, ugfx.width(), info_height, "Press MENU", justification=ugfx.Label.CENTER) ugfx.set_default_font(ugfx.FONT_SMALL) status = ugfx.Label(0, ugfx.height() - info_height * 2 - status_height, ugfx.width(), status_height, "", justification=ugfx.Label.CENTER) text = "" value_wifi_strength = wifi_strength() value_battery = battery() if value_wifi_strength: text += "Wi-Fi: %s%%, " % int(value_wifi_strength) if value_battery: text += "Battery: %s%%" % int(value_battery) status.text(text)
def home_main(): global orientation, next_tick, tick ugfx.area(0,0,320,240,sty_tb.background()) ugfx.set_default_font(ugfx.FONT_MEDIUM) win_bv = ugfx.Container(0,0,80,25, style=sty_tb) win_wifi = ugfx.Container(82,0,60,25, style=sty_tb) win_name = ugfx.Container(0,25,320,240-25-60, style=dialogs.default_style_badge) win_text = ugfx.Container(0,240-60,320,60, style=sty_tb) windows = [win_bv, win_wifi, win_text] obj_name = apps.home.draw_name.draw(0,25,win_name) buttons.init() gc.collect() ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) hook_feeback = ugfx.List(0, 0, win_text.width(), win_text.height(), parent=win_text) win_bv.show() win_text.show() win_wifi.show() min_ctr = 28 # Create external hooks so other apps can run code in the context of # the home screen. # To do so an app needs to have an external.py with a tick() function. # The tick period will default to 60 sec, unless you define something # else via a "period" variable in the module context (use milliseconds) # If you set a variable "needs_wifi" in the module context tick() will # only be called if wifi is available # If you set a variable "needs_icon" in the module context tick() will # be called with a reference to a 25x25 pixel ugfx container that you # can modify external_hooks = [] icon_x = 150 for path in get_external_hook_paths(): try: module = __import__(path) if not hasattr(module, "tick"): raise Exception("%s must have a tick function" % path) hook = { "name": path[5:-9], "tick": module.tick, "needs_wifi": hasattr(module, "needs_wifi"), "period": module.period if hasattr(module, "period") else 60 * 1000, "next_tick_at": 0 } if hasattr(module, "needs_icon"): hook["icon"] = ugfx.Container(icon_x, 0, 25, 25) icon_x += 27 external_hooks.append(hook) except Exception as e: # Since we dont know what exception we're looking for, we cant do much print ("%s while parsing background task %s. This task will not run! " % (type(e).__name__, path[5:-9])) sys.print_exception(e) continue # If the module fails to load or dies during the setup, skip it backlight_adjust() inactivity = 0 last_rssi = 0 ## start connecting to wifi in the background wifi_timeout = 30 #seconds wifi_reconnect_timeout = 0 try: wifi.connect(wait = False) except OSError: print("Creating default wifi settings file") wifi.create_default_config() while True: pyb.wfi() ugfx.poll() if (next_tick <= pyb.millis()): tick = True next_tick = pyb.millis() + 1000 #if wifi still needs poking if (wifi_timeout > 0): if wifi.nic().is_connected(): wifi_timeout = -1 #wifi is connected, but if becomes disconnected, reconnect after 5 sec wifi_reconnect_timeout = 5 else: wifi.nic().update() if tick: tick = False ledg.on() if (wifi_timeout > 0): wifi_timeout -= 1; # change screen orientation ival = imu.get_acceleration() if ival['y'] < -0.5: if orientation != 0: ugfx.orientation(0) elif ival['y'] > 0.5: if orientation != 180: ugfx.orientation(180) if orientation != ugfx.orientation(): inactivity = 0 ugfx.area(0,0,320,240,sty_tb.background()) orientation = ugfx.orientation() for w in windows: w.hide(); w.show() apps.home.draw_name.draw(0,25,win_name) #if wifi timeout has occured and wifi isnt connected in time if (wifi_timeout == 0) and not (wifi.nic().is_connected()): print("Giving up on Wifi connect") wifi_timeout = -1 wifi.nic().disconnect() #give up wifi_reconnect_timeout = 30 #try again in 30sec wifi_connect = wifi.nic().is_connected() #if not connected, see if we should try again if not wifi_connect: if wifi_reconnect_timeout>0: wifi_reconnect_timeout -= 1 if wifi_reconnect_timeout == 0: wifi_timeout = 60 #seconds wifi.connect(wait = False) ledg.on() # display the wifi logo rssi = wifi.nic().get_rssi() if rssi == 0: rssi = last_rssi else: last_rssi = rssi draw_wifi(sty_tb.background(),rssi, wifi_connect,wifi_timeout>0,win_wifi) battery_percent = onboard.get_battery_percentage() draw_battery(sty_tb.background(),battery_percent,win_bv) inactivity += 1 # turn off after some period # takes longer to turn off in the 'used' position if ugfx.orientation() == 180: inactivity_limit = 120 else: inactivity_limit = 30 if battery_percent > 120: #if charger plugged in if ugfx.backlight() == 0: ugfx.power_mode(ugfx.POWER_ON) ugfx.backlight(100) elif inactivity > inactivity_limit: low_power() else: backlight_adjust() ledg.off() for hook in external_hooks: try: if hook["needs_wifi"] and not wifi.nic().is_connected(): continue; if hook["next_tick_at"] < pyb.millis(): text = None if "icon" in hook: text = hook["tick"](hook["icon"]) else: text = hook["tick"]() hook["next_tick_at"] = pyb.millis() + hook["period"] if text: if hook_feeback.count() > 10: hook_feeback.remove_item(0) hook_feeback.add_item(text) if hook_feeback.selected_index() >= (hook_feeback.count()-2): hook_feeback.selected_index(hook_feeback.count()-1) except Exception as e: # if anything in the hook fails to work, we need to skip it print ("%s in %s background task. Not running again until next reboot! " % (type(e).__name__, hook['name'])) sys.print_exception(e) external_hooks.remove(hook) continue if buttons.is_pressed("BTN_MENU"): pyb.delay(20) break if buttons.is_pressed("BTN_A"): inactivity = 0 tick = True if buttons.is_pressed("BTN_B"): inactivity = 0 tick = True for hook in external_hooks: if "icon" in hook: hook["icon"].destroy() for w in windows: w.destroy() apps.home.draw_name.draw_destroy(obj_name) win_name.destroy() hook_feeback.destroy() if ugfx.backlight() == 0: ugfx.power_mode(ugfx.POWER_ON) ugfx.backlight(100) ugfx.orientation(180) #if we havnt connected yet then give up since the periodic function wont be poked if wifi_timeout >= 0: # not (wifi.nic().is_connected()): wifi.nic().disconnect()
def write_some_text(): ugfx.clear(ugfx.html_color(0x000000)) ugfx.orientation(270) ugfx.text(5, 5, "Text here", ugfx.WHITE) ugfx.text(5, 25, "More here", ugfx.WHITE) ugfx.text(5, 45, "Last line", ugfx.WHITE)