def Main(): try: log.info("starting") openConfigFile() checkAndUpdateToken() UpdateWeatherData() epd = epd4in2.EPD() log.info("init and clear") epd.init() epd.Clear() # Drawing on the Horizontal image log.info("init initial image") log.debug("X=" + str(epd.height) + " y=" + str(epd.width)) HBlackimage = Image.new('1', (epd.height, epd.width), 255) #black buffer drawblack = ImageDraw.Draw(HBlackimage) DrawScreen(HBlackimage, drawblack) HBlackimage = HBlackimage.rotate(180) epd.display(epd.getbuffer(HBlackimage)) except IOError as e: logging.info(e) except KeyboardInterrupt: logging.info("ctrl + c:") epd4in2.epdconfig.module_exit() exit()
def displayToEPD(image, clearToo): try: from waveshare_epd import epd4in2 epd = epd4in2.EPD() epd.init() if(clearToo): epd.Clear() epd.display(epd.getbuffer(image)) epd.sleep() except: traceback.print_exc() epd4in2.epdconfig.module_exit()
def __init__(self): self.epd = epd4in2.EPD() self.mScale = 1 #0=1h 1=3h 2=6h 3=12h 4=24h 5=48h 6=72h 7=96h 8=120h self.pression = 0.0 self.temperature = 0.0 self.humidity = 0.0 self.tendance = () self.durees = [1, 3, 6, 12, 24, 48, 72, 96, 120] self.typeGRF = 0 self.initScreen() self.clearScreen() self.drawAxes() self.epd.display(self.epd.getbuffer(self.mImage))
def update_screen(dt, weather, tasks): # TODO: add decription # TODO: add unittest # Update time dt.update() try: logging.info("Update Screen") # init the screen epd = epd4in2.EPD() logging.info("Init Screen") epd.init() # Drawing on the Horizontal image logging.info("Drawing on the Horizontal image...") Himage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame draw = ImageDraw.Draw(Himage) # Draw lines logging.info("Drawing lines") draw_horizontal_line(draw) draw_vertical_line(draw) # Time section draw_date_and_time(draw) # Weather section paste_weather_icon(Himage, weather) draw_temperature(draw, weather) draw_weather_desc(draw, weather) # Task Section draw_todo(draw) draw_tasks(draw, tasks) # Displaying image on screen logging.info("Displaying image") epd.display(epd.getbuffer(Himage)) except IOError as e: logging.info(e) except KeyboardInterrupt: logging.info("ctrl + c:") epd4in2.epdconfig.module_exit() exit()
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic') libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib') if os.path.exists(libdir): sys.path.append(libdir) import logging from waveshare_epd import epd4in2 import time from PIL import Image,ImageDraw,ImageFont logging.basicConfig(level=logging.DEBUG) try: logging.info("epd4in2 Demo") epd = epd4in2.EPD() logging.info("init and Clear") epd.init() epd.Clear() font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24) font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18) font35 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 35) # Drawing on the Horizontal image logging.info("1.Drawing on the Horizontal image...") Himage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame draw = ImageDraw.Draw(Himage) draw.text((10, 0), 'hello world', font = font24, fill = 0) draw.text((10, 20), '4.2inch e-Paper', font = font24, fill = 0) draw.text((150, 0), u'微雪电子', font = font24, fill = 0)
def mainLoop(): metric_units = False twenty_four_clock = False epd = epd4in2.EPD() WIDTH, HEIGHT = epd.width, epd.height r_ctx_type, r_ctx_title, l_ctx_type, l_ctx_title = "", "", "", "" time_elapsed = 15.0 old_time, temp_tuple = None, None # countTo5 is used to get weather every 5 minutes countTo5 = 0 sunset_time_tuple = None # First loop, init EPD DID_EPD_INIT = False try: while True: # Get time variables. old_time is used to ensure even time difference intervals between updates sec_left, time_str, date_str, old_time = getTimeFromDatetime( time_elapsed, old_time, twenty_four_clock) print(time_str) # Firstly, this is for my own edifice to know how long a loop takes for the Pi # Secondly, this is used to 'push' our clock forward such that our clock update is exactly on time start = time() # OPENWEATHER API CALL if temp_tuple == None or countTo5 == 4: temp_tuple, sunset_time_tuple = getWeather(metric_units) # CREATE BLANK IMAGE Himage = Image.new('1', (WIDTH, HEIGHT), 128) draw = ImageDraw.Draw(Himage) # GET left user's SPOTIPY AUTH OBJECT, TOKEN print("Get left user's Spotify Token") l_oauth = spotipy.oauth2.SpotifyOAuth(l_spot_client_id, l_spot_client_secret, REDIRECT_URI, scope=SPOT_SCOPE, cache_path=l_cache, requests_timeout=10) l_token_info = l_oauth.get_cached_token() l_token = getSpotipyToken(l_oauth, l_token_info) if l_token: l_track, l_artist, l_time_since, temp_context_type, temp_context_name = getSpotipyInfo( l_token) l_ctx_type = temp_context_type if temp_context_type != "" else l_ctx_type l_ctx_title = temp_context_name if temp_context_name != "" else l_ctx_title else: print(":( Left's access token unavailable") l_track, l_artist = "", "" # GET right user's SPOTIFY TOKEN print("Get right user's Spotify Token") r_oauth = spotipy.oauth2.SpotifyOAuth(r_spot_client_id, r_spot_client_secret, REDIRECT_URI, scope=SPOT_SCOPE, cache_path=r_cache, requests_timeout=10) r_token_info = r_oauth.get_cached_token() r_token = getSpotipyToken(r_oauth, r_token_info) if r_token: r_track, r_artist, r_time_since, temp_context_type, temp_context_name = getSpotipyInfo( r_token) r_ctx_type = temp_context_type if temp_context_type != "" else r_ctx_type r_ctx_title = temp_context_name if temp_context_name != "" else r_ctx_title else: print(":( Right's access token unavailable") r_track, r_artist = "", "" # If we have no context read, grab context our context.txt json file if (l_ctx_type == "" and l_ctx_title == "") or (r_ctx_type == "" and r_ctx_title == ""): try: fh = open('context.txt') l_ctx_type, l_ctx_title, r_ctx_type, r_ctx_title = contextIO.readJsonContext( (l_ctx_type, l_ctx_title), (r_ctx_type, r_ctx_title)) fh.close() except: print("context.txt doesn't exist") # Afterwords, if we have to write a new context to our context.txt json file, do so if (l_ctx_type != "" and l_ctx_title != "") or (r_ctx_type != "" and r_ctx_title != ""): contextIO.writeJsonContext((l_ctx_type, l_ctx_title), (r_ctx_type, r_ctx_title)) # USER 1 & 2 TRACK TITLES CONTEXT ---------------------------------------------------------------- track_line_count, track_text_size = epdDraw.drawTrackText( draw, l_track, 5, 26) epdDraw.drawArtistText(draw, l_artist, track_line_count, track_text_size, 5, 26) epdDraw.drawSpotContext(draw, Himage, l_ctx_type, l_ctx_title, 25, 204) track_line_count, track_text_size = epdDraw.drawTrackText( draw, r_track, 207, 26) epdDraw.drawArtistText(draw, r_artist, track_line_count, track_text_size, 207, 26) epdDraw.drawSpotContext(draw, Himage, r_ctx_type, r_ctx_title, 227, 204) # DRAW NAMES TIME SINCE ---------------------------------------------------------------- l_name_width, l_name_height = epdDraw.drawName(draw, "Alex", 8, 0) epdDraw.drawUserTimeAgo(draw, l_time_since, 18 + l_name_width, l_name_height // 2) r_name_width, r_name_height = epdDraw.drawName( draw, "Emma", 210, 0) epdDraw.drawUserTimeAgo(draw, r_time_since, 220 + r_name_width, r_name_height // 2) # DRAW LINES DATE TIME TEMP ---------------------------------------------------------------- epdDraw.drawBorderLines(draw) epdDraw.drawDateTimeTemp(draw, time_str, date_str, temp_tuple, metric_units) # Get 24H clock current_hour to determine sleep duration before refresh date = dt.now() + timedelta(seconds=time_elapsed) current_hour = int(date.strftime("%-H")) current_minute = int(date.strftime("%-M")) # from 2:01 - 5:59am, don't init the display, return from main, and have .sh script run again in 3 mins if 2 <= current_hour and current_hour <= 5: if DID_EPD_INIT == True: # in sleep() from epd4in2.py, epdconfig.module_exit() is never called # I hope this does not create long term damage 🤞 print("EPD Sleep(ish) ....") break else: print("Don't Wake") break elif DID_EPD_INIT == False: print("EPD INIT") epd.init() epd.Clear() sunsetTimeTuple = None DID_EPD_INIT = True # HIDDEN DARK MODE # Himage = ImageMath.eval('255-(a)',a=Himage) # HIDDEN sunset DARK MODE. if sunsetFlip = True invert display 24 mintues after sunset sunsetFlip = True if sunsetFlip: sunset_hour, sunset_minute = sunset_time_tuple if (sunset_hour < current_hour or current_hour < 2) or ( sunset_hour == current_hour and sunset_minute <= current_minute): print("Night Time Dark Mode @ {} {}".format( current_hour, current_minute)) Himage = ImageMath.eval('255-(a)', a=Himage) if DID_EPD_INIT: image_buffer = epd.getbuffer(Himage) print("\tDrawing Image to EPD") epd.display(image_buffer) # Look @ start variable above. find out how long it takes to compute our image stop = time() time_elapsed = stop - start remaining_time = sec_left - time_elapsed if remaining_time < 0: remaining_time = 60 if 5 < current_hour and current_hour < 24: # 6:00am - 12:59pm update screen every 3 minutes print( "\t", round(time_elapsed, 2), "\tseconds per loop\t", "sleeping for {} seconds".format( int(remaining_time) + 120)) sleep(remaining_time + 120) elif current_hour < 2: # 12:00am - 1:59am update screen every 5ish minutes print( "\t", round(time_elapsed, 2), "\tseconds per loop\t", "sleeping for {} seconds".format( int(remaining_time) + 240)) sleep(remaining_time + 240) # Increment counter for Weather requests countTo5 = 0 if countTo5 == 4 else countTo5 + 1 except Exception as e: with open("bugs.txt", "a+") as error_file: file_text = error_file.read() if str(e) in file_text: print("bug already caught") else: print("new bug caught\n" + str(e)) error_file.write(str(e) + "\n") print("Retrying mainLoop() in 20 seconds...") sleep(20) mainLoop()
def __init__(self, size="4in2"): if size == "4in2": self.epd = epd4in2.EPD() self.epd.init()
def update_display(): now = time.localtime() img = Image.new("L", (800, 600), color=ui.WHITE_COLOR) ui.draw_cache_age(img, net.time_since_last_fetch()) ui.draw_date(img, now) ui.draw_clock(img, 300, 300, 280, now.tm_hour, now.tm_min) ui.draw_stoplight( img, 620, 130, 30, storage.cache["temperature"] > 24 and storage.cache["bme_temperature"] < storage.cache["temperature"]) #ui.draw_gauge(img, 510, 130, 50, "Inside", # storage.cache["bme_temperature"], 10, 40) ui.draw_chart(img, 620, 390, 50, "°C", storage.cache["bme_temperature_history"], storage.cache["bme_temperature_history"][0]) ui.draw_gauge(img, 730, 130, 50, "°F Outside", ctof(storage.cache["temperature"]), 50, 100) # ui.draw_gauge(img, 510, 260, 50, "Ozone", storage.cache["ozone"], 0, 200) ui.draw_stoplight( img, 620, 260, 30, storage.cache["ozone"] > 100 or storage.cache["pm25"] > 100) #ui.draw_chart(img, 620, 260, 50, "AQI", # storage.cache["aqi_history"], storage.cache["aqi_history"][0], alpha=0.01) ui.draw_gauge(img, 730, 260, 50, "PM 2.5", storage.cache["pm25"], 0, 200) #ui.draw_gauge(img, 510, 390, 50, "Pressure", # storage.cache["bme_pressure"], 1000, 1026.5) #ui.draw_chart(img, 730, 390, 50, "hPa", # storage.cache["bme_pressure_history"], storage.cache["bme_pressure_history"][0]) ui.draw_gauge(img, 730, 390, 50, "Humidity", storage.cache["bme_humidity"], 0, 100) #dow = [] #for v in storage.cache["DIA_intraday"]: # dow.append(v*100) #ui.draw_gauge(img, 70, 520, 50, "Dow", # dow[-1], 20000, 30000) #ui.draw_chart(img, 180, 520, 50, "Dow", # dow, storage.cache["DIA_previous"]*100, alpha=0.1) #sp500 = [] #for v in storage.cache["SPY_intraday"]: # sp500.append(v*10) #ui.draw_gauge(img, 290, 520, 50, "S&P", # sp500[-1], 2000, 4000) #ui.draw_chart(img, 400, 520, 50, "S&P", # sp500, storage.cache["SPY_previous"]*10, alpha=0.1) #ui.draw_gauge(img, 290, 520, 50, "GME", # storage.cache["GME_intraday"][-1], 0, 500) #ui.draw_chart(img, 400, 520, 50, "GME", # storage.cache["GME_intraday"], storage.cache["GME_intraday"][0]) #ui.draw_gauge(img, 510, 520, 50, "Goog", # storage.cache["GOOG_intraday"][-1], 1000, 2000) ui.draw_gauge(img, 620, 520, 50, "Bitcoin", storage.cache["btc_usd"], 0, 40000) if "btc_history" in storage.cache: ui.draw_chart(img, 730, 520, 50, "BTC", storage.cache["btc_history"], storage.cache["btc_history"][0]) img = img.resize((400, 300), resample=Image.BOX) img = Image.eval(img, ui.image_correction) # img.show() epd = epd4in2.EPD() epd.Init_4Gray() epd.display_4Gray(epd.getbuffer_4Gray(img)) epd.sleep()