Skip to content

Commit 7ac5141

Browse files
committed
Reduce the CPU load when waiting for the display
In many places a `pass` instruction is used in loops to wait until we hear back back from the display. This causes the loop to execute continuously which uses all of the available CPU, if we instead sleep for 1ms, we don't lose much time (less than 1ms each wait) and we dramatically reduce the load on the CPU. Before the change (updating a 3-color 2.13" display): ``` time python3 ./update_display real 0m19.664s user 0m17.622s sys 0m1.046s ``` After the change: time python3 ./update_display real 0m19.730s user 0m3.563s sys 0m0.792s1 The total time to run the script is about the same, but the CPU time has reduced dramatically.
1 parent dfcdd62 commit 7ac5141

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

adafruit_epd/epd.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy
7272
# SPI interface (required)
7373
self.spi_device = spi
7474
while not self.spi_device.try_lock():
75-
pass
75+
sleep(0.01)
7676
self.spi_device.configure(baudrate=1000000) # 1 Mhz
7777
self.spi_device.unlock()
7878

@@ -99,7 +99,7 @@ def display(self): # pylint: disable=too-many-branches
9999

100100
if self.sram:
101101
while not self.spi_device.try_lock():
102-
pass
102+
sleep(0.01)
103103
self.sram.cs_pin.value = False
104104
#send read command
105105
self._buf[0] = mcp_sram.Adafruit_MCP_SRAM.SRAM_READ
@@ -114,7 +114,7 @@ def display(self): # pylint: disable=too-many-branches
114114
databyte = self.write_ram(0)
115115

116116
while not self.spi_device.try_lock():
117-
pass
117+
sleep(0.01)
118118
self._dc.value = True
119119

120120
if self.sram:
@@ -131,7 +131,7 @@ def display(self): # pylint: disable=too-many-branches
131131

132132
if self.sram:
133133
while not self.spi_device.try_lock():
134-
pass
134+
sleep(0.01)
135135
self.sram.cs_pin.value = False
136136
#send read command
137137
self._buf[0] = mcp_sram.Adafruit_MCP_SRAM.SRAM_READ
@@ -147,7 +147,7 @@ def display(self): # pylint: disable=too-many-branches
147147
databyte = self.write_ram(1)
148148

149149
while not self.spi_device.try_lock():
150-
pass
150+
sleep(0.01)
151151
self._dc.value = True
152152

153153
if self.sram:
@@ -182,7 +182,7 @@ def command(self, cmd, data=None, end=True):
182182
self._cs.value = False
183183

184184
while not self.spi_device.try_lock():
185-
pass
185+
sleep(0.01)
186186
ret = self._spi_transfer(cmd)
187187

188188
if data is not None:

adafruit_epd/il0373.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def busy_wait(self):
9696
busy pin, or pausing"""
9797
if self._busy:
9898
while not self._busy.value:
99-
pass
99+
time.sleep(0.01)
100100
else:
101101
time.sleep(0.5)
102102

adafruit_epd/il91874.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def busy_wait(self):
105105
busy pin, or pausing"""
106106
if self._busy:
107107
while not self._busy.value:
108-
pass
108+
time.sleep(0.01)
109109
else:
110110
time.sleep(0.5)
111111

0 commit comments

Comments
 (0)