From e39a0c1b911740f17cc6071df4c6d17e3544285b Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Sun, 1 Oct 2017 09:04:55 +0200 Subject: [PATCH] Modif mise en page affichage sur EPD + Commentaires=>DocString --- cameteo-interface/mqtt2epaper.py | 76 +++++++++++++++++++------------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/cameteo-interface/mqtt2epaper.py b/cameteo-interface/mqtt2epaper.py index d6ba66b..eda7938 100644 --- a/cameteo-interface/mqtt2epaper.py +++ b/cameteo-interface/mqtt2epaper.py @@ -21,6 +21,14 @@ from PIL import ImageFont mqtt_client_id = "epaper_display" +# Colors codes +white = 255 +black = 0 + + +""" +Update ePaper Display +""" def update_epd(): #rotation de l'image de 90° + 180° si le paramètre epd_rotate est différent de 0 # la translation permet de rattraper un petit décalage en cas de rotation de 90+180° @@ -33,7 +41,9 @@ def update_epd(): # CALLBACKS # ############# -#Callback pour la connection au MQTT : souscriptions aux topics +""" +Callback pour la connection au MQTT : souscriptions aux topics +""" def on_connect(client, userdata, flags, rc): print(mqtt.connack_string(rc)) if rc == 0: @@ -43,8 +53,10 @@ def on_connect(client, userdata, flags, rc): ) print("OK") -#Callback de gestion des messages arrivant au MQTT : -# affichage et enregistrement en base de données +""" +Callback de gestion des messages arrivant au MQTT : +affichage et enregistrement en base de données +""" def on_message(client, userdata, msg): top=msg.topic[len(mqtt_topic)-1:].strip() subtopics = top.split("/") @@ -65,22 +77,20 @@ def on_message(client, userdata, msg): val['unit'] = val['unit'].replace('deg', '°') #Affichage des données - coord_type = { 'AT' : (2, 50), - 'RH' : (127, 50), - 'MSLP' : (2, 66), - 'ALTI' : (127, 66), + coord_type = { 'AT' : (12, 50), + 'RH' : (137, 50), + 'MSLP' : (12, 72), + 'ALTI' : (137, 72), } if val['type'] in coord_type: #erase former text - draw.rectangle(coord_type[val['type']] + (coord_type[val['type']][0] + 122, coord_type[val['type']][1] + 14), fill=255) + draw.rectangle(coord_type[val['type']] + (coord_type[val['type']][0] + 112, coord_type[val['type']][1] + 20), fill=white) #draw new values - draw.text(coord_type[val['type']], "{:8.1f} {} ".format(val['value'], val['unit']), font=font14, fill=0) -# #Update the display -# screen = image.rotate(90, expand=True) -# epd.set_frame_memory(screen, 0, 0) -# epd.display_frame() + draw.text(coord_type[val['type']], "{:6.1f}{} ".format(val['value'], val['unit']), font=font20_bold, fill=black) -#Callback managing date and time from MQTT broker +""" +Callback managing date and time from MQTT broker +""" def on_message_date(client, userdata, msg): payload = msg.payload.decode() print("Date : " + payload) @@ -92,29 +102,33 @@ def on_message_date(client, userdata, msg): dt_texte = payload #Affichage du résultat - draw.rectangle((0, 34, epd2in13.EPD_HEIGHT, 48), fill=255) - draw.text((0, 34), dt_texte, font=font14, fill=0) + draw.rectangle((0, 34, epd2in13.EPD_HEIGHT, 48), fill=white) + draw.text((0, 34), dt_texte, font=font14, fill=black) update_epd() -#Update display with info from camera (camera shooting new photo or name of the -# last photo) +""" +Update display with info from camera (camera shooting new photo or name of the +last photo) +""" def on_message_camera(client, userdata, msg): pl = msg.payload.decode() print("{} : {} ({})".format(msg.topic, pl, type(pl))) # Photo en cours if pl == "1" and msg.topic == camera_mqtt_topic + "/shooting" : - draw.rectangle((0, 105, epd2in13.EPD_HEIGHT, 120), fill=255) - draw.text((0, 105), "Shooting photo... ", font=font14, fill=0) + draw.rectangle((0, 106, epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), fill=white) + draw.text((0, 106), "Shooting photo... ", font=font14, fill=black) update_epd() #Dernière photo prise if msg.topic == camera_mqtt_topic + "/last_photo": - draw.rectangle((0, 105, epd2in13.EPD_HEIGHT, 120), fill=255) - draw.text((0, 105), "Last: " + pl, font=font14, fill=0) + draw.rectangle((0, 106, epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), fill=white) + draw.text((0, 106), "Last: " + pl, font=font14, fill=black) update_epd() -#Callback de déconnexion au broker MQTT +""" +Callback de déconnexion au broker MQTT +""" def on_disconnect(client, userdata, msg): if msg != 0: print("Déconnexion imprévu : %s" % msg) @@ -132,9 +146,11 @@ epd.init(epd.lut_full_update) font12 = ImageFont.truetype(epd_font_file, 12) font14 = ImageFont.truetype(epd_font_file, 14) font16 = ImageFont.truetype(epd_font_file, 16) +font20 = ImageFont.truetype(epd_font_file, 20) font12_bold = ImageFont.truetype(epd_bold_font_file, 12) font14_bold = ImageFont.truetype(epd_bold_font_file, 14) font16_bold = ImageFont.truetype(epd_bold_font_file, 16) +font20_bold = ImageFont.truetype(epd_bold_font_file, 20) #font12_obl = ImageFont.truetype(epd_italic_font_file, 12) #font14_obl = ImageFont.truetype(epd_italic_font_file, 14) #font16_obl = ImageFont.truetype(epd_italic_font_file, 16) @@ -145,13 +161,13 @@ image = Image.new('1', (epd2in13.EPD_HEIGHT, epd2in13.EPD_WIDTH), 255) draw = ImageDraw.Draw(image) #Draw basic text and frame : -draw.rectangle((0, 0, epd2in13.EPD_HEIGHT, 18), fill=0) -draw.text((60, 2), "Projet Camétéo", font=font16_bold, fill=255) -draw.text((0, 20), "{} : IP= {}".format(socket.gethostname(), netifaces.ifaddresses('wlan0')[2][0]['addr']), font = font12, fill=0 ) -draw.line((0,49,epd2in13.EPD_HEIGHT, 49), fill=0) -draw.line((0,65,epd2in13.EPD_HEIGHT, 65), fill=0) -draw.line((0,81,epd2in13.EPD_HEIGHT, 81), fill=0) -draw.line((125, 49, 125, 81), fill=0) +draw.rectangle((0, 0, epd2in13.EPD_HEIGHT, 18), fill=black) +draw.text((60, 1), "Projet Camétéo", font=font16_bold, fill=white) +draw.text((0, 19), "{} : IP= {}".format(socket.gethostname(), netifaces.ifaddresses('wlan0')[2][0]['addr']), font = font12, fill=black ) +draw.line((0,49,epd2in13.EPD_HEIGHT, 49), fill=black) +draw.line((0,71,epd2in13.EPD_HEIGHT, 71), fill=black) +draw.line((0,93,epd2in13.EPD_HEIGHT, 93), fill=black) +draw.line((125, 49, 125, 93), fill=black) #Display on ePaper screen epd.clear_frame_memory(0xFF)