caption_position=(10, 220), caption_color=0x000000) # track the last value so we can play a sound when it updates last_follower = 0 while True: # Followers: # A limitation of the PyPortal object is it must have an equal number of # text objects and json fields. So in this case, get some dummy information, # then replace the text with the word followers after. It's a bit ugly. # Alternatively, do this outside of a pyportal object manually. try: pyportal._json_path = main_json_path dummy, followers = pyportal.fetch(MAIN_DATA_SOURCE) pyportal.set_text("Followers:", index=0) followers = int(followers) print("followers:", followers) if last_follower < followers: # another follower print("New Follower!") pyportal.play_file(cwd + "/coin.wav") last_follower = followers except RuntimeError as e: print("Some error occured, retrying! -", e) except ValueError as e: print("Value error occured, retrying! -", e) except: print("An unknown error occured, retrying!") time.sleep(30)
#### END INIT PORTAL #### # Play boot-up sound play_panel_beep(20) # Update device local time and preload the font pyportal.preload_font() pyportal.get_local_time() screen_brightness() #### Primary screen operation loop #### while True: # Primary functions inside exception handler with 1-minute timeout. try: # Run any scheduled activities #run_scheduler(last_metar_time) # When screen is touched: if pyportal.touchscreen.touch_point: play_panel_beep(0) # If a new METAR is scheduled to be obtained if time_for_new_metar(last_metar_time): response = pyportal.fetch() play_panel_beep(2) last_metar_time = time.time() except Exception as e: print("Some error occured, retrying in 1 minute: ", e) pyportal.set_background(error_bg) pyportal.set_text('LCARS', index=2) play_offline_audio() time.sleep(60)
contests = Contests() contests.load_contests() counter = 0 while True: if contests: if not contests.contest_refresh or \ (time.monotonic() - contests.contest_refresh) > (contests.update_minutes * 60): contests.load_contests() cleanup_cache() counter += 1 text, graphic = contests.get_next_contest_string_and_graphic() print(f'START: Loop #{counter}; Contest {contests.index}') pyportal.set_text('', index=text_index) graphics.set_background(graphic) time.sleep(1) pyportal.set_text(text, index=text_index) text = None graphic_url = None gc.collect() print(f'END: Loop #{counter}; Contest {contests.index}') time.sleep(15)
import board from strategies import strategies from adafruit_pyportal import PyPortal cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is) # create pyportal object w no data source (we'll feed it text later) pyportal = PyPortal(url = None, json_path = None, status_neopixel = board.NEOPIXEL, default_bg = None, text_font = cwd+"fonts/Arial-ItalicMT-17.bdf", text_position = (30, 120), text_color = 0xFFFFFF, ) pyportal.set_text("loading ...") # display while user waits pyportal.preload_font() # speed things up by preloading font pyportal.set_text("OBLIQUE STRATEGIES\nBrian Eno / Peter Schmidt") # show title while True: if pyportal.touchscreen.touch_point: # get random string from array and wrap w line breaks strat = pyportal.wrap_nicely(random.choice(strategies), 35) outstring = '\n'.join(strat) # display new text pyportal.set_text(outstring, 0) # don't repeat until a new touch begins while pyportal.touchscreen.touch_point: continue
pm25min, pm25max = pm25Indices[i] epaMin, epaMax = epaIndices[i] i += 1 AQI = ((epaMax - epaMin) / (pm25max - pm25min)) * (value - pm25min) + epaMin return int(AQI) while True: try: value = pyportal.fetch() AQI = convertToEPAAQI(value) pyportal.set_text(AQI, 0) if 0 <= AQI <= 50: pyportal.set_background(0x66bb6a) # good if 51 <= AQI <= 100: pyportal.set_background(0xffeb3b) # moderate if 101 <= AQI <= 150: pyportal.set_background(0xf39c12) # sensitive if 151 <= AQI <= 200: pyportal.set_background(0xff5722) # unhealthy if 201 <= AQI <= 300: pyportal.set_background(0x8e24aa) # very unhealthy if 301 <= AQI <= 500: pyportal.set_background(0xb71c1c) # hazardous time.sleep(10 * 60) # wait 10 minutes before getting again
(50, 135)), # definition location text_color=(0x8080FF, 0xFF00FF, 0xFFFFFF), text_wrap=( 0, # characters to wrap for text 0, 28), text_maxlen=(180, 30, 115), # max text size for word, part of speech and def caption_text=CAPTION, caption_font=cwd + "/fonts/Arial-ItalicMT-17.bdf", caption_position=(50, 220), caption_color=0x808080) print("loading...") # print to repl while waiting for font to load pyportal.preload_font() # speed things up by preloading font pyportal.set_text("\nWord of the Day") # show title while True: if pyportal.touchscreen.touch_point: try: #set the JSON path here to be able to change between definition and example # pylint: disable=protected-access pyportal._json_path = (['word'], PART_OF_SPEECH, DEFINITION_EXAMPLE_ARR[definition_example]) value = pyportal.fetch() print("Response is", value) except RuntimeError as e: print("Some error occured, retrying! -", e) #Change between definition and example if definition_example == 0: definition_example = 1