# AIO Throttle: Fetch Example for the Matrix Portal S3

from adafruit_matrixportal.matrixportal import MatrixPortal

mp = MatrixPortal(width=64, height=32, bit_depth=6, debug=False, rotation=90, status_neopixel=board.NEOPIXEL)

def get_last_value(feed_key):
    """Fetch the latest value of the AIO feed.
    :param str feed_key: The AIO feed key."""
    try:
        # print(f"throttle limit: {mp.network.io_client.get_remaining_throttle_limit()}")
        while mp.network.io_client.get_remaining_throttle_limit() <= 10:
            time.sleep(1)  # Wait until throttle limit increases
        last_value = mp.network.io_client.receive_data(feed_key)["value"]
        return last_value
    except (KeyError, IndexError) as e:
        print(f"Error fetching data from feed {feed_key}: {e}")