adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.08k stars 1.21k forks source link

board.SPI lost after being used by display #3581

Closed cwalther closed 3 years ago

cwalther commented 3 years ago

While playing around with creating and destroying displays to exercise the supervisor heap, I came across some strange behavior: When a board.SPI() object has ever been used by a FourWire or a SharpMemoryFramebuffer that has since been deinitialized, subsequent sessions can never reconstruct it, because its pins are still in use.

Steps to reproduce:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.0.0-rc.0-61-g1a677406b-dirty on 2020-10-21; CWtest with samd51G19
>>> import board, displayio
>>> displayio.FourWire(board.SPI(), command=board.RX, chip_select=board.TX)
<FourWire>
>>> displayio.release_displays()
>>> 
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Hello World!

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.0.0-rc.0-61-g1a677406b-dirty on 2020-10-21; CWtest with samd51G19
>>> import board
>>> board.SPI()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: SCK in use
>>> 

or

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.0.0-rc.0-61-g1a677406b-dirty on 2020-10-21; CWtest with samd51G19
>>> import board, displayio, sharpdisplay
>>> sharpdisplay.SharpMemoryFramebuffer(board.SPI(), board.RX, 400, 240)
<SharpMemoryFramebuffer>
>>> displayio.release_displays()
>>> 
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Hello World!

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.0.0-rc.0-61-g1a677406b-dirty on 2020-10-21; CWtest with samd51G19
>>> import board
>>> board.SPI()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: SCK in use
>>> 

What happens is:

I’m not sure how this is supposed to work, but there seems to be a hole in the logic somewhere. I suppose someone should call common_hal_busio_spi_deinit() at some point so that the pins are made resettable again? Maybe reset_board_busses() before it effectively leaks the resources used by the spi_singleton, but then reset_port() and reset_board_busses() come in the wrong order?

Some of the observations made by @hatchman in https://github.com/adafruit/circuitpython/issues/3508#issuecomment-706211348 seem related (but probably unrelated to the actual issue of #3508, so I’m opening a new one).

jepler commented 3 years ago

I have seen similar behavior with the Sharp Memory Display. I'm not sure what the fix is, but I hope you will consider making a pull request to fix this. @tannewt may have a suggestion.

cwalther commented 3 years ago

Finding a good fix will require more analysis on my part, so I was hoping someone of you maintainers would have an idea more quickly. But I can give it a try. No promises though. It’s not urgent as far as I am concerned.

hatchman commented 3 years ago

@cwalther yes! this seems to be the case, thank you for a more clear description of what is going on....

unfortunate to hear its not that urgent, I wish I had more expertise to help with the sharp displays. Adafruit seems to not have anyone to test these. I got myself invested into a project where I have a keyboard button array, a sharp memory display and an nrf based board hoping to use circuit python. but in my case, its several limited hitting issues with memory not being freed on reboots, where the frame buffer is just sitting on extra memory, and in cases where speed is needed, the board cannot be reset even properly after a reboot and call back to the display. its made everything I spent developing into a brick which is sad because the information of these issues seems to be something no one haas ever tested. in my opinion the issues here make the display unusable and unstable in projects when I have to jam at the reset button and really has been a cloud on my circuit python experience, and as such the use of sharp memory displays doesnt seem fair to be claimed as stable to less practiced coders such as myself.

perhaps I am just really bad at programming and all this can be avoided, but it really seems to be small stuff pushing over the memory budget, and holding a display really bogs down Any key scanning and higher speed code, where I only want to update the display in long passes of time, and dont mind second long refreshes.

cwalther commented 3 years ago

Regarding the memory leaks, could you try a recent “absolute newest” build (newer than 6.0.0-rc.0)? #3482 should help with these, although I’m not sure if the first commit of #3498 is also needed. (@jepler, maybe you could rebase that and leave out the second commit that is obsoleted by #3542, to get a new build to test?)

hatchman commented 3 years ago

in regard to memory leaks, if anything my experience has got worse. I just flashed adafruit-circuitpython-feather_bluefruit_sense-en_US-20201020-1a67740.uf2 and here is my experience:

prior to this build, up to 6.0.0 beta 2 when I save my files, it would reboot my code, and have about 72,000 bytes avail after freshboot init sequence, and displays this on screen.

anytime a software reboot is launched, saving a file, hitting the reset button once, or calling a soft reboot with code, it would reboot with the same sequence and yield 53,000 on average. the only way around is to double press reset to get into bootloader mode then once more for a 'fresh' boot.

now, my code (same stuff used in prior builds) launches with 66,000 bytes after init, but exhibits odd behavior. files that previously loaded aren't loading which is likely due to the 6,000 bytes missing, but most of my programs dont run.for reference, I am running a modified Adafruit clue library adding my hardware callouts for keypresses using adafruits matrix keypad library, an encoder using rotaryio, expanded the onboard neopixel by adding less off the output pin on the onboard neopixel to run a strip (powered and buffered appropriately with resistors and caps) and changing the display driver out for the sharp frame buffered display, then compiling to .mpy. then I have a main menu using clue simpletextdisplay. that reads and executes other python files onboard my storage.

what is MORE strange, is that if I then issue a single button reboot, or save a file to trigger a reboot, my code doesnt make it to even load the main menu, which should load with 66,000 bytes free which is the case on a fresh boot.

hatchman commented 3 years ago

@cwalther I am happy to help test any of this and learn more as long as there is attention, but the recent builds have definitely got worse for my hardware combination. im close to switching back to the Arduino side even through it slows my ability to develop about 10x, at least id have room to program more than display 5 words in a tiny byte font and blink a light :/

edit: memory issue is above not fixed yet, just found one of my code snippets that launched, and was able to reboot successfully, yielding only 54,000 bytes again, so the issue is persisting, and whats more, is that more memory is now taken up on a fresh boot than in previous builds.

cwalther commented 3 years ago

As far as my contributions are concerned, I am grateful for any testing with Sharp Memory Displays, because I don’t have one. I can test memory management by pretending there was a display, but I can’t check if anything sensible shows up on the display. (I’m also unfamiliar with nRF52 hardware. Does the reset button really do a soft reboot there? That’s not what I would expect from a reset button.)

Do you think you could distill your testing procedure into a series of simple steps that someone else could follow, and show us the output from the different builds you’ve been testing with, similarly to what I did in the first post?

hatchman commented 3 years ago

yeah I totally get that! Adafruit should be shipping this hardware out to hard working folks like you! especially since not many seems to be championing the development of a product Adafruit sells thats still newer.

I can certainly work to optimize my test methods and describe them better, but I would need a bit of guidance. I understand that I have a unique combination of hardware and weird software modifications like to the .mpy libraries. I just dont know how fast I can do something thats more straight forward. I will try to write something quickly using the graphic libraries I suppose.

as far as reset button functionality, I was under the impression that the reset button isnt doing a full reboot ever? for example circuit python still holds on to the display to show serial commands if not released manually on a reset button press.

in code, I call import supervisor supervisor.reload() to soft reboot. seems to be functionally similar to a single reset button press but maybe not under the hood.

hatchman commented 3 years ago

so best I can do quickly, is use this standalone piece of graphics code I wrote. it doesnt max out memory, but does point to about 10,000 bytes being used up between soft reboots. attached is my code, and the modified clue library with the sharp memory display swap, plus my additional sensors:

code.py:


import gc
import time
from adafruit_clue import clue
import displayio
import supervisor
from vectorio import Circle, Polygon, VectorShape
print(gc.mem_free(),'bytes free --> import success')

clue.pixels.fill((0,0,0,0))

a = 1/30
live = displayio.Group(max_size=2, x=200, y=120, scale=1)
cblack = displayio.Palette(2)
cblack.make_transparent(0)
cblack[1] = 0x000000
cwhite = displayio.Palette(2)
cwhite.make_transparent(0)
cwhite[1] = 0xFFFFFF
print(gc.mem_free(),'bytes free --> initialize display variables')
clue.display.show(live)
oc = Circle(radius=78)
ic = Circle(radius=39)
ahm = Polygon(points=[(-22,5), (-17,-5),(-18,-14),(-14,-11),(-6,-27),
                     (2,-11), (10,-27), (13,-21),   (5,-5),  (10,5),
                     (18,-11), (21,-5),  (13,11),  (14,20), (10,17),
                     (5,27),    (2,21),   (7,11),    (2,1), (-3,11),
                     (-2,20),  (-6,17),  (-11,27),(-14,21), (-9,11),
                     (-14,1),(-19,11)])
ahs = Polygon(points=[(-11,-5),(-6,-15),(-1,-5),(-6,5)])
cblack = displayio.Palette(2)
cblack.make_transparent(0)
cblack[1] = 0x000000
cwhite = displayio.Palette(2)
cwhite.make_transparent(0)
cwhite[1] = 0xFFFFFF

ahoutline = VectorShape(shape=oc, x=0, y=0, pixel_shader=cwhite)
ahcircle = VectorShape(shape=ic, x=0, y=0, pixel_shader=cblack)
ahmain = VectorShape(shape=ahm, x=0, y=0, pixel_shader=cwhite)
ahsub = VectorShape(shape=ahs, x=0, y=0, pixel_shader=cblack)

AHlogo = displayio.Group(max_size=4, x=0, y=0, scale=1)
AHlogo.append(ahoutline)
AHlogo.append(ahcircle)
AHlogo.append(ahmain)
AHlogo.append(ahsub)
live.append(AHlogo)
time.sleep(a)
AHlogo.scale = 2
time.sleep(a)
AHlogo.scale = 3
time.sleep(a)
print(gc.mem_free(),'bytes free --> low res logo')

live.pop()
time.sleep(a)
oc2 = Circle(radius=234)
ic2 = Circle(radius=118)
ahm2 = Polygon(points=[(-66,15), (-51,-15),(-54,-42),(-42,-33),(-18,-81),
                     (6,-33), (30,-81), (39,-63),   (15,-15),  (30,15),
                     (54,-33), (63,-15),  (39,33),  (42,60), (30,51),
                     (15,81),    (6,63),   (21,33),    (6,3), (-9,33),
                     (-6,60),  (-18,51),  (-33,81),(-42,63), (-27,33),
                     (-42,3),(-57,33)])
ahs2 = Polygon(points=[(-33,-15),(-18,-45),(-3,-15),(-18,15)])

ahoutline2 = VectorShape(shape=oc2, x=0, y=0, pixel_shader=cwhite)
ahcircle2 = VectorShape(shape=ic2, x=0, y=0, pixel_shader=cblack)
ahmain2 = VectorShape(shape=ahm2, x=0, y=0, pixel_shader=cwhite)
ahsub2 = VectorShape(shape=ahs2, x=0, y=0, pixel_shader=cblack)

AHlogo2 = displayio.Group(max_size=4, x=0, y=0, scale=1)
AHlogo2.append(ahoutline2)
time.sleep(a)
AHlogo2.append(ahcircle2)
time.sleep(a)
AHlogo2.append(ahmain2)
time.sleep(a)
AHlogo2.append(ahsub2)
time.sleep(a)
live.append(AHlogo2)
time.sleep(a)
print(gc.mem_free(),'bytes free --> hi res logo')
ic3 = Circle(radius=12)
ahm3 = Polygon(points=[(-8,2), (-6,-2),(-6,-5),(-5,-4),(-2,-10),
                     (1,-4), (4,-10), (5,-8),   (2,-2),  (4,2),
                     (7,-4), (8,-2),  (5,4),  (5,7), (4,6),
                     (2,10),    (1,8),   (3,4),    (1,0), (-1,4),
                     (-1,7),  (-2,6),  (-4,10),(-5,8), (-3,4),
                     (-5,0),(-7,4)])
ahs3 = Polygon(points=[(-4,-2),(-2,-6),(0,-2),(-2,2)])

ahcircle3 = VectorShape(shape=ic3, x=0, y=0, pixel_shader=cblack)
ahmain3 = VectorShape(shape=ahm3, x=0, y=0, pixel_shader=cwhite)
ahsub3 = VectorShape(shape=ahs3, x=0, y=0, pixel_shader=cblack)

AHlogo3 = displayio.Group(max_size=3, x=-160, y=-80, scale=1)
AHlogo3.append(ahcircle3)
AHlogo3.append(ahmain3)
AHlogo3.append(ahsub3)

ic4 = Circle(radius=22)
ahm4 = Polygon(points=[(-16,4), (-12,-4),(-12,-10),(-10,-8),(-4,-20),
                     (2,-8), (8,-20), (10,-16),   (4,-4),  (8,4),
                     (14,-8), (16,-4),  (10,8),  (10,8), (8,12),
                     (4,20),    (2,16),   (6,8),    (2,0), (-2,8),
                     (-2,14),  (-4,12),  (-8,20),(-10,16), (-6,8),
                     (-10,0),(-14,8)])
ahs4 = Polygon(points=[(-8,-4),(-4,-12),(0,-4),(-4,4)])

ahcircle4 = VectorShape(shape=ic4, x=0, y=0, pixel_shader=cblack)
ahmain4 = VectorShape(shape=ahm4, x=0, y=0, pixel_shader=cwhite)
ahsub4 = VectorShape(shape=ahs4, x=0, y=0, pixel_shader=cblack)

AHlogo4 = displayio.Group(max_size=3, x=-160, y=-80, scale=1)
AHlogo4.append(ahcircle4)
AHlogo4.append(ahmain4)
AHlogo4.append(ahsub4)

live.append(AHlogo3)
time.sleep(a)
AHlogo3.x = 160
time.sleep(a)
AHlogo3.y = 80
time.sleep(a)
AHlogo3.x = -160
time.sleep(a)
AHlogo3.y = -80
live.pop(1)
live.append(AHlogo4)
time.sleep(a)
AHlogo4.x = 160
time.sleep(a)
AHlogo4.y = 80
time.sleep(a)
AHlogo4.x = -160
time.sleep(a)
AHlogo4.y = -80
AHlogo3.scale = 3
live.pop(1)
live.append(AHlogo3)
time.sleep(a)
time.sleep(a)
AHlogo3.x = 160
time.sleep(a)
AHlogo3.y = 80
time.sleep(a)
AHlogo3.x = -160
time.sleep(a)
AHlogo3.y = -80
AHlogo4.scale = 3
live.pop(1)
live.append(AHlogo4)
time.sleep(a)
AHlogo4.x = 160
time.sleep(a)
AHlogo4.y = 80
time.sleep(a)
AHlogo4.x = -160
time.sleep(a)
AHlogo4.y = -80

live.pop(1)

AHlogo4.x = 201
time.sleep(a)
AHlogo4.y = 121
time.sleep(a)
AHlogo4.x = 200
time.sleep(a)
AHlogo4.y = 120
time.sleep(a)

while True:
    if clue.key_esc:
        print(gc.mem_free(),'bytes free --> program quit sucessfully')
        supervisor.reload()

Modified Adafruit clue library (I compile to .mpy when I modify it and run that) :


`# SPDX-FileCopyrightText: Copyright (c) 2020 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# The MIT License (MIT)
#
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_clue`
================================================================================

A high level library representing all the features of the Adafruit CLUE.

* Author(s): Kattni Rembor

Implementation Notes
--------------------

**Hardware:**

.. "* `Adafruit CLUE - nRF52840 Express with Bluetooth LE <https://www.adafruit.com/product/4500>`_"

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
  https://github.com/adafruit/circuitpython/releases

 * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
 * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
 * Adafruit's LSM6DS CircuitPython Library:
   https://github.com/adafruit/Adafruit_CircuitPython_LSM6DS
 * Adafruit's LIS3MDL CircuitPython Library:
   https://github.com/adafruit/Adafruit_CircuitPython_LIS3MDL
 * Adafruit's APDS9960 CircuitPython Library:
   https://github.com/adafruit/Adafruit_CircuitPython_APDS9960
 * Adafruit's BMP280 CircuitPython Library:
   https://github.com/adafruit/Adafruit_CircuitPython_BMP280
 * Adafruit's SHT31D CircuitPython Library:
   https://github.com/adafruit/Adafruit_CircuitPython_SHT31D
 * Adafruit's NeoPixel CircuitPython Library:
   https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel
"""

import time
import array
import math
import board
import digitalio
import analogio
import neopixel
import adafruit_apds9960.apds9960
import adafruit_bmp280
import adafruit_lis3mdl
import adafruit_lsm6ds.lsm6ds33
import adafruit_sht31d
import audiobusio
import audiopwmio
import audiocore
#import gamepad
import adafruit_matrixkeypad
import rotaryio
#import touchio
import displayio
import framebufferio
import sharpdisplay

__version__ = "2.2.6"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CLUE.git"

class _ClueSimpleTextDisplay:
    """Easily display lines of text on CLUE display."""

    def __init__(  # pylint: disable=too-many-arguments
        self,
        title=None,
        title_color=0xFFFFFF,
        title_scale=1,
        text_scale=1,
        font=None,
        colors=None,
        add_height=0,
    ):
        # pylint: disable=import-outside-toplevel
        from adafruit_display_text import label

        # pylint: enable=import-outside-toplevel

        if not colors:
            colors = (
                Clue.WHITE,
            )

        self._colors = colors
        self._label = label
        self.display = clue.display
        self._add_height = add_height

        if font:
            self._font = font
        else:
            import terminalio
            self._font = terminalio.FONT

        self.text_group = displayio.Group(max_size=20, scale=text_scale)

        if title:
            # Fail gracefully if title is longer than 60 characters.
            if len(title) > 60:
                raise ValueError("Title must be 60 characters or less.")

            title = label.Label(
                self._font,
                text=title,
                max_glyphs=60,
                color=title_color,
                scale=title_scale,
            )

            title.x = 0
            title.y = 8 + (self._add_height * title_scale) - (self._add_height//2)
            self._y = title.y + 18 + (self._add_height * title_scale)

            self.text_group.append(title)
        else:
            self._y = 3

        self._lines = []
        for num in range(1):
            self._lines.append(self.add_text_line(color=colors[num % len(colors)]))

    def __getitem__(self, item):
        """Fetch the Nth text line Group"""
        if len(self._lines) - 1 < item:
            for _ in range(item - (len(self._lines) - 1)):
                self._lines.append(
                    self.add_text_line(color=self._colors[item % len(self._colors)])
                )
        return self._lines[item]

    def add_text_line(self, color=0xFFFFFF):
        """Adds a line on the display of the specified color and returns the label object."""
        text_label = self._label.Label(self._font, text="", max_glyphs=45, color=color)
        text_label.x = 0
        text_label.y = self._y
        self._y = text_label.y + 13 + self._add_height
        if self._add_height != 0:
            self._y += 5
        self.text_group.append(text_label)

        return text_label

    def show(self):
        """Call show() to display the data list."""
        self.display.show(self.text_group)

    def show_terminal(self):
        """Revert to terminalio screen."""
        self.display.show(None)

class Clue:  # pylint: disable=too-many-instance-attributes, too-many-public-methods
    """Represents a single CLUE."""

    # Color variables available for import.
    RED = (255, 0, 0)
    YELLOW = (255, 255, 0)
    ORANGE = (255, 150, 0)
    GREEN = (0, 255, 0)
    TEAL = (0, 255, 120)
    CYAN = (0, 255, 255)
    BLUE = (0, 0, 255)
    PURPLE = (180, 0, 255)
    MAGENTA = (255, 0, 150)
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)

    GOLD = (255, 222, 30)
    PINK = (242, 90, 255)
    AQUA = (50, 255, 255)
    JADE = (0, 255, 40)
    AMBER = (255, 100, 0)
    VIOLET = (255, 0, 255)
    SKY = (0, 180, 255)

    RAINBOW = (RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE)

    def __init__(self):
        # Define I2C:
        self._i2c = board.I2C()

        # Define touch:
        # Initially, self._touches stores the pin used for a particular touch. When that touch is
        # used for the first time, the pin is replaced with the corresponding TouchIn object.
        # This saves a little RAM over using a separate read-only pin tuple.
        # For example, after `clue.touch_2`, self._touches is equivalent to:
        # [board.D0, board.D1, touchio.TouchIn(board.D2)]
        #self._touches = [board.D0, board.D1, board.D2]
        #self._touch_threshold_adjustment = 0

        # Define buttons:
        self._topbutton = digitalio.DigitalInOut(board.SWITCH)
        self._topbutton.switch_to_input(pull=digitalio.Pull.UP)

        cols = [digitalio.DigitalInOut(x) for x in (board.D13, board.D12, board.D11, board.D10)]
        rows = [digitalio.DigitalInOut(x) for x in (board.D2, board.D5, board.D6, board.D9)]

        keys = (("A","B","C","D"),
                (  3,  6,  9,"E"),
                (  2,  5,  8,"F"),
                (  1,  4,  7,  0))

        self._keypad = adafruit_matrixkeypad.Matrix_Keypad(cols, rows, keys)
        self._encoder = rotaryio.IncrementalEncoder(board.A3, board.A4)

        # Define LEDs:
        self._pixels = neopixel.NeoPixel(board.NEOPIXEL, 15, pixel_order=(1, 0, 2, 3))
        self._red_led = digitalio.DigitalInOut(board.BLUE_LED)
        self._red_led.switch_to_output()
        self._batvol = analogio.AnalogIn(board.BATTERY)

        # Define audio:
        self._mic = audiobusio.PDMIn(
            board.MICROPHONE_CLOCK,
            board.MICROPHONE_DATA,
            sample_rate=16000,
            bit_depth=16,
        )
        self._sample = None
        self._samples = None
        self._sine_wave = None
        self._sine_wave_sample = None

        # Define sensors:
        # Accelerometer/gyroscope:
        self._accelerometer = adafruit_lsm6ds.lsm6ds33.LSM6DS33(self._i2c)

        # Magnetometer:
        self._magnetometer = adafruit_lis3mdl.LIS3MDL(self._i2c)

        # DGesture/proximity/color/light sensor:
        self._sensor = adafruit_apds9960.apds9960.APDS9960(self._i2c)

        # Humidity sensor:
        self._humidity = adafruit_sht31d.SHT31D(self._i2c)

        # Barometric pressure sensor:
        self._pressure = adafruit_bmp280.Adafruit_BMP280_I2C(self._i2c)

        # Create displayio object for passing.
        displayio.release_displays()
        self._framebuffer = sharpdisplay.SharpMemoryFramebuffer(board.SPI(), board.A5, 400, 240)
        self.display = framebufferio.FramebufferDisplay(self._framebuffer, auto_refresh = True)

    #def _touch(self, i):
    #    if not isinstance(self._touches[i], touchio.TouchIn):
            # First time referenced. Get the pin from the slot for this touch
            # and replace it with a TouchIn object for the pin.
    #        self._touches[i] = touchio.TouchIn(self._touches[i])
    #        self._touches[i].threshold += self._touch_threshold_adjustment
    #    return self._touches[i].value

    #@property
    #def touch_0(self):
        """Detect touch on capacitive touch pad 0.

        .. image :: ../docs/_static/pad_0.jpg
          :alt: Pad 0

        This example prints when pad 0 is touched.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              if clue.touch_0:
                  print("Touched pad 0")
        """
    #    return self._touch(0)

    #@property
    #def touch_1(self):
        """Detect touch on capacitive touch pad 1.

        .. image :: ../docs/_static/pad_1.jpg
          :alt: Pad 1

        This example prints when pad 1 is touched.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              if clue.touch_1:
                  print("Touched pad 1")
        """
    #    return self._touch(1)

    #@property
    #def touch_2(self):
        """Detect touch on capacitive touch pad 2.

        .. image :: ../docs/_static/pad_2.jpg
          :alt: Pad 2

        This example prints when pad 2 is touched.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              if clue.touch_2:
                  print("Touched pad 2")
        """
    #    return self._touch(2)

        """``True`` when Button A is pressed. ``False`` if not.

        .. image :: ../docs/_static/button_a.jpg
          :alt: Button A

        This example prints when button A is pressed.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              if clue.key_x:
                  print("key x pressed")
        """
    def deinitdisp(self):
        displayio.release_displays()
        del self._framebuffer
        #digitalio.board.A5.deinit()

    def reinitdisp(self):   
        self._framebuffer = sharpdisplay.SharpMemoryFramebuffer(board.SPI(), board.A5, 400, 240)
        self.display = framebufferio.FramebufferDisplay(self._framebuffer, auto_refresh = True)

    @property
    def vbat(self):
        return (self._batvol.value * 3.3) / 65536 * 2

    @property
    def enc_pos(self):
        return self._encoder.position

    @property
    def key_top(self):
        return not self._topbutton.value

    @property
    def key_1(self):

        if clue._keypad.pressed_keys == [1]:
            return True
        else:
            return False

    @property
    def key_2(self):
        if clue._keypad.pressed_keys == [2]:
            return True
        else:
            return False

    @property
    def key_3(self):
        if clue._keypad.pressed_keys == [3]:
            return True
        else:
            return False

    @property
    def key_4(self):
        if clue._keypad.pressed_keys == [4]:
            return True
        else:
            return False

    @property
    def key_5(self):
        if clue._keypad.pressed_keys == [5]:
            return True
        else:
            return False

    @property
    def key_6(self):
        if clue._keypad.pressed_keys == [6]:
            return True
        else:
            return False

    @property
    def key_7(self):
        if clue._keypad.pressed_keys == [7]:
            return True
        else:
            return False

    @property
    def key_8(self):
        if clue._keypad.pressed_keys == [8]:
            return True
        else:
            return False

    @property
    def key_9(self):
        if clue._keypad.pressed_keys == [9]:
            return True
        else:
            return False

    @property
    def key_0(self):
        if clue._keypad.pressed_keys == [0]:
            return True
        else:
            return False

    @property
    def key_A(self):
        if clue._keypad.pressed_keys == ["A"]:
            return True
        else:
            return False

    @property
    def key_B(self):
        if clue._keypad.pressed_keys == ["B"]:
            return True
        else:
            return False

    @property
    def key_C(self):
        if clue._keypad.pressed_keys == ["C"]:
            return True
        else:
            return False

    @property
    def key_D(self):
        if clue._keypad.pressed_keys == ["D"]:
            return True
        else:
            return False

    @property
    def key_E(self):
        if clue._keypad.pressed_keys == ["E"]:
            return True
        else:
            return False

    @property
    def key_F(self):
        if clue._keypad.pressed_keys == ["F"]:
            return True
        else:
            return False

    @property
    def key_esc(self):
        if clue._keypad.pressed_keys == ["A","D",1,0]:
            return True
        else:
            return False

    @property
    def were_pressed(self):
        """Returns a set of the buttons that have been pressed.

        .. image :: ../docs/_static/button_b.jpg
          :alt: Button B

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              print(clue.were_pressed)
        """
        ret = []
        keyz = clue._keypad.pressed_keys
        if keyz == []:
            if clue.key_top:
                ret = ["Top_Button"]
        else:
            ret = keyz
            if clue.key_top:
                ret = [keyz, "Top_Button"]
        return ret

    def shake(self, shake_threshold=30, avg_count=10, total_delay=0.1):
        """
        Detect when the accelerometer is shaken. Optional parameters:

        :param shake_threshold: Increase or decrease to change shake sensitivity. This
                                requires a minimum value of 10. 10 is the total
                                acceleration if the board is not moving, therefore
                                anything less than 10 will erroneously report a constant
                                shake detected. (Default 30)

        :param avg_count: The number of readings taken and used for the average
                          acceleration. (Default 10)

        :param total_delay: The total time in seconds it takes to obtain avg_count
                            readings from acceleration. (Default 0.1)
         """
        shake_accel = (0, 0, 0)
        for _ in range(avg_count):
            # shake_accel creates a list of tuples from acceleration data.
            # zip takes multiple tuples and zips them together, as in:
            # In : zip([-0.2, 0.0, 9.5], [37.9, 13.5, -72.8])
            # Out: [(-0.2, 37.9), (0.0, 13.5), (9.5, -72.8)]
            # map applies sum to each member of this tuple, resulting in a
            # 3-member list. tuple converts this list into a tuple which is
            # used as shake_accel.
            shake_accel = tuple(map(sum, zip(shake_accel, self.acceleration)))
            time.sleep(total_delay / avg_count)
        avg = tuple(value / avg_count for value in shake_accel)
        total_accel = math.sqrt(sum(map(lambda x: x * x, avg)))
        return total_accel > shake_threshold

    @property
    def acceleration(self):
        """Obtain acceleration data from the x, y and z axes.

        .. image :: ../docs/_static/accelerometer.jpg
          :alt: Accelerometer

        This example prints the values. Try moving the board to see how the printed values change.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              print("Accel: {:.2f} {:.2f} {:.2f}".format(*clue.acceleration))
        """
        return self._accelerometer.acceleration

    @property
    def gyro(self):
        """Obtain x, y, z angular velocity values in degrees/second.

        .. image :: ../docs/_static/accelerometer.jpg
          :alt: Gyro

        This example prints the values. Try moving the board to see how the printed values change.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              print("Gyro: {:.2f} {:.2f} {:.2f}".format(*clue.gyro))
        """
        return self._accelerometer.gyro

    @property
    def magnetic(self):
        """Obtain x, y, z magnetic values in microteslas.

        .. image :: ../docs/_static/magnetometer.jpg
          :alt: Magnetometer

        This example prints the values. Try moving the board to see how the printed values change.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              print("Magnetic: {:.3f} {:.3f} {:.3f}".format(*clue.magnetic))
        """
        return self._magnetometer.magnetic

    @property
    def proximity(self):
        """A relative proximity to the sensor in values from 0 - 255.

        .. image :: ../docs/_static/proximity.jpg
          :alt: Proximity sensor

        This example prints the value. Try moving your hand towards and away from the front of the
        board to see how the printed values change.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              print("Proximity: {}".format(clue.proximity))
        """
        self._sensor.enable_proximity = True
        return self._sensor.proximity

    @property
    def color(self):
        """The red, green, blue, and clear light values. (r, g, b, c)

        .. image :: ../docs/_static/proximity.jpg
          :alt: Color sensor

        This example prints the values. Try holding something up to the sensor to see the values
        change. Works best with white LEDs enabled.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              print("Color: R: {} G: {} B: {} C: {}".format(*clue.color))
        """
        self._sensor.enable_color = True
        return self._sensor.color_data

    @property
    def gesture(self):
        """A gesture code if gesture is detected. Shows ``0`` if no gesture detected.
        ``1`` if an UP gesture is detected, ``2`` if DOWN, ``3`` if LEFT, and ``4`` if RIGHT.

        .. image :: ../docs/_static/proximity.jpg
          :alt: Gesture sensor

        This example prints the gesture values. Try moving your hand up, down, left or right over
        the sensor to see the value change.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              value = clue.gesture
              if value:
                  print("gesture: {}".format(value))
        """
        self._sensor.enable_gesture = True
        self._sensor.enable_proximity = True
        # set rotation to match sensor orientation on CLUE
        self._sensor.rotation = 270
        return self._sensor.gesture()

    @property
    def mic(self):
        return self._mic

    @property
    def humidity(self):
        """The measured relative humidity in percent.

        .. image :: ../docs/_static/humidity.jpg
          :alt: Humidity sensor

        This example prints the value. Try breathing on the sensor to see the values change.

        To use with the CLUE:

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              print("Humidity: {:.1f}%".format(clue.humidity))
        """
        return self._humidity.relative_humidity

    @property
    def pressure(self):
        """The barometric pressure in hectoPascals.

        .. image :: ../docs/_static/pressure.jpg
          :alt: Barometric pressure sensor

        This example prints the value.

        To use with the CLUE:

        .. code-block:: python

            from adafruit_clue import clue

            print("Pressure: {:.3f}hPa".format(clue.pressure))
        """
        return self._pressure.pressure

    @property
    def temperature(self):
        """The temperature in degrees Celsius.

        .. image :: ../docs/_static/pressure.jpg
          :alt: Temperature sensor

        This example prints the value. Try touching the sensor to see the value change.

        To use with the CLUE:

        .. code-block:: python

            from adafruit_clue import clue

            print("Temperature: {:.1f}C".format(clue.temperature))
        """
        return self._pressure.temperature

    @property
    def altitude(self):
        """The altitude in meters based on the sea level pressure at your location. You must set
        ``sea_level_pressure`` to receive an accurate reading.

        .. image :: ../docs/_static/pressure.jpg
          :alt: Altitude sensor

        This example prints the value. Try moving the board vertically to see the value change.

        .. code-block:: python

            from adafruit_clue import clue

            clue.sea_level_pressure = 1015

            print("Altitude: {:.1f}m".format(clue.altitude))
        """
        return self._pressure.altitude

    @property
    def sea_level_pressure(self):
        """Set to the pressure at sea level at your location, before reading altitude for
        the most accurate altitude measurement.

        .. image :: ../docs/_static/pressure.jpg
          :alt: Barometric pressure sensor

        This example prints the value.

        To use with the CLUE:

        .. code-block:: python

            from adafruit_clue import clue

            clue.sea_level_pressure = 1015

            print("Pressure: {:.3f}hPa".format(clue.pressure))
        """
        return self._pressure.sea_level_pressure

    @sea_level_pressure.setter
    def sea_level_pressure(self, value):
        self._pressure.sea_level_pressure = value

    @property
    def white_leds(self):
        """The red led next to the USB plug labeled LED.

        .. image :: ../docs/_static/white_leds.jpg
          :alt: White LEDs

        This example turns on the white LEDs.

        To use with the CLUE:

        .. code-block:: python

            from adafruit_clue import clue

            clue.white_leds = True
        """
        return self._white_leds.value

    @white_leds.setter
    def white_leds(self, value):
        self._white_leds.value = value

    @property
    def red_led(self):
        """The red led next to the USB plug labeled LED.

        .. image :: ../docs/_static/red_led.jpg
          :alt: Red LED

        This example turns on the red LED.

        To use with the CLUE:

        .. code-block:: python

            from adafruit_clue import clue

            clue.red_led = True
        """
        return self._red_led.value

    @red_led.setter
    def red_led(self, value):
        self._red_led.value = value

    @property
    def pixels(self):
        """The NeoPixel RGB LED.

        .. image :: ../docs/_static/neopixel.jpg
          :alt: NeoPixel

        This example turns the NeoPixel purple.

        To use with the CLUE:

        .. code-block:: python

            from adafruit_clue import clue

            while True:
                clue.pixel.fill((255, 0, 255))
        """
        return self._pixels

    @staticmethod
    def _sine_sample(length):
        tone_volume = (2 ** 15) - 1
        shift = 2 ** 15
        for i in range(length):
            yield int(tone_volume * math.sin(2 * math.pi * (i / length)) + shift)

    def _generate_sample(self, length=100):
        if self._sample is not None:
            return
        self._sine_wave = array.array("H", self._sine_sample(length))
        self._sample = audiopwmio.PWMAudioOut(board.A1)
        self._sine_wave_sample = audiocore.RawSample(self._sine_wave)

    def play_tone(self, frequency, duration):
        """ Produce a tone using the speaker. Try changing frequency to change
        the pitch of the tone.

        :param int frequency: The frequency of the tone in Hz
        :param float duration: The duration of the tone in seconds

        .. image :: ../docs/_static/speaker.jpg
          :alt: Speaker

        This example plays a 880 Hz tone for a duration of 1 second.

        To use with the CLUE:

        .. code-block:: python

            from adafruit_clue import clue

            clue.play_tone(880, 1)
        """
        # Play a tone of the specified frequency (hz).
        self.start_tone(frequency)
        time.sleep(duration)
        self.stop_tone()

    def start_tone(self, frequency):
        """ Produce a tone using the speaker. Try changing frequency to change
        the pitch of the tone.

        :param int frequency: The frequency of the tone in Hz

        .. image :: ../docs/_static/speaker.jpg
          :alt: Speaker

        This example plays a 523Hz tone when button A is pressed and a 587Hz tone when button B is
        pressed, only while the buttons are being pressed.

        To use with the CLUE:

        .. code-block:: python

             from adafruit_clue import clue

             while True:
                 if clue.button_a:
                     clue.start_tone(523)
                 elif clue.button_b:
                     clue.start_tone(587)
                 else:
                     clue.stop_tone()
        """
        length = 100
        if length * frequency > 350000:
            length = 350000 // frequency
        self._generate_sample(length)
        # Start playing a tone of the specified frequency (hz).
        self._sine_wave_sample.sample_rate = int(len(self._sine_wave) * frequency)
        if not self._sample.playing:
            self._sample.play(self._sine_wave_sample, loop=True)

    def stop_tone(self):
        """ Use with start_tone to stop the tone produced.

        .. image :: ../docs/_static/speaker.jpg
          :alt: Speaker

        This example plays a 523Hz tone when button A is pressed and a 587Hz tone when button B is
        pressed, only while the buttons are being pressed.

        To use with the CLUE:

        .. code-block:: python

             from adafruit_clue import clue

             while True:
                 if clue.button_a:
                     clue.start_tone(523)
                 elif clue.button_b:
                     clue.start_tone(587)
                 else:
                     clue.stop_tone()
        """
        # Stop playing any tones.
        if self._sample is not None and self._sample.playing:
            self._sample.stop()
            self._sample.deinit()
            self._sample = None

    @staticmethod
    def _normalized_rms(values):
        mean_values = int(sum(values) / len(values))
        return math.sqrt(
            sum(
                float(sample - mean_values) * (sample - mean_values)
                for sample in values
            )
            / len(values)
        )

    @property
    def sound_level(self):
        """Obtain the sound level from the microphone (sound sensor).

        .. image :: ../docs/_static/microphone.jpg
          :alt: Microphone (sound sensor)

        This example prints the sound levels. Try clapping or blowing on
        the microphone to see the levels change.

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              print(clue.sound_level)
        """
        if self._sample is None:
            self._samples = array.array("H", [0] * 160)
        self._mic.record(self._samples, len(self._samples))
        return self._normalized_rms(self._samples)

    def loud_sound(self, sound_threshold=200):
        """Utilise a loud sound as an input.

        :param int sound_threshold: Threshold sound level must exceed to return true (Default: 200)

        .. image :: ../docs/_static/microphone.jpg
          :alt: Microphone (sound sensor)

        This example turns the NeoPixel LED blue each time you make a loud sound.
        Try clapping or blowing onto the microphone to trigger it.

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              if clue.loud_sound():
                  clue.pixel.fill((0, 50, 0))
              else:
                  clue.pixel.fill(0)

        You may find that the code is not responding how you would like.
        If this is the case, you can change the loud sound threshold to
        make it more or less responsive. Setting it to a higher number
        means it will take a louder sound to trigger. Setting it to a
        lower number will take a quieter sound to trigger. The following
        example shows the threshold being set to a higher number than
        the default.

        .. code-block:: python

          from adafruit_clue import clue

          while True:
              if clue.loud_sound(sound_threshold=300):
                  clue.pixel.fill((0, 50, 0))
              else:
                  clue.pixel.fill(0)
        """

        return self.sound_level > sound_threshold

    @staticmethod
    def simple_text_display(  # pylint: disable=too-many-arguments
        title=None,
        title_color=(255, 255, 255),
        title_scale=1,
        text_scale=1,
        font=None,
        colors=None,
        add_height=0,
    ):
        """Display lines of text on the CLUE display. Lines of text are created in order as shown
        in the example below. If you skip a number, the line will be shown blank on the display,
        e.g. if you include ``[0]`` and ``[2]``, the second line on the display will be empty, and
        the text specified for lines 0 and 2 will be displayed on the first and third line.
        Remember, Python begins counting at 0, so the first line on the display is 0 in the code.

        Setup occurs before the loop. For data to be dynamically updated on the display, you must
        include the data call in the loop by using ``.text =``. For example, if setup is saved as
        ``clue_data = simple_text_display()`` then ``clue_data[0].text = clue.proximity`` must be
        inside the ``while True:`` loop for the proximity data displayed to update as the
        values change. You must call ``show()`` at the end of the list for anything to display.
        See example below for usage.

        :param str title: The title displayed above the data. Set ``title="Title text"`` to provide
                          a title. Defaults to None.
        :param title_color: The color of the title. Not necessary if no title is provided. Defaults
                            to white (255, 255, 255).
        :param int title_scale: Scale the size of the title. Not necessary if no title is provided.
                                Defaults to 1.
        :param int text_scale: Scale the size of the data lines. Scales the title as well.
                               Defaults to 1.
        :param str font: The font to use to display the title and data. Defaults to built in
                     ``terminalio.FONT``.
        :param colors: A list of colors for the lines of data on the display. If you provide a
                       single color, all lines will be that color. Otherwise it will cycle through
                       the list you provide if the list is less than the number of lines displayed.
                       Default colors are used if ``colors`` is not set. For example, if creating
                       two lines of data, ``colors=((255, 255, 255), (255, 0, 0))`` would set the
                       first line white and the second line red, and if you created four lines of
                       data with the same setup, it would alternate white and red.

        .. image :: ../docs/_static/display_clue_data.jpg
          :alt: Display Clue Data demo

        This example displays three lines with acceleration, gyro and magnetic data on the display.
        Remember to call ``show()`` after the list to update the display.

        .. code-block:: python

          from adafruit_clue import clue

          clue_data = clue.simple_text_display(title="CLUE Sensor Data!", title_scale=2)

          while True:
              clue_data[0].text = "Acceleration: {:.2f} {:.2f} {:.2f}".format(*clue.acceleration)
              clue_data[1].text = "Gyro: {:.2f} {:.2f} {:.2f}".format(*clue.gyro)
              clue_data[2].text = "Magnetic: {:.3f} {:.3f} {:.3f}".format(*clue.magnetic)
              clue_data.show()
        """
        return _ClueSimpleTextDisplay(
            title=title,
            title_color=title_color,
            title_scale=title_scale,
            text_scale=text_scale,
            font=font,
            colors=colors,
            add_height=add_height
        )

clue = Clue()  # pylint: disable=invalid-name
"""Object that is automatically created on import.

   To use, simply import it from the module:

   .. code-block:: python

   from adafruit_clue import clue
"""

to elaborate, I just reran this code on a fresh boot, and a full power cycle, full power cycle I got 75680 bytes free --> low res logo 74288 bytes free --> hi res logo ^ quickest commands I see when opening the serial

EVERY soft reboot or reset button press, or code save reload, the same point of code yields: 63584 bytes free --> low res logo 62192 bytes free --> hi res logo

thats a lot to loose, and idk how to fix it.

furthermore, If I try to do anything to deinitialize the display at any point using the function I added in adafruitclue.py, I get SPI errors, and can no longer use the display until a full power cycle. heres that specific function:

def deinitdisp(self): displayio.release_displays() del self._framebuffer digitalio.board.A5.deinit()

even tried deleting the frame buffer object which I know doesnt actually do what I think it does in effort to release some pins. but I know its more going on with issues mentioned earlier.

let me know how else I can be of value. Thank you for taking the time!

tannewt commented 3 years ago

@cwalther I think the bug is setting the singleton back to NULL while it's being used by the display still. It should be preserved by release_displays() in case it's referenced before it. I suspect https://github.com/adafruit/circuitpython/pull/3378 is related.

@hatchman supervisor.reload() is not the same as pressing the reset button. "reloads" don't change display state because the display will be used by CircuitPython after the VM completes.

tannewt commented 3 years ago

Actually, why isn't this working: https://github.com/adafruit/circuitpython/blob/main/shared-module/board/__init__.c#L162

cwalther commented 3 years ago

@hatchman: I was thinking of something a little shorter. :) Could you try to strip down this clue library to just the display-related parts needed for this application? I need to remove at least these imports to get past the import section on my board: neopixel, adafruit_apds9960.apds9960, adafruit_bmp280, adafruit_lis3mdl, adafruit_lsm6ds.lsm6ds33, adafruit_sht31d, audiobusio, audiopwmio, adafruit_matrixkeypad.

Regarding the original issue with the inability to reinitialize board.SPI, the workaround is to call board.SPI().deinit() between release_displays() and the soft reboot.

@tannewt: I’m not sure I follow. In my case, there is no display using it anymore at the time of the soft reboot.

dhalbert commented 3 years ago

If you want to do a hard reset, you can use microcontroller.reset().

jepler commented 3 years ago

Having a decreased amount of memory the second time you use a sharp memory display is hopefully going to be addressed by #3498, which is an in-progress pull request.

Assuming that what you are seeing is that the amount of available memory decreases from the first to second run, but stays broadly similar in second and following runs, I believe that is what you are seeing.

cwalther commented 3 years ago

I think the bug is setting the singleton back to NULL […] It should be preserved by release_displays() in case it's referenced before it.

~I’m not sure about that. The object is spi_obj and still exists, so any Python references should remain valid, spi_singleton that is set to NULL is just a pointer to it and can be identically restored by calling board.SPI() again (at least until the end of the session, due to the bug).~

Whoops, got confused there. No, I agree that release_displays() should preserve it, as it currently does.

hatchman commented 3 years ago

@tannewt @dhalbert yes, thank you for the clarifications on all this. I did change my code to use the microcontroller function, which is more what I was looking for.

@cwalther yeah sorry about that, that was just something quick, ill prune the file and add it here to test with the Adafruit sense specific stuff removed and my other hardware. the quick fix of calling board.SPI().deinit() after releasing displays and resetting is just the trick for what I need. now ill see if I can easily reinit the display in the same boot, and id be real happy.

@jepler I will try to get something together for @cwalther to test, and reproduce this more regularly, my approach is now changing, but the issue will persist in other use cases.

to each of you: has anyone told you you are a beautiful human lately? BIG THANKS!

hatchman commented 3 years ago

@cwalther try this for code.py: displays my logo at a few sizes. prints during operation to serial. press the onboard button to issue a supervisor reload.

import gc
import time
from adafruit_clue import clue
import displayio
import supervisor
from vectorio import Circle, Polygon, VectorShape
print(gc.mem_free(),'bytes free --> import success')

clue.pixels.fill((0,0,0,0))

a = 1/30
live = displayio.Group(max_size=2, x=200, y=120, scale=1)
cblack = displayio.Palette(2)
cblack.make_transparent(0)
cblack[1] = 0x000000
cwhite = displayio.Palette(2)
cwhite.make_transparent(0)
cwhite[1] = 0xFFFFFF
print(gc.mem_free(),'bytes free --> initialize display variables')
clue.display.show(live)
oc = Circle(radius=78)
ic = Circle(radius=39)
ahm = Polygon(points=[(-22,5), (-17,-5),(-18,-14),(-14,-11),(-6,-27),
                     (2,-11), (10,-27), (13,-21),   (5,-5),  (10,5),
                     (18,-11), (21,-5),  (13,11),  (14,20), (10,17),
                     (5,27),    (2,21),   (7,11),    (2,1), (-3,11),
                     (-2,20),  (-6,17),  (-11,27),(-14,21), (-9,11),
                     (-14,1),(-19,11)])
ahs = Polygon(points=[(-11,-5),(-6,-15),(-1,-5),(-6,5)])
cblack = displayio.Palette(2)
cblack.make_transparent(0)
cblack[1] = 0x000000
cwhite = displayio.Palette(2)
cwhite.make_transparent(0)
cwhite[1] = 0xFFFFFF

ahoutline = VectorShape(shape=oc, x=0, y=0, pixel_shader=cwhite)
ahcircle = VectorShape(shape=ic, x=0, y=0, pixel_shader=cblack)
ahmain = VectorShape(shape=ahm, x=0, y=0, pixel_shader=cwhite)
ahsub = VectorShape(shape=ahs, x=0, y=0, pixel_shader=cblack)

AHlogo = displayio.Group(max_size=4, x=0, y=0, scale=1)
AHlogo.append(ahoutline)
AHlogo.append(ahcircle)
AHlogo.append(ahmain)
AHlogo.append(ahsub)
live.append(AHlogo)
time.sleep(a)
AHlogo.scale = 2
time.sleep(a)
AHlogo.scale = 3
time.sleep(a)
print(gc.mem_free(),'bytes free --> low res logo')

live.pop()
time.sleep(a)
oc2 = Circle(radius=234)
ic2 = Circle(radius=118)
ahm2 = Polygon(points=[(-66,15), (-51,-15),(-54,-42),(-42,-33),(-18,-81),
                     (6,-33), (30,-81), (39,-63),   (15,-15),  (30,15),
                     (54,-33), (63,-15),  (39,33),  (42,60), (30,51),
                     (15,81),    (6,63),   (21,33),    (6,3), (-9,33),
                     (-6,60),  (-18,51),  (-33,81),(-42,63), (-27,33),
                     (-42,3),(-57,33)])
ahs2 = Polygon(points=[(-33,-15),(-18,-45),(-3,-15),(-18,15)])

ahoutline2 = VectorShape(shape=oc2, x=0, y=0, pixel_shader=cwhite)
ahcircle2 = VectorShape(shape=ic2, x=0, y=0, pixel_shader=cblack)
ahmain2 = VectorShape(shape=ahm2, x=0, y=0, pixel_shader=cwhite)
ahsub2 = VectorShape(shape=ahs2, x=0, y=0, pixel_shader=cblack)

AHlogo2 = displayio.Group(max_size=4, x=0, y=0, scale=1)
AHlogo2.append(ahoutline2)
time.sleep(a)
AHlogo2.append(ahcircle2)
time.sleep(a)
AHlogo2.append(ahmain2)
time.sleep(a)
AHlogo2.append(ahsub2)
time.sleep(a)
live.append(AHlogo2)
time.sleep(a)
print(gc.mem_free(),'bytes free --> hi res logo')

while True:
    if clue.key_top:
        print(gc.mem_free(),'bytes free --> program quit sucessfully')
        supervisor.reload()

here is the highly trimmed Adafruit clue library with my frame buffer modified. I use pin A5 on the blue fruit sense for the display chip select:


import time
import array
import math
import board
import digitalio
import analogio
import neopixel
import displayio
import framebufferio
import sharpdisplay
class _ClueSimpleTextDisplay:
    def __init__(  # pylint: disable=too-many-arguments
        self,
        title=None,
        title_color=0xFFFFFF,
        title_scale=1,
        text_scale=1,
        font=None,
        colors=None,
        add_height=0,
    ):
        from adafruit_display_text import label
        if not colors:
            colors = (
                Clue.WHITE,
            )
        self._colors = colors
        self._label = label
        self.display = clue.display
        self._add_height = add_height
        if font:
            self._font = font
        else:
            import terminalio
            self._font = terminalio.FONT
        self.text_group = displayio.Group(max_size=20, scale=text_scale)
        if title:
            if len(title) > 60:
                raise ValueError("Title must be 60 characters or less.")
            title = label.Label(
                self._font,
                text=title,
                max_glyphs=60,
                color=title_color,
                scale=title_scale,
            )
            title.x = 0
            title.y = 8 + (self._add_height * title_scale) - (self._add_height//2)
            self._y = title.y + 18 + (self._add_height * title_scale)
            self.text_group.append(title)
        else:
            self._y = 3
        self._lines = []
        for num in range(1):
            self._lines.append(self.add_text_line(color=colors[num % len(colors)]))
    def __getitem__(self, item):
        if len(self._lines) - 1 < item:
            for _ in range(item - (len(self._lines) - 1)):
                self._lines.append(
                    self.add_text_line(color=self._colors[item % len(self._colors)])
                )
        return self._lines[item]
    def add_text_line(self, color=0xFFFFFF):
        text_label = self._label.Label(self._font, text="", max_glyphs=45, color=color)
        text_label.x = 0
        text_label.y = self._y
        self._y = text_label.y + 13 + self._add_height
        if self._add_height != 0:
            self._y += 5
        self.text_group.append(text_label)
        return text_label
    def show(self):
        """Call show() to display the data list."""
        self.display.show(self.text_group)
    def show_terminal(self):
        """Revert to terminalio screen."""
        self.display.show(None)
class Clue:  # pylint: disable=too-many-instance-attributes, too-many-public-methods

    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)

    def __init__(self):

        self._topbutton = digitalio.DigitalInOut(board.SWITCH)
        self._topbutton.switch_to_input(pull=digitalio.Pull.UP)
        self._pixels = neopixel.NeoPixel(board.NEOPIXEL, 15, pixel_order=(1, 0, 2, 3))
        self._red_led = digitalio.DigitalInOut(board.BLUE_LED)
        self._red_led.switch_to_output()
        self._batvol = analogio.AnalogIn(board.BATTERY)
        displayio.release_displays()
        self._framebuffer = sharpdisplay.SharpMemoryFramebuffer(board.SPI(), board.A5, 400, 240)
        self.display = framebufferio.FramebufferDisplay(self._framebuffer, auto_refresh = True)

    def deinitdisp(self):
        displayio.release_displays()
        board.SPI().deinit()

    def reinitdisp(self):  
        displayio.release_displays() 
        self._framebuffer = sharpdisplay.SharpMemoryFramebuffer(board.SPI(), board.A5, 400, 240)
        self.display = framebufferio.FramebufferDisplay(self._framebuffer, auto_refresh = True)

    @property
    def vbat(self):
        return (self._batvol.value * 3.3) / 65536 * 2
    @property
    def key_top(self):
        return not self._topbutton.value

    @property
    def white_leds(self):
        return self._white_leds.value

    @white_leds.setter
    def white_leds(self, value):
        self._white_leds.value = value

    @property
    def red_led(self):
        return self._red_led.value

    @red_led.setter
    def red_led(self, value):
        self._red_led.value = value

    @property
    def pixels(self):
        return self._pixels

    @staticmethod
    def simple_text_display(  # pylint: disable=too-many-arguments
        title=None,
        title_color=(255, 255, 255),
        title_scale=1,
        text_scale=1,
        font=None,
        colors=None,
        add_height=0,
    ):
        return _ClueSimpleTextDisplay(
            title=title,
            title_color=title_color,
            title_scale=title_scale,
            text_scale=text_scale,
            font=font,
            colors=colors,
            add_height=add_height
        )

clue = Clue()

in my experience, the first time the code loads, I get

129216 bytes free --> low res logo on a fresh power cycle consistently, and 115952 bytes free --> low res logo consistently every time that soft reboot is shown in the serial.

tannewt commented 3 years ago

@cwalther Huh, weird. Maybe this is a case where we mark the SPI as "never reset" but when we deinit the display we don't mark it as "ok to reset". Usually this is done by deinit but that's not quite what we want...

cwalther commented 3 years ago

@hatchman: I have now been able to run your code. The decrease in free memory between a power cycle and subsequent soft reboots is indeed fixed by #3498 for me, which should be merged soon (or you can download a build here under Artifacts).

However, I do not see a decrease in free memory between 6.0.0-beta.2 and 1a67740. After a power cycle, they match exactly, and after soft reboots I get 256 bytes more in 1a67740.

cwalther commented 3 years ago

Addendum to the original issue: The only reason this does not happen with I2C displays is that reset_board_busses() neglects to check displays[i].bus_base.type to see whether that display slot is still in use and then compares stale data and wrongly concludes that the I2C bus is still in use by the display that has actually been deinited already.

So a proper solution will apply to both SPI and I2C. (Working on it.)

hatchman commented 3 years ago

@cwalther Sorry it has taken me so long to test and get back,

I tested the build with the #3498 fix, and seems to eliminated the issue when I run the code I had sent you.

I do have some concerns however that you have not seen the same memory losses I saw on 6.0.0 beta 2 and previous. the only difference here are some libraries which shouldn't have held onto anything, but perhaps some of the I2C devices may be the culprit. I am reworking my code, and will report back if what I am experiencing comes back when I start working with i2c again, but for now, I assume everything is fixed and I am just a janky coder ;) thank all for the help and dedication to the project!

deshipu commented 3 years ago

Importing libraries will always use up some unrecoverable memory, because strings such as variable names and string constants are getting interned globally.

tannewt commented 3 years ago

Importing libraries will always use up some unrecoverable memory, because strings such as variable names and string constants are getting interned globally.

What do you mean by unrecoverable? Interned strings should be reset on reload because the string pool lives on the heap.

dhalbert commented 3 years ago

@cwalther Do you consider that this bug is now fixed by #3498 #3603? Thanks.

cwalther commented 3 years ago

Yes, but 6.0.x hasn’t been merged into main yet, I assume that’s why it hasn’t been automatically closed yet.

cwalther commented 3 years ago

Er, wait. It’s not fixed by #3498 but by #3603. Possible confusion?

dhalbert commented 3 years ago

Er, wait. It’s not fixed by #3498 but by #3603. Possible confusion?

Oops, yes, sloppy reading, sorry.