def one_round(): ball = Ball() topPaddle = Paddle(0) bottomPaddle = Paddle(1) ugfx.clear(bgColor) ugfx.backlight(100) ugfx.set_default_font(ugfx.FONT_TITLE) while True: topPaddle.update() bottomPaddle.update() ball.update(topPaddle, bottomPaddle) if ball.isDead(): if(ball.y > SCREEN_HEIGHT/2): return [1,0] else: return [0,1] topPaddle.draw() bottomPaddle.draw() ball.draw() #draw the net for i in range(0,7): ugfx.area(int(i*2*SCREEN_WIDTH/13), int(SCREEN_HEIGHT/2-1), int(SCREEN_WIDTH/13), 3, netColor) ugfx.orientation(0) ugfx.text(130, 0, "%d " % (points[0]),netColor) ugfx.text(170, 0, "%d " % (points[1]),netColor) ugfx.orientation(270) time.sleep_ms(1)
def init_screen(orientation): # initialize screen ugfx.clear() ugfx.orientation(orientation) ugfx.backlight(50) # show initial screen # photo credit: https://www.flickr.com/photos/remedy451/8061918891 ugfx.display_image(0, 0, 'trains/splash.gif', 90)
def __init__(self): ugfx.init() if ugfx.backlight() == 0: ugfx.power_mode(ugfx.POWER_ON) ugfx.backlight(70) self.rm = chuckie.render.RenderManager() self.raster = raster.RasterTile() self._tick = time.ticks_ms() self._delta = 0 buttons.init()
def display_idle(self, lh_call="", lh_name=""): if self.__debug: print("emf_hub_mon: Drawing Idle Screen") self.__pending_idle = False ugfx.backlight(40) self.draw_background(ugfx.html_color(0xf7f4f4)) ugfx.set_default_font(ugfx.FONT_TITLE) self.__text_centre("IDLE", ugfx.FONT_TITLE, ugfx.GREY, 120) self.__display_lh(lh_call, lh_name) self.__led.off()
def tx(self, call, name): if self.__debug: print("emf_hub_mon: Drawing the Tx display") ugfx.backlight(100) self.draw_background(ugfx.html_color(0xaefcbd)) ugfx.set_default_font(ugfx.FONT_TITLE) self.__text_centre(call, ugfx.FONT_TITLE, ugfx.BLACK, 110) self.__text_centre(name, ugfx.FONT_MEDIUM_BOLD, ugfx.BLACK, 140) self.__led.on()
def backlight_adjust(): if ugfx.backlight() == 0: ugfx.power_mode(ugfx.POWER_ON) l = pyb.ADC(16).read() if (l > 90): ugfx.backlight(100) elif (l > 20): ugfx.backlight(70) else: ugfx.backlight(30)
def low_power(): ugfx.backlight(0) ugfx.power_mode(ugfx.POWER_OFF)
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_wifi" 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 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()
# Graphics setup ugfx.init() ugfx.clear() buttons.init() sty = ugfx.Style(dialogs.default_style_badge) sty.set_enabled([ ugfx.WHITE, ugfx.html_color(0xA66FB0), ugfx.html_color(0x5e5e5e), ugfx.RED ]) sty.set_background(ugfx.html_color(0xA66FB0)) ugfx.set_default_style(sty) ugfx.area(0, 0, 320, 240, sty.background()) ugfx.set_default_font(ugfx.FONT_MEDIUM) ugfx.backlight(30) LINE_HEIGHT = 20 current_line = 0 def write_line(text): global current_line ugfx.text(5, 5 + (current_line * LINE_HEIGHT), text, ugfx.WHITE) print(text) current_line += 1 # This will immediately return if we're already connected, otherwise # it'll attempt to connect or prompt for a new network. Proceeding # without an active network connection will cause the getaddrinfo to
"shared/nyan/1.png", "shared/nyan/2.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)
newHookStatus = bool(pinHook.value()) if (newHookStatus != hookStatus): hookStatus = newHookStatus currentNumber = "" #reset number dialed ugfx.area(5, 90, 235, 20, ugfx.WHITE) hookTimeLastToggle = currentTimeMs print("Hook: " + str(hookStatus)) ##### MAIN ##### # initialize screen ugfx.init() ugfx.clear() ugfx.backlight(0) ugfx.text(5, 60, "NUMBER", ugfx.BLACK) # MAIN loop while True: currentTimeMs = utime.ticks_ms() updateDial(currentTimeMs) updateBell(currentTimeMs) updatePhone() updateHook(currentTimeMs) updateGeneralInfo(currentTimeMs)
from imu import IMU import pyb import ugfx import buttons imu = IMU() ugfx.init() bgcolor = ugfx.BLACK fgcolor = ugfx.YELLOW ugfx.clear(bgcolor) ugfx.backlight(20) buttons.init() t4 = pyb.Timer(4, freq=100, mode=pyb.Timer.CENTER) while not buttons.is_pressed("BTN_MENU"): accel = imu.get_acceleration() accel_x = accel.get('x') accel_y = accel.get('y') accel_z = accel.get('z') accel_use = accel_y t4.freq(700 + accel_use * 200) ch1 = t4.channel(1, pyb.Timer.PWM, pin=pyb.Pin("BUZZ"), pulse_width=(t4.period() + 1) // 2) #ugfx.text(10,10,str(accel),ugfx.GREEN)
def wifiled(enable): ledr = pyb.LED(1) if enable: rssi = wifi.nic().get_rssi() intensuty = (rssi+100)*3 ledr.intensity(intensuty) print("Rssi: " + str(rssi)) else: ledr.intensity(255) ugfx.init() ugfx.clear() buttons.init() ugfx.set_default_font(ugfx.FONT_NAME) ugfx.backlight(100) 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) def kim(foo): ugfx.display_image(0, 0, "apps/home/kim.gif") def nick_screen(container_handle):