Skip to content

Commit 214c48b

Browse files
authored
Merge pull request #679 from mathoudebine/dev/optimize-load
Optimize load to avoid "burst" at startup, fix rev. B corruption
2 parents 64d3fa2 + 953daec commit 214c48b

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

library/lcd/lcd_comm_rev_b.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,10 @@ def DisplayPILImage(
245245
# Send image data by multiple of "display width" bytes
246246
for chunk in chunked(rgb565be, self.get_width() * 8):
247247
self.SendLine(chunk)
248+
249+
# Implement a cooldown between two bitmaps, because we are not listening to events coming from the display
250+
# Cooldown of 0.05 decreases "corrupted bitmap" significantly without slowing down too much
251+
if self.update_queue:
252+
self.update_queue.put((time.sleep, [0.05]))
253+
else:
254+
time.sleep(0.05)

main.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@
8080
logger.debug("Using Python %s" % sys.version)
8181

8282

83+
def wait_for_empty_queue(timeout: int = 5):
84+
# Waiting for all pending request to be sent to display
85+
logger.info("Waiting for all pending request to be sent to display (%ds max)..." % timeout)
86+
87+
wait_time = 0
88+
while not scheduler.is_queue_empty() and wait_time < timeout:
89+
time.sleep(0.1)
90+
wait_time = wait_time + 0.1
91+
92+
logger.debug("(Waited %.1fs)" % wait_time)
93+
8394
def clean_stop(tray_icon=None):
8495
# Turn screen and LEDs off before stopping
8596
display.turn_off()
@@ -88,15 +99,8 @@ def clean_stop(tray_icon=None):
8899
# Instead, ask the scheduler to empty the action queue before stopping
89100
scheduler.STOPPING = True
90101

91-
# Allow 5 seconds max. delay in case scheduler is not responding
92-
wait_time = 5
93-
logger.info("Waiting for all pending request to be sent to display (%ds max)..." % wait_time)
94-
95-
while not scheduler.is_queue_empty() and wait_time > 0:
96-
time.sleep(0.1)
97-
wait_time = wait_time - 0.1
98-
99-
logger.debug("(%.1fs)" % (5 - wait_time))
102+
# Waiting for all pending request to be sent to display
103+
wait_for_empty_queue(5)
100104

101105
# Remove tray icon just before exit
102106
if tray_icon:
@@ -194,34 +198,42 @@ def on_win32_wm_event(hWnd, msg, wParam, lParam):
194198
win32api.SetConsoleCtrlHandler(on_win32_ctrl_event, True)
195199

196200
# Initialize the display
201+
logger.info("Initialize display")
197202
display.initialize_display()
198203

204+
# Start serial queue handler
205+
scheduler.QueueHandler()
206+
199207
# Create all static images
200208
display.display_static_images()
201209

202210
# Create all static texts
203211
display.display_static_text()
204212

205-
# Run our jobs that update data
213+
# Wait for static images/text to be displayed before starting monitoring (to avoid filling the queue while waiting)
214+
wait_for_empty_queue(10)
215+
216+
# Start sensor scheduled reading. Avoid starting them all at the same time to optimize load
217+
logger.info("Starting system monitoring")
206218
import library.stats as stats
207219

208-
scheduler.CPUPercentage()
209-
scheduler.CPUFrequency()
210-
scheduler.CPULoad()
211-
scheduler.CPUTemperature()
212-
scheduler.CPUFanSpeed()
220+
scheduler.CPUPercentage(); time.sleep(0.25)
221+
scheduler.CPUFrequency(); time.sleep(0.25)
222+
scheduler.CPULoad(); time.sleep(0.25)
223+
scheduler.CPUTemperature(); time.sleep(0.25)
224+
scheduler.CPUFanSpeed(); time.sleep(0.25)
213225
if stats.Gpu.is_available():
214-
scheduler.GpuStats()
215-
scheduler.MemoryStats()
216-
scheduler.DiskStats()
217-
scheduler.NetStats()
218-
scheduler.DateStats()
219-
scheduler.SystemUptimeStats()
220-
scheduler.CustomStats()
221-
scheduler.WeatherStats()
222-
scheduler.PingStats()
223-
scheduler.QueueHandler()
224-
226+
scheduler.GpuStats(); time.sleep(0.25)
227+
scheduler.MemoryStats(); time.sleep(0.25)
228+
scheduler.DiskStats(); time.sleep(0.25)
229+
scheduler.NetStats(); time.sleep(0.25)
230+
scheduler.DateStats(); time.sleep(0.25)
231+
scheduler.SystemUptimeStats(); time.sleep(0.25)
232+
scheduler.CustomStats(); time.sleep(0.25)
233+
scheduler.WeatherStats(); time.sleep(0.25)
234+
scheduler.PingStats(); time.sleep(0.25)
235+
236+
# OS-specific tasks
225237
if tray_icon and platform.system() == "Darwin": # macOS-specific
226238
from AppKit import NSBundle, NSApp, NSApplicationActivationPolicyProhibited
227239

0 commit comments

Comments
 (0)