Skip to content

sharpdisplay: Prevent 'ValueError: <pin> in use' exception #3277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 18, 2020

Conversation

jepler
Copy link

@jepler jepler commented Aug 14, 2020

.. when constructing the SPI bus again after reset.

Testing performed: on nRF52840, that these sequences would work repeatedly when soft-resetting in between invocations:

import displayio, board, sharpdisplay
displayio.release_displays()
bus = board.SPI()
framebuffer = sharpdisplay.SharpMemoryFramebuffer(bus, board.D2, 400, 240)

and

import displayio, board, sharpdisplay, busio
displayio.release_displays()
bus = busio.SPI(board.SCK, MOSI=board.MOSI)
framebuffer = sharpdisplay.SharpMemoryFramebuffer(bus, board.D2, 400, 240)

.. and that an actual demo would work across soft resets as well.

This feels a bit ad-hoc. Better would be to make "never reset bus" be a counted thing and add some "un-never-resets" in the right spots. An area for future improvement.

@jepler jepler requested a review from tannewt August 14, 2020 15:00
@tannewt
Copy link
Member

tannewt commented Aug 14, 2020

Which of those two cases didn't work? Do we have this issue with FourWire displays too? This feels weird.

@jepler
Copy link
Author

jepler commented Aug 14, 2020

Neither case worked. I did not test with FourWire displays. I also didn't test with a different platform than nrf52.

@jepler
Copy link
Author

jepler commented Aug 14, 2020

Fourwire buses are not affected according to my testing (6.0.0-a.1 on particle xenon)

import displayio, board, busio
displayio.release_displays()
#spi = busio.SPI(board.SCK, MOSI=board.MOSI)
spi = board.SPI()
displayio.FourWire(spi, command=board.D2, chip_select=board.D3)

@@ -148,12 +153,19 @@ void reset_board_busses(void) {
bool display_using_spi = false;
#if CIRCUITPY_DISPLAYIO
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
if (displays[i].fourwire_bus.bus == spi_singleton) {
mp_const_obj_t bus_type = displays[i].bus_base.type;
if (bus_type == &displayio_fourwire_type && displays[i].fourwire_bus.bus == spi_singleton) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready for a fresh look! Instead of adding a special case I removed one, so it's sure to be right(-er) this time.

@tannewt Is this specific change (to check the type of the bus before checking its bus property) right? I assume it's right (because if it's, say an I2CDisplay then the content at that address is unrelated but also would never equal the spi_singleton) but if it's intentional I'd change it back and add a comment explaining it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, this looks correct to me. We check the type to know which union entry to use.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thanks!

@@ -148,12 +153,19 @@ void reset_board_busses(void) {
bool display_using_spi = false;
#if CIRCUITPY_DISPLAYIO
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
if (displays[i].fourwire_bus.bus == spi_singleton) {
mp_const_obj_t bus_type = displays[i].bus_base.type;
if (bus_type == &displayio_fourwire_type && displays[i].fourwire_bus.bus == spi_singleton) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, this looks correct to me. We check the type to know which union entry to use.

@tannewt tannewt merged commit 33c9bdb into adafruit:main Aug 18, 2020
@jepler jepler deleted the sharpmemory-bugs branch November 3, 2021 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants