From 7ac51415098e2d0445068c333a9b131ac2811dad Mon Sep 17 00:00:00 2001 From: Mario Visic Date: Thu, 6 Feb 2020 20:45:16 +1100 Subject: [PATCH 1/2] 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. --- adafruit_epd/epd.py | 12 ++++++------ adafruit_epd/il0373.py | 2 +- adafruit_epd/il91874.py | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/adafruit_epd/epd.py b/adafruit_epd/epd.py index ac49c1b..4fd7300 100644 --- a/adafruit_epd/epd.py +++ b/adafruit_epd/epd.py @@ -72,7 +72,7 @@ def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy # SPI interface (required) self.spi_device = spi while not self.spi_device.try_lock(): - pass + sleep(0.01) self.spi_device.configure(baudrate=1000000) # 1 Mhz self.spi_device.unlock() @@ -99,7 +99,7 @@ def display(self): # pylint: disable=too-many-branches if self.sram: while not self.spi_device.try_lock(): - pass + sleep(0.01) self.sram.cs_pin.value = False #send read command self._buf[0] = mcp_sram.Adafruit_MCP_SRAM.SRAM_READ @@ -114,7 +114,7 @@ def display(self): # pylint: disable=too-many-branches databyte = self.write_ram(0) while not self.spi_device.try_lock(): - pass + sleep(0.01) self._dc.value = True if self.sram: @@ -131,7 +131,7 @@ def display(self): # pylint: disable=too-many-branches if self.sram: while not self.spi_device.try_lock(): - pass + sleep(0.01) self.sram.cs_pin.value = False #send read command self._buf[0] = mcp_sram.Adafruit_MCP_SRAM.SRAM_READ @@ -147,7 +147,7 @@ def display(self): # pylint: disable=too-many-branches databyte = self.write_ram(1) while not self.spi_device.try_lock(): - pass + sleep(0.01) self._dc.value = True if self.sram: @@ -182,7 +182,7 @@ def command(self, cmd, data=None, end=True): self._cs.value = False while not self.spi_device.try_lock(): - pass + sleep(0.01) ret = self._spi_transfer(cmd) if data is not None: diff --git a/adafruit_epd/il0373.py b/adafruit_epd/il0373.py index afd650e..0db613b 100644 --- a/adafruit_epd/il0373.py +++ b/adafruit_epd/il0373.py @@ -96,7 +96,7 @@ def busy_wait(self): busy pin, or pausing""" if self._busy: while not self._busy.value: - pass + time.sleep(0.01) else: time.sleep(0.5) diff --git a/adafruit_epd/il91874.py b/adafruit_epd/il91874.py index cad88a3..32a5c84 100644 --- a/adafruit_epd/il91874.py +++ b/adafruit_epd/il91874.py @@ -105,7 +105,7 @@ def busy_wait(self): busy pin, or pausing""" if self._busy: while not self._busy.value: - pass + time.sleep(0.01) else: time.sleep(0.5) From 7b064da7549a3a1ac656edd46a4c78a58ab85935 Mon Sep 17 00:00:00 2001 From: Mario Visic Date: Thu, 6 Feb 2020 21:35:46 +1100 Subject: [PATCH 2/2] Call sleep() on time --- adafruit_epd/epd.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_epd/epd.py b/adafruit_epd/epd.py index 4fd7300..9cbd3bf 100644 --- a/adafruit_epd/epd.py +++ b/adafruit_epd/epd.py @@ -72,7 +72,7 @@ def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy # SPI interface (required) self.spi_device = spi while not self.spi_device.try_lock(): - sleep(0.01) + time.sleep(0.01) self.spi_device.configure(baudrate=1000000) # 1 Mhz self.spi_device.unlock() @@ -99,7 +99,7 @@ def display(self): # pylint: disable=too-many-branches if self.sram: while not self.spi_device.try_lock(): - sleep(0.01) + time.sleep(0.01) self.sram.cs_pin.value = False #send read command self._buf[0] = mcp_sram.Adafruit_MCP_SRAM.SRAM_READ @@ -114,7 +114,7 @@ def display(self): # pylint: disable=too-many-branches databyte = self.write_ram(0) while not self.spi_device.try_lock(): - sleep(0.01) + time.sleep(0.01) self._dc.value = True if self.sram: @@ -131,7 +131,7 @@ def display(self): # pylint: disable=too-many-branches if self.sram: while not self.spi_device.try_lock(): - sleep(0.01) + time.sleep(0.01) self.sram.cs_pin.value = False #send read command self._buf[0] = mcp_sram.Adafruit_MCP_SRAM.SRAM_READ @@ -147,7 +147,7 @@ def display(self): # pylint: disable=too-many-branches databyte = self.write_ram(1) while not self.spi_device.try_lock(): - sleep(0.01) + time.sleep(0.01) self._dc.value = True if self.sram: @@ -182,7 +182,7 @@ def command(self, cmd, data=None, end=True): self._cs.value = False while not self.spi_device.try_lock(): - sleep(0.01) + time.sleep(0.01) ret = self._spi_transfer(cmd) if data is not None: