Skip to content

Commit ae06111

Browse files
committed
Simu mode: save to temporary files, copy with shutil once save is done (avoid incomplete files during save)
1 parent 1903bb7 commit ae06111

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

library/lcd_simulated.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import mimetypes
2+
import shutil
23
from http.server import BaseHTTPRequestHandler, HTTPServer
34

45
from library.lcd_comm import *
56

7+
SCREENSHOT_FILE = "screencap.png"
8+
69

710
# This webserver offer a blank page displaying simulated screen with auto-refresh
811
class SimulatedLcdWebServer(BaseHTTPRequestHandler):
@@ -22,8 +25,8 @@ def do_GET(self):
2225
self.wfile.write(bytes("}, 250);", "utf-8"))
2326
self.wfile.write(bytes("</script>", "utf-8"))
2427
elif self.path.startswith("/screencap.png"):
25-
imgfile = open("screencap.png", 'rb').read()
26-
mimetype = mimetypes.MimeTypes().guess_type("screencap.png")[0]
28+
imgfile = open(SCREENSHOT_FILE, 'rb').read()
29+
mimetype = mimetypes.MimeTypes().guess_type(SCREENSHOT_FILE)[0]
2730
self.send_response(200)
2831
self.send_header('Content-type', mimetype)
2932
self.end_headers()
@@ -36,7 +39,8 @@ def __init__(self, com_port: str = "AUTO", display_width: int = 320, display_hei
3639
update_queue: queue.Queue = None):
3740
LcdComm.__init__(self, com_port, display_width, display_height, update_queue)
3841
self.screen_image = Image.new("RGB", (self.get_width(), self.get_height()), (255, 255, 255))
39-
self.screen_image.save("screencap.png", "PNG")
42+
self.screen_image.save("tmp", "PNG")
43+
shutil.copyfile("tmp", SCREENSHOT_FILE)
4044
self.orientation = Orientation.PORTRAIT
4145

4246
webServer = HTTPServer(("localhost", 5678), SimulatedLcdWebServer)
@@ -74,7 +78,8 @@ def SetOrientation(self, orientation: Orientation = Orientation.PORTRAIT):
7478
# Just draw the screen again with the new width/height based on orientation
7579
with self.update_queue_mutex:
7680
self.screen_image = Image.new("RGB", (self.get_width(), self.get_height()), (255, 255, 255))
77-
self.screen_image.save("screencap.png", "PNG")
81+
self.screen_image.save("tmp", "PNG")
82+
shutil.copyfile("tmp", SCREENSHOT_FILE)
7883

7984
def DisplayPILImage(
8085
self,
@@ -102,4 +107,5 @@ def DisplayPILImage(
102107

103108
with self.update_queue_mutex:
104109
self.screen_image.paste(image, (x, y))
105-
self.screen_image.save("screencap.png", "PNG")
110+
self.screen_image.save("tmp", "PNG")
111+
shutil.copyfile("tmp", SCREENSHOT_FILE)

0 commit comments

Comments
 (0)