From 6141e4a9b2dfe66eba5533ac64a5117271adec16 Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Wed, 15 Aug 2018 22:04:29 +0200 Subject: [PATCH] Wait data for full screen refresh. There is no partial refresh and the full refresh is slow => wait few seconds for other data to arrive before refreshing instead of refreshing every many times. --- raspberry/python/mqtt2epaper.py | 60 ++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/raspberry/python/mqtt2epaper.py b/raspberry/python/mqtt2epaper.py index 0e77a01..3950901 100644 --- a/raspberry/python/mqtt2epaper.py +++ b/raspberry/python/mqtt2epaper.py @@ -45,6 +45,8 @@ white = inkyphat.WHITE black = inkyphat.BLACK grey = inkyphat.RED +new_data_flag = False + ############# # CALLBACKS # ############# @@ -63,6 +65,8 @@ def on_connect(client, userdata, flags, rc): def on_message(client, userdata, msg): """Callback for managing data sent by MQTT broker""" + global new_data_flag + top=msg.topic[len(mqtt_topic)-1:].strip() #List of the subtopics subtopics = top.split("/") @@ -99,7 +103,8 @@ def on_message(client, userdata, msg): "{:6.1f}{} ".format(val['value'], val['unit']), font=data_font, fill=black) - inkyphat.show() + new_data_flag = True + update_display() # Print feather's date and time on epaper display elif subtopics[0] == "SYS": @@ -114,31 +119,34 @@ def on_message(client, userdata, msg): dt_texte = payload print(dt_texte) # First erase old text - inkyphat.rectangle((0, 31, inkyphat.WIDTH, 45), fill=white) + inkyphat.rectangle((0, date_line, inkyphat.WIDTH, data1_line - 2), fill=white) # and draw the new date inkyphat.text((0, 31), dt_texte, font=font12, fill=black) - - inkyphat.show() + new_data_flag = True + update_display() def on_message_camera(client, userdata, msg): """Update display with info from camera (camera shooting new picture or name of the last photo)""" + global new_data_flag + pl = msg.payload.decode() print("{} : {} ({})".format(msg.topic, pl, type(pl))) # If a picture is been taken if pl == "1" and msg.topic == camera_mqtt_topic + "/shooting" : - inkyphat.rectangle((0, inkyphat.HEIGHT - 13, inkyphat.WIDTH, inkyphat.HEIGHT), fill=white) - inkyphat.text((0, inkyphat.HEIGHT - 13), "Shooting photo... ", font=font12, fill=black) - inkyphat.show() + text = "Shooting photo... " + inkyphat.rectangle((0, inkyphat.HEIGHT - 12, inkyphat.WIDTH, inkyphat.HEIGHT), fill=white) + inkyphat.text((0, inkyphat.HEIGHT - 12), text, font=font12, fill=black) + new_data_flag = True #Last picture name (with date/time) if msg.topic == camera_mqtt_topic + "/last_photo": + text = "Last: {}".format(pl) + new_data_flag = True inkyphat.rectangle((0, inkyphat.HEIGHT - 12, inkyphat.WIDTH, inkyphat.HEIGHT), fill=white) - inkyphat.text((0, inkyphat.HEIGHT - 12), "Last: " + pl, font=font12, fill=black) - inkyphat.show() - + inkyphat.text((0, inkyphat.HEIGHT - 12), text, font=font12, fill=black) def on_disconnect(client, userdata, msg): """Disconnection callback""" @@ -146,6 +154,29 @@ def on_disconnect(client, userdata, msg): print("Unexpected disconnection : {}".format(msg)) exit() +############ +# Function # +############ + +def update_display(): + global new_data_flag + global refresh_timer + global refresh_rate + + if new_data_flag == True: + if refresh_timer < 0: + # New data just arrived : wait for other data for a short period + print("new data...") + refresh_timer = time.time() + refresh_rate + elif time.time() >= refresh_timer : + # No new data to display : update the screen and reset the flags! + print("Refresh eink screen with new data ! ({} / {})".format(refresh_timer, time.time())) + inkyphat.show() + new_data_flag = False + refresh_timer = -1 + print("OK") + return + ######## # Main # ######## @@ -196,8 +227,6 @@ inkyphat.line((0, data2_line - 1, inkyphat.WIDTH, data2_line - 1), fill=black) inkyphat.line((0, data_table_bottom_line, inkyphat.WIDTH, data_table_bottom_line), fill=black) inkyphat.line((inkyphat.WIDTH / 2, data1_line - 1, inkyphat.WIDTH / 2, data_table_bottom_line), fill=black) -inkyphat.show() - #Connect to MQTT broker and loop... mqtt_client = mqtt.Client(mqtt_client_id, clean_session=True) mqtt_client.username_pw_set(mqtt_user, mqtt_pass) @@ -210,4 +239,9 @@ mqtt_client.message_callback_add(camera_mqtt_topic + "/#", on_message_camera) print(mqtt_host + ":" + str(mqtt_port)) mqtt_client.connect(mqtt_host, int(mqtt_port), 60) -mqtt_client.loop_forever() +refresh_rate = 5 +refresh_timer = -1 + +while True: + mqtt_client.loop() + update_display()