diff --git a/compLib/Spi.py b/compLib/Spi.py index ad712a9..5ce69f9 100644 --- a/compLib/Spi.py +++ b/compLib/Spi.py @@ -4,6 +4,7 @@ import multiprocessing from enum import IntEnum import time import sys +import random from compLib.LogstashLogging import Logging @@ -17,6 +18,7 @@ SPI_HEALTH = True spi_found = importlib.util.find_spec("spidev") is not None spi = None spi_lock = multiprocessing.Lock() +last_spi_call = time.time() if spi_found: import spidev spi = spidev.SpiDev() @@ -113,7 +115,7 @@ class Spi(object): def transfer(tx_buffer: list): if not spi_found: return [] * SPI_BUFFER_SIZE - + write_reg = tx_buffer[1] tx_buffer_copy = tx_buffer.copy() @@ -123,9 +125,12 @@ class Spi(object): if rx_buffer[1] != write_reg: Logging.get_logger().error(f"Warning! SPI error during read/write of register {write_reg}! Retrying automagically") - time.sleep(0.01) + time.sleep(random.uniform(0.0, 0.1)) return Spi.transfer(tx_buffer_copy) + global last_spi_call + last_spi_call = time.time() + return rx_buffer @staticmethod @@ -204,8 +209,9 @@ class Spi(object): @staticmethod def health_check_loop(): while SPI_HEALTH: - Spi.health_check() - time.sleep(0.75) + if last_spi_call + 0.75 < time.time(): + Spi.health_check() + time.sleep(0.5) @staticmethod def get_version():