Skip to content

Commit 4dbe36b

Browse files
committed
Add a webserver for simulated screen, fix issue for tuple not iterable in Python < 3.9
1 parent 51b81dc commit 4dbe36b

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,7 @@ dmypy.json
132132
.idea/
133133

134134
# Git mergetool backup
135-
*.orig
135+
*.orig
136+
137+
# Simulated display capture
138+
screencap.png

library/lcd_comm.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import threading
33
from abc import ABC, abstractmethod
44
from enum import IntEnum
5+
from typing import Tuple
56

67
import serial
78
from PIL import Image, ImageDraw, ImageFont
@@ -119,7 +120,7 @@ def SetBrightness(self, level: int):
119120
pass
120121

121122
@abstractmethod
122-
def SetBackplateLedColor(self, led_color: tuple[int, int, int]):
123+
def SetBackplateLedColor(self, led_color: Tuple[int, int, int] = (255, 255, 255)):
123124
pass
124125

125126
@abstractmethod
@@ -147,8 +148,8 @@ def DisplayText(
147148
y: int = 0,
148149
font: str = "roboto-mono/RobotoMono-Regular.ttf",
149150
font_size: int = 20,
150-
font_color: tuple[int, int, int] = (0, 0, 0),
151-
background_color: tuple[int, int, int] = (255, 255, 255),
151+
font_color: Tuple[int, int, int] = (0, 0, 0),
152+
background_color: Tuple[int, int, int] = (255, 255, 255),
152153
background_image: str = None
153154
):
154155
# Convert text to bitmap using PIL and display it
@@ -195,9 +196,9 @@ def DisplayText(
195196

196197
def DisplayProgressBar(self, x: int, y: int, width: int, height: int, min_value: int = 0, max_value: int = 100,
197198
value: int = 50,
198-
bar_color: tuple[int, int, int] = (0, 0, 0),
199+
bar_color: Tuple[int, int, int] = (0, 0, 0),
199200
bar_outline: bool = True,
200-
background_color: tuple[int, int, int] = (255, 255, 255),
201+
background_color: Tuple[int, int, int] = (255, 255, 255),
201202
background_image: str = None):
202203
# Generate a progress bar and display it
203204
# Provide the background image path to display progress bar with transparent background

library/lcd_comm_rev_a.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def SetBrightness(self, level: int = 25):
8888
# Level : 0 (brightest) - 255 (darkest)
8989
self.SendCommand(Command.SET_BRIGHTNESS, level_absolute, 0, 0, 0)
9090

91-
def SetBackplateLedColor(self, led_color: tuple[int, int, int] = (255, 255, 255)):
91+
def SetBackplateLedColor(self, led_color: Tuple[int, int, int] = (255, 255, 255)):
9292
logger.info("HW revision A does not support backplate LED color setting")
9393
pass
9494

library/lcd_comm_rev_b.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def SetBrightness(self, level_user: int = 25):
147147

148148
self.SendCommand(Command.SET_BRIGHTNESS, payload=[level])
149149

150-
def SetBackplateLedColor(self, led_color: tuple[int, int, int] = (255, 255, 255)):
150+
def SetBackplateLedColor(self, led_color: Tuple[int, int, int] = (255, 255, 255)):
151151
if self.is_flagship():
152152
self.SendCommand(Command.SET_LIGHTING, payload=led_color)
153153
else:

library/lcd_simulated.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1+
import mimetypes
2+
from http.server import BaseHTTPRequestHandler, HTTPServer
3+
14
from library.lcd_comm import *
25

36

7+
# This webserver offer a blank page displaying simulated screen with auto-refresh
8+
class SimulatedLcdWebServer(BaseHTTPRequestHandler):
9+
def log_message(self, format, *args):
10+
return
11+
12+
def do_GET(self):
13+
if self.path == "/":
14+
self.send_response(200)
15+
self.send_header("Content-type", "text/html")
16+
self.end_headers()
17+
self.wfile.write(bytes("<img src=\"screencap.png\" id=\"myImage\" />", "utf-8"))
18+
self.wfile.write(bytes("<script>", "utf-8"))
19+
self.wfile.write(bytes("setInterval(function() {", "utf-8"))
20+
self.wfile.write(bytes(" var myImageElement = document.getElementById('myImage');", "utf-8"))
21+
self.wfile.write(bytes(" myImageElement.src = 'screencap.png?rand=' + Math.random();", "utf-8"))
22+
self.wfile.write(bytes("}, 250);", "utf-8"))
23+
self.wfile.write(bytes("</script>", "utf-8"))
24+
elif self.path.startswith("/screencap.png"):
25+
imgfile = open("screencap.png", 'rb').read()
26+
mimetype = mimetypes.MimeTypes().guess_type("screencap.png")[0]
27+
self.send_response(200)
28+
self.send_header('Content-type', mimetype)
29+
self.end_headers()
30+
self.wfile.write(imgfile)
31+
32+
33+
# Simulated display: write on a screencap.png file instead of serial port
434
class LcdSimulated(LcdComm):
535
def __init__(self, com_port: str = "AUTO", display_width: int = 320, display_height: int = 480,
636
update_queue: queue.Queue = None):
@@ -9,6 +39,11 @@ def __init__(self, com_port: str = "AUTO", display_width: int = 320, display_hei
939
self.screen_image.save("screencap.png", "PNG")
1040
self.orientation = Orientation.PORTRAIT
1141

42+
webServer = HTTPServer(("localhost", 5678), SimulatedLcdWebServer)
43+
logger.debug("To see your simulated screen, open http://%s:%s" % ("localhost", 5678))
44+
45+
threading.Thread(target=webServer.serve_forever).start()
46+
1247
@staticmethod
1348
def auto_detect_com_port():
1449
return None
@@ -31,7 +66,7 @@ def ScreenOn(self):
3166
def SetBrightness(self, level: int = 25):
3267
pass
3368

34-
def SetBackplateLedColor(self, led_color: tuple[int, int, int] = (255, 255, 255)):
69+
def SetBackplateLedColor(self, led_color: Tuple[int, int, int] = (255, 255, 255)):
3570
pass
3671

3772
def SetOrientation(self, orientation: Orientation = Orientation.PORTRAIT):

0 commit comments

Comments
 (0)